How to use the keyword 'references' in MySQL?

后端 未结 5 1008
庸人自扰
庸人自扰 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: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;
    

提交回复
热议问题