我正在开发循环赛,我有问题如何设计数据库。
首先我有季节。赛季包含锦标赛列表。锦标赛包含组列表。组包含列表参与者。然后我有球员名单。玩家和参与者的区别在于参与者是属于组的注册玩家。然后我有由 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;