且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在mySql中创建外键时出现问题

更新时间:2022-11-08 18:27:39

表格可能是MyISAM格式,支持外键。



尝试将其转换为InnoDB:

 改变表用户引擎= InnoDB; 


I created a foreign key in my sql by the following statemnt..

ALTER TABLE `users` ADD FOREIGN KEY ( `id`) 
REFERENCES `user_login` (`user_id`) 
ON DELETE CASCADE ;

The creation appears to succeed then after that I execute a delete statement

DELETE From user_login WHERE user_id = 1576;

yet in users the row still exists that is referencing that. I open up the mysql workbench and it doesn't show any signs that the foreign key was created. Does anyone know why this would be? Or what I am doing wrong? It is a one-to-one relationship in the two tables.

The table may be in MyISAM format, which does not support foreign keys.

Try converting it to InnoDB first:

alter table users engine=InnoDB;