我是mysql的新手,我试图创建下表。球员和场地表工作,但目标,比赛和球队没有。是由于外键还是别的什么?表目标teamh和teamA参考表匹配(teamh和teamA)和表匹配参考表团队(teamname)
CREATE TABLE PLAYER (
PlayerID int NOT NULL,
Playername varchar (255),
Playersurname varchar (255),
Teamname varchar (255),
Age int,
Nationality varchar (255),
Primary key (PlayerID)
);
CREATE TABLE GOALS (
TeamH varchar (255),
TeamA varchar (255),
PlayerID int,
Minute time,
Primary key (Minute),
Foreign key (TeamH) references MATCHES(TeamH),
Foreign key (TeamA) references MATCHES(TeamA),
Foreign key (PlayerId) references PLAYER(PlayerID)
);
CREATE TABLE MATCHES (
TeamH varchar (255),
TeamA varchar (255),
GoalH int,
GoalA int,
Mdate date,
VenueName varchar (255),
Attendance varchar (255),
Primary key (TeamH, TeamA),
Foreign key (TeamH) references TEAMS(Teamname),
Foreign key (TeamA) references TEAMS(Teamname),
Foreign key (Venuename) references VENUES(Venuename)
);
CREATE TABLE TEAMS (
Teamname varchar (255),
Year date,
President varchar (255),
Primary key (Teamname)
);
CREATE TABLE VENUES (
Venuename varchar(255),
Capacity int,
Address varchar (255),
primary key (venuename)
);
答案 0 :(得分:0)
可能是您在创建外键表之前尝试使用外键创建表,以下是否有效?
-- first create the primary tables, PLAYER, TEAMS, VENUES
CREATE TABLE PLAYER (
PlayerID int NOT NULL,
Playername varchar (255),
Playersurname varchar (255),
Teamname varchar (255),
Age int,
Nationality varchar (255),
Primary key (PlayerID)
);
CREATE TABLE TEAMS (
Teamname varchar (255),
Year date,
President varchar (255),
Primary key (Teamname)
);
CREATE TABLE VENUES (
Venuename varchar(255),
Capacity int,
Address varchar (255),
primary key (venuename)
);
-- Now create the foreign key using tables, MATCHES, GOALS
CREATE TABLE MATCHES (
TeamH varchar (255),
TeamA varchar (255),
GoalH int,
GoalA int,
Mdate date,
VenueName varchar (255),
Attendance varchar (255),
Primary key (TeamH, TeamA),
Foreign key (TeamH) references TEAMS(Teamname),
Foreign key (TeamA) references TEAMS(Teamname),
Foreign key (Venuename) references VENUES(Venuename)
);
CREATE TABLE GOALS (
TeamH varchar (255),
TeamA varchar (255),
PlayerID int,
Minute time,
Primary key (Minute),
Foreign key (TeamH) references MATCHES(TeamH),
Foreign key (TeamA) references MATCHES(TeamA),
Foreign key (PlayerId) references PLAYER(PlayerID)
);
答案 1 :(得分:0)
您正在按顺序创建表格。您的各种CREATE TABLE
语句引用其他表,并且在调用语句时需要存在这些表。尝试使用此订单:
CREATE TABLE PLAYER
CREATE TABLE TEAMS
CREATE TABLE VENUES
CREATE TABLE MATCHES -- refers to TEAMS and VENUES
CREATE TABLE GOALS -- refers to MATCHES and PLAYER