1. Foreign Key: association relationship between the table (the primary table) (from the table) is associated with the table
2. The three kinds of correspondence relationship between foreign key: many, one-many
3. The rules of grammar and
And grammar rules:
Foreign key: Syntax: foreign key (relationship table of the current foreign key field) references are associated with the table name (id)
Rules: Create principle: create the associated table (the primary table), creating the associated table (the table); remove updates principle (no cascade delete): delete, update data associated with the associated table, then delete the associated data table in the
cascade delete: delete limit in order to solve the problem: delete the associated table (the primary table) the relationship can be deleted even associated table data (from table)
cascade syntax: when you create a foreign key in the related table plus on update cascade on delete cascade
dep_id int not null, foreign key(dep_id) references dep2(id) on update cascade on delete cascade
AUTO_INCREMENT: from zero by default, may be worth the time to write, writing the initial value is then incremented
to-many: two table is associated (the primary table) by an association table (Table) to achieve foreign key association of two tables
many: association table (the table) are related by a foreign key to the association table (the primary table) is not the only foreign keys
one: association table (the table) by the foreign key is associated with a unique decorrelation table (the primary table) foreign key unique application: a table field division table using too much, with examples
Many examples:
Create a table # was associated table: dep2: create table dep2( id int primary key auto_increment, dep_name varchar(16), dep_desc varchar(255) ); # Association table: EMP2: create table emp2( id int primary key auto_increment, name varchar(16), age int, gender enum('male', 'female', 'others') default 'male', dep_id int not null, foreign key(dep_id) references dep2(id) on update cascade on delete cascade );
One example:
One: - one relationship between the two, will be a large amount of data tables, split into two tables. - user_info: id, name, age, gender, hobby, id_card - user: id , name, age, detail_id(外键) - detail: id, gender, hobby, id_card user with the detail table to establish a one to one foreign key relationships. foreign key should be built at a higher frequency side. - Create a table # was linked table () the Create the Table the Customer ( id int primary key auto_increment, name varchar(16), media varchar(32) ); # Association table (Table) Create Table Student ( id int primary key auto_increment, addr varchar(255), phone char(11), id_card char(18), # Foreign key must be set to a unique customer_id int unique, foreign key(customer_id) references customer(id) on update cascade on delete cascade );
Many to many: the use of the third table to build two tables-many foreign key relationship
- book: create table book( id int primary key auto_increment, title varchar(20), price int, book_content varchar(255) ); - author: create table author( id int primary key auto_increment, name varchar(16), age int ); - book2author: create table book2author( id int primary key auto_increment, book_id int, author_id int, foreign key(book_id) references book(id) on update cascade on delete cascade, foreign key(author_id) references author(id) on update cascade on delete cascade );
4. Modify the operating table
Modifying operation table and column renaming
Table 5. Replication
The copy operation table: + Record copy table structure (key will not be copied: primary keys, foreign keys and indexes) mysql> create table new_service select * from service; Just copy the table structure # The select * from service where 1 = 2 ; ---> not real data, a table structure MySQL> Create Table new_customer SELECT * from Customer WHERE. 1 = 2;