Primary key column name: Id or CustomerId?

Primary key column name: name it {Table}Id, such as CustomerId, not plain Id. What each choice does to joins, foreign keys, EF Core and role names.

Id or CustomerId: what is the difference?

Two conventions name most primary key columns. One calls the key Id in every table and lets the table say whose key it is: Customer.Id, CustomerOrder.Id. The other puts the table’s name in front, {Table}Id, also written TableNameId: CustomerId, CustomerOrderId. Name it {Table}Id. The key then has one name in every table that holds it, and the sections below show what that does to joins, searches and EF Core, and where Id fits better.

Both name a foreign key column after its parent, so an order holds CustomerId either way. The difference is whether the key has that same name in its own table.

Here are three tables of the online shopping system template, keyed the second way:

CREATE TABLE dbo.Customer (
    CustomerId int IDENTITY (1, 1) NOT NULL,
    Email nvarchar(254) NOT NULL,
    FullName nvarchar(200) NOT NULL,
    CONSTRAINT PK_Customer PRIMARY KEY (CustomerId),
    CONSTRAINT AK_Customer_Email UNIQUE (Email)
);

CREATE TABLE dbo.CustomerOrder (
    CustomerOrderId int IDENTITY (1, 1) NOT NULL,
    CustomerId int NOT NULL,
    PlacedAt datetime2(0) NOT NULL,
    CONSTRAINT PK_CustomerOrder PRIMARY KEY (CustomerOrderId),
    CONSTRAINT FK_CustomerOrder_Customer_CustomerId
        FOREIGN KEY (CustomerId)
        REFERENCES dbo.Customer (CustomerId)
);

CREATE TABLE dbo.Payment (
    PaymentId int IDENTITY (1, 1) NOT NULL,
    CustomerOrderId int NOT NULL,
    Amount decimal(10, 2) NOT NULL,
    CONSTRAINT PK_Payment PRIMARY KEY (PaymentId),
    CONSTRAINT FK_Payment_CustomerOrder_CustomerOrderId
        FOREIGN KEY (CustomerOrderId)
        REFERENCES dbo.CustomerOrder (CustomerOrderId)
);
dbo.Customer with primary key CustomerId; dbo.CustomerOrder with primary key CustomerOrderId and foreign key CustomerId; dbo.Payment with primary key PaymentId and foreign key CustomerOrderId; a line from Customer to CustomerOrder and one from CustomerOrder to Payment.
Every foreign key column has the name of the key it holds: CustomerId in the customer and in the order.

And the same three tables with Id, in a database of their own:

CREATE TABLE dbo.Customer (
    Id int IDENTITY (1, 1) NOT NULL,
    Email nvarchar(254) NOT NULL,
    FullName nvarchar(200) NOT NULL,
    CONSTRAINT PK_Customer PRIMARY KEY (Id),
    CONSTRAINT AK_Customer_Email UNIQUE (Email)
);

CREATE TABLE dbo.CustomerOrder (
    Id int IDENTITY (1, 1) NOT NULL,
    CustomerId int NOT NULL,
    PlacedAt datetime2(0) NOT NULL,
    CONSTRAINT PK_CustomerOrder PRIMARY KEY (Id),
    CONSTRAINT FK_CustomerOrder_Customer_CustomerId
        FOREIGN KEY (CustomerId)
        REFERENCES dbo.Customer (Id)
);

CREATE TABLE dbo.Payment (
    Id int IDENTITY (1, 1) NOT NULL,
    CustomerOrderId int NOT NULL,
    Amount decimal(10, 2) NOT NULL,
    CONSTRAINT PK_Payment PRIMARY KEY (Id),
    CONSTRAINT FK_Payment_CustomerOrder_CustomerOrderId
        FOREIGN KEY (CustomerOrderId)
        REFERENCES dbo.CustomerOrder (Id)
);
dbo.Customer, dbo.CustomerOrder and dbo.Payment, each with primary key Id; CustomerOrder with foreign key CustomerId and Payment with foreign key CustomerOrderId, drawn as CustomerId.Id and CustomerOrderId.Id; a line from Customer to CustomerOrder and one from CustomerOrder to Payment.
A table can't name its foreign key Id as well, so every foreign key column is named apart from the key it holds: each is a role name, drawn as role.base, CustomerId.Id.

How a join reads with Id and with CustomerId

With Id, every join pairs two different names: o.CustomerId = c.Id. With {Table}Id, it pairs a name with itself: o.CustomerId = c.CustomerId. Same name on both sides is the pattern, and a join that breaks it is the one to read twice.

That counts most when the join is wrong. Here are two customers, three orders with a payment each, and two joins on the Id tables:

INSERT INTO dbo.Customer (Email, FullName)
VALUES (N'rosa@example.com', N'Rosa Mendes'),
       (N'felix@example.com', N'Felix Wong');

INSERT INTO dbo.CustomerOrder (CustomerId, PlacedAt)
VALUES (2, '2026-03-02 10:15'),
       (1, '2026-03-02 11:40'),
       (2, '2026-03-03 09:05');

INSERT INTO dbo.Payment (CustomerOrderId, Amount)
VALUES (1, 42.00), (2, 18.50), (3, 7.25);

SELECT o.Id, c.FullName
FROM dbo.CustomerOrder AS o
JOIN dbo.Customer AS c ON o.CustomerId = c.Id;

SELECT o.Id, c.FullName
FROM dbo.CustomerOrder AS o
JOIN dbo.Customer AS c ON o.Id = c.Id;

The first join is right: Felix placed orders 1 and 3, Rosa order 2. The second compares the order’s key with the customer’s key. SQL Server runs it without a word and returns two rows, order 1 for Rosa and order 2 for Felix, both wrong; order 3 drops out because there is no customer 3. o.Id = c.Id looks like every other join in the code, so it gets through review.

With {Table}Id keys the same mistake is ON o.CustomerOrderId = c.CustomerId. It runs as well and returns the same two wrong rows. Nothing stops it from running, but its two sides have different names. The only correct joins in this schema whose sides differ are those on a role name, such as c.ReferredById = r.CustomerId further down, where the column’s name says what it holds. Any other mismatch is what a reader catches.

A search works the same way. CustomerId is one string wherever a customer’s key goes: the key itself, every foreign key column, every join, every parameter and property in the application. Search the code for it and you find them all. With Id keys the customer’s own key is Id, the name every table shares, so a search for it finds every key in the schema, and only the code around each hit says which table it belongs to.

Ambiguous column name ‘Id’

A join of Id tables puts several columns named Id into one FROM. Leave one of them unqualified and SQL Server can’t tell which you mean:

SELECT Id, FullName, Amount
FROM dbo.CustomerOrder AS o
JOIN dbo.Customer AS c ON c.Id = o.CustomerId
JOIN dbo.Payment AS p ON p.CustomerOrderId = o.Id;
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'Id'.

Msg 209 is a compile error, so the query returns nothing. The fix is an alias on every Id you name, o.Id, and often a column alias too, o.Id AS CustomerOrderId, since the rows go on to code that reads columns by name. SELECT * over the same three tables returns Id, CustomerId, PlacedAt, Id, Email, FullName, Id, CustomerOrderId and Amount: three columns named Id, told apart only by where they stand.

{Table}Id doesn’t make Msg 209 go away. CustomerId is in the order and in the customer, and an unqualified CustomerId in the same join gets the same message, naming 'CustomerId'. What changes is what the repeated columns hold. With {Table}Id keys, the same SELECT * returns CustomerId twice and CustomerOrderId twice, and each pair repeats because the join made its two values equal. Either one is the right value, where the three Id columns hold three different things.

What EF Core expects

Entity Framework Core takes either name. By convention, a property named Id or <type name>Id becomes the primary key, so Customer.Id and Customer.CustomerId both work without configuration.

For a foreign key, EF Core takes a property whose type fits the parent’s key and whose name is one of four: the navigation’s name and the parent key’s name, the navigation’s name and Id, the parent type’s name and the key’s name, or the parent type’s name and Id. An order with a Customer navigation and a CustomerId property matches however the customer’s key is named. So the foreign key column in the database is CustomerId under both conventions, and only the parent’s own column differs. Leave the property out and EF Core adds a shadow foreign key, also named CustomerId under either key name.

When CustomerId is not enough: role names

IDEF1X defines a role name as “a name assigned to a foreign key attribute to represent the use of the foreign key in the entity” (FIPS 184, 2.58). Under Id keys every foreign key column is named apart from its key, so each one is a role name; under {Table}Id only the two cases below are, where the parent’s name is taken. A customer referred by another customer points from dbo.Customer to dbo.Customer, and the table already has a CustomerId: its own key. An order billed to one address and shipped to another points at dbo.Address twice, and a table can’t hold two columns named AddressId (SQL Server refuses it with Msg 2705). Each of these columns takes a name for what the key means in this table:

ALTER TABLE dbo.Customer ADD
    ReferredById int NULL,
    CONSTRAINT FK_Customer_Customer_ReferredById
        FOREIGN KEY (ReferredById)
        REFERENCES dbo.Customer (CustomerId);

CREATE TABLE dbo.Address (
    AddressId int IDENTITY (1, 1) NOT NULL,
    CustomerId int NOT NULL,
    Line1 nvarchar(200) NOT NULL,
    City nvarchar(100) NOT NULL,
    PostalCode varchar(20) NOT NULL,
    CONSTRAINT PK_Address PRIMARY KEY (AddressId),
    CONSTRAINT FK_Address_Customer_CustomerId
        FOREIGN KEY (CustomerId)
        REFERENCES dbo.Customer (CustomerId)
);

ALTER TABLE dbo.CustomerOrder ADD
    BillingAddressId int NOT NULL,
    ShippingAddressId int NOT NULL,
    CONSTRAINT FK_CustomerOrder_Address_BillingAddressId
        FOREIGN KEY (BillingAddressId)
        REFERENCES dbo.Address (AddressId),
    CONSTRAINT FK_CustomerOrder_Address_ShippingAddressId
        FOREIGN KEY (ShippingAddressId)
        REFERENCES dbo.Address (AddressId);
dbo.Customer with primary key CustomerId and foreign key ReferredById.CustomerId on a line back to itself; dbo.Address with foreign key CustomerId; dbo.CustomerOrder with foreign keys CustomerId, BillingAddressId.AddressId and ShippingAddressId.AddressId on two lines from Address; dbo.Payment with foreign key CustomerOrderId.
Each role-named column is drawn role.base: the role before the dot, the key it holds after it.

The template copies the shipping address onto the order; this version points at saved addresses instead. ReferredById allows NULL, for a customer who came on their own. A table that points at itself is a unary relationship, which has a post of its own.

Once the role is in the column’s name, the key it holds drops out of it. IDEF1X (3.14.3) writes a role-named column as the role name, a period and the migrated column’s name: ReferredById.CustomerId, BillingAddressId.AddressId. Read it as “ReferredById, which holds a CustomerId”. Under Id keys the same columns read ReferredById.Id and BillingAddressId.Id, which says a key is held but not whose.

EF Core maps both address keys by convention: properties named BillingAddressId and ShippingAddressId match the navigation’s name and Id for navigations named BillingAddress and ShippingAddress. Both are required, so by convention both cascade, and SQL Server refuses two cascade paths from dbo.Address to dbo.CustomerOrder with Msg 1785. So you set OnDelete on them. A collection of orders on Address for each role needs configuring as well, since EF Core pairs navigations by convention only while two types share one relationship.

Which one to choose

Name the primary key column {Table}Id. It gives the key one name in every table that holds it, role names aside:

  • a join’s two sides carry the same name, and a join whose names differ stands out;
  • a search for the name finds the key, every foreign key and every query that uses it;
  • columns that repeat in a join’s result hold equal values;
  • where the parent’s name can’t be used, a role name says why, and role.base keeps the key in sight.

Id suits an application whose SQL is nearly all written by EF Core or another ORM, where the code reads customer.Id and few people query the tables by hand: the joins are generated, so the mistakes above are rarer, and the shorter name is what the code uses most. It also suits a schema that already has Id everywhere. A half-converted schema is worse than either convention.

Both conventions are for surrogate keys. A natural key keeps the name of what it holds, as Sku and OrderNumber do in the template; surrogate key vs natural key is the choice before this one. The names around the key, constraints and indexes included, are in database naming conventions.

How ERDsmith helps

The key’s name is settled once and copied into every table that points at it, so it belongs to design time, before any query or class uses it:

  • A table’s first key column is an int identity named by the key column template, {Table}Id by default, so dbo.Customer starts with CustomerId.
  • The EF Core naming preset keys a table Id and puts the parent table’s name before it in the child, so Customer.Id still arrives in the order as CustomerId.
  • With Role names as role.base on (it is off by default), an Id schema under the default naming preset shows its order column as CustomerId.Id, since that column is named apart from its key; under the EF Core preset CustomerId is the template’s own name and shows no dot, and under {Table}Id only the role-named columns carry one, BillingAddressId.AddressId.