如何使用命令行更改MySQL表的外键

如何使用命令行更改MySQL表的外键

问题描述:

如何使用命令行更改MySQL中的现有表,将外键设置为另一个表?

How to alter an existing table in MySQL, setting foreign key to another table, using the command line?

您必须删除现有的foreign key并创建另一个.例如这样的

You have to drop existing foreign key and create another one. For example like this:

ALTER TABLE my_table DROP FOREIGN KEY my_key;
ALTER TABLE my_table ADD CONSTRAINT my_key FOREIGN KEY ('some_id') 
REFERENCES some_new_table ('some_other_id') ON UPDATE CASCADE ON DELETE CASCADE;