MySQL FK syntax: insert column called practice into table cred_insurances that is FK to table practices
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
精彩评论