MySQL - 无法将外键添加到表中

发布于 2024-12-06 09:57:05 字数 900 浏览 0 评论 0原文

我有一个使用 MySQL 2005 的数据库。我有两个表,Enrollment 和 AlertMsg。

注册的主键有两列:UnitCode 和StudentID。这两列都是另一个表的外键。

AlertMsg 的主键是三列:UnitCode、StudentID 和AlertNo。

当我尝试在 AlertMsg 表中使用 UnitCode 创建外键时,如下所示:

ALTER TABLE AlertMsg
ADD FOREIGN KEY (UnitCode)
REFERENCES Enrolment(UnitCode)

我收到以下错误:

SQL Execution Error.

Executed SQL statement: ALTER TABLE AlertMsg

ADD FOREIGN KEY (UnitCode)
REFERENCES Enrolment (UnitCode)
Error Source: .Net SqlClient Data Provider
Error Message: There are no primary or candidate keys in the referenced table 'Enrolment' that match the referencing column list in the foreign key 'FK_AlertMsg_UnitCo_571DF1D5'.
Could not create constraint. See previous errors.

经过一番搜索后,似乎这是因为 UnitCode 不是 Enrolment 的主键。但 Enrolment 的表定义似乎表明确实如此。我是 SQL 的新手,所以我假设如果表定义的最左边的列中有一个键,则意味着它是表的主键。

有人可以帮忙吗?提前致谢。

I have a database using MySQL 2005. I have two tables, Enrolment and AlertMsg.

The primary keys for enrolment are two columns, UnitCode and StudentID. Both these two columns are foreign keys to another table.

The primary keys for AlertMsg are three columns, UnitCode, StudentID and AlertNo.

When I try to make a foreign key with UnitCode in the AlertMsg table, like so:

ALTER TABLE AlertMsg
ADD FOREIGN KEY (UnitCode)
REFERENCES Enrolment(UnitCode)

I get the following error:

SQL Execution Error.

Executed SQL statement: ALTER TABLE AlertMsg

ADD FOREIGN KEY (UnitCode)
REFERENCES Enrolment (UnitCode)
Error Source: .Net SqlClient Data Provider
Error Message: There are no primary or candidate keys in the referenced table 'Enrolment' that match the referencing column list in the foreign key 'FK_AlertMsg_UnitCo_571DF1D5'.
Could not create constraint. See previous errors.

After some searching it seems that this is because UnitCode isn't a primary key of Enrolment. But Enrolment's table definition seems to show that it is. I'm a bit of a newbie at SQL, so I assume that if the far left column of the table definition has a key in it, it means its a primary key of the table.

Can anyone help? Thanks in advance.

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

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

发布评论

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

评论(3

不疑不惑不回忆 2024-12-13 09:57:05

注册的主键有两列:UnitCode 和
学生ID。这两列都是另一个表的外键。

AlertMsg 的主键是三列,UnitCode、StudentID
和警报号。

你说“......的主键”。实际上,它可能不是多个键,而是使用多个列创建的一个键。这些列一起在表中必须是唯一的,因此您不能仅引用外键中的列之一。

如果您希望此语句起作用

ALTER TABLE AlertMsg
ADD FOREIGN KEY (UnitCode)
REFERENCES Enrolment(UnitCode)

Enrolment 中的 UnitCode 中的值需要是唯一的,并且您需要在该列上添加唯一约束。我想情况并非如此,因此您无法添加必要的唯一约束。

你可能应该使用这个:

ALTER TABLE AlertMsg
ADD FOREIGN KEY (UnitCode, StudentID)
REFERENCES Enrolment(UnitCode, StudentID)

The primary keys for enrolment are two columns, UnitCode and
StudentID. Both these two columns are foreign keys to another table.

The primary keys for AlertMsg are three columns, UnitCode, StudentID
and AlertNo.

You say "the primary keys for ...". Well actually it is probably not multiple keys but one key that is create using multiple columns. Those columns together is what needs to be unique in your table and therefore you can not just reference one of the columns in a foreign key.

If you want this statement to work

ALTER TABLE AlertMsg
ADD FOREIGN KEY (UnitCode)
REFERENCES Enrolment(UnitCode)

The values in UnitCode in table Enrolment needs to be unique and you need to add a unique constraint on that column. I guess that is not the case so you can't add the necessary unique constraint.

You should probably use this instead:

ALTER TABLE AlertMsg
ADD FOREIGN KEY (UnitCode, StudentID)
REFERENCES Enrolment(UnitCode, StudentID)
百变从容 2024-12-13 09:57:05

标准主键不必是表中最左边的索引列,这只是定义表的开发人员和 DBA 的常见习惯,将主键列放在前面。

在标准 SQL 中,外键必须引用:

  • 在引用的表中定义为 PRIMARY KEYUNIQUE KEY 的列。
  • 外键中的列不必具有相同的名称,但它们的数量、顺序和数据类型必须相同。

我怀疑您实际上使用的是 Microsoft SQL Server 2005,而不是 MySQL。没有称为“MySQL 2005”的此类产品版本。

Standard primary keys don't have to be the leftmost indexed column in a table, it's just a common habit that developers and DBA's who define the table put the primary key column(s) first.

In standard SQL, a foreign key must reference:

  • Column(s) defined as a PRIMARY KEY or UNIQUE KEY in the referenced table.
  • The columns in the foreign key don't have to have the same names, but they must be the same in number, order, and data type.

I suspect you're really using Microsoft SQL Server 2005, not MySQL. There's no such product release known as "MySQL 2005".

趁年轻赶紧闹 2024-12-13 09:57:05

您可以尝试:

  • 将外键约束添加到 Enrollment 表的 2 列(UnitCode,StudentID)。
    或者,
  • 如果您只需要 UnitCode 列的引用,则向 Enrollment 表的列 (UnitCode) 添加唯一约束。

哈。

You can try:

  • Add foreign key constraint to 2 columns (UnitCode,StudentID) of Enrolment table.
    Or
  • Add unique constraint to column (UnitCode) of Enrolment table if you only need the reference on UnitCode column.

Hth.

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