我正在尝试创建4个表格层次结构-运动类别,联赛,团队和玩家。直到这一刻,这就是我创造的:
-- Create PLAYER table
create table PLAYER
(
PLAYER_ID INT not null,
NAME_OF_PLAYER VARCHAR(36) not null,
NUMBER_OF_PLAYER INT not null,
TEAM_ID INT not null
) ;
alter table PLAYER
add constraint PLAYER_PK primary key (PLAYER_ID);
alter table PLAYER MODIFY COLUMN PLAYER_ID INT auto_increment;
alter table PLAYER
add constraint PLAYER_UK unique (NAME_OF_PLAYER, TEAM_ID);
-- Create TEAM table
create table TEAM
(
TEAM_ID INT not null,
NAME_OF_TEAM VARCHAR(36) not null,
LEAGUE_ID INT not null
) ;
alter table TEAM
add constraint TEAM_PK primary key (TEAM_ID);
alter table TEAM MODIFY COLUMN TEAM_ID INT auto_increment;
alter table TEAM
add constraint TEAM_UK unique (NAME_OF_TEAM);
-- Create LEAGUE table
create table LEAGUE
(
LEAGUE_ID INT not null,
NAME_OF_LEAGUE VARCHAR(36) not null,
SPORT_CATEGORY_ID INT not null
) ;
alter table LEAGUE
add constraint LEAGUE_PK primary key (LEAGUE_ID);
alter table LEAGUE MODIFY COLUMN LEAGUE_ID INT auto_increment;
alter table LEAGUE
add constraint LEAGUE_UK unique (NAME_OF_LEAGUE);
-- Create Sport_Category table
create table SPORT_CATEGORY
(
SPORT_CATEGORY_ID INT not null,
NAME_OF_CATEGORY VARCHAR(36) not null
) ;
alter table SPORT_CATEGORY
add constraint SPORT_CATEGORY_PK primary key (SPORT_CATEGORY_ID);
alter table SPORT_CATEGORY MODIFY COLUMN SPORT_CATEGORY_ID INT auto_increment;
alter table SPORT_CATEGORY
add constraint SPORT_CATEGORY_UK unique (NAME_OF_CATEGORY);
alter table PLAYER
add constraint PLAYER_FK foreign key (TEAM_ID)
references TEAM (TEAM_ID);
alter table TEAM
add constraint TEAM_FK1 foreign key (LEAGUE_ID)
references LEAGUE (LEAGUE_ID);
alter table LEAGUE
add constraint LEAGUE_FK1 foreign key (SPORT_CATEGORY_ID)
references SPORT_CATEGORY (SPORT_CATEGORY_ID);
当我执行此sql文件时,大约需要15秒才能完成。通过一些测试,我注意到alter table语句会使它变慢。这种关系表正常吗?是因为表格的复杂性吗?
我刚开始处理关系,但对此非常热情。任何帮助将不胜感激。