添加外键时,错误'Table'。\ schema \ table'已存在

时间:2012-08-09 21:41:00

标签: mysql sql foreign-keys

我有以下现有表格。

显示创建表格ExpeditedAddress;

CREATE TABLE `expeditedaddress` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `description` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `city` varchar(255) NOT NULL,
  `country` varchar(255) NOT NULL,
  `stateProvince` varchar(255) NOT NULL,
  `zipPostal` varchar(255) NOT NULL,
  `brokerageId` bigint(20) NOT NULL,
  `timezoneCode` varchar(100) NOT NULL,
  `productionFacilityId` bigint(20) NOT NULL,
  `deliveryCutoffTime` time NOT NULL,
  `deliveredByTime` time NOT NULL,
  `daysToDeliver` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_ExpeditedAddress_Brokerage_ID` (`brokerageId`),
  KEY `FK_ExpeditedAddress_Country_StateProvince` (`country`,`stateProvince`),
  CONSTRAINT `ExpeditedAddress_ibfk_1` FOREIGN KEY (`brokerageId`) REFERENCES `brokerage` (`id`),
  CONSTRAINT `ExpeditedAddress_ibfk_2` FOREIGN KEY (`country`, `stateProvince`) REFERENCES `stateprovince` (`countryName`, `name`)
) ENGINE=InnoDB AUTO_INCREMENT=149 DEFAULT CHARSET=utf8;

引用此表

show create table ProductionFacility;

CREATE TABLE `productionfacility` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `description` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `city` varchar(255) NOT NULL,
  `country` varchar(255) NOT NULL,
  `stateProvince` varchar(255) NOT NULL,
  `zipPostal` varchar(255) NOT NULL,
  `timezoneCode` varchar(100) NOT NULL,
  `workingWeek` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_ProductionFacility_Country_StateProvince` (`country`,`stateProvince`),
  CONSTRAINT `productionfacility_ibfk_1` FOREIGN KEY (`country`, `stateProvince`) REFERENCES `stateprovince` (`countryName`, `name`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

然后,当我尝试添加一个像这样的外键时......

alter table ExpeditedAddress add foreign key FK_ExpeditedAddress_Productionfacility(productionFacilityId) references ProductionFacility(id);

我得到以下内容......

[2012-08-09 15:39:22] [42S01][1050] Table '.\realtorprint_dev_prod\expeditedaddress' already exists
[2012-08-09 15:39:22] [HY000][1025] Error on rename of '.\realtorprint_dev_prod\#sql-1d74_710a' to '.\realtorprint_dev_prod\expeditedaddress' (errno: -1)
[2012-08-09 15:39:22] [42S01][1050] Table '.\realtorprint_dev_prod\expeditedaddress' already exists

看起来像是一个非常奇怪的错误。列的类型都是“bigint(20)not null”。两个表都是InnoDB。引用的列是主键。

有人有什么想法吗?

此外,这是运行

的结果

显示引擎InnoDB状态;

=====================================
120809 15:45:41 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 54 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 9322 1_second, 9321 sleeps, 846 10_second, 894 background, 894 flush
srv_master_thread log flush and writes: 9323
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 520, signal count 520
Mutex spin waits 197, rounds 836, OS waits 12
RW-shared spins 499, rounds 14942, OS waits 498
RW-excl spins 2, rounds 242, OS waits 8
Spin rounds per wait: 4.24 mutex, 29.94 RW-shared, 121.00 RW-excl
------------------------
LATEST FOREIGN KEY ERROR
------------------------
120809 15:34:11 Transaction:
TRANSACTION 40CE, ACTIVE 0 sec inserting, thread declared inside InnoDB 485
mysql tables in use 2, locked 2
7 lock struct(s), heap size 1248, 16 row lock(s), undo log entries 7
MySQL thread id 28938, OS thread handle 0x16d4, query id 350923 localhost 127.0.0.1 rpuser copy to tmp table
alter table ExpeditedAddress add foreign key FK_ExpeditedAddress_Productionfacility(id) references ProductionFacility(id)
Foreign key constraint fails for table `realtorprint_dev_prod`.`#sql-1d74_710a`:
,
  CONSTRAINT `#sql-1d74_710a_ibfk_1` FOREIGN KEY (`id`) REFERENCES `productionfacility` (`id`)
Trying to add in child table, in index `PRIMARY` tuple:
DATA TUPLE: 15 fields;
 0: len 8; hex 8000000000000008; asc         ;;
 1: len 6; hex 0000000040ce; asc     @ ;;
 2: len 7; hex a3000005f90176; asc       v;;
 3: len 12; hex 536f757468204f6666696365; asc South Office;;
 4: len 25; hex 233235322c2031313438382d20323420537472656574205345; asc #252, 11488- 24 Street SE;;
 5: len 7; hex 43616c67617279; asc Calgary;;
 6: len 6; hex 43616e616461; asc Canada;;
 7: len 7; hex 416c6265727461; asc Alberta;;
 8: len 7; hex 54325a20344339; asc T2Z 4C9;;
 9: len 8; hex 8000000000000012; asc         ;;
 10: len 16; hex 416d65726963612f45646d6f6e746f6e; asc America/Edmonton;;
 11: len 8; hex 8000000000000001; asc         ;;
 12: len 3; hex 81adb0; asc    ;;
 13: len 3; hex 829810; asc    ;;
 14: len 4; hex 80000001; asc     ;;

But in parent table `realtorprint_dev_prod`.`productionfacility`, in index `PRIMARY`,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 11; compact format; info bits 0
 0: len 8; hex 8000000000000007; asc         ;;
 1: len 6; hex 00000000401f; asc     @ ;;
 2: len 7; hex b6000003a10176; asc       v;;
 3: len 16; hex 4461746147726f7570204c6f6e646f6e; asc DataGroup London;;
 4: len 21; hex 313837302043686561707369646520537472656574; asc 1870 Cheapside Street;;
 5: len 6; hex 4c6f6e646f6e; asc London;;
 6: len 6; hex 43616e616461; asc Canada;;
 7: len 7; hex 4f6e746172696f; asc Ontario;;
 8: len 7; hex 4e355620334537; asc N5V 3E7;;
 9: len 15; hex 416d65726963612f546f726f6e746f; asc America/Toronto;;
 10: len 0; hex ; asc ;;

InnoDB: Renaming table `realtorprint_dev_prod`.`#sql-1d74_710a` to `realtorprint_dev_prod`.`expeditedaddress` failed!
InnoDB: Renaming table `realtorprint_dev_prod`.`#sql-1d74_710a` to `realtorprint_dev_prod`.`expeditedaddress` failed!
InnoDB: Renaming table `realtorprint_dev_prod`.`#sql-1d74_710a` to `realtorprint_dev_prod`.`expeditedaddress` failed!
InnoDB: Renaming table `realtorprint_dev_prod`.`#sql-1d74_710b` to `realtorprint_dev_prod`.`expeditedaddress` failed!
------------
TRANSACTIONS
------------
Trx id counter 40FD
Purge done for trx's n:o < 40F2 undo n:o < 0
History list length 827
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 40F3, not started
MySQL thread id 28939, OS thread handle 0x2ed8, query id 350949 localhost 127.0.0.1 rpuser
show engine innodb status
--------
FILE I/O
--------
I/O thread 0 state: wait Windows aio (insert buffer thread)
I/O thread 1 state: wait Windows aio (log thread)
I/O thread 2 state: wait Windows aio (read thread)
I/O thread 3 state: wait Windows aio (read thread)
I/O thread 4 state: wait Windows aio (read thread)
I/O thread 5 state: wait Windows aio (read thread)
I/O thread 6 state: wait Windows aio (write thread)
I/O thread 7 state: wait Windows aio (write thread)
I/O thread 8 state: wait Windows aio (write thread)
I/O thread 9 state: wait Windows aio (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
 ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
1363 OS file reads, 17403 OS file writes, 4157 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.11 writes/s, 0.11 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 11 merges
merged operations:
 insert 11, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 1458971, node heap has 15 buffer(s)
3.54 hash searches/s, 12.39 non-hash searches/s
---
LOG
---
Log sequence number 121733086
Log flushed up to   121733086
Last checkpoint at  121601659
0 pending log writes, 0 pending chkp writes
2674 log i/o's done, 0.11 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 754434048; in additional pool allocated 0
Dictionary memory allocated 795806
Buffer pool size   44992
Free buffers       42494
Database pages     2483
Old database pages 896
Modified db pages  36
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 335, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 1352, created 1131, written 13915
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 2483, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Main thread id 7616, state: sleeping
Number of rows inserted 194766, updated 699, deleted 143, read 806318
2.74 inserts/s, 0.00 updates/s, 0.00 deletes/s, 2.74 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

1 个答案:

答案 0 :(得分:0)

如果查看“最新的外键错误”,那个长的错误会抱怨您尝试添加的外键被违反:您现有的数据不符合外键约束。

(我不是100%肯定这就是它的说法,它可能会抱怨索引损坏,在这种情况下你需要弄清楚你的索引如何变得腐败并重建索引,但我认为不是。我认为自从我上次看到它以来,它们刚刚更改了外键违反错误消息。