简体   繁体   中英

Adding foreign key Error 150

Ok look I know there are many questions out there about error 150.

But as SO says, " If those answers do not fully address your question, please ask a new question." so i am asking a new question.

I have two tables tableA and tableB, both engine are 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)

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

Then I tried again same query but same error. So here's the situation so far

  • both tables are using same engine
  • both tables have compatible column url same datatype varchar(100) NOT NULL default ""

show create table tableA gives;

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

 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

 `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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM