-2

I have 3 tables say A,B,C. Table A has primary key column a1 which is the foreign key for table B.Table B has primary key b1 and which is Foreign key for table C.Table C has primary key c1. I am deleting a row from Table A by cascade deleting. its removes the corresponding values from table B but not in table C. Any can explain me With Example please.

Mathan
  • 49
  • 3
  • 10
  • 1
    http://stackoverflow.com/questions/21312276/how-to-use-a-foreign-key-in-sqlite. check this if it helps – Raghunandan Feb 28 '14 at 14:41
  • Thanks but i can do it with 2 tables as given in this link but the problem is i need to do with 3 tables – Mathan Feb 28 '14 at 14:49

1 Answers1

0

Both FOREIGN KEY constraints must be marked with ON DELETE CASCADE:

> CREATE TABLE A(a1 PRIMARY KEY);
> CREATE TABLE B(b1 PRIMARY KEY, fa REFERENCES A(a1) ON DELETE CASCADE);
> CREATE TABLE C(c1 PRIMARY KEY, fb REFERENCES B(b1) ON DELETE CASCADE);
> INSERT INTO A VALUES (1);
> INSERT INTO B VALUES (2, 1);
> INSERT INTO C VALUES (3, 2);
> DELETE FROM A;
> SELECT COUNT(*) FROM C;
0
CL.
  • 173,858
  • 17
  • 217
  • 259