[MySQL] Table integrity constraints - non-foreign key constraints

First, the integrity constraints of the table

        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 types of integrity constraints, as shown in the table. The Check constraint is the support provided in MySQL8.

 

2. Non-foreign key constraints

1. Primary key constraints and auto-increment constraints

A primary key constraint means that if a primary key constraint is added to a field, then the value of this field can uniquely identify a record. An auto-increment constraint means that the value of the constrained field is automatically incremented. Primary key constraints and self-increment constraints are generally used together, and are often used in constraint sequence numbers.

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.

create   table student(
       stu_id int(10) primary key,
       stu_name varchar(3),
       stu_sex varchar (1)
);
/*为student表中的主键字段添加自增约束*/
alter   table student11 modify stu_id int(10) auto_increment;

Use the ALTER TABLE statement to drop the auto-increment constraint:

alter table student modify stu_id int(10);

2. Non-empty constraints

Constrained field values ​​cannot be empty.

3. Unique constraints

The value of a field with a unique constraint cannot be repeated.

4. Check constraints

When adding a check constraint, you can set the range of values, and values ​​that do not meet the range will not be added to the database table.

5. Default value constraints

After adding a default value constraint to a field, if the field does not have a value passed in, the default value will be used.

3. Classification of non-foreign key constraints

Non-foreign key constraints can be divided into two categories in terms of function:

  • Table-level constraints : Any one or more fields in the table can be constrained. It is independent from the column definition and is not included in the column definition; it is separated from the definition by a comma; the name of the column to be constrained must be indicated; for example:
    constraint pk_stu primary key (sno)  -- pk_stu 主键约束的名字
  • Column-level constraints : Included in a column definition, immediately following other definitions for that column, separated by spaces; no column name is required; for example:
    sno int(6) primary key auto_increment

1. Create a table using column-level constraints . The grammatical structure is: column name, column type constraint name

When creating the table containing the student data, add various non-foreign key constraints to it:

create table t_student(
	sno int(6) primary key auto_increment, -- 自增约束+主键约束
	sname varchar(5) not null, -- 非空约束
	sex char(1) default '男' check(sex='男' || sex='女'), -- 检查约束+默认值约束
	age int(3) check(18<=age<35),
	enterdate date,
	classname varchar(10),
	email varchar(15) unique -- 唯一约束
);

2. Use table-level constraints to build a table. The grammatical structure is: constraint The name given to this constraint Constraint name (field name + condition)

When creating the table containing the student data, add various non-foreign key constraints to it:

create table t_student(
        sno int(6) auto_increment, 
        sname varchar(5) not null, 
        sex char(1) default '男',
        age int(3),
        enterdate date,
        classname varchar(10),
        email varchar(15),
        constraint pk_stu primary key (sno),  -- pk_stu 主键约束的名字
        constraint ck_stu_sex check (sex = '男' || sex = '女'),
        constraint ck_stu_age check (age >= 18 and age <= 50),
        constraint uq_stu_email unique (email)
);

There is another way to write table-level constraints, which is to add constraints through SQL statements after the table is created, for example:

alter table t_student add constraint pk_stu primary key (sno) ; -- 主键约束
alter table t_student modify sno int(6) auto_increment; -- 修改自增条件
alter table t_student add constraint ck_stu_sex check (sex = '男' || sex = '女');
alter table t_student add constraint ck_stu_age check (age >= 18 and age <= 50);
alter table t_student add constraint uq_stu_email unique (email);

Guess you like

Origin blog.csdn.net/hold_on_qlc/article/details/129806437