MySQL foreign key (the relationship between the table and the table)

Foreign key : to establish a relationship between two tables

Key grammar: foreign key (to establish the concept of Western foreign key field in the current table) references are associated with the table name (id)
  • The relationship between the three tables and table

    • Many

    • Many to many

    • One to One

  • Study the relationship between tables and table

    • If all data is stored in a table of the drawbacks:

      • 1. The structure is not clear ----> fatal

      • 2. waste of space ----> fatal

      • 3. Scalability poor ----> disadvantages can not be ignored

        • Similar to all py python code stored in a file, a strong coupling together ----> ---- decoupled> Split table

      • Wendy split solve the above table.

    • This time we need above-mentioned "foreign key", two tables to build a strong relationship.

  • Confirm the relationship between the table and the table (one to many, many to many, one)

    • Note: At this time have to stand in two positions to think, be considered from two tables to establish relations

      • Take the position of the employee table as an example: multiple employees can correspond to a department? The answer is obvious: Can! ! ! ! ! ! ! !

        Employees and departments: Many

         

      • Then position the department table from thinking of it: multiple departments can correspond to an employee? Is not it.

        Many-sector employees to form table

        Summary: Any way many-to-table relationship, said the jobs-to-many foreign key relationships.

No nonsense, directly on the code

# Create two tables
        1. You must first establish the association table, then association table

    # Is associated table:
        dep:
            create table dep(
                id int primary key auto_increment,
                dep_name varchar(16),
                dep_desc varchar(255)
            );

    # Association Table:
        emp:
            create table emp(
                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 dep(id)
            );
            
            
     # Insert data:
        - 1. The insert must be associated with a data table (DEP), and then inserted in the data relation table (EMP) is.
        # dep:
            insert into dep(dep_name, dep_desc) values
            ( 'Nb_ Ministry of Foreign Affairs', 'international ambassador sector'),
            ( 'Sb_ Teaching Department', 'made programmers department !!!!'),
            ( 'Technology', 'technology limited sector');

        # emp:
            insert into emp(name, age, gender, dep_id)
            values('tank', 17, 'male', 1),
            ('jason', 70, 'male', 2),
            ('sean', 50, 'male', 2),
            ( 'Egon', 88, 'male', 2),
            ('owen', 95, 'female', 3);
            
            
         # Error,
            insert into emp(name, age, gender, dep_id) values
            ( 'Cake', 100, 'others', 999);

    # Update data or delete data
        - update data
        # Error
        update emp set dep_id=100 where id=2;
        # Error
        update dep set id=100 where id=1;

        # Delete dep_id field is associated, in order to modify the association id field dep table.
        delete from emp where id=1;
        update dep set id=100 where id=1;

        # Delete: delete the records associated with the table, delete the associated record in the table
            - Remove the emp table is recorded dep_id 2
            delete from emp where dep_id=2;

            - and then delete the record id table dep 2
            delete from dep where id=2;   

Cascading update and cascade delete

	- on update cascade
        - on delete cascade
    - Create a table
        # Is 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
                );
                
         - insert data
        # dep:
            insert into dep2(dep_name, dep_desc) values
            ( 'Nb_ Ministry of Foreign Affairs', 'international ambassador sector'),
            ( 'Sb_ Teaching Department', 'made programmers department !!!!'),
            ( 'Technology', 'technology limited sector');

        # emp:
            insert into emp2(name, age, gender, dep_id)
            values('tank', 17, 'male', 1),
            ('jason', 70, 'male', 2),
            ('sean', 50, 'male', 2),
            ( 'Egon', 88, 'male', 2),
            ('owen', 95, 'female', 3);

            # Error,
            insert into emp(name, age, gender, dep_id) values
            ( 'Cake', 100, 'others', 999);


    - updating data or deleting data
        - update record
            update dep2 set id=200 where id=1;

        - Delete Record
            delete from dep2 where id=200;

 

Note: mysql as long as there are no many-to-many

Many to many:

  We must also stand to think about the position of two tables;

 

 - using the third table, the establishment of "many to many foreign key relationships" for the two tables.
        - 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
            );

    - insert data
        - book
        insert into book(title, price, book_content) values
        ( 'Golden bottles mei', 199, 'tells the story of the little hazy time'),
        ( 'Python from entry to breathe', 2000, 'the bald one night to learn how to'),
        ( 'Trisomy', 200, 'followed by big brother into the universe fantasy world')
        ;

        - author
        insert into author(name, age) values
        ( 'A', 68),
        ('jason', 88);

        - book2author:
        insert into book2author(book_id, author_id) values
        (1, 1),
        (1, 2),
        (2, 2),
        (3, 1);

        # Error, insert data, book_id, author_id must exist
        insert into book2author(book_id, author_id) values (4, 4);

        # Update or delete
            # Update
                - update book set price=6666 where id=1;
                - update book set id=4 where id=1;

            # Delete
                - delete from book where id=4;

 

One:

One to 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
            # Is associated table
            create table customer(
                id int primary key auto_increment,
                name varchar(16),
                media varchar(32)
            );


            # Association 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
            );

        - insert data
            insert into customer(name, media) values
            ('hcy', 'facebook'),
            ( 'Zsb1', 'la');
            ('zsb2', 'vk'),
            ( 'Hb', 'beating');


            insert into student(addr, phone, id_card, customer_id) values
            ( 'Shanghai', '15214546711', '440888888888888888', 1),
            ( 'Beijing' '18888888888', '440777777777777777', 2);

            # Error, one to one, one to one relationship must
            insert into student(addr, phone, id_card, customer_id) values ('上海', '15214546711', '440888888888888888', 1);

  

  

  

 

 

 

Guess you like

Origin www.cnblogs.com/lvguchujiu/p/12031223.html