Weak entity: owner, partial key and the SQL Server table

A weak entity has no key of its own: its owner's key plus a partial key identify it. How it becomes a SQL Server table, and what SQL Server refuses.

What is a weak entity?

A weak entity is an entity that can’t be uniquely identified by its own attributes. It depends on another entity, its owner, and is identified by the owner’s key, usually together with a partial key of its own, also called a discriminator. An entity with a key of its own is a strong entity. Textbooks that speak of entity sets call the whole group a weak entity set; the idea is the same.

Take a purchase order. Order 1 asks a supplier for 500 M8 bolts on line 1 and 200 on line 2. “Line 2” names nothing until you say of which order, because every order has a line 2. The line number is unique within one order and nowhere else, so it is the partial key, and order 1 plus line 2 is the line’s full identity. A line also can’t exist without its order: delete the order and its lines mean nothing.

Chen notation, where the idea comes from, has a symbol for each part:

Chen diagram: the rectangle PurchaseOrder with key PurchaseOrderId and OrderedOn; the double diamond has between PurchaseOrder (1) and the double rectangle PurchaseOrderLine (N) on a double line; PurchaseOrderLine with LineNumber under a dashed line and Quantity; the diamond for between PurchaseOrderLine (N, double line) and the rectangle Product (1) with key ProductId and Name.
A purchase order and its lines in Chen notation. Drawn for this post: ERDsmith draws tables, not Chen.
  • A double rectangle is the weak entity, PurchaseOrderLine.
  • A double diamond is its identifying relationship, has, the one that ties it to its owner.
  • A dashed underline marks the partial key, LineNumber; the owner’s key gets a solid one .
  • A double line is total participation: every line takes part. A weak entity always takes part in its identifying relationship this way.

The for diamond to Product is an ordinary relationship. Its line is double too, since every order line names a product, but the product lends the line no part of its key.

How a weak entity becomes a table

The weak entity gets a table of its own. The owner’s primary key travels into it as a column with a foreign key back to the owner, and the table’s primary key is that column plus the partial key:

CREATE TABLE dbo.Product (
  ProductId int           NOT NULL,
  Sku       nvarchar(20)  NOT NULL,
  Name      nvarchar(100) NOT NULL,
  CONSTRAINT PK_Product PRIMARY KEY (ProductId)
);
CREATE TABLE dbo.PurchaseOrder (
  PurchaseOrderId int  NOT NULL,
  OrderedOn       date NOT NULL,
  CONSTRAINT PK_PurchaseOrder PRIMARY KEY (PurchaseOrderId)
);
CREATE TABLE dbo.PurchaseOrderLine (
  PurchaseOrderId int      NOT NULL,
  LineNumber      smallint NOT NULL,
  ProductId       int      NOT NULL,
  Quantity        int      NOT NULL,
  CONSTRAINT PK_PurchaseOrderLine
    PRIMARY KEY (PurchaseOrderId, LineNumber),
  CONSTRAINT FK_PurchaseOrderLine_PurchaseOrder_PurchaseOrderId
    FOREIGN KEY (PurchaseOrderId)
    REFERENCES dbo.PurchaseOrder (PurchaseOrderId),
  CONSTRAINT FK_PurchaseOrderLine_Product_ProductId
    FOREIGN KEY (ProductId)
    REFERENCES dbo.Product (ProductId)
);
dbo.Product and dbo.PurchaseOrder; dbo.PurchaseOrderLine with primary key PurchaseOrderId and LineNumber, where PurchaseOrderId is a foreign key to dbo.PurchaseOrder; a solid line from PurchaseOrder and a dashed line from Product to PurchaseOrderLine.
PurchaseOrderId is in the primary key of dbo.PurchaseOrderLine and is its foreign key to dbo.PurchaseOrder, so that line is solid and the box rounded. ProductId sits outside the key, so its line is dashed.

The double rectangle became the rounded box , the double diamond the solid line , and the dashed-underlined LineNumber the second column of a composite key. Total participation became NOT NULL: PurchaseOrderId is a primary key column, and SQL Server makes every primary key column NOT NULL, so the parent’s end of the line stays bare , exactly one order, never the ring of an optional parent.

Put the owner’s key first. PK_PurchaseOrderLine is a clustered index sorted by order, then line, so an order’s lines sit together and reading them is a seek on PurchaseOrderId.

What is an identifying relationship?

In the tables, FK_PurchaseOrderLine_PurchaseOrder_PurchaseOrderId is the identifying relationship. When every column of a foreign key is also in the child’s primary key, the relationship is identifying: the child can’t be named without its parent. The foreign key to dbo.Product is non-identifying, a dashed line . The product is a fact about the line and plays no part in naming it: change it, and the row is still line 2 of order 1.

An identifying relationship is one-to-many or one-to-one from the owner, never many-to-many, because each weak entity row has exactly one owner per identifying relationship. It can have more than one of them, though. Stock is how many of a product a warehouse holds. Neither the warehouse nor the product names a stock row alone; the pair does, and there is no partial key at all:

CREATE TABLE dbo.Warehouse (
  WarehouseId int          NOT NULL,
  Code        nvarchar(10) NOT NULL,
  CONSTRAINT PK_Warehouse PRIMARY KEY (WarehouseId)
);
CREATE TABLE dbo.Stock (
  WarehouseId int NOT NULL,
  ProductId   int NOT NULL,
  OnHand      int NOT NULL,
  CONSTRAINT PK_Stock PRIMARY KEY (WarehouseId, ProductId),
  CONSTRAINT FK_Stock_Warehouse_WarehouseId
    FOREIGN KEY (WarehouseId)
    REFERENCES dbo.Warehouse (WarehouseId),
  CONSTRAINT FK_Stock_Product_ProductId
    FOREIGN KEY (ProductId)
    REFERENCES dbo.Product (ProductId)
);
dbo.Product and dbo.Warehouse; dbo.Stock with primary key WarehouseId and ProductId, both foreign keys; solid lines from Product and from Warehouse to Stock.
Both foreign keys of dbo.Stock are its whole primary key, so both lines are solid: two identifying relationships and no key column of its own.

Many modelers draw stock in Chen as a many-to-many diamond between warehouse and product with OnHand on it, not as a double rectangle with two owners. The table comes out the same either way.

The partial key is a choice, too. The inventory management system template keys its order lines by order and product instead of a line number, so each product appears once per order and both lines into dbo.PurchaseOrderLine are solid.

What SQL Server refuses

The two constraints of the line table are the weak entity’s two rules. The foreign key says a line can’t exist without its order, and the primary key says the line number is unique within its order:

INSERT INTO dbo.Product (ProductId, Sku, Name)
VALUES (7, N'BOLT-M8', N'M8 hex bolt');
INSERT INTO dbo.PurchaseOrder (PurchaseOrderId, OrderedOn)
VALUES (1, '2026-03-02'), (2, '2026-03-05');
INSERT INTO dbo.PurchaseOrderLine
  (PurchaseOrderId, LineNumber, ProductId, Quantity)
VALUES (1, 1, 7, 500), (1, 2, 7, 200), (2, 1, 7, 50);
INSERT INTO dbo.PurchaseOrderLine
  (PurchaseOrderId, LineNumber, ProductId, Quantity)
VALUES (9, 1, 7, 100);
INSERT INTO dbo.PurchaseOrderLine
  (PurchaseOrderId, LineNumber, ProductId, Quantity)
VALUES (1, 2, 7, 300);
Msg 547, Level 16, State 1, Line 8
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_PurchaseOrderLine_PurchaseOrder_PurchaseOrderId". The conflict occurred in database "Warehouse", table "dbo.PurchaseOrder", column 'PurchaseOrderId'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 11
Violation of PRIMARY KEY constraint 'PK_PurchaseOrderLine'. Cannot insert duplicate key in object 'dbo.PurchaseOrderLine'. The duplicate key value is (1, 2).
The statement has been terminated.

Three lines go in, two on order 1 and one on order 2. Both orders have a line 1, which is fine: the partial key repeats across owners. The line for order 9 fails with Msg 547, because there is no order 9 for it to belong to. Line 2 of order 1 a second time fails with Msg 2627 on PK_PurchaseOrderLine, which prints the duplicate in key order, (1, 2).

The foreign key also works from the other side. Order 2 still has its line, so deleting the order is refused:

DELETE FROM dbo.PurchaseOrder WHERE PurchaseOrderId = 2;
Msg 547, Level 16, State 1, Line 1
The DELETE statement conflicted with the REFERENCE constraint "FK_PurchaseOrderLine_PurchaseOrder_PurchaseOrderId". The conflict occurred in database "Warehouse", table "dbo.PurchaseOrderLine", column 'PurchaseOrderId'.
The statement has been terminated.

This Msg 547 names the REFERENCE constraint and the child table. The foreign key’s delete action is NO ACTION unless you set one, and NO ACTION refuses to leave a line without its order.

Should deleting the order delete its lines?

Usually, since a line means nothing without its order. SQL Server will do it for you if the foreign key says ON DELETE CASCADE. A foreign key’s actions can’t be altered in place, so the script drops it and adds it again under the same name. The diagram doesn’t change: ON DELETE CASCADE is a setting of the relationship, not a mark on its line.

ALTER TABLE dbo.PurchaseOrderLine
  DROP CONSTRAINT
    FK_PurchaseOrderLine_PurchaseOrder_PurchaseOrderId;
ALTER TABLE dbo.PurchaseOrderLine
  ADD CONSTRAINT
    FK_PurchaseOrderLine_PurchaseOrder_PurchaseOrderId
    FOREIGN KEY (PurchaseOrderId)
    REFERENCES dbo.PurchaseOrder (PurchaseOrderId)
    ON DELETE CASCADE;

Now the same DELETE of order 2 succeeds and takes its line with it. SQL Server reports (1 row affected), the order; the cascaded line isn’t counted. Order 1 and its two lines are untouched.

Cascade fits a draft order that someone cancels. It fits less well once an order is a record you have to keep, or once another table points at its lines: a receipt for a delivered line would block the cascade, or, with a cascade of its own, disappear with it. If an order with lines should never be deleted in one statement, keep NO ACTION and let Msg 547 stop it.

When is a surrogate key the better choice?

The composite key works while nothing references the line. Once receipts record deliveries against lines, each receipt has to carry the pair (PurchaseOrderId, LineNumber), and a child under the receipt would carry it again. Renumbering a line that has a receipt then fails: in a run with a receipt on line 2, UPDATE dbo.PurchaseOrderLine SET LineNumber = 4 failed with Msg 547 against the receipt’s foreign key.

The alternative gives the line a key of its own, an identity, and keeps the pair unique as an alternate key. This script replaces dbo.PurchaseOrderLine, built on the same dbo.Product and dbo.PurchaseOrder, and adds the receipt:

CREATE TABLE dbo.PurchaseOrderLine (
  PurchaseOrderLineId int      NOT NULL IDENTITY
    CONSTRAINT PK_PurchaseOrderLine PRIMARY KEY,
  PurchaseOrderId     int      NOT NULL,
  LineNumber          smallint NOT NULL,
  ProductId           int      NOT NULL,
  Quantity            int      NOT NULL,
  CONSTRAINT AK_PurchaseOrderLine_PurchaseOrderId_LineNumber
    UNIQUE (PurchaseOrderId, LineNumber),
  CONSTRAINT FK_PurchaseOrderLine_PurchaseOrder_PurchaseOrderId
    FOREIGN KEY (PurchaseOrderId)
    REFERENCES dbo.PurchaseOrder (PurchaseOrderId),
  CONSTRAINT FK_PurchaseOrderLine_Product_ProductId
    FOREIGN KEY (ProductId)
    REFERENCES dbo.Product (ProductId)
);
CREATE TABLE dbo.Receipt (
  ReceiptId           int  NOT NULL IDENTITY
    CONSTRAINT PK_Receipt PRIMARY KEY,
  PurchaseOrderLineId int  NOT NULL,
  ReceivedOn          date NOT NULL,
  Quantity            int  NOT NULL,
  CONSTRAINT FK_Receipt_PurchaseOrderLine_PurchaseOrderLineId
    FOREIGN KEY (PurchaseOrderLineId)
    REFERENCES dbo.PurchaseOrderLine (PurchaseOrderLineId)
);
dbo.Product and dbo.PurchaseOrder; dbo.PurchaseOrderLine with primary key PurchaseOrderLineId and alternate key PurchaseOrderId and LineNumber, dashed lines from Product and PurchaseOrder; dbo.Receipt with foreign key PurchaseOrderLineId, a dashed line from PurchaseOrderLine.
dbo.PurchaseOrderLine keeps the pair as its alternate key (AK) and gets a key of its own, so its line from dbo.PurchaseOrder is dashed and its box square.

In the tables, the line is no longer a weak entity: it has a key of its own, and the relationship to its order is non-identifying. The business rule hasn’t changed, and the constraints still hold it. PurchaseOrderId is NOT NULL, the foreign key refuses an order that doesn’t exist, and the unique constraint still refuses line 2 of order 1 twice, with Msg 2627 on AK_PurchaseOrderLine_PurchaseOrderId_LineNumber. A receipt now points at PurchaseOrderLineId alone, and renumbering that line to 4 goes through without touching the receipt.

What you give up is the order’s id in the receipt: to list one order’s receipts, you join through dbo.PurchaseOrderLine. So keep the composite key for a weak entity that nothing references, and switch to a surrogate when other tables point at it or its partial key can change. Surrogate key vs natural key weighs the two more generally.

How ERDsmith helps

Everything that makes dbo.PurchaseOrderLine a weak entity is decided when its keys are: whether the order’s key is in the line’s primary key, and what happens to the lines when an order goes. ERDsmith designs physical models, so there is no double rectangle to draw. It reads the weak entity from the keys you give the table:

  • PurchaseOrderId inside the line’s primary key is what makes its relationship identifying: the line from dbo.PurchaseOrder is solid and the box rounded , while ProductId, outside the key, hangs on a dashed line .
  • An identifying relationship is mandatory, so its owner’s end never takes the ring of an optional parent.
  • Swap the composite key for an identity, as the surrogate design does, and the same edit turns the order’s line dashed and the box square; no separate setting marks a line identifying.
  • A new relationship takes the project’s default actions, NO ACTION until someone sets that default, so deleting an order with lines stays refused until you choose CASCADE.