Wanting some opinions as the best way of doing this.
So I'm creating a system for a competition, similar to the way sports such as football/soccer work, etc.
Currently I have a Season, a season then has many Teams, a team can then have many Members.
A season then has many Competitions, of which all the teams will always be participating in each competition, however not all the members from each team will participate in the competition.
What is the best way to store what members from a team will participate in a competition?
Currently I was thinking of just a pivot between the competitions table and members, however this doesn't indicate as to what team the member is part of and a member can belong to many teams. Can you have a 3 way pivot table?
Heres my current table structure
CREATE TABLE `seasons` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`archived` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`season_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `teams_season_id_index` (`season_id`),
CONSTRAINT `teams_season_id_foreign` FOREIGN KEY (`season_id`) REFERENCES `seasons` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `members` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`display_name` varchar(12) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `members_display_name_unique` (`display_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `members_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`member_id` int(10) unsigned NOT NULL,
`team_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `members_teams_member_id_index` (`member_id`),
KEY `members_teams_team_id_index` (`team_id`),
CONSTRAINT `members_teams_member_id_foreign` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `members_teams_team_id_foreign` FOREIGN KEY (`team_id`) REFERENCES `teams` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `competitions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`start_at` datetime NOT NULL,
`end_at` datetime NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`skill` int(11) NOT NULL,
`season_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `competitions_season_id_index` (`season_id`),
CONSTRAINT `competitions_season_id_foreign` FOREIGN KEY (`season_id`) REFERENCES `seasons` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I would have a players
table so that players’ information are segregated from any team information. You could then maybe have a competition_players
table or similar, that has three foreign keys: team ID, player ID, and competition ID, so you can see who played for what team in each competition.