How to alter an existing table in MySQL, setting foreign key to another table, using the command line?
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
I was able to achieve the same thing by :
You have to drop existing
foreign key
and create another one. For example like this:Execute
help alter table
atmysql
command prompt and the output is very much self explanatory.Look for
add constraint
withforeign key
clause and apply it on your table.If you have multiple foreign keys chained together and you get an error that ends with
errorno 15x
it most likely means that there are other tables that are dependent on the foreign key that you're trying to drop.To drop the foreign key when you get that error, you will need to do
SET FOREIGN_KEY_CHECKS = 0;
and then you must first drop the foreign keys on the tables that don't have any other tables dependent on them. You can then successfully drop the foreign keys on the next table up in the chain and so on.When you're done, make sure you run
SET FOREIGN_KEY_CHECKS = 1;
again.