简体   繁体   English

在mysql中创建表失败

[英]Unsuccessful creating tables in mysql

I am new to mysql and im trying to create the below tables. 我是mysql的新手,我正在尝试创建下表。 The player and venue table work but the goals, matches and teams does not. 球员和场地表有效,但目标,比赛和团队无效。 Is it due to the the foreign keys or something else? 是由于外键还是其他原因? Tables goals teamh and teamA reference table matches(teamh and teamA) and table matches reference table teams (teamname) 表目标teamh和teamA参考表匹配(teamh和teamA)和表匹配参考表team(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)
);

It may be that you are trying to create the tables with foreign keys before the foreign key tables have been created, does the following work? 可能是您在创建外键表之前尝试使用外键创建表,是否可以使用以下功能?

-- 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)
);

You are creating your tables out of order. 您正在按顺序创建表。 Your various CREATE TABLE statements refer to other tables, and those tables need to exist at the time the statement is called. 您的各种CREATE TABLE语句引用其他表,并且这些表在调用该语句时需要存在。 Try using this order: 尝试使用以下顺序:

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

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

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