mysql - How To delete parents and child table row using SQL query? -


i want delete rows parent , child tables.

for example

i have 2 tables: 1) group 2) group_member

group table have following fields:

group_id, group_name, user_id

and group_member have following fields:

id, group_id, user_id

i delete this,

supposing group table have user_id 2 , members of user_id available on group_member table

table 1 image

enter image description here

table 2 image

enter image description here

if delete query fires - records id = 19 deleted both tables

you need use delete cascade, use below tables , test.

-- ---------------------------- -- table structure candidate_master -- ---------------------------- drop table if exists `candidate_master`; create table `candidate_master` (   `candidate_id` int(10) unsigned not null,   `first_name` varchar(100) default null,   `middle_name` varchar(100) default null,   `last_name` varchar(100) default null,   `phone_number` varchar(25) not null,   `callerid` varchar(25) default null,   `duration` int(11) default null,   `city` varchar(50) default null,   `state` varchar(80) default null,   `date_modified` datetime default null,   `key_skills` text,   `visa` varchar(20) default null,   `status` varchar(100) default null,   `pvm` enum('voicemail','ping') default null,   `id` int(11) not null,   `isdnc` enum('no','yes') not null default 'no',   unique key `number` (`phone_number`) using btree,   key `candidate_master_ibfk_1` (`id`),   constraint `candidate_master_ibfk_1` foreign key (`id`) references `cglistupdatelog` (`id`) on delete cascade ) engine=innodb default charset=latin1;  -- ---------------------------- -- table structure cglistupdatelog -- ---------------------------- drop table if exists `cglistupdatelog`; create table `cglistupdatelog` (   `id` int(11) not null auto_increment,   `file_name` varchar(255) default null,   `pass` int(11) default null,   `fail` int(11) default null,   `updatedon` timestamp not null default current_timestamp on update current_timestamp,   `status` enum('new','processing','processed','failed') default null,   `dialstatus` enum('yes','no') default 'no',   `notdialed` int(11) default null,   `filestatus` enum('processed','pending') default 'processed',   `comment` varchar(50) default null,   primary key (`id`) ) engine=innodb auto_increment=101 default charset=latin1; 

for further let me know!!


Comments