Rahul Vyas
Rahul Vyas

Reputation: 28740

how to create triggers in sqlite3?

i am new to this topic. i want something like this.

i have two tables in my sqliteDatabase one is master and another is child. Now if i delete a record from master suppose i delete a row from master where id=5, then all the records from the child table whose id = 5 deleted automatically. I don't know how to create triggers and how to apply foreign key constraints so someone please tell me a way to do this in sqlite3 Manager of firefox. Thanks

Upvotes: 0

Views: 1789

Answers (2)

Peter Lang
Peter Lang

Reputation: 55594

You don't need a trigger for that, your foreign key will do that if you define ON DELETE CASCADE:

CREATE TABLE child(
  id         INTEGER,
  some_info  TEXT, 
  master_id  INTEGER,
  FOREIGN KEY(master_id) REFERENCES master(id) ON DELETE CASCADE
);

See documentation about foreign keys.

EDIT:

If you really need to do it using a trigger, have a look at Foreing Key Triggers.

Upvotes: 3

Related Questions