SQL如何构建元素经常更改值的数据库

时间:2019-01-24 01:31:47

标签: sql database sqlite

我正在尝试为一项具有多个联赛的运动创建数据库,在这些联赛中,有很多球队包含经常更换联赛和球队的球员。

我想了解构建数据库的最佳方法,这样我就可以跟踪球员何时更换球队,以及知道他们参加过哪些球队以及何时参加比赛。

顺便说一句,我对SQL还是很陌生,所以任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:1)

要做的第一件事是考虑核心要素。您说过联赛,球队和球员,并且您还想要一些记录球员换队的方法,我想球队换联赛也是如此。

那可能是5张桌子-

  • 排行榜
  • 团队表
  • 玩家表
  • 球队所属联赛的历史(包括当前)
  • 玩家所在(并因此一直在)所在球队(包括现在)的历史

因此,我相信以下内容可能符合适当的结构(也填充了一些测试数据):-

DROP TABLE IF EXISTS player;
DROP TABLE If EXISTS team;
DROP TABLE IF EXISTS leauge;
DROP TABLE IF EXISTS team_league_history;
DROP TABLE IF EXISTS player_team_history;

CREATE TABLE IF NOT EXISTS league (league_id INTEGER PRIMARY KEY, league_name TEXT);
-- Add some leauges
INSERT INTO league (league_name) VALUES
    ('1st Division'),('2nd Division'),('3rd Division'),('4th Division');

CREATE TABLE IF NOT EXISTS team (team_id INTEGER PRIMARY KEY, team_name TEXT);
-- Add some teams
INSERT INTO team (team_name) VALUES
    ('Team A'),('Team B'),('Team C'),('Team D'),('Team E'),('Team F'),('Team G'),('Team H'),('Team I'),('Team J'),('Team K')
;

CREATE TABLE IF NOT EXISTS player (player_id INTEGER PRIMARY KEY, player_name);
-- Add some players
INSERT INTO player (player_name) VALUES
    ('P0'),('P1'),('P2'),('P3'),('P4'),('P5'),('P6'),('P7'),('P8'),('P9'),
    ('P10'),('P11'),('P12'),('P13'),('P14'),('P15'),('P16'),('P17'),('P18'),('P19'),
    ('P20'),('P21'),('P22'),('P23'),('P24'),('P25'),('P26'),('P27'),('P28'),('P29'),
    ('P30'),('P31'),('P32'),('P33'),('P34'),('P35'),('P36'),('P37'),('P38'),('P39'),
    ('P40'),('P41'),('P42'),('P43'),('P44'),('P45'),('P46'),('P47'),('P48'),('P49')
;

CREATE TABLE IF NOT EXISTS team_league_history (
    tlh_team_reference INTEGER, 
    tlh_league_reference INTEGER, 
    tlh_from_date TEXT DEFAULT CURRENT_DATE, 
    tlh_to_date TEXT DEFAULT '3000-12-31'
);
-- Add current standings
INSERT INTO team_league_history (tlh_team_reference,tlh_league_reference) VALUES 
    (6,3), -- Team F in 3rd Div
    (1,4), -- Team A in 4th div
    (2,1),(3,1),(11,1),
  (4,2),(7,2),(9,2),
    (5,3),(10,3),
    (8,4)
;

CREATE TABLE IF NOT EXISTS player_team_history (
    pth_player_reference INTEGER, 
    pth_team_reference INTEGER, 
    pth_from_date TEXT DEFAULT CURRENT_DATE, 
    pth_to_date TEXT DEFAULT '3000-12-31'
);
-- Add current team composition
INSERT INTO player_team_history (pth_player_reference, pth_team_reference) VALUES
    (1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,2),(8,2),(9,2),(10,2),
        (11,3),(12,3),(13,3),(14,3),(15,4),(16,4),(17,4),(18,4),(19,4),(20,4),
        (21,5),(22,5),(23,5),(24,5),(25,5),(26,6),(27,6),(28,6),(29,6),(30,6),
        (31,7),(32,7),(33,7),(34,7),(35,7),(36,8),(37,8),(38,8),(39,8),(40,8),
        (41,9),(42,9),(43,9),(44,9),(45,9),(46,10),(47,10),(48,11),(49,11),(50,11)
;

可能会以上述示例用途的方式出现

基于上述示例:-

每个联赛中的队伍

--List Teams in each League
SELECT league_name AS Leauge, group_concat(team_name) AS Teams
FROM team_league_history 
    JOIN league ON tlh_league_reference = league_id
    JOIN team ON tlh_team_reference = team_id
GROUP BY league_id
;
  • 结果 enter image description here

团队球员

-- Players in Teams
SELECT team_name AS Team, group_concat(player_name) AS Players
FROM player_team_history
    JOIN team ON pth_team_reference = team_id
    JOIN player ON pth_player_reference = player_id
GROUP BY team_name
;
  • 结果 enter image description here

与联赛有队的球员

-- Players in Teams and Leauge
SELECT team_name AS Team, 
group_concat(player_name) AS Players, 
 (
        SELECT league_name 
        FROM team_league_history 
            JOIN league ON tlh_league_reference = league_id
        WHERE tlh_team_reference = team_id
    )AS League
FROM player_team_history
    JOIN team ON pth_team_reference = team_id
    JOIN player ON pth_player_reference = player_id
GROUP BY team_name
ORDER BY league, team
;

-结果 enter image description here

也许考虑:-

-- Making changes
-- 1 Change a Team name
UPDATE team SET team_name = 'My changed name' WHERE team_name = 'Team B';

-- Adjust some League compositions
-- DROP Team B and K down to Div 2
UPDATE team_league_history SET tlh_league_reference = 2 WHERE tlh_team_reference = 2 or tlh_team_reference = 11;
UPDATE team_league_history SET tlh_league_reference = 1 WHERE tlh_team_reference = 4 or tlh_team_reference = 9;

-- Move some players between teams (not history though)
-- Hopefully players P35-37 (id's 36-38) moved to team A
UPDATE player_team_history SET pth_team_reference = 1 WHERE pth_player_reference > 35 AND pth_player_reference < 39;

那么,球员,球队,联赛将是:-

enter image description here

历史

上面的演示都是对当前所做的更改(插入时使用DEFAULT CURRENT_DATE和DEFAULT 3000-12-31作弊)。

,但历史记录(两个表)/每个球员(pth)或球队(lth)将有很多行,且from_date-to_date涵盖了该情况存在的时间段(球队属于联赛,球员属于球队)。如果没有排期就表示不存在(团队破裂/解散,球员在医院/监狱中,等等),尽管如果此类事件本身可能具有历史记录行以及事件指示符(如果这是正确的说法, )表)。

注意

  • 以上内容是根据数字引用(例如P25等于player_id 26 (为方便起见,选择了名字),我们凡人都容易混淆。通常,尽管用户会使用图形界面,但是尽管Joe Bloggs的ID为x,但他们只是选择了Joe Bloggs。

答案 1 :(得分:0)

This should give you a good start

您还应该查看临时表-它们将使您能够随时保留有关与特定团队的球员关系的历史信息。