外键遇到问题

发布于 2024-11-29 19:26:36 字数 768 浏览 0 评论 0原文

我试图将 Budget2000 表中的类别作为 mainBudget 中类别的外键。类别不是唯一的数字,因此它不能是主键。当我运行代码时,我收到了著名的错误 1005。当我使用 id 将类别作为 mainBudget 中主键的一部分时,代码会运行,但这会在以后产生问题。我该怎么做才能使类别成为外键。我使用的是mysql 5.5。

这是我的代码

create table mainBudget(
id  SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
year    Year NOT NULL,
amount  double(10,2) NOT NULL,
category SMALLINT UNSIGNED NOT NULL,
primary key(id)
)ENGINE=INNODB;



create table budget2000(
id  SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
categories  SMALLINT UNSIGNED NOT NULL,
INDEX categoryNumber (categories),
subCategory SMALLINT NOT NULL,
amount   FLOAT(10,2) NOT NULL,
date    DATE NOT NULL,
description VARCHAR(300) NOT NULL,
primary key(id),
FOREIGN KEY (categories) REFERENCES mainBudget(category)
)ENGINE=INNODB;

I am trying to have categories in my budget2000 table be the foreign key to category in mainBudget. Category is not a unique number so it cannot be a primary key. When I run the code I get the famous error 1005. When I make category part of the primary key in mainBudget with id the code runs, however this will create problems later on. What can I do to make categories a foreign key. I am using mysql 5.5.

Here is my code

create table mainBudget(
id  SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
year    Year NOT NULL,
amount  double(10,2) NOT NULL,
category SMALLINT UNSIGNED NOT NULL,
primary key(id)
)ENGINE=INNODB;



create table budget2000(
id  SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
categories  SMALLINT UNSIGNED NOT NULL,
INDEX categoryNumber (categories),
subCategory SMALLINT NOT NULL,
amount   FLOAT(10,2) NOT NULL,
date    DATE NOT NULL,
description VARCHAR(300) NOT NULL,
primary key(id),
FOREIGN KEY (categories) REFERENCES mainBudget(category)
)ENGINE=INNODB;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦毁影碎の 2024-12-06 19:26:36

category 未在 mainBudget 中建立索引。引用表中的列必须建立索引(或索引的左前缀)。

顺便说一句,您确定拥有一个额外的表 category 并拥有 mainBudget.categorybudget200.categories 两个外键不是更好吗?这张桌子?您当前的设置看起来有点奇怪,特别是 mainBudget 中引用的列不是唯一的。

category is not indexed in mainBudget. The column in the referenced table has to be indexed (or the left prefix of an index).

Incidentally, are you sure it isn't better to have an additional table category and have mainBudget.category and budget200.categories both foreign keys to this table? Your current setup looks a little odd, particularly with the referenced column in mainBudget not being unique.

空城缀染半城烟沙 2024-12-06 19:26:36

让 FK 引用非唯一列不是标准 SQL。即使 MySQL InnoDB 允许这样做,也不意味着这是一个好主意。

制作一些ER图规范化您的表格。 (如果没有其他事情迫使您不这样做,请使用 3.NF。)为 Category 建立一个单独的表似乎是正确的方法。另一方面,现有表的命名让我认为这些应该只是一个表,否则它们的命名很糟糕。

当这个 2000 与一年或其他什么有关时,那就忘记它吧。您可以在查询中轻松选择此选项。无论是哪一年,只需将所有内容放在一张表中即可。

你的问题似乎与我的设计有关。

Having FKs referencing non-unique columns is not standard SQL. Even when MySQL InnoDB allows this, it does not mean that it is a good idea.

Make some ER-Diagrams and normalize your tables. (Use 3.NF if nothing else forces you not to do.) Having a separate table for Category seems to be the way to go. On the other hand the naming of your exiting tables makes me thinking these should be only one table or their naming is bad.

And when this 2000 has something to do with a year or what then forget about it. You can select this easy in your Queries. Just put everything in one table no matter what year it is.

Your question/problem seems to be design-related to me.

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