我在尝试添加外键时遇到错误,我做错了什么?

时间:2021-04-06 21:54:13

I have an episode table with episodeID (PK), airdate, and title

我有episodeID(PK),airdate和title的情节表

I have a show table with showID (PK) which is VARCHAR(5)

我有一个showID(PK)的show table,它是VARCHAR(5)

I am using the following:

我使用以下内容:

ALTER TABLE episode
ADD FOREIGN KEY (showID)
REFERENCES show(showID);

and I get this error:

我收到此错误:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show(showID)' at line 3

1064 - 您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第3行的“show(showID)”附近使用正确的语法

1 个解决方案

#1


1  

You want to add a foreign key constraint, so I recommend naming it.

您想要添加外键约束,因此我建议命名它。

However, your problem is that show is a reserved word:

但是,你的问题是show是一个保留字:

ALTER TABLE episode ADD CONSTRAINT fk_episode_showid
     FOREIGN KEY (showID) REFERENCES `show`(showID);

I usually name tables in the plural (shows rather than show). This both captures that they contain multiple rows. And, it also makes them much less likely to conflict with reserved words.

我通常以复数形式命名表格(显示而不是显示)。这两个都捕获它们包含多行。而且,它也使他们与保留字冲突的可能性更小。

#1


1  

You want to add a foreign key constraint, so I recommend naming it.

您想要添加外键约束,因此我建议命名它。

However, your problem is that show is a reserved word:

但是,你的问题是show是一个保留字:

ALTER TABLE episode ADD CONSTRAINT fk_episode_showid
     FOREIGN KEY (showID) REFERENCES `show`(showID);

I usually name tables in the plural (shows rather than show). This both captures that they contain multiple rows. And, it also makes them much less likely to conflict with reserved words.

我通常以复数形式命名表格(显示而不是显示)。这两个都捕获它们包含多行。而且,它也使他们与保留字冲突的可能性更小。