0

我使用了以下语法。请在这个问题上帮助我创建外键。

mysql> create table userdetails(UserId int(3) not null, ClientId int(3) not null, UserName varchar(30), Password varchar(30) character set binary, RoleId int(3) not null, primary key(UserId, ClientId), foreign key(RoleId) references UserRoles(RoleId) on delete cascade on update cascade)ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.44 sec)

mysql> create table useraddress (UserId int(3) NOT NULL, Address1 varchar(100),Address2 varchar(100), City varchar(50), Pincode varchar(10), PhoneNumber varchar(20), MobileNumber varchar(20), foreign key(UserId) references userdetails(UserId)on delete cascade on update cascade) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.09 sec)

mysql> create table clientaddressdetails (Address1 varchar(100), Address2 varchar(100), City varchar(50), Pincode varchar(10), PhoneNumber varchar(20), MobileNumber varchar(20), ClientId int(3), foreign key(ClientId) references userdetails(ClientId)on delete cascade on update cascade) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ERROR 1005 (HY000): Can't create table '.\mydb\clientaddressdetails.frm' (errno: 150)
4

1 回答 1

4

“在创建外键约束时,MySQL 需要在引用表和被引用表上都有一个可用索引。如果引用表上的索引不存在,则会自动创建索引,但被引用表上的索引需要手动创建(来源)。您的似乎丢失了。”

请参阅MySQL 外键错误 1005 errno 150

于 2013-10-02T11:39:13.573 回答