mardi 3 novembre 2015

SQLite on delete cascade with two foreign keys

If I have two tables and a third table that is the foreign key to the first and second table as follows:

CREATE TABLE A
(
    name    VARCHAR(255),
    PRIMARY KEY(name)
);

CREATE TABLE B
(
    number  INT,
    PRIMARY KEY(number)
);

CREATE TABLE C
(
    cname   VARCHAR(255),
    cnumber INT,
    PRIMARY KEY(cname, cnumber),
    FOREIGN KEY(cname) REFERENCES A(name) ON DELETE CASCADE,
    FOREIGN KEY(cnumber) REFERENCES B(number) ON DELETE CASCADE
);

INSERT INTO A values("John");
INSERT INTO A values("Sam");
INSERT INTO B values(1);
INSERT INTO B values(2);
INSERT INTO C values("John", 1);
INSERT INTO C values("John", 2);
INSERT INTO C values("Sam", 2);

I want to delete 1 such that (1) is deleted from B and the entry (John,1) is also deleted from C and (John) in A is also deleted.

Because there is DELETE CASCADE I should be able to do it but:

DELETE FROM B WHERE number = 1;

only removes 1 from B and (John,1) from C but (John) in A is not deleted. Deleting from table C only deletes that one entry so it's not useful...

Aucun commentaire:

Enregistrer un commentaire