【Constraint Constraint Constraint】

constraint

Concept: Limit the data in the table to ensure the correctness, validity and integrity of the data.
Classification:
1. Primary key constraint: primary key
2. Non-null constraint: not null
3. Unique constraint: unique
4. External constraint: foreign key
non-null constraint: not null: value cannot be null
1. Add constraints when creating a table
CREATE TABLE stu(
id INT,
name VARCHAR(20) NOT NULL
);
2. Add a non-null constraint after creating the table
ALTER TABLE stu MODIFY name VARCHAR(20) NOT NULL;
3. Delete the non-null constraint of name
ALTER TABLE stu MODIFY NAME VARCHAR (20);
Unique constraint: unique, the value cannot be repeated
1. When creating a table, add a unique constraint
CREATE TABLE stu(
id INT,
phone_number VARCHAR(20) UNIQUE
);
Note: In mysql, there can be many column values ​​limited by the unique constraint null
2. Add a unique constraint after creating the table
ALTER TABLE stu MODIEF phone_number VARCHAR(20) UNIQUE;
3. Delete the unique constraint
ALTER TABLE stu DROP INDEX phone_number;
primary key constraint: primary key
1. Note:
1. Meaning: non-empty and unique
2. A table can only have one field as the primary key
3. The primary key is the unique identifier of the records in the table
2. When creating When adding a table, add a primary key constraint
CREATE TABLE stu(
id INT PRIMARY KEY, – add a primary key constraint to id
NAME VARCHAR(20)
);
3. After creating a table, add a primary key
ALTER TABLE stu MODIFY id INT PRIMARY KEY;
4. Delete the primary key
ALTER TABLE stu DROP PRIMARY KEY;
5. Automatic growth:
1. Concept: If a column is of numeric type, use auto_increment to complete the automatic growth of the value
2. When creating a table, add a primary key constraint and complete the primary key self-growth
CREATE TABLE stu (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR
);
3. Add auto growth:
ALTER TABLE stu MODIFY id INT AUTO_INCREMENT;
4. Delete auto growth:
ALTER TABLE stu MODIFY id INT;
External constraints: FOREIGN KEY makes the relationship between the table and the table, so as to ensure the correctness of the data
1. When creating a table, you can add a foreign key
Syntax:
CREATE TABLE table name (
…,
– foreign key column
CONSTRAINT Foreign key name FOREIGN KEY (foreign key column name) REFERENCES main table name (main table column name); );
2.
After creating the table, add foreign key
ALTER TABLE table name ADD CONSTRAINT foreign key name FOREIGN KEY (foreign key field name) REFERENCES Main table name (main table column name);
3. Delete foreign key
ALTER TABLE table name DROP FOREIGN KEY foreign key name;
4. Cascade operation
1. Add cascade operation:
Syntax: 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. Cascade update: ON UPDATE CASCADE
2. Cascade delete: ON DELETE CASCADE

Guess you like

Origin blog.csdn.net/RSssr/article/details/109728409