Sub topic 1
The example schema and the constraints that guard it
This chapter sets up the two tables used in the rest of the tutorial and shows how to add rows to them. Constraints decide which rows the database accepts, so you need to understand them before you change any data.
Prerequisites
This chapter assumes you can:
- Write a basic
SELECTwithWHERE. - Describe tables, rows, and columns.
- Use common types such as
INTEGER,VARCHAR, andNUMERIC. - Explain what a primary key and a foreign key are.
The examples use standard SQL. Where a dialect differs, the chapter says so.
The example schema
The tutorial uses a customers table and an orders table. Each order points to one customer.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE,
status VARCHAR(20) NOT NULL DEFAULT 'active'
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (id),
total NUMERIC(10, 2) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'new'
);Constraints in this schema
PRIMARY KEYmakes eachidunique and never empty.NOT NULLrequires a value in that column.UNIQUErejects a second row with the same email.DEFAULTsupplies a value when the insert omits the column.REFERENCESis a foreign key. It rejects an order whosecustomer_iddoes not exist incustomers.
Adding a row with INSERT
INSERT adds a row. Name the columns in the list so the statement stays correct if the table gets new columns later.
INSERT INTO customers (id, name, email)
VALUES (1, 'Ada Lovelace', '[email protected]');The status column is not listed, so it takes its default, 'active'.
Defaults and NULLs
A default fills a column that the insert leaves out. NULL means the value is missing. It is not the same as an empty string or zero.
| Situation | Result |
|---|---|
| Column omitted, has a `DEFAULT` | Default value is stored |
| Column omitted, no `DEFAULT`, nullable | `NULL` is stored |
| Column omitted, `NOT NULL`, no `DEFAULT` | Insert fails |
| `NULL` written explicitly into a `NOT NULL` column | Insert fails |
Inserting several rows in one statement
A single INSERT can add several rows. Separate each row's values with a comma. Each row is one all-or-nothing change, so an error in any row stops the statement.
INSERT INTO customers (id, name, email)
VALUES
(2, 'Grace Hopper', '[email protected]'),
(3, 'Alan Turing', NULL);Customer 3 has no email. The column allows NULL, so the insert succeeds.
Copying rows with INSERT ... SELECT
INSERT ... SELECT copies rows returned by a query into another table. Columns match by position, so the order of the selected columns must match the order of the listed columns.
CREATE TABLE customers_archive (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255)
);
INSERT INTO customers_archive (id, name, email)
SELECT id, name, email
FROM customers
WHERE status = 'inactive';Exercise
Exercise
Which of the following statements fails, and why? (a) INSERT INTO customers (id, name) VALUES (4, 'Kim'); (b) INSERT INTO customers (id, name, email) VALUES (5, NULL, '[email protected]');
Show answer
Statement (b) fails. The name column is NOT NULL, so it cannot hold NULL. Statement (a) succeeds because email is nullable and status takes its default, 'active'.