-2

Take the Loan table below as an example. Does it require any primary key?

Loan Table

Fate
  • 1

2 Answers2

1

Yes.

Your Loan table looks like a junction table, so the PK should be the combination of DocumentID and EmployeeID.

Andre
  • 26,751
  • 7
  • 36
  • 80
  • I think a document can be borrowed multiple times by the same employee, so it's not unique. If the date fields are `datetime` you can add one to get it unique, but I would advise to use an`id`field that auto increments in every table as primary key to be safe. – BitAccesser Jul 14 '17 at 00:09
1

Does every table NEED a primary key: No.

SHOULD every table have a primary key: Yes. There may be situations where a table doesn't need a primary key, but those are rare and in general every table should have one. This is because you will want to uniquely identify a record and it becomes difficult to work with records that don't have primary keys.

In your case, YES your loan table should have a primary key. As Andre has already said in his answer it should be a combination of the DocumentID and the EmployeeID.

At the risk of introducing some confusion: Use either DocumentID + EmployeeID or EmployeeID + DocumentID. The order can make a difference. If you go for say DocumentID + EmployeeID (in that order) then the database will use the primary key when searching on DocumentID plus EmployeeID (any order in your query), and for searches on just the DocumentID but won't use it when searching on just EmployeeID.

Brian Cryer
  • 2,126
  • 18
  • 18