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

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

本文介绍了在mySql中创建外键时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ALTER TABLE`users`添加外键( `id`)
REFERENCES`user_login`(`user_id`)
ON DELETE CASCADE;

创建成功后,我执行一条删除语句

  DELETE从user_login WHERE user_id = 1576; 

然而在用户行中仍然存在引用它的行。我打开了mysql工作台,并没有显示任何外键创建的迹象。有谁知道这是为什么?或者我做错了什么?这是两个表中的一对一的关系。

解决方案

表格可能是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;

这篇关于在mySql中创建外键时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:49