How to use the keyword 'references' in MySQL?

后端 未结 5 992
庸人自扰
庸人自扰 2020-12-13 19:08

How is the references keyword used when creating a table?

Let\'s say I want to create two tables person and hobby and I want t

相关标签:
5条回答
  • 2020-12-13 19:19

    Reference keyword is used actually to know where the foreign key has come. That means which is the table name and what is the name of this in that table.

    I say this is correct :

    CREATE TABLE person (person_id INT NOT NULL, 
    PRIMARY KEY (person_id));
    
    CREATE TABLE hobby (hobby_id INT NOT NULL, person_id INT NOT NULL,
    PRIMARY KEY(hobby_id),
    FOREIGN KEY(person_id) REFERENCES person(person_id));
    

    Then, look at this line :

    FOREIGN KEY(person_id) REFERENCES person(person_id));
    

    Here person_id is the foreign key and it has come from person table and in that table it's name is person_id... That's it.

    0 讨论(0)
  • 2020-12-13 19:25

    Here is an example of how you can use it.

    create table hobby(id int references person(id),person_id int,hobby_varchar(20), primary key(id));
    

    For what it means, references allows us to to specify the target table column to which a foreign key refers.

    0 讨论(0)
  • 2020-12-13 19:29

    Create the hobby table similarly to this:

    CREATE TABLE hobby (
      id INT NOT NULL AUTO_INCREMENT,
      person_id INT NOT NULL,
      hobby_name VARCHAR(255),
      PRIMARY KEY(id),
      FOREIGN KEY(person_id) REFERENCES person(id))
    
    0 讨论(0)
  • 2020-12-13 19:30
    CREATE TABLE person (person_id INT NOT NULL, 
    PRIMARY KEY (person_id));
    
    CREATE TABLE hobby (hobby_id INT NOT NULL, person_id INT NOT NULL,
    PRIMARY KEY(hobby_id),
    FOREIGN KEY(person_id) REFERENCES person(person_id));
    

    The references keyword is used to define which table and column is used in a foreign key relationship. This means that a record in the hobby table must have a person_id that exists in the person table or else at the time of insert you will receive an error that the key does not exist.

    To answer your question above about what "ON DELETE CASCADE" does, it allows you to delete a parent key record (in person) and it's corresponding children records (in hobby) without having to delete all the children records first.

    To clarify, if you have children records attached to a primary key entry and you attempt to delete the primary key entry like:

    DELETE FROM person where person_id = 1;
    

    without having the DELETE ON CASCADE, you would receive an error if any records in hobby had person_id's of 1. You would have delete all of those records first before doing the delete above. With DELETE ON CASCADE used, the above delete would succeed and automatically delete any and all records from table hobby table linked to the person_id being deleted from the primary key table.

    0 讨论(0)
  • 2020-12-13 19:34

    Here is an example directly from MySQL website:

    CREATE TABLE parent (id INT NOT NULL,
                         PRIMARY KEY (id)
    ) ENGINE=INNODB;
    
    CREATE TABLE child (id INT, parent_id INT,
                        INDEX par_ind (parent_id),
                        FOREIGN KEY (parent_id) REFERENCES parent(id)
                        ON DELETE CASCADE
    ) ENGINE=INNODB;
    
    0 讨论(0)
提交回复
热议问题