Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL帮助可以';t删除行(错误号:150)外键约束_Mysql_Foreign Keys - Fatal编程技术网

MySQL帮助可以';t删除行(错误号:150)外键约束

MySQL帮助可以';t删除行(错误号:150)外键约束,mysql,foreign-keys,Mysql,Foreign Keys,在尝试删除我的一个表中的记录时,我得到了以下结果 #1451 - Cannot delete or update a parent row: a foreign key constraint fails (`pasremotedb`.`plans`, CONSTRAINT `FK_plans` FOREIGN KEY (`plan_id`) REFERENCES `plan_options` (`plan_id`)) 所以我做了一些研究,看起来我有一些乱七八糟的外键。我试图取下钥匙,但后来我得

在尝试删除我的一个表中的记录时,我得到了以下结果

#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`pasremotedb`.`plans`, CONSTRAINT `FK_plans` FOREIGN KEY (`plan_id`) REFERENCES `plan_options` (`plan_id`))
所以我做了一些研究,看起来我有一些乱七八糟的外键。我试图取下钥匙,但后来我得到了这个

mysql> ALTER TABLE `plan_options` DROP INDEX `plan_id`;
ERROR 1025 (HY000): Error on rename of './pasremotedb/#sql-1c0f_31ea' to './pasremotedb/plan_options' (errno: 150)
进行了更多的研究,并决定进行
显示INNODB状态
,以获取
最新外键错误
,结果如下:

110824 15:07:33 Error in foreign key constraint of table pasremotedb/plans:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
  CONSTRAINT "FK_plans" FOREIGN KEY ("plan_id") REFERENCES "plan_options" ("plan_id")
The index in the foreign key in table is "PRIMARY"
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

这就是我能做到的。我对MySQL dbs还不是很在行,当涉及到外键时,我肯定会挂断电话。有人能帮忙吗

看起来涉及的两个表是
计划
计划选项
。以下是两种结构:

--
-- Table structure for table `plan_options`
--

CREATE TABLE IF NOT EXISTS `plan_options` (
  `account_id` int(11) NOT NULL,
  `plan_id` tinyint(1) NOT NULL,
  `discipline_id` int(2) NOT NULL,
  `practice_type_id` int(1) NOT NULL,
  `discipline_other` varchar(100) NOT NULL,
  PRIMARY KEY (`account_id`),
  KEY `plan_id` (`plan_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `plan_options`
--

INSERT INTO `plan_options` (`account_id`, `plan_id`, `discipline_id`, `practice_type_id`, `discipline_other`) VALUES
(1, 3, 5, 1, ''),
(2, 2, 3, 1, ''),
(3, 1, 6, 1, ''),
(4, 2, 1, 2, ''),
(5, 3, 1, 1, ''),
(6, 2, 5, 1, ''),
(7, 2, 3, 1, ''),
(12, 2, 7, 2, 'MD'),
(13, 1, 2, 2, ''),
(14, 3, 1, 2, ''),
(16, 1, 1, 2, ''),
(18, 2, 7, 1, 'AMA Guides'),
(21, 2, 5, 1, '');

--
-- Constraints for dumped tables
--

--
-- Constraints for table `plan_options`
--
ALTER TABLE `plan_options`
  ADD CONSTRAINT `FK_plan_options` FOREIGN KEY (`account_id`) REFERENCES `account_details` (`account_id`);
--
-- Table structure for table `plans`
--

CREATE TABLE IF NOT EXISTS `plans` (
  `plan_id` tinyint(1) NOT NULL AUTO_INCREMENT,
  `plan_name` varchar(50) NOT NULL,
  PRIMARY KEY (`plan_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `plans`
--

INSERT INTO `plans` (`plan_id`, `plan_name`) VALUES
(1, 'Gold'),
(2, 'Platinum'),
(3, 'Supremacy');

--
-- Constraints for dumped tables
--

--
-- Constraints for table `plans`
--
ALTER TABLE `plans`
  ADD CONSTRAINT `FK_plans` FOREIGN KEY (`plan_id`) REFERENCES `plan_options` (`plan_id`);

外键位于
计划
表上:

但是请注意,这把钥匙(以及它阻止你的事实)可能是一件可取的事情。其原因是防止选项进入与计划无关的
plan\u options
表。因此,在删除以下内容时,您可能希望同时从两个表中删除:

DELETE FROM plans, plan_options
  WHERE plan_id = ?
DELETE FROM plans, plan_options
  WHERE plan_id = ?