error in adding foreign key
-
24-10-2019 - |
문제
I have two tables: tab1,tab2
I want add a foreign key to tab2 and wrote this query
ALTER TABLE tab2
ADD FOREIGN KEY(name) REFERENCES tab1(name)
But I get this error:
error 1005(HY000):can't create table 'club.#sql-6f0_2' (errno:150)
What is wrong?
Edit
i write this tables only for test.
tab1
name char(20) private key
lname char(20)
tab2
ssn int private key
name char(20)
해결책
I answered this for you about an hour ago in a comment on your other question, but here it is again:
You can get details on that error by running SHOW ENGINE INNODB STATUS\G
and reading the LATEST FOREIGN KEY ERROR
section.
The most likely reasons for this failure:
- tab1.name and tab2.name are not exactly the same data type
- tab1.name is not unique
다른 팁
try this:
alter table tab2
add foreign key my_key(name) references tab1(name)
maybe Your key should have a name?
You can not define Foreign Keys on default MySQL storage engines (ISAM, MyISAM), use InnoDB as engine, then add foreign key constraints.
제휴하지 않습니다 StackOverflow