[英]Adding foreign key Error 150
Ok look I know there are many questions out there about error 150. 好的,我知道关于错误150的问题很多。
But as SO says, " If those answers do not fully address your question, please ask a new question." 但正如SO所说:“如果这些答案不能完全解决您的问题,请提出一个新问题。” so i am asking a new question.
所以我问一个新问题。
I have two tables tableA and tableB, both engine are innoDB 我有两个表tableA和tableB,两个引擎都是innoDB
My query alter table tableA add foreign key (url) references tableB(url)
gives Error 1005 (HY000): can't create table myDatabaseName.#sql-3134 e52(error 150)
我的查询
alter table tableA add foreign key (url) references tableB(url)
给出Error 1005 (HY000): can't create table myDatabaseName.#sql-3134 e52(error 150)
So here's what i did. 所以这就是我所做的。
alter tableA drop column url;
alter tableA add column url varchar(100) NOT NULL default "";
alter tableB drop column url;
alter tableB add column url varchar(100) NOT NULL default "";
alter tableB add primary key (url,bName,pID);
alter tableA engine = innodb;
alter tableB engine = innodb;
to confirm i checked SHOW TABLE STATUS
both gave same innoDB engine 确认我检查了
SHOW TABLE STATUS
都赋予了相同的innoDB引擎
Then I tried again same query but same error. 然后我再次尝试了相同的查询,但相同的错误。 So here's the situation so far
所以到目前为止的情况
varchar(100) NOT NULL default ""
varchar(100) NOT NULL default ""
show create table tableA
gives; show create table tableA
给出的内容;
CREATE TABLE `tableA` (
`url` varchar(100) NOT NULL DEFAULT '',
`tNo` int(11) NOT NULL,
`bName` varchar(40) NOT NULL,
`pID` int(11) NOT NULL,
`oprNo` int(11) DEFAULT NULL,
`found` int(11) DEFAULT NULL,
`fix` int(11) DEFAULT NULL,
`fixStatus` varchar(40) DEFAULT NULL,
`fixDate` date DEFAULT NULL,
`releaseStatus` varchar(40) DEFAULT NULL,
`releaseDate` date DEFAULT NULL,
PRIMARY KEY (`url`,`tNo`),
CONSTRAINT `tableA_ibfk_1` FOREIGN KEY (`url`) REFERENCES `tableB` (`url`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
show create table tableB
gives show create table tableB
给出
CREATE TABLE `tableB` (
`url` varchar(100) NOT NULL DEFAULT '',
`bName` varchar(40) NOT NULL,
`pID` int(11) NOT NULL,
PRIMARY KEY (`url`,`bName`,`pID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
update I was able to add one foreign key url using mysql workbench But same error is coming for other fields 更新我能够使用mysql workbench添加一个外键URL,但是其他字段也会出现相同的错误
`alter table tableA add foreign key (bName) references tableB(bName)`<br>
`alter table tableA add foreign key (pID) references tableB(pID)`
To create FK as belows: 要创建FK,如下所示:
ALTER TABLE tableA ADD FOREIGN KEY (bName) REFERENCES tableB(bName);
ALTER TABLE tableA ADD FOREIGN KEY (pID) REFERENCES tableB(pID);
You should create INDEX(bName)
, INDEX(pID)
on tableB as follows: 您应该在tableB上创建
INDEX(bName)
, INDEX(pID)
,如下所示:
mysql> SHOW CREATE TABLE tableB\G
*************************** 1. row ***************************
Table: tableB
Create Table: CREATE TABLE `tableB` (
`url` varchar(100) NOT NULL DEFAULT '',
`bName` varchar(40) NOT NULL,
`pID` int(11) NOT NULL,
PRIMARY KEY (`url`,`bName`,`pID`),
KEY `bName` (`bName`),
KEY `pID` (`pID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> alter table tableA add foreign key (bName) references tableB(bName);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table tableA add foreign key (pID) references tableB(pID);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.