A constraint:
Concept: The data in the table are defined to ensure the accuracy, effectiveness and integrity of data.
classification:
1, the primary key constraint: primary key
2, non-empty constraint: not null
3, the only constraint: unique
4, the foreign key constraint: foreign key
--- Non-empty constraint: not null, the value can not be null
1. Create a table to add a constraint
CREATE TABLEstu(
id INT, NAME VARCHAR (20) NOT NULL - name is non-empty);
2. Create a table after adding non-null constraint
ALTER TABLE stu MODIFY NAME VARCHAR(20) NOT NULL;
3. Delete the name of non-empty constraint
ALTER TABLE stu MODIFY NAME VARCHAR(20);
--- The only constraint: unique, value can not be repeated
1. Create a table, add a unique constraint
CREATE TABLE stu(
id INT,
phone_number VARCHAR (20) UNIQUE - add a unique constraint
);
* Note: mysql, the unique constraints defined by a plurality of columns may be null
2. Drop the unique constraints
ALTER TABLE stu DROP INDEX phone_number;
3. After creating the table, add a unique constraint
ALTER TABLE stu MODIFY phone_number VARCHAR(20) UNIQUE;
--- primary key constraint: primary key.
1. Note:
1. Meaning: a non-empty and the only
2. A table can have only one primary key field
3. The primary key is a unique identifier recorded in the table
2. When you create a table, add a primary key constraint
create table stu(
id int primary key, - adding to the primary key constraint id
name varchar(20)
);
3. Drop Primary
alter table stu modify id int; - Error
ALTER TABLE stu DROP PRIMARY KEY; - the right way
4. After creating the table, add a primary key
ALTER TABLE stu MODIFY id INT PRIMARY KEY;
5. Automatic growth:
1. The concept: If a column is a numeric type, use auto_increment can be done automatically increase worth
2. When you create a table, add a primary key constraint, and the completion of the primary key from the growth
create table stu(
id int primary key auto_increment, - to add a primary key constraint id
name varchar(20)
);
3. Remove the automatic growth
ALTER TABLE stu MODIFY id INT;
4. Add automatic growth
ALTER TABLE stu MODIFY id INT AUTO_INCREMENT;
--- foreign key constraint: foreign key (so that the table to produce table relationship, so as to ensure the correctness of the data.)
1. When you create a table, you can add foreign keys
grammar:
create table 表名(
....
Foreign key column
the name of the foreign key constraint foreign key (foreign key column name) references the name of the primary table (the primary table column names)
);
2. Delete the foreign key
ALTER TABLE table DROP FOREIGN KEY foreign key name;
3. After you create a table, add a foreign key
ALTER TABLE table name ADD CONSTRAINT foreign key name FOREIGN KEY (foreign key field name) REFERENCES name master table (the primary table column names);
4. cascade operation
1. Add the cascade operation
Syntax: ALTER TABLE table name ADD CONSTRAINT foreign key name
FOREIGN KEY (foreign key field name) REFERENCES name master table (the primary table column names) ON UPDATE CASCADE ON DELETE CASCADE;
2. Category:
1. Cascade Update: ON UPDATE CASCADE
2. cascading delete: ON DELETE CASCADE