The difference between the column-level constraints of the database and the table-level constraints

There are still 6 months before the autumn recruitment. Tomorrow, we will end the study of the database and enter the study of the web!

One: What is a constraint:

Constraints are used to standardize the structure of the table, which is a kind of restriction, in order to ensure the reliability and stability of the data

Two: What are the common constraints and their effects

① not null is not empty, used to ensure that the value of the field cannot be empty,
such as name, student ID, etc.

② default default constraint, used to ensure that the field has a default value

③ PRIMARY KEY: primary key, used to ensure that the field is unique and non-empty,
such as student ID, employee ID

④ UNIQUE: unique, used to ensure the uniqueness of the field, can be empty

⑤ check: check constraints [not supported in mysql] Used to add conditions, only those that meet the conditions can be added

⑥ FOREGIN KEY foreign key: used to restrict the relationship between the two tables, used to ensure that the value of the field must come from the main table, the value of the associated column of the main table means that the foreign key constraint is added from the table to refer to the main table The value of a column in the table, such as the professional number of the student table, the department number of the employee table

Three: The difference between column and constraint and table-level constraint
① Different position: column-level constraint is written after the column, and when marking the constraint, it is written at the end of all fields

② The supported constraint types are different: column-level constraints can support all constraint types, and table-level constraints cannot support non-empty and default

③ Column-level constraints cannot be named constraints, table-mark constraints can be named constraints (except the primary key, the PRIMARY KEY used by the primary key)

Guess you like

Origin blog.csdn.net/weixin_46351306/article/details/114342266