简体   繁体   English

外键- SQL 开发人员

[英]Foreign key- SQL developer

So the problem im having is the foreign keys in my table, when I run without the foreign key is creates the tables.所以我遇到的问题是表中的外键,当我在没有外键的情况下运行时会创建表。 the error im will be posted at the bottom, Just trying to get familiar with SQL.错误即时消息将发布在底部,只是试图熟悉 SQL。

CREATE TABLE Player
(
Player_ID VARCHAR(20) NOT NULL,
Fname VARCHAR(15),
Sname VARCHAR(15),
DOB    NUMBER(3),
Height NUMBER(3),
Weight NUMBER(3),
Position VARCHAR(15),
Team_ID VARCHAR(20) NOT NULL,

CONSTRAINT Player_primary_key PRIMARY KEY (Player_ID),
CONSTRAINT Player_foreign_key FOREIGN KEY(Team_ID) REFERENCES TEAM(Team_ID)
);


CREATE TABLE Team
(
Team_ID VARCHAR(20) NOT NULL,
Tname VARCHAR(20),
Tlocation VARCHAR(20),
Coach VARCHAR(20),
Gameswon NUMBER(10),
Gameslost NUMBER (10),

CONSTRAINT Team_primary_key PRIMARY KEY (Team_ID)
);


CREATE TABLE Match
(
Match_ID VARCHAR(20) NOT NULL,
Hometeam VARCHAR(20), 
Awayteam VARCHAR(20), 
Score NUMBER (10),
Mdate NUMBER(10),
Mtime NUMBER(10),

CONSTRAINT Match_primary_key PRIMARY KEY (Match_ID),
CONSTRAINT Match_foreign_keyHome FOREIGN KEY (Team_ID) REFERENCES HOST(Team_ID),
CONSTRAINT Match_foreign_keyAway FOREIGN KEY (Team_ID) REFERENCES TEAM(Team_ID)
);

CREATE TABLE HOST
(
Team_ID VARCHAR(20) NOT NULL,
Match_ID VARCHAR(20) NOT NULL,

CONSTRAINT Host_Foriegn_Key_Team FOREIGN KEY (Team_ID) REFERENCES TEAM(Team_ID),
CONSTRAINT Host_Foriegn_Key_Match FOREIGN KEY (Match_ID) REFERENCES MATCH(Match_ID)
);

Well your first error seems to be related to the fact that you have not defined the Team table yet.好吧,您的第一个错误似乎与您尚未定义 Team 表的事实有关。 Since you are using:由于您正在使用:

CONSTRAINT Player_foreign_key FOREIGN KEY(Team_ID) REFERENCES TEAM(Team_ID)

For the Match table you are creating a foreign key with Team_ID even though it does not exist in your table.对于 Match 表,您正在使用 Team_ID 创建外键,即使它不存在于您的表中。 That's why it's throwing the error:这就是它抛出错误的原因:

CONSTRAINT Match_foreign_keyAway FOREIGN KEY (Team_ID) REFERENCES TEAM(Team_ID)

And for your last error MATCH is actually a SQL command which is why it turns blue when you put it as a code sample.对于你的最后一个错误 MATCH 实际上是一个 SQL 命令,这就是为什么当你把它作为代码示例时它变成蓝色的原因。 I'm thinking this is why it's looking for a table name but assuming you're not providing one.我想这就是为什么它在寻找一个表名但假设你没有提供一个。 That's just my guess for the last error.这只是我对最后一个错误的猜测。

1) So I would first create your Team table instead of creating Player first. 1) 所以我会先创建你的 Team 表,而不是先创建 Player。
2) Then I would probably change Team_ID for Match_foreign_keyHome to Hometeam: 2)然后我可能会将 Match_foreign_keyHome 的 Team_ID 更改为 Hometeam:

CONSTRAINT Match_foreign_keyHome FOREIGN KEY (Hometeam) REFERENCES HOST(Team_ID),

And then I would change Team_ID for Match_foreign_keyAway to Awayteam:然后我将 Match_foreign_keyAway 的 Team_ID 更改为 Awayteam:

CONSTRAINT Match_foreign_keyHome FOREIGN KEY (Awayteam) REFERENCES HOST(Team_ID),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM