Cale
Cale

Reputation: 35

How to create MySql delete trigger

I have created 3 tables.

create table Employees 
( eid integer, ename char(20),age integer,salary real);

create table Departments 
(did integer,dname char(20));

create table Works 
(eid integer,did integer);

The Works.eid is a foreign key referencing Employees.eid, and the Works.did is a foreign key referencing Departments.did

I now need to create two triggers listening to the delete event on the Employees and Departments respectively, and after a deletion on the Employees table or the Departments table, the deleted employee or department should also get deleted from the Works table.

Im a Noob, any help apreeciated.

Upvotes: 1

Views: 41

Answers (1)

Uueerdo
Uueerdo

Reputation: 15941

Just use ON DELETE CASCADE when defining the foreign keys.

http://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html

Upvotes: 1

Related Questions