如何设计循环赛的数据库

时间:2013-10-18 11:05:30

标签: sql

我正在开发循环赛,我有问题如何设计数据库。

首先我有季节。赛季包含锦标赛名单。锦标赛包含组列表。组包含列表参与者。然后我有玩家列表。玩家和参与者之间的不同之处在于参与者是属于组的注册玩家。然后我有实体游戏,由2名参与者和他们的分数定义。所以参与者有游戏列表。

P =参与者 G =游戏

   P1 P2 P3
P1 X  G1 G2
P2 G3 X  G4
P3 G5 G6 X

这是我锦标赛的好模特吗?我不这么认为,因为我的数据库中存在两面性。 G1与G3相反,但我不知道如何更好地实现这个模型

-- --------------------------------------------------------

--
-- Table structure for table `GAME`
--

CREATE TABLE IF NOT EXISTS `GAME` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `HOME_PARTICIPANT_ID` int(11) NOT NULL,
  `AWAY_PARTICIPANT_ID` int(11) NOT NULL,
  `HOME_SCORE` int(4) DEFAULT NULL,
  `AWAY_SCORE` int(4) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `HOME_PARTICIPANT_ID` (`HOME_PARTICIPANT_ID`),
  KEY `AWAY_PARTICIPANT_ID` (`AWAY_PARTICIPANT_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `GROUPS`
--

CREATE TABLE IF NOT EXISTS `GROUPS` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(255) NOT NULL,
  `TOURNAMENT_ID` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `NAME` (`NAME`,`TOURNAMENT_ID`),
  KEY `TOURNAMENT_ID` (`TOURNAMENT_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `PLAYER`
--

CREATE TABLE IF NOT EXISTS `PLAYER` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `SURNAME` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `CLUB` varchar(255) DEFAULT NULL,
  `USER_ID` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `USER_ID` (`USER_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `PARTICIPANT`
--

CREATE TABLE IF NOT EXISTS `PARTICIPANT` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `POINTS` int(4) NOT NULL DEFAULT '0',
  `RANK` int(4) DEFAULT NULL,
  `GROUP_ID` int(11) NOT NULL,
  `PLAYER_ID` int(11) NOT NULL,
  `SCORE` varchar(10) NOT NULL DEFAULT '0:0',
  PRIMARY KEY (`ID`),
  KEY `PLAYER_ID` (`PLAYER_ID`),
  KEY `GROUP_ID` (`GROUP_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 


-- --------------------------------------------------------

--
-- Table structure for table `SEASON`
--

CREATE TABLE IF NOT EXISTS `SEASON` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(255) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `USER_ID` (`USER_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `TOURNAMENT`
--

CREATE TABLE IF NOT EXISTS `TOURNAMENT` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(255) NOT NULL,
  `SEASON_ID` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `SEASON_ID` (`SEASON_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;    


--
-- Constraints for table `GAME`
--
ALTER TABLE `GAME`
  ADD CONSTRAINT `GAME_ibfk_1` FOREIGN KEY (`HOME_PARTICIPANT_ID`) REFERENCES `PARTICIPANT` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `GAME_ibfk_2` FOREIGN KEY (`AWAY_PARTICIPANT_ID`) REFERENCES `PARTICIPANT` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table `GROUPS`
--
ALTER TABLE `GROUPS`
  ADD CONSTRAINT `GROUPS_ibfk_1` FOREIGN KEY (`TOURNAMENT_ID`) REFERENCES `TOURNAMENT` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;  


--
-- Constraints for table `PARTICIPANT`
--
ALTER TABLE `PARTICIPANT`
  ADD CONSTRAINT `PARTICIPANT_ibfk_1` FOREIGN KEY (`GROUP_ID`) REFERENCES `GROUPS` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `PARTICIPANT_ibfk_2` FOREIGN KEY (`PLAYER_ID`) REFERENCES `PLAYER` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;    


--
-- Constraints for table `TOURNAMENT`
--
ALTER TABLE `TOURNAMENT`
  ADD CONSTRAINT `TOURNAMENT_ibfk_1` FOREIGN KEY (`SEASON_ID`) REFERENCES `SEASON` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;

2 个答案:

答案 0 :(得分:1)

如果你说你有两面性,那么你需要重新访问你的设计。您是否使用过增强型关系图?这是一个很好的工具,可以确保您拥有正确的数据库设计。

如果您之前没有这样做,这是一个很好的帮助您的网站:http://users.csc.calpoly.edu/~jdalbey/205/Lectures/HOWTO-ERD.html

答案 1 :(得分:-1)

锦标赛只是一棵二叉树。您可以使用嵌套集或邻接列表。