← Database & SQL
MODULE 05 · DATABASE & SQL
Constraint
A database rule that limits what data can be stored so it stays valid and consistent.
01 · What is it?
A constraint is a database rule that limits what data can be stored so it stays valid and consistent.
The database checks the rule when data is added or changed. If the data breaks the rule, the database rejects the operation.
Simple Definition: A constraint is a guardrail for database data.
02 · Think of it like…
Think of a constraint like a rule at the entrance to a well-organized building. Only information that follows the rule is allowed inside.
Incoming dataNew value
→
ConstraintCheck the rule
→
DatabaseValid data only
03 · What does it do?
A constraint protects the data by checking rules such as:
Required valuesA required field cannot be left empty.
Unique valuesA value such as an email address cannot repeat.
Valid relationshipsA reference must point to a real related record.
Allowed valuesA value must fit an expected range or set.
04 · Where is it used?
Constraints are defined on database tables and are checked during inserts and updates.
Table definitionRules can be declared when a table is created.
Data changesRules run when rows are inserted or updated.
Application safetyRules protect the data even when several applications write to it.
05 · Example
Protecting a users table
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
age INTEGER CHECK (age >= 13)
);
INSERT INTO users (id, email, age)
VALUES (1, 'sam@example.com', 12);
The insert is rejected because the CHECK constraint requires the age to be at least 13.
06 · How it connects
1Application input
→
2Backend validation
→
3SQL write
→
4Constraint check
→
5Success or error
Application validation improves the user experience; database constraints protect the stored data at the final boundary.
07 · Do Not Confuse
Constraint
A database rule that the stored data must obey.
Validation
A check often performed in application code before data reaches the database.
Index
A structure that helps find data faster; it is not mainly a validity rule.
08 · Topic Glossary
| ENGLISH TERM | 中文 | SIMPLE ENGLISH | 中文小白理解 |
| Constraint | 约束 | A database rule that limits allowed data. | 限制哪些数据可以被保存的数据库规则。 |
| Rule | 规则 | A condition that data must follow. | 数据必须遵守的条件。 |
| NOT NULL | 非空 | A rule that requires a value to be present. | 要求这个字段必须有值,不能空着。 |
| UNIQUE | 唯一 | A rule that prevents duplicate values. | 要求某个字段的值不能重复。 |
| CHECK | 检查约束 | A rule that tests whether a value meets a condition. | 检查数据是否满足指定条件。 |
| PRIMARY KEY | 主键 | A value that uniquely identifies each row. | 唯一标识每一行数据的字段。 |
| FOREIGN KEY | 外键 | A value that links one table to a related table. | 把一张表的数据连接到另一张相关表的字段。 |
| Valid | 有效 | Following the rules required by the database. | 符合数据库要求的数据。 |
09 · What to Remember
A constraint keeps invalid data out of the database.
Constraints protect required fields, unique values, relationships, and other data rules.
Application checks help users; database constraints protect the final stored state.
Data+Rule→Valid database state