奇怪的 mysql 错误
你好,我有一个 mysql 数据库,其中有一个地址表。
这就是它的创建方式:
CREATE TABLE IF NOT EXISTS `addresses` (
`adr_id` int(11) NOT NULL AUTO_INCREMENT,
`per_id` int(11) NOT NULL,
`adr_street` varchar(50) NOT NULL,
`adr_houseno` int(11) DEFAULT NULL,
`adr_housenoadd` varchar(10) DEFAULT NULL,
`adr_postcode` varchar(25) NOT NULL,
`adr_city` varchar(20) NOT NULL,
`adr_type` varchar(45) DEFAULT NULL,
`cnt_id` int(11) NOT NULL,
`adr_date` date DEFAULT NULL,
`sys-mut-dt` timestamp NULL DEFAULT NULL,
`sys-mut-user` varchar(20) DEFAULT NULL,
`sys-mut-id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`adr_id`),
KEY `per_id` (`per_id`),
KEY `cnt_id` (`cnt_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
ALTER TABLE `addresses`
ADD CONSTRAINT `cnt_id` FOREIGN KEY (`cnt_id`) REFERENCES `countries` (`cnt_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `per_id` FOREIGN KEY (`per_id`) REFERENCES `persons` (`per_id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
现在我想删除约束 per_id 因为我认为它不是必需的。所以我删除了它,并在这里看到错误:
SQL 查询:
ALTER TABLE `addresses` DROP INDEX `per_id`
MySQL said: Documentation
#1025 - Error on rename of './ladosa/#sql-521_d2' to './ladosa/addresses' (errno: 150)
然后我决定删除表并在没有约束的情况下再次创建,但是当我删除表时,我得到:
#1217 - Cannot delete or update a parent row: a foreign key constraint fails
那么我怎样才能摆脱这个问题呢?
Hello I have a mysql database with in it an addresses table.
This is the way it's created:
CREATE TABLE IF NOT EXISTS `addresses` (
`adr_id` int(11) NOT NULL AUTO_INCREMENT,
`per_id` int(11) NOT NULL,
`adr_street` varchar(50) NOT NULL,
`adr_houseno` int(11) DEFAULT NULL,
`adr_housenoadd` varchar(10) DEFAULT NULL,
`adr_postcode` varchar(25) NOT NULL,
`adr_city` varchar(20) NOT NULL,
`adr_type` varchar(45) DEFAULT NULL,
`cnt_id` int(11) NOT NULL,
`adr_date` date DEFAULT NULL,
`sys-mut-dt` timestamp NULL DEFAULT NULL,
`sys-mut-user` varchar(20) DEFAULT NULL,
`sys-mut-id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`adr_id`),
KEY `per_id` (`per_id`),
KEY `cnt_id` (`cnt_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
ALTER TABLE `addresses`
ADD CONSTRAINT `cnt_id` FOREIGN KEY (`cnt_id`) REFERENCES `countries` (`cnt_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `per_id` FOREIGN KEY (`per_id`) REFERENCES `persons` (`per_id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
Now i wanted to delete the constraint per_id since I decided it is not nessecary. So i deleted it and see here the error:
SQL query:
ALTER TABLE `addresses` DROP INDEX `per_id`
MySQL said: Documentation
#1025 - Error on rename of './ladosa/#sql-521_d2' to './ladosa/addresses' (errno: 150)
Then i decided to drop the table and created again without the constraint but when i did a drop table i get:
#1217 - Cannot delete or update a parent row: a foreign key constraint fails
so how can i get out of this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
索引与外键不同。试试这个:
An index is not the same as a foreign key. Try this instead: