Answer the question
In order to leave comments, you need to log in
What to choose: primary key or unique for a unique foreign key?
Given
Table Field Author
: ID |… |…
Table Book
Field: ID | … |… Table Book Field Author:
BookID
| AuthorID
Relationship:
BookID -> Book.ID
AuthorID -> Author.ID
(-> = foreign key)
Desired behavior: the same book-author can only be specified once.
What is the best way to do this? Set (BookID, AuthorID) as UNIQUE or (BookID, AuthorID) as a composite Primary Key?
NULL is not supposed to be used...
Answer the question
In order to leave comments, you need to log in
Just in case: make sure the "book - author" is unique. For example, if you mean an online store or a catalog, you need to consider that the same book can be published by different publishers. In addition, even in one publishing house, the author can appear in different guises: "B. Akunin", "Akunin B." "Boris Akunin", etc.
If for an educational project, then it is better as a primary ID.
Better UNIQUE. This will allow you to have a separate simple exact id for the linked table. This usually makes things easier later if you have to work with the linked table in parallel queries outside of serialized transactions. Extra index for int primary key is not a problem.
In any case, there must be a unique identifier.
If we are talking about books, then I would duplicate the uniqueness by ID UNIQUE and the field with ISBN, even if one book is from different publishers, the number will be different
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question