liveBook · Manning

Check constraint

As explained in Learn SQL in a Month of Lunches

A CHECK constraint is a database rule that permits a column value only when it satisfies a specified Boolean expression.

A CHECK constraint protects data by allowing a column value only when it satisfies a Boolean expression. It works like a rule written in plain English: shipment methods must be “P” or “E,” and shipment costs must stay within an allowed range. section 19.1.4

Validating shipment data

CREATE TABLE shipment ( ShipmentID int NOT NULL, OrderID int NOT NULL, ShipmentCost decimal(5,2) NOT NULL CHECK (ShipmentCost BETWEEN 0.00 AND 999.99), ShipmentMethod char(1) NOT NULL CHECK (ShipmentMethod IN ('P', 'E')), TrackingNumber varchar(20) NOT NULL UNIQUE, ShipmentDate datetime NOT NULL DEFAULT (CURRENT_DATE()), CONSTRAINT PK_shipment PRIMARY KEY (ShipmentID), CONSTRAINT FK_shipment_orderheader FOREIGN KEY (OrderID) REFERENCES orderheader(OrderID) ); Here, both endpoints of the cost range are valid because BETWEEN includes its beginning and ending values. ShipmentMethod accepts only P or E. section 19.1.4

Use CHECK constraints for practical data-quality rules such as:

  • Restricting PaymentYear to a reasonable range such as 2000 through 2100, preventing likely typos. section 23.1.2
  • Allowing only positive PaymentAmount values, preventing negative payments. section 23.1.2
  • Combining conditions that involve multiple columns in one larger expression; place such a table-level constraint after the column definitions. section 19.1.4
CREATE TABLE shipment (
    ShipmentID int NOT NULL,
    OrderID int NOT NULL,
    ShipmentCost decimal(5,2) NOT NULL
        CHECK (ShipmentCost BETWEEN 0.00 AND 999.99),
    ShipmentMethod char(1) NOT NULL CHECK (ShipmentMethod IN ('P', 'E')),
    TrackingNumber varchar(20) NOT NULL UNIQUE,
    ShipmentDate datetime NOT NULL DEFAULT (CURRENT_DATE()),
    CONSTRAINT PK_shipment PRIMARY KEY (ShipmentID),
    CONSTRAINT FK_shipment_orderheader FOREIGN KEY (OrderID)
        REFERENCES orderheader(OrderID)
    );

Inspect the complete shipment definition to see CHECK constraints alongside NOT NULL, UNIQUE, DEFAULT, PRIMARY KEY, and FOREIGN KEY constraints. section 19.1.4

How Learn SQL in a Month of Lunches covers it

How deep it goes
Defines range, membership, and multi-column expressions, but does not develop complex constraint design beyond simple examples.
Examples
Uses shipment costs, shipment methods, payment years, and payment amounts as validation examples.
What it assumes
Assumes familiarity with columns, data types, CREATE TABLE definitions, and basic constraints.
What you can do after
Build it from scratch by adding range, membership, and positive-value checks to CREATE TABLE definitions.
How it connects
Relates CHECK constraints to NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY constraints, and indexes.

Also explained in

Compare how 2 books explain check constraint →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.