Home  >  Article  >  Database  >  MySQL constraints overview

MySQL constraints overview

autoload
autoloadOriginal
2021-03-26 15:46:422649browse

MySQL constraints overview

In MySQL, a constraint refers to a constraint on the data in the table, which can help the database administrator better manage the database and ensure that the Data correctness and validity. Therefore, it is necessary to use constraints to limit the table.

1) Primary key constraints

Primary key constraints are the most frequently used constraints. When designing a data table, it is generally required to set a primary key in the table.
The primary key is a special field in the table that uniquely identifies each piece of information in the table. For example, the student number in the student information table is unique.

PS: Setting primary key constraints does not allow null values.

2) Foreign key constraints

Foreign key constraints are often used together with primary key constraints to ensure data consistency.
For example, a fruit stall only has four kinds of fruits: apples, peaches, plums, and watermelons. Then, when you come to the fruit stall to buy fruits, you can only choose apples, peaches, plums, and watermelons, and you cannot buy other fruits.

3) Unique constraint

A unique constraint ensures that the data in a field or a group of fields is unique compared to the data in other rows in the table. Unique constraints are similar to primary key constraints in that they both ensure the uniqueness of columns. Different from primary key constraints, there can be multiple unique constraints in a table, and the column where the unique constraint is set is allowed to have null values, although there can only be one null value.

4) Check constraint


# A check constraint is a rule that confirms that the data in a record in a table is acceptable field value. Check constraints help enforce domain integrity. Domain integrity defines the valid values ​​of a field in a database table. Check constraints can verify the domain integrity of an individual field or a group of fields. You can have multiple integrity checks on a single field. If the data being inserted or updated violates a check constraint, the database engine will not allow the insert or update operation to occur.

5) Non-null constraint

Non-null constraint means that the content of a field in the table is not allowed to be empty , if you want to use non-null constraints, you only need to use the "NOT NULL" statement after each column.

6) Default value constraint

Default value constraint is used to specify the default value of a certain column. For example, if there are more male students in the class, then when creating the "Gender" column of the class table, it can be set to "Male" by default.

Recommendation: "mysql tutorial"

The above is the detailed content of MySQL constraints overview. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn