我是MySql的新手。 我有一個與foriegnKeys插入到另一個表的問題。下面提到的是父表和子表的語法。用foreignKeys插入表的Mysql問題
===家長====
CREATE TABLE `userTable_temp` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`contactNum` varchar(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `contactNum` (`contactNum`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
====兒童=====
CREATE TABLE `friendTable_temp` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`userId` int(32) DEFAULT NULL,
`friendId` int(32) DEFAULT NULL,
`last_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `uc_test` (`userId`,`friendId`),
KEY `friendId` (`friendId`),
CONSTRAINT `friendTable_temp_ibfk_2` FOREIGN KEY (`friendId`) REFERENCES `userTable` (`id`),
CONSTRAINT `friendTable_temp_ibfk_1` FOREIGN KEY (`userId`) REFERENCES `userTable` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1
我可以插入一些記錄friendTable_temp ,但在第四次插入後,我總是得到以下錯誤
"Cannot add or update a child row: a foreign key constraint fails (`contacts_db`.`friendTable_temp`, CONSTRAINT `friendTable_temp_ibfk_1` FOREIGN KEY (`userId`) REFERENCES `userTable` (`id`))"
發生此問題時,我在UserTable_temp中有以下數據:
mysql> select id,contactNum from userTable_temp;
+ ---- + --------------- +
| id | contactNum |
+ ---- + --------------- +
| 1 | 123455677 |
| 2 | 56465465464 |
| 3 | 567576567 |
| 4 | 1231231231 |
| 5 | 35453453454 |
| 6 | 45645645645 |
+ ---- + --------------- +
6組(0.00秒)
和上面的錯誤行發生時,我試圖這樣做
insert into friendTable_temp(userId,friendId)values(6,1);
你能幫我找出我做錯了什麼?插入friendTable_temp(userId,friendId)值(5,1);插入到friendTable_temp(userId,friendId)值(5,1);
查詢行,1行受影響(0.00秒)
感謝
int(32)不是有效的聲明。 –
@Trinity從什麼時候開始?括號中的數字就是顯示寬度 – Phil