views:

44

answers:

4

I have Group and each group has contacts associated with it. When a user deletes a group, if the group is not empty then it will alert them that all contacts in that group will be deleted if they continue. Well anyways, so my problem is setting up that feature.

I have tried to figure out how I can delete all contacts that belong to that group and delete the group as well.

Before I continue I'm wondering but is there a sorta of automated way of doing this via foreign keys?

Well if not its ok, this is my query but SQL Workbench is throwing out the following error

DELETE c
FROM `list_`.`contacts` AS c
INNER JOIN `list_`.`groups` AS g ON c.group_id = g.id
WHERE g.group = 'School' 
  AND c.user_id = 2;

error:

Error Code: 1046 No database selected

really confused here, also I have also tried c.*

A: 

I think you can do that with a trigger.

redhatlab
how would i do that?
s2xi
+1  A: 

You don't state your RDBMS, but in SQL Server you could turn on cascade deletes, BUT I wouldn't advise doing that; it's too dangerous.

Update: MySQL InnoDB supports cascading deletes as well: FOREIGN KEY Constraints

It is safer to first manually delete all the referencing rows, and then delete the group.

The error message "Error Code: 1046 No database selected" suggests that it is NOT your TSQL at fault. Are you pointing to a database?

Mitch Wheat
i'm using InnoDB
s2xi
Hmmm, ok well I think I'll go the manual way. After all I just need to set it up once and let the PHP do the repetitive work for me ;-) But in regards to my query, whats wrong?
s2xi
so are you saying that using the constraints would be the way to go?
s2xi
Well this is the thing, my database is list_ which is included in my query.
s2xi
+1  A: 

In Mysql you can try something like this for the trigger:

DELIMITER $$
DROP TRIGGER IF EXISTS `deluser`$$

CREATE TRIGGER `deluser` BEFORE DELETE on `biguser`
FOR EACH ROW
BEGIN
DELETE FROM smalluser WHERE id=OLD.id;
END$$

DELIMITER ;

Note: the trigger has to be before delete otherwise you might lose the key you want to use to delete the records.

redhatlab
+2  A: 

MySQL supports multi-table deletions in a single statement - use:

DELETE c, g
  FROM `list_`.`contacts` AS c
  JOIN `list_`.`groups` AS g ON c.group_id = g.id
                            AND g.group = 'School' 
 WHERE c.user_id = 2;

Regarding error code 1046, when using WorkBench make sure the appropriate database/catalog is selected in the drop down menu found above the Object Browser tab. You can specify the default schema/database/catalog for the connection - click the "Manage Connections" options under the SQL Development heading of the Workbench splash screen.

OMG Ponies
I tried something similar to your suggestion, but my problem here is that the Error Code 1046, no database selected is not going away. No idea what to do
s2xi
@s2xi: See my additional comment - you've made a connection to the database, but it needs a schema/database/catalog context. Having the database in the query isn't enough.
OMG Ponies