# # Database backup and restore
1. Command line:
* Backup: mysqldump -u username -p password database name> save path
* Restore:
1. Log in to the database
2. Create a database
3. Use the database
4. Execute the file. source file path
2. Graphical tools: unwritten
# # 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, the value cannot be null
3. Unique constraint: unique
4. Foreign key constraint: foreign key
# Non-empty constraints
1. Add a non-empty constraint when creating a table:
CREATE TABLE stu ( id INT , NAME VARCHAR ( 20 ) NOT NULL - name is not empty );
2. After creating the table, add a non-empty constraint:
ALTER TABLE stu MODIFY NAME VARCHAR(20) NOT NULL;
3. Remove the non-null constraint of name:
ALTER TABLE stu MODIFY NAME VARCHAR(20);
# Unique constraint: unique, value cannot be repeated
1. When creating a table, add a unique constraint
CREATE TABLE stu ( id INT , phone_number VARCHAR ( 20 ) UNIQUE - added unique constraint );
* Note that in mysql, the value of a column restricted by a unique constraint can have multiple nulls
2. Remove the unique constraint
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: Not 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 a table, add a primary key constraint
Create Table STU ( id int Primary Key , - to add a primary key constraint id name VARCHAR ( 20 is ) );
3. Delete the primary key
-- 错误 alter table stu modify id int ; ALTER TABLE stu DROP PRIMARY KEY;
4. After creating the table, add the primary key
ALTER TABLE stu MODIFY id INT PRIMARY KEY;
5. Automatic growth:
1. Concept: If a column is of numeric type, use auto_increment to complete the automatic growth
2. When creating the table, add the primary key constraint, and complete the primary key self-growth
Create Table STU ( id int Primary Key AUTO_INCREMENT, - to add a primary key constraint id name VARCHAR ( 20 is ) );
3. Delete 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 has a relationship with the table, thereby ensuring the accuracy of the data.
1. When creating a table, you can add a foreign key
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. Delete the foreign key
ALTER TABLE table name DROP FOREIGN KEY foreign key name;
3. After creating the table, add the 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);
4. Cascade operation
1. Add cascading operations
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