Sub topic 12
Column-Level and Table-Level Constraint Syntax
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE, and that you understand tables, rows, columns, and common data types. You will use that knowledge to place constraints where they are easiest to read and maintain.
Column-level constraints
A column-level constraint appears directly after a column's data type. Use it when the rule concerns only that column.
CREATE TABLE customers (
customer_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
full_name VARCHAR(100) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);Here, NOT NULL, UNIQUE, DEFAULT, and PRIMARY KEY are attached to individual columns. Each rule is easy to associate with the column it protects.
A column-level CHECK can also express a single-column rule:
CREATE TABLE products (
product_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price NUMERIC(10, 2) NOT NULL CHECK (price >= 0)
);The price rule rejects negative values.
Table-level constraints
A table-level constraint appears after the column definitions, before the closing parenthesis. Use it when a rule involves multiple columns, when you want a named constraint, or when the table structure is easier to read separately from its rules.
CREATE TABLE order_items (
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL,
CONSTRAINT pk_order_items
PRIMARY KEY (order_id, product_id),
CONSTRAINT ck_order_items_quantity
CHECK (quantity > 0)
);The primary key is composite, meaning it uses two columns together. An order can contain many products, and a product can appear in many orders, but the same product can occur only once within one order.
A table-level foreign key is also useful when you want to name the relationship:
CREATE TABLE orders (
order_id INTEGER GENERATED ALWAYS AS IDENTITY,
customer_id INTEGER NOT NULL,
total_amount NUMERIC(10, 2) NOT NULL,
CONSTRAINT pk_orders
PRIMARY KEY (order_id),
CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id)
REFERENCES customers (customer_id),
CONSTRAINT ck_orders_total
CHECK (total_amount >= 0)
);Choosing a form
Both forms can enforce many of the same rules. Choose based on scope and readability.
| Situation | Suitable form | Reason |
|---|---|---|
| A column must contain a value | Column-level `NOT NULL` | The rule concerns one column |
| A value must be nonnegative | Column-level `CHECK` | The expression uses one column |
| Two or more columns form one key | Table-level `PRIMARY KEY` | The rule concerns the combination |
| A relationship should have a readable name | Table-level `FOREIGN KEY` | The named rule is easier to change |
| A rule compares columns | Table-level `CHECK` | The expression uses multiple columns |
Visualizing constraint scope
Practice
When a rule uses several columns, place it at table level. Naming it also makes later changes clearer with ALTER TABLE.
Exercise
Should the rule "enddate must be on or after startdate" be written at column level or table level?
Show answer
Table level, because the rule compares two columns. For example: CONSTRAINT ckdates CHECK (enddate >= start_date).