无法创建表“student.#sql-f40_3” (错误号:150)

发布于 2025-01-15 08:39:08 字数 843 浏览 3 评论 0原文

表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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百善笑为先 2025-01-22 08:39:08

MySQL 规定:

条件和限制

1.外键中对应的列
并且引用的键必须具有相似的数据类型。尺寸和标志
固定精度类型(如 INTEGER 和 DECIMAL)必须相同。
字符串类型的长度不必相同。对于非二元
(字符)字符串列,字符集和排序规则必须是
一样的。

2.MySQL需要外键和引用键上的索引,以便外键检查可以很快并且不需要表扫描。在
引用表,必须有一个索引,其中外键
列按相同顺序列为第一列。这样一个
如果引用表没有索引,则会自动在引用表上创建索引
存在。如果您创建,此索引可能会在稍后被静默删除
另一个可用于强制外键约束的索引。
index_name(如果给定)将按前面所述使用。

数据类型必须相同。

您可以这样做:

alter table personal add foreign key(city) references applications(city)

但是,两个表上的列都应该建立索引。
请参阅此处

MySQL specifies:

Conditions and Restrictions

1.Corresponding columns in the foreign key
and the referenced key must have similar data types. The size and sign
of fixed precision types such as INTEGER and DECIMAL must be the same.
The length of string types need not be the same. For nonbinary
(character) string columns, the character set and collation must be
the same.

2.MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the
referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order. Such an
index is created on the referencing table automatically if it does not
exist. This index might be silently dropped later if you create
another index that can be used to enforce the foreign key constraint.
index_name, if given, is used as described previously.

The data type must be the same.

You could do:

alter table personal add foreign key(city) references applications(city)

But, the columns on both tables should be indexed.
See here

往事随风而去 2025-01-22 08:39:08

您在未规范化的

个人表中设计的应该仅引用 ID。

应用程序中的城市名称应该是唯一的,所以我将其添加到创建表中,表中不需要两个或多个delhis(请参阅规范化)

如果您确实想在个人中使用城市名称,您必须喜欢我已引用应用程序的 Coty 名称或为该列定义一个 KEY。

此外,外键的两个表中的列的数据类型必须始终相同

create table personal(

id int not null auto_increment unique,
name char(20) not null,
age int not null,
city int not null default 0
);

create table applications(
 app_id int not null auto_increment primary key,
 city varchar(10) not null unique default 'Delhi'
);

alter table personal add foreign key(city) references applications(app_id)

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

create table personal(

id int not null auto_increment unique,
name char(20) not null,
age int not null,
city int not null default 0
);

create table applications(
 app_id int not null auto_increment primary key,
 city varchar(10) not null unique default 'Delhi'
);

alter table personal add foreign key(city) references applications(app_id)
温柔女人霸气范 2025-01-22 08:39:08

您有一些小错误,例如没有在自动增量插入中放入null,并且如果它是主键 strong> 你不应该输入not null

表个人

create table personal(

id int auto_increment primary key,
name char(20) not null,
age int not null,
city varchar(20) not null default 'Delhi'
);
insert into personal values (null,'anubhav',22,'delhi'),
                            (null,'rohit',24,'agra');

表应用程序

create table applications(
 app_id int(5) auto_increment primary key,
 city varchar(10) not null default 'Delhi'
);
insert into applications values(null,'kolkata'),
                               (null,'mumbai'),
                               (null,'mumbai'),
                               (null,'delhi'),
                               (null,'agra'),
                               (null,'agra');

更改表

alter table personal add foreign key(city) references applications(app_id)

You have small bugs such as not putting null in the insert for the autoincrement and if it is primary key you should not put not null.

Table personal

create table personal(

id int auto_increment primary key,
name char(20) not null,
age int not null,
city varchar(20) not null default 'Delhi'
);
insert into personal values (null,'anubhav',22,'delhi'),
                            (null,'rohit',24,'agra');

Table applications

create table applications(
 app_id int(5) auto_increment primary key,
 city varchar(10) not null default 'Delhi'
);
insert into applications values(null,'kolkata'),
                               (null,'mumbai'),
                               (null,'mumbai'),
                               (null,'delhi'),
                               (null,'agra'),
                               (null,'agra');

Alter table

alter table personal add foreign key(city) references applications(app_id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文