SQL Constraints
Rules the database enforces on every change.
Syntax
PRIMARY KEY · NOT NULL · UNIQUE · CHECK (…) · DEFAULT x · REFERENCES t(col)
A change that breaks a constraint is rejected with an error, which keeps bad data out.
Example
Query (on the shop database used in the SQL course)
CREATE TABLE t (id INTEGER PRIMARY KEY, email TEXT UNIQUE NOT NULL); INSERT INTO t VALUES (1, '[email protected]'); INSERT OR IGNORE INTO t VALUES (2, '[email protected]'); -- rejected by UNIQUE, ignored SELECT * FROM t;
Result
| id | |
|---|---|
| 1 | [email protected] |
1 row
Good to know. SQLite enforces foreign keys only after PRAGMA foreign_keys = ON.
Related
Learn it step by step in the free lesson Creating Tables, with exercises that check your queries.