mysql中的外键约束。我无法修复它?

时间:2015-03-28 17:04:40

标签: mysql

我在mysql中创建了以下表格,如下所示:

CREATE TABLE employee (
  fname    varchar(15) not null, 
  minit    varchar(1),
  lname    varchar(15) not null,
  ssn      char(9),
  bdate    date,
  address  varchar(50),
  sex      char,
  salary   decimal(10,2),
  superssn char(9),
  dno      integer(4),
  primary key (ssn),
  foreign key (superssn) references employee(ssn)
);

然后我尝试插入以下信息:

INSERT INTO employee VALUES ("Bob","B","Bender","666666600",
"1968-04-17","8794 Garfield, Chicago, IL","M","96000.00","null","8");

可悲的是,我收到了这个错误:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint 
fails (`test`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY 
(`superssn`) REFERENCES `employee` (`ssn`))

我尝试更改表,以便superssn可以为null,但没有解决方案正常工作。

ALTER TABLE employee MODIFY COLUMN superssn CHAR(9) default null;

1 个答案:

答案 0 :(得分:1)

"null"null不同。删除引号。