I'm wondering what the best practice is for naming primary/unique keys in a database with many tables. Should you always just call each table's primary key id
or is ok to not have an id
field in each table and just name each of them something1_id
, something2_id
, etc?
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
- sqlyog export query result as csv
It is a matter of preference; however, there is an advantage to using (something)ID in that column names become less ambiguous when you are doing joins between tables.
It's personal preference. At the end of the day it makes absolutely no difference. I personally prefer using just
Id
as I find referring to the field (let's sayCustomer
table andId
field)Customer.CustomerId
to be cumbersome, whereCustomer.Id
seems to make more sense.Whatever you do, pick one or the other and stick to that standard. There are pros and cons for each.
I prefer
SomethingID
but other people prefer justID
. In the system I work with there are well over a thousand tables and having the PK and the FK have the exact same names makes things easier.id
is just a convenient convention - you can call the field anything as long as you declare it as a key field.Prefixing the id with a relevant context can be helpful when it comes to reading and writing your code, and especially improves readability when you are joining data from multiple tables and foreign keys.
I'd suggest you use an abbreviation for long names and use (something)Id (like
user_id
) for regular tables. So for the tablecustomer_company_relation_metadata
useccm_id
. But its fine to useid
as well