mysql外键
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql外键相关的知识,希望对你有一定的参考价值。
https://stackoverflow.com/questions/1905470/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails
CREATE TABLE IF NOT EXISTS `advertisers` (
`advertiser_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`password` char(32) NOT NULL,
`email` varchar(128) NOT NULL,
`address` varchar(255) NOT NULL,
`phone` varchar(255) NOT NULL,
`fax` varchar(255) NOT NULL,
`session_token` char(30) NOT NULL,
PRIMARY KEY (`advertiser_id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
INSERT INTO `advertisers` (`advertiser_id`, `name`, `password`, `email`, `address`, `phone`, `fax`, `session_token`) VALUES
(1, ‘TEST COMPANY‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘);
CREATE TABLE IF NOT EXISTS `jobs` (
`job_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`advertiser_id` int(11) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`address` varchar(255) NOT NULL,
`time_added` int(11) NOT NULL,
`active` tinyint(1) NOT NULL,
`moderated` tinyint(1) NOT NULL,
PRIMARY KEY (`job_id`),
KEY `advertiser_id` (`advertiser_id`,`active`,`moderated`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
INSERT INTO `jobs` (`job_id`, `advertiser_id`, `name`, `shortdesc`, `longdesc`, `address`, `active`, `moderated`) VALUES
(1, 1, ‘TEST‘, ‘TESTTEST‘, ‘TESTTESTES‘, ‘‘, 0, 0);
ALTER TABLE `advertisers`
ADD CONSTRAINT `advertisers_ibfk_1` FOREIGN KEY (`advertiser_id`) REFERENCES `jobs` (`advertiser_id`);
1.第一种情况
advertisers 设置外键
advertisers_ibfk_1 advertiser_id db03 jobs advertiser_id CASCADE RESTRICT
那么
删除的话,父和字表都可以删除,如果RESTRICT,父表不能删除
更新和插入的话,必须先插入字表,然后在插入父表,而且advertiser_id必须一致
比如
INSERT INTO `jobs` (`job_id`, `advertiser_id`, `name`, `shortdesc`, `longdesc`, `address`, `active`, `moderated`) VALUES
(1, 2, ‘TEST‘, ‘TESTTEST‘, ‘TESTTESTES‘, ‘‘, 0, 0);
INSERT INTO `advertisers` (`advertiser_id`, `name`, `password`, `email`, `address`, `phone`, `fax`, `session_token`) VALUES
(2, ‘TEST COMPANY‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘);
2.第二种情况
如果两个表都设置了外键
ALTER TABLE `jobs`
ADD FOREIGN KEY (`advertiser_id`) REFERENCES `advertisers` (`advertiser_id`);
advertiser
advertisers_ibfk_1 advertiser_id db03 jobs advertiser_id CASCADE RESTRICT
jobs
jobs_ibfk_1 advertiser_id db03 advertisers advertiser_id RESTRICT RESTRICT
delete from advertisers where advertiser_id=‘2‘; 无法删除
INSERT INTO `advertisers` (`advertiser_id`, `name`, `password`, `email`, `address`, `phone`, `fax`, `session_token`) VALUES
(3, ‘TEST COMPANY‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘);
INSERT INTO `jobs` (`job_id`, `advertiser_id`, `name`, `shortdesc`, `longdesc`, `address`, `active`, `moderated`) VALUES
(2, 3, ‘TEST‘, ‘TESTTEST‘, ‘TESTTESTES‘, ‘‘, 0, 0);
执行两个insert全部报错
[SQL]INSERT INTO `jobs` (`job_id`, `advertiser_id`, `name`, `shortdesc`, `longdesc`, `address`, `active`, `moderated`) VALUES
(2, 3, ‘TEST‘, ‘TESTTEST‘, ‘TESTTESTES‘, ‘‘, 0, 0);
[Err] 1452 - Cannot add or update a child row: a foreign key constraint fails (`db03`.`jobs`, CONSTRAINT `jobs_ibfk_1` FOREIGN KEY (`advertiser_id`) REFERENCES `advertisers` (`advertiser_id`))
外键总结
1.CASCADE RESTRICT的区别
2.如果是RESTRICT,insert必须先插入字表,就是没有外键的表
3.如果是RESTRICT,delete必须先删父表,然后再删除子表
4.如果是整个drop 父表,还是会报错的,只能先禁用外键
本文出自 “要有梦想,万一实现了呢” 博客,请务必保留此出处http://szgb17.blog.51cto.com/340201/1971646
以上是关于mysql外键的主要内容,如果未能解决你的问题,请参考以下文章