Deleting A specific table in msql - mysql

I would like to create a trigger that will delete all database records from the survey_responses for a given survey_responder when delete a record from survey_responder.
In other words, when I delete a survey_responder I want to delete their responses from the database so that there are no orphan records.
The thing is I keep getting an error. Any help?
DELIMITER $$
CREATE TRIGGER delete_log AFTER DELETE on survey_responders
FOR EACH ROW
BEGIN
delete * from survey_responses
where survey_responders.id = survey_responses.survey_responder_id;
END$$
DELIMITER ;

You might want to remove * from delete query.

Related

Updating after delete | mysql trigger

I have two tables for example table1 and table2. If something is deleted in table1 i want to update a column in table2. Is this even possible with a trigger in phpmyadmin? if yes what do i have to add or which syntax i have to use for it to work?
I tried this so far:
DELIMITER //
CREATE TRIGGER `update` AFTER DELETE ON table1
FOR EACH ROW BEGIN
-- this is the part i dont know what to do and i couldnt find any related to my task
END//
DELIMITER ;
Well, in your TRIGGER you can access the value you just deleted with OLD.your_column_name.
So just do :
DELIMITER //
CREATE TRIGGER `update` AFTER DELETE ON table1
FOR EACH ROW BEGIN
UPDATE table2 SET column_name = your_new_value WHERE column_name = OLD.old_value;
END//
DELIMITER ;

MySQL select statement result in after delete trigger always null

I'm having a little issue with a trigger in a MySQL database. I have a DB with two tables: "tasks" and "files". The "tasks" table have a field which is a foreign key of the primary key from the "files" table. It also sometimes may be null.
What I'm trying to acomplish is to delete in the first place a row in the "tasks" table, and after that delete the corresponding row in the "files" table using a trigger.
This is the trigger I'm using right now:
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DECLARE fileId int;
SELECT file INTO fileId FROM tasks WHERE id=old.id;
DELETE FROM files WHERE id=fileId;
END;//
DELIMITER ;
The field "file" in the "tasks" table is the one containing the foreign key. In the examples I've been running, that field has never been null.
The problem is that the select statement always returns null. The delete statement that triggers this trigger goes fine, but the row in the "files" table is never deleted. I've tried to insert the "fieldId" variable on a testing table, and it's always saving a null value.
Is there any problem on that trigger? Maybe I'm trying to do something merely impossible?
All the help is much appreciated :)
Since it should be looping over each deleted row, why would this not work?
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DELETE FROM files WHERE id=old.file;
END;//
DELIMITER ;
If that doesn't work, could try this:
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DELETE FROM files INNER JOIN tasks ON files.id=tasks.file WHERE tasks.id=old.id;
END;//
DELIMITER ;
but I don't think that should be necessary.
AFTER delete means that the data is deleted, of course you can't find it. Try creating the trigger for BEFORE delete.
You could also more carefully use all of the old values rather than selecting from the table that was deleted from.

What is MySQL's correct syntax for before delete trigger?

I'd like to create a before delete trigger that deletes rows from two different tables. But I can't figure out which parameters to use.
I got a house table, and when I delete a row, I'd like to delete every row in my two other tables: user_house and firm_house, which contains same house id as the one triggering the event.
What does FOR EACH ROW mean? And how can I properly set my trigger up?
USE `mydb`;
DELIMITER $$
CREATE TRIGGER `deleteUnions` BEFORE DELETE ON `house`
FOR EACH ROW
BEGIN
DELETE FROM user_house WHERE ?? = ??;
DELETE FROM firm_house WHERE ?? = ??;
END
Some details about the structure:
user_house is joined by user_id and house_id;
firm_houise is joined by firm_id and house_id.
Refer to the record that gets deleted in the trigger with OLD. Then use the id to delete from the other tables.
DELETE FROM user_house WHERE house_id = OLD.house_id;
DELETE FROM firm_house WHERE house_id = OLD.house_id;

MySQL Trigger with conditional statement and update another table

On MySQL 5.5, I am trying to write a trigger to do the following:
Checks a condition for the insert (is group_id between 8-20?)
If the condition is true, then update the user with the same user_id on another table, called phpbb_users.
The update on this other table is to set user_rank to NEW.group_id from the insert on the table that has the trigger.
Else, do nothing.
Not experienced at all, and stuck. It's not working when I run the query. Here is what I've got so far.
DELIMITER $$
CREATE OR REPLACE TRIGGER upd_rank AFTER INSERT ON `phpbb_user_group`
FOR EACH ROW BEGIN
IF (NEW.group_id BETWEEN 8 AND 20) THEN
UPDATE phpbb_users
SET user_rank=NEW.group_id
WHERE user_id=NEW.user_id;
ELSE
UPDATE phpbb_users
SET user_rank='0'
WHERE user_id=NEW.user_id;
END IF;
END$$;
DELIMITER;
(Note, the "else" clause is unnecessary really - happy for any suggestions for improvement of this code!)
if you only want to get rid of the else try
CREATE TRIGGER upd_rank AFTER INSERT ON `phpbb_user_group`
FOR EACH ROW
UPDATE phpbb_users
SET user_rank=IF(NEW.group_id BETWEEN 8 AND 20,NEW.group_id,"0")
WHERE user_id=NEW.user_id;

MYSQL Stored Proc: How to delete a just updated record

I have two table Name: registeredList & deregisteredlist. Now when a user getting getting deregistered from "registeredlist" table then a trigger update his info to deregistration table and delete the record from registration table. On my below proc i can update it properly but can't delete the user from registered table. My proc:
DELIMITER $$
USE `abc_db`$$
DROP TRIGGER `UnsubscriberListTrigger`$$
CREATE
TRIGGER `UnsubscriberListTrigger` AFTER UPDATE ON `registeredlist` FOR EACH ROW
BEGIN
IF (old.SubscriberStatus='registered') THEN
INSERT INTO deregisteredlist(name,SubscriberStatus,DeRegistrationDate)
VALUES(old.name,'Deregistered',NOW());
DELETE from registeredlist where old.id=new.id;/???????/I am getting problem here
END IF;
END $$
DELIMITER ;
Thanks in advance.
I think all you need is to change WHERE in DELETE statement.
It should go like this:
DELETE from registeredlist where id=old.id; // (or new.id cause in this case old.id is equal to new.id)
... because you want to match it against id column.
UPDATE:
Another possibility is this:
- create AFTER INSERT TRIGGER on deregisteredlist which will do the DELETE in registeredlist. That way you shouldn't get that error.
Try this::
DELETE from registeredlist order by updateddat desc limit 1;