Constraint classification
- Primary key: PRIMARY key
Used to uniquely identify a row in a table and cannot be duplicated. e.g.:id INT(10) PRIMARY KEY
- Defaults: Default
When inserted, the value specified by default if no value is specified for the column. e.g.:age INT(10) DEFAULT10
- Unique: Unique
A specified column in a table, no duplicate values can be
- FOREIGN key: FOREIGN key
Maintain the integrity of the table and reflect the relationships in the table. A table can have more than one foreign key, each foreign key must references (reference) the primary key of another table, the value of the column that is constrained by the foreign key, and must have a corresponding value in its referenced column
- Non-empty: NOT NULL
In MySQL, inserting data violates a non-null constraint and only gives a warning, but executes successfully
To add a constraint to a column in a table
To add a constraint when a table is built:<列名> 类型(长度) <约束>
MySQL Learning-sql constraints