Advertisement
MarkUa

Untitled

May 16th, 2019
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.65 KB | None | 0 0
  1. USE [master]
  2.  
  3. IF NOT EXISTS (SELECT name FROM sys.DATABASES WHERE name = N'HockeyTeamAccountingDBUA')
  4.     CREATE DATABASE [HockeyTeamAccountingDBUA]
  5. ELSE
  6.     DROP DATABASE [HockeyTeamAccountingDBUA]
  7.     CREATE DATABASE [HockeyTeamAccountingDBUA]
  8. GO
  9. USE [HockeyTeamAccountingDBUA]
  10.  
  11. CREATE TABLE Team
  12. (
  13. team_id INT IDENTITY(1,1) NOT NULL,
  14. team_country VARCHAR(50) NOT NULL,
  15. team_city VARCHAR(50) NOT NULL,
  16. team_name VARCHAR(50) NOT NULL,
  17. team_fondation_year int NOT NULL,
  18. team_main_stadion INT,
  19. CONSTRAINT UniqueTeams UNIQUE (team_name),
  20. PRIMARY KEY(team_id)
  21. );/*+*/
  22. CREATE TABLE Trainer
  23. (
  24. trainer_id INT IDENTITY(1,1) NOT NULL,
  25.  
  26. trainer_name VARCHAR(50) NOT NULL,
  27. trainer_surname VARCHAR(50) NOT NULL,
  28. trainer_country VARCHAR(50) NOT NULL,
  29. birth_date DateTime not null,
  30. current_team_id int,
  31.  
  32. CONSTRAINT UniqueTrainer UNIQUE (trainer_name,trainer_surname,trainer_country,birth_date/*,team_id*/),
  33. PRIMARY KEY(trainer_id)
  34. );
  35.  
  36.  
  37. CREATE TABLE Stadion
  38. (
  39. stadion_id INT IDENTITY(1,1) NOT NULL,
  40.  
  41. stadion_name VARCHAR(50) NOT NULL,
  42. stadion_country VARCHAR(50) NOT NULL,
  43. stadion_city VARCHAR(50) NOT NULL,
  44. CONSTRAINT UniqueStadion_ UNIQUE (stadion_name),
  45.  
  46. PRIMARY KEY(stadion_id)
  47. );/*+*/
  48.  
  49. CREATE TABLE Players
  50. (
  51. player_id INT IDENTITY(1,1) NOT NULL,
  52. current_team_id INT,
  53. player_name VARCHAR(50) NOT NULL,
  54. player_surname VARCHAR(50) NOT NULL,
  55. player_country  VARCHAR(50) NOT NULL,
  56. player_born_date DateTime NOT NULL,
  57. player_amplua VARCHAR(50) NOT NULL,
  58. CONSTRAINT UniquePlayers UNIQUE (player_name,player_surname,player_country,player_born_date/*,team_id*/),
  59. PRIMARY KEY(player_id)
  60. );
  61.  
  62.  
  63.  
  64.  
  65.  
  66. CREATE TABLE Team_Tournament
  67. (
  68. team_tournament_id INT IDENTITY(1,1) NOT NULL,
  69. tournament_id INT NOT NULL,
  70. team_id INT NOT NULL,
  71. CONSTRAINT UniqueStadion UNIQUE (tournament_id,team_id/*,team_id*/),
  72. PRIMARY KEY(team_tournament_id )
  73. );
  74.  
  75.  
  76. CREATE TABLE Tournament
  77. (
  78. tournament_id INT IDENTITY(1,1) NOT NULL,
  79. tournament_name VARCHAR(50) NOT NULL,
  80.  
  81. tournament_start Datetime not null,
  82. tournament_finish Datetime not null,
  83.  
  84. PRIMARY KEY(tournament_id)
  85. );
  86.  
  87. CREATE TABLE Game
  88. (
  89. game_id INT IDENTITY(1,1) NOT NULL,
  90. tournament_id INT NOT NULL,
  91.  
  92. home_team_id INT NOT NULL,
  93. guest_team_id INT NOT NULL,
  94.  
  95. stadion_id INT ,
  96. home_team_trainer_id INT,
  97. guest_team_trainer_id INT,
  98.  
  99. home_team_score INT DEFAULT 0,
  100. guest_team_score INT DEFAULT 0 ,
  101. overtime_home_team_score INT DEFAULT 0,
  102. overtime_guest_team_score INT DEFAULT 0,
  103. bullits_home_team_score INT DEFAULT 0,
  104. bullits_guest_team_score INT DEFAULT 0,
  105. start_time DATETIME DEFAULT NULL,
  106.  
  107. PRIMARY KEY(game_id)
  108. );
  109.  
  110.  
  111. CREATE TABLE PlayerGameApplication
  112. (
  113. gameApplication_id INT IDENTITY(1,1) NOT NULL,
  114. game_id INT NOT NULL,
  115. player_id INT NOT NULL,
  116. team_id int not null,
  117. ganeral_game_time_goal_scored int Default 0,
  118. overtime_goal_scored int Default 0,
  119. after_total_time_bullit_scored int Default 0,
  120. PRIMARY KEY(gameApplication_id),
  121. CONSTRAINT UC UNIQUE (gameApplication_id,game_id,player_id/*,team_id*/)
  122. );
  123.  
  124.  
  125.  
  126.  
  127. ALTER TABLE Players
  128. WITH CHECK ADD CONSTRAINT FK_Current_Team_ID FOREIGN KEY(current_team_id)
  129. REFERENCES Team(team_id);
  130.  
  131.  
  132.  
  133. ALTER TABLE Team_Tournament
  134. WITH CHECK ADD CONSTRAINT FK_Tournament_id FOREIGN KEY(tournament_id)
  135. REFERENCES Tournament(tournament_id);
  136.  
  137. ALTER TABLE Team_Tournament
  138. WITH CHECK ADD CONSTRAINT FK_Team3_id FOREIGN KEY(team_id)
  139. REFERENCES Team(team_id);
  140.  
  141. ALTER TABLE Game
  142. WITH CHECK ADD CONSTRAINT FK_Tournament2_id FOREIGN KEY(tournament_id)
  143. REFERENCES Tournament(tournament_id);
  144.  
  145.  ALTER TABLE Team
  146. WITH CHECK ADD CONSTRAINT FK_team_main_stadion_id FOREIGN KEY(team_main_stadion)
  147. REFERENCES Stadion(stadion_id);
  148.  
  149. ALTER TABLE Game
  150. WITH CHECK ADD CONSTRAINT FK_Stadion_id FOREIGN KEY(stadion_id)
  151. REFERENCES Stadion(stadion_id);
  152.  
  153.  
  154. ALTER TABLE PlayerGameApplication
  155. WITH CHECK ADD CONSTRAINT FK_Game1_id FOREIGN KEY(game_id)
  156. REFERENCES Game(game_id);
  157.  
  158.  ALTER TABLE PlayerGameApplication
  159. WITH CHECK ADD CONSTRAINT FK_Game15_id FOREIGN KEY(player_id)
  160. REFERENCES Players(player_id);
  161.  
  162.  
  163.  
  164.  
  165.  ALTER TABLE Game
  166. WITH CHECK ADD CONSTRAINT FK_Team_ID_199 FOREIGN KEY(home_team_id)
  167. REFERENCES Team(team_id);
  168.  
  169.  ALTER TABLE Game
  170. WITH CHECK ADD CONSTRAINT FK_Team_ID_1999 FOREIGN KEY(guest_team_id)
  171. REFERENCES Team(team_id);
  172.  
  173.   ALTER TABLE Game
  174. WITH CHECK ADD CONSTRAINT FK_Home_Team_Trainer_ID FOREIGN KEY(home_team_trainer_id)
  175. REFERENCES Trainer(trainer_id);
  176.  
  177.   ALTER TABLE Game
  178. WITH CHECK ADD CONSTRAINT FK_Guest_Team_Trainer_ID FOREIGN KEY(guest_team_trainer_id)
  179. REFERENCES Trainer(trainer_id);
  180.  
  181.  
  182.  
  183. ALTER TABLE Trainer
  184. WITH CHECK ADD CONSTRAINT FK_CurrentTeam_id FOREIGN KEY(current_team_id)
  185. REFERENCES Team(team_id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement