Several constraints common to MySQL

Series Article Directory

follow-up supplement



foreword

insert image description here

In order to prevent non-compliant data from being stored in the database, MySQL provides a mechanism to check whether the data in the database meets the specified conditions when users insert, modify, and delete data to ensure the accuracy of the data in the database. Consistency and consistency, this mechanism is integrity constraints.
MySQL mainly supports the following integrity constraints, as shown in the table. The Check constraint is the support provided in MySQL8.

insert image description here


1. Primary key constraints

The primary key constraint (PRIMARY KEY, abbreviated as PK) is the most important constraint in the database. Its function is to uniquely identify a record by a certain field in the constraint table. Therefore, using primary key constraints can quickly look up records in the table. Just like a person's ID card, a student's student number, etc., the value of the field set as the primary key cannot be repeated (unique), nor can it be empty (non-empty), otherwise a record cannot be uniquely identified.

A primary key can be a single field or a combination of multiple fields. For the addition of a single-field primary key, you can use table-level constraints or column-level constraints; for the addition of multi-field primary keys, you can only use table-level constraints.

Two, non-null constraints

The non-null constraint (NOT NULL, abbreviated as NK) stipulates that the value of a specified field in a table cannot be empty (NULL). For a field with a non-null constraint set, when the inserted data is NULL, the database will prompt an error, resulting in that the data cannot be inserted.

Whether it is a single field or multiple fields, only column-level constraints can be used to add non-null constraints (non-null constraints have no table-level constraints).

Add not-null constraints to fields in existing tables

 alter   table student8 modify stu_sex varchar(1) not null;

Use the ALTER TABLE statement to drop NOT NULL constraints

alter  table student8 modify stu_sex varchar(1) null;

Three, the only constraint

The unique constraint (UNIQUE, abbreviated as UK) is relatively simple. It stipulates that the value of a certain field specified in a table cannot be repeated, that is, each value of this field is unique. If you want the value of a certain field not to be repeated, you can add this field as a unique constraint.

Both column-level constraints and table-level constraints can be used to add unique constraints on a single field or multiple fields.

4. Check constraints

Check constraints (CHECK) are used to limit the value range of a field, which can be defined as column-level constraints or table-level constraints. MySQL8 began to support check constraints.

5. Default value constraints

The default value constraint (DEFAULT) is used to specify the default value of the field. If a field that is set as a DEFAULT constraint does not insert a specific value, then the value of the field will be filled with the default value.

The setting of the default value constraint is the same as that of the not-null constraint, and only column-level constraints can be used.

Six, the field value automatically increases the constraint

An auto-increment constraint (AUTO_INCREMENT) can automatically increase the value of a field in a table. There can only be one auto-increment field in a table, and the field must have constraints defined (the constraints can be primary key constraints, unique constraints, and foreign key constraints). If the auto-increment field does not define constraints, the database will prompt "Incorrect table definition; there can be only one auto column and it must be defined as a key" error.

Since auto-increment constraints automatically generate unique IDs, auto-increment constraints are usually used with primary keys and are only applicable to integer types. In general, the value of the auto-increment constraint field will start from 1, and the value of the field will increase by 1 for each additional record.

Add an auto-increment constraint to a field in an existing table

/*创建表student11*/

 create   table student11 (

       stu_id int(10) primary key,

       stu_name varchar(3),

       stu_sex varchar (1)

);

/*为student11表中的主键字段添加自增约束*/

alter   table student11 modify stu_id int(10) auto_increment;

Use the ALTER TABLE statement to drop auto-increment constraints

alter   table studen11 modify stu_id int(10);

Seven, foreign key constraints

Foreign key constraints (FOREIGN KEY, abbreviated as FK) are used to implement the referential integrity of database tables. Foreign key constraints can make two tables closely combined, especially for cascading operations of modification or deletion, which will ensure data integrity.
A foreign key means that the value of a field in a table depends on the value of a field in another table, and the dependent field must have a primary key constraint or a unique constraint. The dependent table is usually called the parent table or the main table, and the table with foreign key constraints is called the child table or the secondary table.

Summarize

Integrity constraints in MySQL include entity integrity, referential integrity, and user-defined integrity.

Entity integrity: Each row in a table is required to have a unique primary key to ensure the uniqueness of data.
Referential integrity: It is required to refer to the primary key or foreign key of another table in one table to ensure data consistency and integrity.
User-defined integrity: users can define some constraints, such as non-null constraints, unique constraints, etc.

Supongo que te gusta

Origin blog.csdn.net/weixin_43905975/article/details/131884175
Recomendado
Clasificación