ER Diagrams   «Prev  Next»

Lesson 12Mandatory and Optional Participation
ObjectiveDescribe mandatory participation and enforce a required parent reference without confusing it with a parent's minimum number of children.

Mandatory Participation in Database Relationships

Mandatory participation means that each instance of an entity must take part in a particular relationship. Its minimum participation is one. Optional participation allows zero related instances. These rules apply separately in each direction and come from the business requirements.

For example, every order must belong to a customer, but a customer may register before placing an order. Orders participate mandatorily in this relationship; customers participate optionally. The same relationship can therefore be mandatory from one viewpoint and optional from the other.

Read Minimum and Maximum Together

Ask two questions for each entity: Must a related instance exist? and How many can exist? Mandatory or optional describes the minimum; one or many describes the maximum.

Customer and order business rules
Starting fromRelated instancesMinimumMaximum
One orderIts customer11
One customerTheir orders0Many

In the diagram, 1 near Customers means one customer per order. 0..many near Orders means zero or many orders per customer. In Crow's Foot notation, read the complete minimum/maximum symbol pair: a circle indicates zero, a bar indicates one, and the crow's foot indicates many. A single bar in isolation does not describe both limits.

Mandatory Participation: Every Order Needs Its Customer

A customer may place zero or many orders. Every order belongs to exactly one customer.

An order needs a matching customer, not merely some row somewhere in Customers. If an order references CustomerID 999, the presence of customer 101 does not satisfy the rule.

Customers 101 Ava and 102 Ben exist. Orders 5001 and 5002 both reference 101. Ben can have no orders. A reference to missing customer 999 fails the foreign key; a NULL customer reference fails NOT NULL.
Each order must reference exactly one existing customer. Repeated CustomerID values in Orders allow many orders for the same customer. Customer 102 can exist without an order.

Use NOT NULL and a Foreign Key Together

CREATE TABLE Customers (
    CustomerID INT NOT NULL PRIMARY KEY,
    CustomerName VARCHAR(100) NOT NULL
);

CREATE TABLE Orders (
    OrderID INT NOT NULL PRIMARY KEY,
    CustomerID INT NOT NULL,
    OrderDate DATE NOT NULL,
    CONSTRAINT fk_orders_customer
        FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
  • NOT NULL prevents an order from omitting its CustomerID.
  • The foreign key requires that the supplied CustomerID match a parent row.
  • The parent's primary key makes that match unique.
  • No UNIQUE constraint on Orders.CustomerID means the same customer can be referenced by several orders.

NOT NULL alone accepts a nonexistent identifier. A nullable foreign key alone permits an absent reference. Together, these constraints enforce the stated required-parent rule when constraint enforcement is enabled.

Try Valid Rows First

INSERT INTO Customers (CustomerID, CustomerName) VALUES
    (101, 'Ava'),
    (102, 'Ben');

INSERT INTO Orders (OrderID, CustomerID, OrderDate) VALUES
    (5001, 101, '2026-10-01'),
    (5002, 101, '2026-10-02');

Both orders are valid. Ben's customer row is also valid even though no order references it. In a practice database, test these statements separately; each is expected to fail:

-- Rejected: customer 999 does not exist.
INSERT INTO Orders (OrderID, CustomerID, OrderDate)
VALUES (5003, 999, '2026-10-03');

-- Rejected: the required customer reference is missing.
INSERT INTO Orders (OrderID, CustomerID, OrderDate)
VALUES (5004, NULL, '2026-10-03');

The examples assume ordinary immediate constraint checking: insert the parent before inserting its child. Some engines support explicitly deferred foreign keys, which can permit a different statement order within a transaction while still requiring a valid relationship when the constraint is checked. The enduring rule is a matching parent at validation time.

Choose Deletion Rules Deliberately

The example requests no cascade. With the foreign key enforced, deleting Ava while her orders remain is rejected. Deleting Ben, who has no orders, is allowed. Deleting an order does not delete its customer.

Use a restrictive policy when orders must be retained with their customer reference. Consider deactivation or an appropriate retention design rather than deleting required parent records. ON DELETE CASCADE deletes dependent orders when the customer is deleted; use it only if that is the intended business policy. SET NULL conflicts with this NOT NULL relationship.

Database engines differ in support and timing for RESTRICT and NO ACTION. Choose the supported action for your engine; neither term should be treated as a universal substitute for the other in deferred-constraint designs.


Database Design

A Required Parent Does Not Require Every Parent to Have Children

The foreign key checks existing Orders rows. It does not scan Customers to ensure that each customer has an order. Likewise, an order-line foreign key does not enforce “every submitted order has at least one line.”

If a business rule requires a minimum number of children, identify when it must hold. A draft order may temporarily have no lines, while submission requires at least one. Enforce that transition through an appropriate transactional workflow or database mechanism. A simple row-level CHECK cannot generally count related rows, and a concurrent deletion must not bypass the rule.

Mandatory Relationships and Weak Entities

Mandatory participation does not automatically make an entity weak. In classical ER modeling, a weak entity depends on an owner's key for its identification. An order with its own independent OrderID can require a customer without being identified by that customer's key.

An order line identified by (OrderID, LineNo) illustrates an identifying relationship: LineNo distinguishes lines only within one order. Add this table to the Customers/Orders schema above:

CREATE TABLE OrderLine (
    OrderID INT NOT NULL,
    LineNo INT NOT NULL CHECK (LineNo > 0),
    ItemDescription VARCHAR(200) NOT NULL,
    Quantity INT NOT NULL CHECK (Quantity > 0),
    CONSTRAINT pk_orderline PRIMARY KEY (OrderID, LineNo),
    CONSTRAINT fk_orderline_order
        FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);

The same LineNo can be used in different orders, but a duplicate (OrderID, LineNo) pair is rejected. Each line must reference an existing order. This simplified example stores a line description; a product catalog would require its own product reference and business rules.

Cascading deletion is a lifecycle choice, not the definition of a weak entity. A restrictive foreign key also prevents orphaned lines by blocking deletion of a referenced order.

Modeling Checklist

  • Write the rule in both directions, including the minimum and maximum.
  • For a child that requires exactly one parent, use a required foreign key to a unique parent key.
  • Do not add uniqueness to the child foreign key unless the maximum really is one child per parent.
  • Separate “every child has a parent” from “every parent has a child.”
  • Decide when minimum-child rules apply, and account for concurrent changes.
  • Test missing references, NULL values and parent deletion as well as successful inserts.

Apply the Rule to the Relationship, Not Just the Table

An entity can participate mandatorily in one relationship and optionally in another. For example, an order may require a customer while allowing an unassigned sales representative. Evaluate each relationship independently instead of describing the whole table as mandatory or optional.

The next lesson examines optional participation, including when an absent parent reference is meaningful.


SEMrush Software 12 SEMrush Banner 12