0

I have an 2 Table

TEAMS -TeamId,TeamName,Country
Players-PlayerID,PlayerName,TotalScore,ToTalMatch,TotalRuns,AvgRunRate.

Now I want to create a table TEAMPLAYERS..which has to contain TEAMID,PlayerId...and if details is deleted in TEAMS,PLAYERS table..the data must be delete in THIRD TABLE..PLS HELP ME

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Your question is unclear. In fact, what is your question? – Gordon Linoff May 04 '16 at 01:45
  • You should be able to use idea of foreign keys & cascade delete. i.e. Add teamId & playerId as foreign keys to your TeamPlayers table. Checkout this link http://www.mysqltutorial.org/mysql-on-delete-cascade/ – MSameer May 04 '16 at 01:49

1 Answers1

0

Here is how you create a table with foreign key constraint with cascade delete.

CREATE TABLE TEAMPLAYERS (
.....
teamId int(11) NOT NULL,
playerId int(11) NOT NULL,
FOREIGN KEY(teamId)
REFERENCES TEAMS(teamId)
ON DELETE CASCADE,
FOREIGN KEY(playerId)
REFERENCES PLAYERS(playerId)
ON DELETE CASCADE
.....
);

Note: ... are to be filled with other desired columns/constraints on TEAMPLAYERS table. Additionally, refer to this other post which asks a similar question MySQL foreign key constraints, cascade delete

Hope this helps!

Community
  • 1
  • 1
MSameer
  • 413
  • 2
  • 9