themoosemind

Klausur A, Datenbanksysteme SS 2012

Jul 28th, 2013
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.24 KB | None | 0 0
  1. --
  2. -- Tabellenstruktur für Tabelle `Cup`
  3. --
  4.  
  5. CREATE TABLE IF NOT EXISTS `Cup` (
  6.   `cup_id` INT(11) NOT NULL AUTO_INCREMENT,
  7.   `cup_name` VARCHAR(255) NOT NULL,
  8.   PRIMARY KEY (`cup_id`)
  9. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
  10.  
  11. --
  12. -- Daten für Tabelle `Cup`
  13. --
  14.  
  15. INSERT INTO `Cup` (`cup_id`, `cup_name`) VALUES
  16. (1, 'EM 2008'),
  17. (2, 'WM 2010'),
  18. (3, 'EM 2012');
  19.  
  20. -- --------------------------------------------------------
  21.  
  22. --
  23. -- Stellvertreter-Struktur des Views `GoalsPerPlayer`
  24. --
  25. CREATE TABLE IF NOT EXISTS `GoalsPerPlayer` (
  26. `player_id` INT(11)
  27. ,`name` VARCHAR(255)
  28. ,`team` VARCHAR(3)
  29. ,`sum_goals` DECIMAL(32,0)
  30. );
  31. -- --------------------------------------------------------
  32.  
  33. --
  34. -- Stellvertreter-Struktur des Views `GoalsPerTeam`
  35. --
  36. CREATE TABLE IF NOT EXISTS `GoalsPerTeam` (
  37. `team` VARCHAR(3)
  38. ,`sum_goals` DECIMAL(54,0)
  39. );
  40. -- --------------------------------------------------------
  41.  
  42. --
  43. -- Tabellenstruktur für Tabelle `Participation`
  44. --
  45.  
  46. CREATE TABLE IF NOT EXISTS `Participation` (
  47.   `player_id` INT(11) DEFAULT NULL,
  48.   `cup_id` INT(11) DEFAULT NULL,
  49.   `goals` INT(11) DEFAULT NULL,
  50.   KEY `player_id` (`player_id`),
  51.   KEY `cup_id` (`cup_id`)
  52. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  53.  
  54. --
  55. -- Daten für Tabelle `Participation`
  56. --
  57.  
  58. INSERT INTO `Participation` (`player_id`, `cup_id`, `goals`) VALUES
  59. (1, 1, 1),
  60. (1, 2, 0),
  61. (1, 3, 1),
  62. (2, 1, 2),
  63. (2, 2, 4),
  64. (2, 3, 1),
  65. (4, 1, 4),
  66. (4, 2, 5),
  67. (5, 1, 2);
  68.  
  69. -- --------------------------------------------------------
  70.  
  71. --
  72. -- Tabellenstruktur für Tabelle `Player`
  73. --
  74.  
  75. CREATE TABLE IF NOT EXISTS `Player` (
  76.   `player_id` INT(11) NOT NULL AUTO_INCREMENT,
  77.   `name` VARCHAR(255) NOT NULL,
  78.   `team` VARCHAR(3) NOT NULL,
  79.   PRIMARY KEY (`player_id`)
  80. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
  81.  
  82. --
  83. -- Daten für Tabelle `Player`
  84. --
  85.  
  86. INSERT INTO `Player` (`player_id`, `name`, `team`) VALUES
  87. (1, 'Lahm', 'GER'),
  88. (2, 'Klose', 'GER'),
  89. (3, 'Odonkor', 'GER'),
  90. (4, 'Villa', 'SPA'),
  91. (5, 'Van Nistelrooy', 'NED');
  92.  
  93. -- --------------------------------------------------------
  94.  
  95. --
  96. -- Struktur des Views `GoalsPerPlayer`
  97. --
  98. DROP TABLE IF EXISTS `GoalsPerPlayer`;
  99.  
  100. CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `GoalsPerPlayer` AS (SELECT `p1`.`player_id` AS `player_id`,`p1`.`name` AS `name`,`p1`.`team` AS `team`,SUM(`pa`.`goals`) AS `sum_goals` FROM (`Player` `p1` LEFT JOIN `Participation` `pa` ON((`p1`.`player_id` = `pa`.`player_id`))) GROUP BY `p1`.`player_id`,`p1`.`name`,`p1`.`team`);
  101.  
  102. -- --------------------------------------------------------
  103.  
  104. --
  105. -- Struktur des Views `GoalsPerTeam`
  106. --
  107. DROP TABLE IF EXISTS `GoalsPerTeam`;
  108.  
  109. CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `GoalsPerTeam` AS (SELECT `GoalsPerPlayer`.`team` AS `team`,SUM(`GoalsPerPlayer`.`sum_goals`) AS `sum_goals` FROM `GoalsPerPlayer` GROUP BY `GoalsPerPlayer`.`team`);
  110.  
  111. --
  112. -- Constraints der exportierten Tabellen
  113. --
  114.  
  115. --
  116. -- Constraints der Tabelle `Participation`
  117. --
  118. ALTER TABLE `Participation`
  119.   ADD CONSTRAINT `Participation_ibfk_1` FOREIGN KEY (`player_id`) REFERENCES `Player` (`player_id`),
  120.   ADD CONSTRAINT `Participation_ibfk_2` FOREIGN KEY (`cup_id`) REFERENCES `Cup` (`cup_id`);
Add Comment
Please, Sign In to add comment