当前位置:网站首页>SQL related knowledge - constraints

SQL related knowledge - constraints

2022-06-26 04:05:00 Code Xiaoyou

The concept of constraints : Limit the data in the table , Make sure the data is correct , Effectiveness and integrity .

classification :

        1. Primary key constraint :primary key

        2. Non empty constraint :not Null

        3. Unique constraint :unique

        4. Foreign key constraints :foreign key

Non empty constraint :not null The value of a column cannot be empty

1. Fields with constraints are required , Cannot save to table without filling

1. Add constraints when creating tables

CREATE TABLE stu(
id INT,
NAME VARCHAR(20) NOT NULL
);

2. After creating the table , Adding constraints

alter table stu change name name varchar(20) not null;

3. Delete non empty constraints

ALTER TABLE stu CHANGE NAME NAME VARCHAR(20);

Unique constraint :unique The value of a column cannot be repeated

1. Be careful :

        The only constraint can have NULL value , But there can only be one record for NULL

2. When the table is created , Add unique constraints

CREATE TABLE stu(
id INT,
NAME VARCHAR(20) UNIQUE
);

3. Delete unique constraint

alter table stu drop index name;

4. After the table is created , Add unique constraints

alter table stu modify name varchar(20) unique;

Primary key constraint :primary key  Non empty and unique

        1. A table can only have one field as the primary key .

        2. The primary key is the unique identification of the record in the table

1. When the table is created , Add primary key

CREATE TABLE stu(
id INT PRIMARY KEY,
NAME VARCHAR(20) 
);

CREATE TABLE stu1(
id INT,
NAME VARCHAR(20),
PRIMARY KEY(NAME)
);

2. Delete primary key

alter table stu drop primary key;

3. After creating the table , Add primary key

alter table stu chane name name varchar(20) primary key;

4. Automatic growth

        1. Concept : If a column is of numeric type , Use auto_increment Can be used to achieve automatic value growth

        2. When the table is created , Add primary key constraint , And complete the primary key self growth

CREATE TABLE stu(
id INT PRIMARY KEY auto_increment,
NAME VARCHAR(20) 
);

        3. Delete auto growth

alter table stu change id id int;

        4. Add autogrow

alter table stu change id id int auto_increment;

Foreign key constraints :foreign key, Let tables relate to tables , So as to ensure the correctness of the data .

        1. When the table is created , Add foreign keys

                  grammar :create table Table name (

                        ''''''

                        Foreign key column ,

                        constraint Foreign key name foreign key ( Foreign key field name )references Main table name ( Main table column name );

                );

CREATE TABLE dep(
department_id INT PRIMARY KEY,
department_name VARCHAR(20)
);


CREATE TABLE stu(
id INT PRIMARY KEY,
NAME VARCHAR(20),
department_id INT,
CONSTRAINT stu_dep_fk FOREIGN KEY (department_id)  REFERENCES dep(department_id)
);

2. Delete foreign key

alter table Table name drop foreign key Name of the foreign key ;

3. After creating the table , Add foreign keys

alter table Table name add constraint Foreign key name foreign key ( Foreign key field name )references Main table name ( Main table column name );

 4. Cascade operation

        1. Add cascade operation

                grammar :alter table Table name add constraint Foreign key name foreign key ( Foreign key field name )references Main table name ( Main table column name ) on update cascade on delete cascade;

        2. classification

                1. update cascade :on update cascade

                2. cascading deletion :on delete cascade

原网站

版权声明
本文为[Code Xiaoyou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180540094945.html