3

我有一张表,我正在尝试更改其主键。这是表定义。

CREATE TABLE `tbl_customer` (
  `PersonId` int(11) NOT NULL,
  `Id` int(10) unsigned NOT NULL,
  `Name` varchar(100) collate utf8_spanish_ci NOT NULL,
  `Alias` varchar(50) collate utf8_spanish_ci NOT NULL,
  `Phone` varchar(30) collate utf8_spanish_ci default NULL,
  `Phone2` varchar(30) collate utf8_spanish_ci default NULL,
  `Email` varchar(50) collate utf8_spanish_ci default NULL,
  `Email2` varchar(50) collate utf8_spanish_ci default NULL,
  `RFC` varchar(13) collate utf8_spanish_ci default NULL,
  `AddressStreetName` varchar(45) collate utf8_spanish_ci default NULL,
  `AddressStreetNumber` varchar(45) collate utf8_spanish_ci default NULL,
  `AddressCityWard` varchar(45) collate utf8_spanish_ci default NULL,
  `AddressCityName` varchar(45) collate utf8_spanish_ci default NULL,
  `AddressStateName` varchar(45) collate utf8_spanish_ci default NULL,
  `AddressCountryName` varchar(45) collate utf8_spanish_ci default NULL,
  `AddressPostalCode` int(10) default NULL,
  `IsDistributor` tinyint(1) NOT NULL default '0' COMMENT '1 = Is Distributor, 0 = Is Not Distributor',
  `ParentCustomerId` int(10) NOT NULL default '11' COMMENT 'Our Id is 11, so by default, all customers right now are our children.',
  PRIMARY KEY  (`Id`),
  KEY `fk_tbl_cliente_tbl_cliente1_idx` (`ParentCustomerId`),
  KEY `fk_tbl_cliente_tbl_person1_idx` (`PersonId`),
  KEY `PersonId` (`PersonId`),
  KEY `PersonId_2` (`PersonId`),
  CONSTRAINT `fk_tbl_cliente_tbl_cliente1` FOREIGN KEY (`ParentCustomerId`) REFERENCES `tbl_customer` (`PersonId`),
  CONSTRAINT `fk_tbl_cliente_tbl_person1` FOREIGN KEY (`PersonId`) REFERENCES `zapata`.`tbl_person` (`Id`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci COMMENT='''Customer'' refers to a person or entity to which we provide '$$

现在,当我第一次尝试:

ALTER TABLE `tbl_customer` DROP PRIMARY KEY;

PRIMARY KEY的是Id。当我试图放下它时,我得到了..

Error Code: 1025. Error on rename of './services/#sql-29a_218cc7f' to './services/tbl_customer' (errno: 150)

因此,我删除了所有FOREIGN KEY引用此表和列的约束,但仍然出现相同的错误。我也去了SHOW ENGINE INNODB STATUS并发现了这一点:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
130226 14:41:11 Error in foreign key constraint of table services/tbl_employee_shift:
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 to the ones in table. Constraint:
,
  CONSTRAINT fk_tbl_employee_shift_tbl_customer1 FOREIGN KEY (CustomerId) REFERENCES services.tbl_customer (Id) ON UPDATE CASCADE

但是,该表services.tbl_employee_shift不存在(它存在一次,但在我尝试此更改前几周被删除)。所以我继续...

CREATE TABLE services.tbl_employee_shift(
    CustomerId INT (11)
);
ALTER TABLE services.tbl_employee_shift ADD CONSTRAINT fk_tbl_employee_shift_tbl_customer1 FOREIGN KEY (CustomerId) REFERENCES avatar.tbl_cliente (Id);
ALTER TABLE services.tbl_employee_shift DROP FOREIGN KEY fk_tbl_employee_shift_tbl_customer1;

它有效......但它没有更正必要的信息,似乎 InnoDB 仍然认为约束fk_tbl_employee_shift_tbl_customer1是有效的,因此,“防止主键丢失以保持一致性”......我使用的是 MySQL 5.0 .95。

编辑:这个问题没有解决,它已经解决了

只有当我们将数据库迁移到较新的服务器(相同的 mysql 版本)时,该问题才能得到纠正,似乎存在对幽灵外键(fk_tbl_employee_shift_tbl_customer1)的损坏/幽灵引用,这阻止了该列被删除。由于这个损坏的/ghostfk 不在新服务器中,因此我可以毫无问题地删除该列。我的猜测是这是一个错误,但不幸的是我无法重新创建它。

4

4 回答 4

3

听起来好像您在设置为 0tbl_employee_shift时掉线了:foreign_key_checks

设置foreign_key_checks为 0 也会影响数据定义语句:DROP SCHEMA删除一个模式,即使它包含具有由模式外的表引用的外键的表,并DROP TABLE删除具有由其他表引用的外键的表。

由于记录了此行为,因此必须将其视为设计使然,因此不是错误。

于 2013-03-06T13:02:45.053 回答
1

当外键公式错误时会发生错误。SQL 是正确的,我在本地主机中运行脚本,我得到同样的错误。解决方案是验证表 tbl_person 是使用引擎“InnoDB”创建的。

问候

于 2013-03-11T16:16:09.867 回答
0

今天刚刚处理了这个问题。在任何模式信息表中都没有两个受影响表的迹象。我搜索了各种系统数据库以寻找 FK,但无济于事。最后,删除数据库立即生效。

于 2014-09-23T23:08:06.960 回答
-1

我有一个类似的错误,mysql 要求你有键和外,类型和大小匹配,例如。person.id int (10) 必须映射到 (10) 中的 service_customer.person_id 如果类型和大小不同,mysql 会报错。

于 2013-03-12T08:05:32.070 回答