If you use the InnoDB storage engine, there is no need for a trigger. You can achieve the same effect using a foreign key with an appropriate cascading action:
ALTER TABLE users_dir ADD FOREIGN KEY (userId) REFERENCES users (userId) ON DELETE CASCADE;
If you use MyISAM or one of the other engines that does not yet enforce foreign key constraints, you can use a trigger:
CREATE TRIGGER users_AD AFTER DELETE ON users FOR EACH ROW DELETE FROM users_dir WHERE userId = OLD.userId;
Note that the foreign key is defined on the users_dir table, but the trigger is defined on the users table.
The examples shown here for both foreign keys and triggers only address behavior if the user is deleted. You will need to decide what action to take if the user's id is updated to a different value instead.
Dig Deeper on Open source databases
Related Q&A from Scott Noyes
Get suggestions for choosing MySQL character sets and field collations from expert Scott Noyes.continue reading
A MySQL expert describes two ways that the multi-master support can be used.continue reading
An expert describes where to find information on detection deadlock algorithms in MySQL.continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.