tags:

views:

212

answers:

5

I have created two tables in MySQL which are called "birthTable" and "deathTable". When I add a person in deathTable which she/he was in birthTable, I want to delete his/her name and family from birthtable because I add him/her in deathTable. But I don't know how I can do that?

+1  A: 

You can write trigger on insert on table deathTable which deletes corresponding row from birthTable.

CREATE TRIGGER trg_deathTable_insert
    BEFORE INSERT ON deathTable
    FOR EACH ROWS
BEGIN
    DELETE FROM birthTable WHERE person_id = NEW.person_id;
END;
alygin
there is no need for triggers here... and deleting by id is probably wrong if the id is autoincremental
knoopx
"deleting by id is probably wrong if the id is autoincremental"Can you, please, explain why it is wrong?
alygin
+4  A: 

This is the SQL:

DELETE FROM birthTable WHERE id = ...

where id is the name of some identifying field.

Here is the documentation for DELETE.

nickf
+14  A: 

What you describe can be done manually or with triggers, but generally you should not do it.

You should have a table called people, and then simply mark them as dead or alive. Instead, you may also create two columns for their birthday and deathday, if you intend to store this information.

In general, you shouldn't be moving records simply because some attribute about them has changed.

Daniel Vassallo
Technically, everything you said is correct (and I agree with you), but nothing you've said here answers the question or helps solve the OP's immediate problem.
Graeme Perrow
It was an attempt to guide the OP to a better solution.
Daniel Vassallo
The best way to answer a question is to help the OP with his problem, not his intended solution to his problem. +1
erikkallen
Your overall solution is unquestionably better. But when I see a question like "How do I do _X_ ", I don't like seeing answers like "You don't want to do _X_ . You want to do _Y_ ." Johanna asked "how do I delete a row" and after reading your answer, she still doesn't know how to delete a row.
Graeme Perrow
@Graeme: I get your point, but I cannot agree... "To work so hard and to dig for long, but to discover you've dug the hole in the wrong place." I prefer to point to the right spot, rather than providing a bigger shovel.
Daniel Vassallo
OK, well put - I'll buy that. Consider yourself un-downvoted.
Graeme Perrow
+2  A: 

What you have to do is construct a DELETE FROM-query. If you only want to delete one row from a table, you need to be certain that the parameters you give it are unique for that row. Usually this is done by having a column called id. Then, knowing the id, all you have to do is this:

DELETE FROM table WHERE id=<your id>;

I think that in your case you could just have a table called "people", and a boolean column "alive" which is 1 if the person is alive and 0 if the person is dead.

If you still want to delete one row from your birthTable, assuming you know the name of this person, you do it like this:

DELETE FROM birthTable WHERE firstName=<first name> AND lastName<last name>;

I've assumed that the column names for first and last names are firstName and lastName, you'll have to modify this to match your column names. This will delete any entry in the birthTable which matches the critera (could be more than one, if you have for example 2 people called Alan Johnson).

Hope that helps. :)

vrutberg
Vrutberg, I hope you understand how amazing it is that you got an accepted-answer tick from Johanna... that's rarer than unicorn silk.
delfuego
A: 
DELETE FROM birthTable WHERE id = xxx
Ahmad Dwaik