I have an additional question related to post: Composite primary keys in databases
Please check out the post over there (otherwise I just need to repeat it).
Now my question is:
If I go for an ID autoincrement as primary key (as I accepted and which allows me to again reference the current table through this key), how can I assure that a combination between User_ID and Admin_ID (both FK's) can only exist once (is unique) on insert?
It is a many-to-many relationship.
It could be done in the programming of the front end (check for existing record by select), however my feelings tell me that this is not the best way and I wonder if I can directly put the restriction in the back end.
I would logically add the FK's to the primary key, but then I'm back to the composite key and this is what I was generally advised rather not to use.
What is the proper way of doing this?
Thanks for your help.
Create a composite key.
It couldn't, unless you are the only client accessing the table.1
In a real, concurrent environment, you can never know if another transaction inserted the same value (as you are trying to insert) after your SELECT but before your INSERT.
And even if you were the only one accessing the database, you'd need an index for efficient execution of SELECT anyway. So why not let the DBMS utilize this index for a key?
Wrong advice. If a column or a combination of columns has to be unique, you have to create a key.2 You cannot skip creating a key that enforces the correctness of your data just because you have another (surrogate) key.
Surrogates cannot generally replace natural keys, they can only be added.3 So the question becomes: is the additional overhead of the surrogate worth it? Sometimes it is, sometimes it isn't, but there are no cut-and-dry rules here.
1 Or are willing to lock the whole table, destroying the scalability in the process.
2 Though it doesn't necessarily need to be primary.
3 What they typically "replace" is the natural key's role as a primary key, but the natural key still continues to live as alternate key.
I would go for a composite key
If I really need an auto-increment primary key, then I'll create a unique index on the two foreign key columns
Side note: The advantage of using a composite key is that when using an ORM tool like Entity Framework, it automatically recognizes it as a many to many relationship, and abstracting away the additional intersection table into just a relationship.
Consider the following (hypothetical) schema. Would you add a surrogate key to the "chessboard" table ? The values for {xxx,yyy,pc} are constrained, either by "restricted" domains, or by the PK+FK constraints.
In which cases would adding a surrogate key (for {xxx,yyy} help?
(additional constraints (such as: no more than one King per Color ...) would be necessary for a real chess game, (but part of) the business rules (such as valid moves ...) would be handled by the "application logic" anyway)
Go with the ID. I strongly agree with the answer on the other page. For quick-and-dirty applications, a composite key is fine. However, I generally put auto-incremented ids into new tables that I create, unless they are static tables used for reporting.
For your specific question, there are at least four answers that I can think of off-hand:
My preference is (4), in combination with (1) or (2). I find that controlling inserts through stored procedures gives me a lot of flexibility, particularly when I want to log or debug problems. That said, I am not usually working with high-volume transactional systems, where reducing overhead is paramount.
There is one advantage to an auto-incrementing id missed on the other answer. The following query:
Returns the most recently added records, assuming the id is the first column (as it is in all my tables). Just the ability to see the most recently inserted records is sufficient for me to use an id.