ON DELETE CASCADE无效

时间:2012-01-04 17:49:08

标签: mysql foreign-keys

这是2个表的SQL

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)
);

但是,当我删除客户时,他们的地址不会被删除,为什么会有任何想法?

1 个答案:

答案 0 :(得分:6)

如果不知道你正在使用哪个数据库,真的无法确定,但我认为你可能需要将fk声明为这样的约束:

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
);