奇怪的 mysql 错误

发布于 2024-08-14 04:21:58 字数 1476 浏览 1 评论 0原文

你好,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄兵必败 2024-08-21 04:21:58

索引与外键不同。试试这个:

ALTER TABLE `addresses` DROP FOREIGN KEY `per_id`

An index is not the same as a foreign key. Try this instead:

ALTER TABLE `addresses` DROP FOREIGN KEY `per_id`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文