无法创建表“student.#sql-f40_3” (错误号:150)
表1
create table personal(
id int not null auto_increment unique,
name char(20) not null,
age int not null,
city varchar(20) not null default 'Delhi'
);
insert into personal(name,age,city) values
('anubhav',22,'delhi'),
('rohit',24,'agra');
表2
create table applications(
app_id int(5) not null auto_increment unique,
city varchar(10) not null default 'Delhi'
);
insert into applications(city) values
('kolkata'),
('mumbai'),
('mumbai'),
('delhi'),
('agra'),
('agra');
然后我在 Alter 命令的帮助下在这里应用外键-
alter table personal add foreign key(city) references applications(app_id)
- 但我收到错误: ERROR 1005 (HY000): Can't create table 'student.#sql-f40_3' (errno: 150)
Table 1
create table personal(
id int not null auto_increment unique,
name char(20) not null,
age int not null,
city varchar(20) not null default 'Delhi'
);
insert into personal(name,age,city) values
('anubhav',22,'delhi'),
('rohit',24,'agra');
Table 2
create table applications(
app_id int(5) not null auto_increment unique,
city varchar(10) not null default 'Delhi'
);
insert into applications(city) values
('kolkata'),
('mumbai'),
('mumbai'),
('delhi'),
('agra'),
('agra');
Then i apply foreign key here with the help of Alter command-
alter table personal add foreign key(city) references applications(app_id)
but i am getting an error: ERROR 1005 (HY000): Can't create table 'student.#sql-f40_3' (errno: 150)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MySQL 规定:
条件和限制
数据类型必须相同。
您可以这样做:
但是,两个表上的列都应该建立索引。
请参阅此处
MySQL specifies:
Conditions and Restrictions
The data type must be the same.
You could do:
But, the columns on both tables should be indexed.
See here
您在未规范化的
个人表中设计的应该仅引用 ID。
应用程序中的城市名称应该是唯一的,所以我将其添加到创建表中,表中不需要两个或多个delhis(请参阅规范化)
如果您确实想在个人中使用城市名称,您必须喜欢我已引用应用程序的 Coty 名称或为该列定义一个 KEY。
此外,外键的两个表中的列的数据类型必须始终相同
you desing in not normalized
your personal table should only reference the id.
City name in the applications should be unique, so i added it in the create table, there is no need for two or more delhis in a table(see normalisation)
If you really want to use in personal the city name, you must like i already made refernece the coty name of appcations or define a KEY for that column.
Further the datatyoes of the columns must always be the saem in both table for the foreign key
表个人
表应用程序
更改表
Table personal
Table applications
Alter table