How to use the keyword 'references' in MySQL?

后端 未结 5 1003
庸人自扰
庸人自扰 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.

提交回复
热议问题