多个表之间的参照完整性

时间:2015-02-26 23:13:36

标签: sql database postgresql database-design constraints

确保进入表格的任何数据与对应关系中的数据匹配的最简单方法是什么?在我的例子中,我有一个体育俱乐部。玩家可以在没有团队的情况下进行运动。我希望强化玩家在加入团队之前参加这项运动。

以下是我的表格:

create table IF NOT EXISTS sports (
    sport_id SERIAL,
    sport_name VARCHAR(50),
    PRIMARY KEY(sport_id)
    );

create table IF NOT EXISTS players (
    player_id SERIAL,
    player_name VARCHAR(50),
    player_phone VARCHAR(20),
    PRIMARY KEY(player_id)
    );

create table IF NOT EXISTS players_sports (
    player_id INT REFERENCES players(player_id),
    sport_id int REFERENCES sports(sport_id),
    PRIMARY KEY (player_id, sport_id)
    );

create table IF NOT EXISTS teams (
    team_name VARCHAR(50),
    sport_id INT,
    captain_id INT,
    FOREIGN KEY (sport_id, captain_id) 
        REFERENCES players_sports(sport_id, player_id),
    PRIMARY KEY (team_name, sport_id)
    );

create table IF NOT EXISTS has_players (
    team_name VARCHAR(50),
    sport_id int,
    player_id INT REFERENCES players(player_id),
    FOREIGN KEY (team_name, sport_id) 
        REFERENCES teams(team_name, sport_id),
    PRIMARY KEY(team_name, sport_id, player_id)
    );

CREATE TYPE DAY AS ENUM (
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday',
  'Saturday',
  'Sunday'
);

create table IF NOT EXISTS time_slot (
    training_id SERIAL,
    training_day DAY,
    training_time TIME,
    team_name VARCHAR(50),
    sport_id int,
    FOREIGN KEY (team_name, sport_id) 
        REFERENCES teams (team_name, sport_id),
    PRIMARY KEY (training_id)
    );

1 个答案:

答案 0 :(得分:1)

  

我希望强化玩家在加入团队之前参加这项运动。

像这样添加另一个FK constraint

CREATE TABLE IF NOT EXISTS has_players (
  team_name varchar(50)
, sport_id  int
, player_id int
, PRIMARY KEY(team_name, sport_id, player_id)
, FOREIGN KEY (team_name, sport_id)
    REFERENCES teams(team_name, sport_id)
, FOREIGN KEY (player_id, sport_id)
    REFERENCES players_sports(player_id, sport_id)
);

您可以在多个FK约束中包含相同的列(如果这样做是让您无法做到的话)。