6

I have a table named user. This table has a foreign key to a department table. One user can be associated with one department. Before deleting a department, I would like to set any user (having that department ID) to a default value (1) to avoid a referential integrity error.

Do you know a good example. Most examples shows that the trigger is applied to one table. Here the trigger should be triggered on department but change values in user table.

Thanks.

2
  • The "trigger is applied to one table" Means a table is bound to a specific act on a specific table, it can't be bound (as in what causes it to run) to multiple tables. It can preform any actions the creator of the trigger has the rights to preform. Commented Dec 22, 2009 at 18:34
  • This SQL worked: CREATE TRIGGER dept_set_user_to_wuk BEFORE DELETE ON dept FOR EACH ROW UPDATE user SET deptid = 1 WHERE deptid = OLD.deptid; where dept is department Commented Jan 4, 2010 at 16:27

3 Answers 3

11

I haven't tested it, but based on the documentation, this looks about right:

CREATE TRIGGER update_user_before_delete BEFORE DELETE ON department
  FOR EACH ROW BEGIN
    UPDATE user SET department = 1 WHERE department = OLD.department;
  END;
Sign up to request clarification or add additional context in comments.

2 Comments

Iget: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE user SET user.deptID = 1 WHERE user.deptID = OLD.deptID' at line 1
You might need to change the delimiter to something else than ";" to create the trigger. Otherwise mysql tries to parse the statement only up to the first semicolon.
3

In most cases it is better to set the child value to NULL when the parent is deleted, rather than using a default of 1 like you are doing.

If you decide that this behavior is appropriate, then you can make it an attribute of the foreign key, and won't require a trigger at all.

Something like this:

ALTER TABLE `user`
ADD CONSTRAINT FK_USER_TO_DEPARTMENT FOREIGN KEY (department_id) 
REFERENCES `department` (department_id) ON DELETE SET NULL;

3 Comments

This makes sense but a requirement is that every user must belong to a department, or at least at department 1, which is the company. Yes it wuold be better and easier to use SET NULL but I cannot do that.
@rtacconi: you can use set default value in foreign key constraint
set default value in foreign key constraint to a specific number can break Mysql's backup when you restore/import your data, even if you disable the constraints check. I sounds crazy but it happened to me.
0

You can use any sql statement in your trigger code. When a record is deleted the trigger-code is fired. You can use the record wich fired the trigger (to select the department id) and then select any user with that id and update that user record. Good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.