I have 3 tables
player, goal, card
how should i build my database so it automatically deletes goal and card row containing player id?
my declarations of tables i suppose i should add on delete cascade but i don’t understand it very well so can any of you help me?
CREATE TABLE IF NOT EXISTS `#__footsal_players` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`ordering` INT(11) NOT NULL ,
`state` TINYINT(1) NOT NULL DEFAULT '1',
`checked_out` INT(11) NOT NULL ,
`checked_out_time` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` INT(11) NOT NULL ,
`id_team` INT(11) NOT NULL ,
`first_name` varchar(255) NOT NULL ,
`last_name` varchar(255) NOT NULL ,
`birth_date` DATE NOT NULL DEFAULT '0000-00-00',
`email` varchar(255) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT COLLATE=utf8_polish_ci
CREATE TABLE IF NOT EXISTS `#__footsal_goals` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`state` TINYINT(1) NOT NULL DEFAULT '1',
`checked_out` INT(11) NOT NULL ,
`checked_out_time` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` INT(11) NOT NULL ,
`id_player` INT(11) NOT NULL ,
`id_resault` INT(11) NOT NULL ,
`id_game` INT(11) NOT NULL ,
`goals_number` VARCHAR(255) NOT NULL ,
`id_session` VARCHAR(11) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT COLLATE=utf8_polish_ci;
CREATE TABLE IF NOT EXISTS `#__footsal_yellow_cards` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`state` TINYINT(1) NOT NULL DEFAULT '1',
`checked_out` INT(11) NOT NULL ,
`checked_out_time` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` INT(11) NOT NULL ,
`id_player` INT(11) NOT NULL ,
`id_game` INT(11) NOT NULL ,
`id_resault` INT(11) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT COLLATE=utf8_polish_ci;
I don’t have MySQL running atm, but code below should work for goals. The idea is that when you define the foreign key, you assign delete and update rules. These apply when the primary key (id in player) is modified. So when the primary key in player is deleted (i.e. the player is deleted), the rows with corresponding foreign keys, behave according to their rule. Cascade means ‘follow’, so in this case delete it too.