user195257
user195257

Reputation: 3316

ON DELETE CASCADE not working

This is the sql for 2 tables

CREATE TABLE Customer(
    c_id        INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    f_name      VARCHAR(50) NOT NULL,
    l_name      VARCHAR(50) NOT NULL,
    email       VARCHAR(100) NOT NULL,
    number      INTEGER,
    date_joined DATE NOT NULL CHECK (date_added <= now())
);

CREATE TABLE Address(
    a_id        INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    c_id        INTEGER NOT NULL REFERENCES Customer(c_id) ON DELETE CASCADE,
    billing     BOOLEAN,
    f_line      VARCHAR(50) NOT NULL,
    s_line      VARCHAR(100),
    county      VARCHAR(20) NOT NULL,
    p_code      VARCHAR(7) NOT NULL,
    number      INTEGER(11)
);

However when i delete a customer, their address does not get deleted, any ideas why?

Upvotes: 2

Views: 2802

Answers (1)

StevieG
StevieG

Reputation: 8709

Can't really tell for sure without knowing which db you're using, but I think you probably need to declare the fk as a constraint like this:

CREATE TABLE Address(
    a_id        INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    c_id        INTEGER NOT NULL,
    billing     BOOLEAN,
    f_line      VARCHAR(50) NOT NULL,
    s_line      VARCHAR(100),
    county      VARCHAR(20) NOT NULL,
    p_code      VARCHAR(7) NOT NULL,
    number      INTEGER(11),
     FOREIGN KEY (c_id) REFERENCES Customer(c_id) ON DELETE CASCADE
);

Upvotes: 6

Related Questions