无法使用ON DELETE SET DEFAULT创建外键

时间:2012-01-16 14:30:13

标签: mysql sql database database-design

我无法使用ON DELETE SET DEFAULT创建外键,但如果我使用ON DELETE CASCADE那么这里所有的工作都是我的sql

CREATE TABLE person(  
    customer_id INT AUTO_INCREMENT PRIMARY KEY,  
    name VARCHAR(100) UNIQUE 
);


CREATE TABLE habits(  
    customer_id INT AUTO_INCREMENT PRIMARY KEY,  
    habit VARCHAR(100) UNIQUE 
);

INSERT INTO `test`.`habits` (`customer_id`, `habit`) VALUES (NULL, 'smoking'), (NULL, 'drinking');
INSERT INTO `test`.`person` (`customer_id`, `name`) VALUES (NULL, 'John'), (NULL, 'Steve');

CREATE TABLE foreigner(  
    customer_id INT AUTO_INCREMENT PRIMARY KEY,  
    customer VARCHAR(100) DEFAULT 'John', 
    habbit  VARCHAR(100) DEFAULT 'smoking', 
    FOREIGN KEY (customer) REFERENCES person(name)  ON DELETE SET DEFAULT ON UPDATE CASCADE,
    FOREIGN KEY (habbit) REFERENCES habits(habit)  ON DELETE SET DEFAULT ON UPDATE CASCADE     
);

如果我使用ON DELETE CASCADE ....任何想法,这很有用吗?

1 个答案:

答案 0 :(得分:3)

来自MySQL Reference Manual ...

  

SET DEFAULT:解析器识别此操作,但InnoDB拒绝表   包含ON DELETE SET DEFAULT或ON的定义   UPDATE SET DEFAULT子句。

您可以尝试SET NULL,但我不确定这是否会导致其他问题。