Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Id or [TableName]Id as primary key / entity identifier

Is it preferred to use "Id" as the column name for a primary key or "[TableName]Id" as a naming convention?

Table: Account
Primary Key: Id

-- versus --

Table: Account
Primary Key: AccountId

It seems to be split about 50% / 50% in the implementations that I've seen. What are the advantages and disadvantages in each approach?

Follow-up:

Does it make sense to use one convention in my database, and another on my entities in code? Or should I keep them consistent? How would does this work best in most ORMs?

like image 395
Even Mien Avatar asked May 14 '09 15:05

Even Mien


People also ask

Why is ID a primary key?

A primary key, also called a primary keyword, is a key in a relational database that is unique for each record. It is a unique identifier, such as a driver license number, telephone number (including area code), or vehicle identification number (VIN). A relational database must always have one and only one primary key.

What is a primary key example?

A primary key is a column -- or a group of columns -- in a table that uniquely identifies the rows of data in that table. For example, in the table below, CustomerNo, which displays the ID number assigned to different customers, is the primary key.


1 Answers

TableNameID for clarity

  1. Improve JOIN readability
  2. Clarity, say, when multiple FK "ID" columns (PK matches FK)
  3. ID is a reserved keyword
like image 87
gbn Avatar answered Nov 15 '22 09:11

gbn