Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 285263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:31:50+00:00 2026-05-12T05:31:50+00:00

How do I best design a database where I have one table of players

  • 0

How do I best design a database where I have one table of players (with primary key player_id) which I want to pair into teams of two so that the database can enforce the constraint that each team consists of exactly two players and each player is in at most one team?

I can think of two solutions, but both I’m not too happy about.

One possibility is to have two columns player1_id and player2_id that are unique foreign keys pointing to the player_id column in the player table. An additional check is needed so that no player is at the same time player1 of one team and player2 of a second team.

The other possibility that comes to my mind is to connect the player table and the team table with a team membership table that has a unique foreign key to the player_id column in the player table and a second foreign key pointing to the primary key of the team table. Here a check has to be added that every team has exactly two members.

Is there a better design that simplifies the checking of the constraints?

If it matters: the database I’m using is PostgreSQL 8.4 and I prefer its powerful rule system to triggers wherever possible.

EDIT: A solution based on the answer of AlexKuznetsov

It doesn’t feel perfect to me yet, but I like it much better than what I had before. I modified Alex’ solution since I don’t want to have a foreign key from players to teams, as there is an application phase where players can enroll.

create table TeamMemberships(
  player_id int not null unique references Players(player_id),
  team_id int not null references Teams(team_id),
  NumberInTeam int not null check(NumberInTeam in (0,1)),
  OtherNumberInTeam int not null, -- check(OtherNumberInTeam in (0,1)) is implied
  check(NumberInTeam + OtherNumberInTeam = 1)
  foreign key (team_id, OtherNumberInTeam) references TeamMemberships(team_id, NumberInTeam),
  primary key (team_id, NumberInTeam)
);

This definition makes sure that team memberships come in couples (and will be inserted pairwise). Now players can be in at most one team and teams can have exactly 0 or exactly 2 players. To ensure that each team has members I could add a foreign key in the team table that points to any of its two memberships. But like Erwin I’m not a fan of deferred constraint checking. Any ideas how to improve with respect to this? Or is there a completely different, better approach?

PS: The methods works also for teams with n>2 players. One just has to replace OtherNumberInTeam by NextNumberInTeam with the value (i.e. constraint) NumberInTeam+1 mod n.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:31:50+00:00Added an answer on May 12, 2026 at 5:31 am

    I don’t know if this can work on Postgress, but here is a SQL Server solution:

    CREATE TABLE dbo.Teams(TeamID INT NOT NULL PRIMARY KEY);
    GO
    CREATE TABLE dbo.Players(PlayerID INT NOT NULL PRIMARY KEY,
      TeamID INT NOT NULL FOREIGN KEY REFERENCES dbo.Teams(TeamID),
      NumberInTeam INT NOT NULL CHECK(NumberInTeam IN (1,2)),
      TeamMateID INT NOT NULL,
      TeamMatesNumberInTeam INT NOT NULL,
    -- if NumberInTeam=1 then TeamMatesNumberInTeam must be 2
    -- and vise versa
      CHECK(NumberInTeam+TeamMatesNumberInTeam = 3), 
      UNIQUE(TeamID, NumberInTeam),
      UNIQUE(PlayerID, TeamID, NumberInTeam),
      FOREIGN KEY(TeamMateID, TeamID, TeamMatesNumberInTeam)
        REFERENCES dbo.Players(PlayerID, TeamID, NumberInTeam)
    );
    
    INSERT INTO dbo.Teams(TeamID) SELECT 1 UNION ALL SELECT 2;
    GO
    

    — you can only insert players in complete pairs

    INSERT INTO dbo.Players(PlayerID, TeamID, NumberInTeam, TeamMateID, TeamMatesNumberInTeam)
    SELECT 1,1,1,2,2 UNION ALL
    SELECT 2,1,2,1,1;
    

    You can try inserting a single player, or deleting a player from a team, or inserting more than two players per team – all will fail due to a complete set of constraints.

    Note: the practice in SQL Server is to explicitly name all constraints. I did not name my constraints just in case that is not compatible with Postgres.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find the best way to design the database in order
Whenever I design a database, I always wonder if there is a best way
I'm looking for a book/site/tutorial on best practices for relational database design, tuning for
Whats the best design pattern to use for LINQ and type tables that exist
What is the best design decision for a 'top-level' class to attach to an
I am doing my best to design my web app with good separation between
What is the best way to design an interface so that very long drop
I'm considering the best way to design a permissions system for an admin web
In short: what is the best way to design and implement a factory+plugin mechanism,
What is your best practical user-friendly user-interface design or principle? Please submit those practices

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.