patternsqlMinor
Delete trigger mysql on another table
Viewed 0 times
triggerdeletemysqlanothertable
Problem
I am new on mysql and I am try to create a before delete trigger on mysql.
I have a table call "persons" and I need delete a record on another table called "ldap_entries"
the table "persons" have a ID, unique and this id it is on "ldap_entries" but the columns it is called "keyval".
so I am try to do like this: but not working
Here is my describe output:
```
mysql> describe persons;
+------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| surname | varchar(255) | NO | | NULL | |
| package | varchar(64) | NO | | NULL | |
| macaddress | varchar(21) | NO | | NULL | |
| customer | varchar(255) | NO | | NULL | |
| address | varchar(140) | NO | | NULL | |
| phone | varchar(9) | NO | | NULL | |
| email | varchar(50) | NO | | NULL | |
| created | date | NO | | NULL | |
| updated | timestamp | YES | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+--------------+------+-----+-------------------+-----------------------------+
11 rows in set (0.00 sec)
mysql> describe ldap_entries
I have a table call "persons" and I need delete a record on another table called "ldap_entries"
the table "persons" have a ID, unique and this id it is on "ldap_entries" but the columns it is called "keyval".
so I am try to do like this: but not working
CREATE TRIGGER `before_delete`
BEFORE DELETE ON `persons`
FOR EACH ROW
BEGIN
DELETE FROM ldap_entries WHERE ldap_entries.keyval = OLD.id
END $Here is my describe output:
```
mysql> describe persons;
+------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| surname | varchar(255) | NO | | NULL | |
| package | varchar(64) | NO | | NULL | |
| macaddress | varchar(21) | NO | | NULL | |
| customer | varchar(255) | NO | | NULL | |
| address | varchar(140) | NO | | NULL | |
| phone | varchar(9) | NO | | NULL | |
| email | varchar(50) | NO | | NULL | |
| created | date | NO | | NULL | |
| updated | timestamp | YES | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+--------------+------+-----+-------------------+-----------------------------+
11 rows in set (0.00 sec)
mysql> describe ldap_entries
Solution
You need to change the delimter to
$$ and then back to ;:DELIMITER $
CREATE TRIGGER `before_delete`
BEFORE DELETE ON `persons`
FOR EACH ROW
BEGIN
DELETE FROM ldap_entries WHERE ldap_entries.keyval = OLD.id
END $
DELIMITER ;Code Snippets
DELIMITER $$
CREATE TRIGGER `before_delete`
BEFORE DELETE ON `persons`
FOR EACH ROW
BEGIN
DELETE FROM ldap_entries WHERE ldap_entries.keyval = OLD.id
END $$
DELIMITER ;Context
StackExchange Database Administrators Q#108037, answer score: 2
Revisions (0)
No revisions yet.