Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use primary ID's for associated tables

I've been reading through questions explaining why primary ID's should always be used in tables, especially if a record in one table needs to associate with a record in another table. However in my case the table is one of users, so has a name field. Surely for associating I could just use the name of the users to associate with, or am I missing something.

like image 672
w4etwetewtwet Avatar asked Dec 05 '25 15:12

w4etwetewtwet


1 Answers

One of the main reasons why you should have a Primary ID is because it should be permanent value since it could be used for referencing foreign keys from the other tables.

You can use name provided that:

  • There is a key defined on it. (obviously, a Primary Key or a Unique Key)
  • It cannot be modified. You have stated in your question: "..associating I could just use the name of the users to associate with..". What if you want to modify the name and there are already records that are depending on it?
like image 147
John Woo Avatar answered Dec 07 '25 05:12

John Woo