Home>Article>Database> What is the use of constraints in mysql?

What is the use of constraints in mysql?

藏色散人
藏色散人 Original
2019-05-20 11:34:13 4293browse

What is the use of constraints in mysql?

What are constraints?

Constraints are a kind of "regulation" that requires data to meet certain conditions.

There are mainly the following constraints:

1. Primary key constraints: Form: primary key (field name);

Meaning (function): The value of this setting field can be used to "uniquely determine a row of data", which actually means "primary key".

2. Unique constraint:Form: unique key (field name);

Meaning (function): Make the value of the set field "unique", Nature is also distinguishable.

3. Foreign key constraints:Form: foreign key (field name) references other table names (corresponding to field names in other tables);

Meaning (function) : The value of the setting field must already exist in the corresponding field in the corresponding table specified by the other party.

4. Non-null constraint:Form: not null, which is actually the "not null" attribute written when setting a field.

This constraint can only be written on field attributes;

5. Default constraint:Form: default XX value; in fact, it is the one written when setting a field. default "Default value" attribute

This constraint can only be written on field attributes;

6. Check constraint:Form: check (some kind of judgment statement), such as:

create table tab1 ( age tinyint, check (age>=0 and age <100) /*这就是检查约束*/ )

In fact, primary key constraints, unique constraints, and foreign key constraints are just "two different perspectives on the same thing." They are also called "primary key index", "unique index", and "foreign key constraints". key index".

Related learning recommendations:mysql database

The above is the detailed content of What is the use of constraints in mysql?. 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