开发者

MySQL FK syntax: insert column called practice into table cred_insurances that is FK to table practices

开发者 https://www.devze.com 2023-01-21 18:27 出处:网络
I need to insert a column called \"practice\" into table \"cred_insurances\" that is a FK referencing table \"pr开发者_开发问答actices\" PK \"id\"You will need to ensure that your MySQL table is using

I need to insert a column called "practice" into table "cred_insurances" that is a FK referencing table "pr开发者_开发问答actices" PK "id"


You will need to ensure that your MySQL table is using the InnoDB engine, by running the following from the mysql prompt.

show create table cred_insurances

the output will include (towards the bottom) the text ENGINE=.... If it is not InnodDB, then you will first need to convert it using the following SQL. You may need to do this to the parent table as well.

ALTER TABLE cred_insurances ENGINE=InnoDB

Then you can add a column and a foreign key constraint with the following command:

ALTER TABLE cred_insurances
    ADD practice INT,
    ADD CONSTRAINT fk_practice
    FOREIGN KEY (practice) REFERENCES practices (ID)

If you are having difficulties with errors whilst adding a foreign key, try the following command to get more detailed information on the error.

SHOW ENGINE INNODB STATUS
0

精彩评论

暂无评论...
验证码 换一张
取 消