The role of constraints in mysql
Editor to share with you the role of constraints in mysql, I hope you will learn a lot after reading this article, let's discuss it together!
What is a constraint?
Constraint is a kind of "stipulation" that requires what conditions the data needs to meet.
There are mainly the following constraints:
Primary key constraint: form: primary key (field name)
Meaning (function): so that the value of the setting field can be used to "uniquely determine a row of data", which actually means "primary key".
Unique constraint: form: unique key (field name)
Meaning (function): make the value of the setting field "unique", which is naturally distinguishable.
Foreign key constraint: form: foreign key (field name) references other table names (corresponding to field names in other tables)
Meaning (function): make the value of the set field already have the value in the corresponding field in the corresponding table.
Non-null constraint: form: not null, which is actually the "not null" attribute that is written when setting a field.
This constraint can only be written on field properties
Default constraint: form: default XX value; in fact, it is the "default default" property written when setting a field.
This constraint can only be written on field properties
Check constraints: form: check (some kind of judgment statement), such as:
Create table tab1 (age tinyint,check (age > = 0 and age)