我在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;
答案 0 :(得分:1)
"null"
与null
不同。删除引号。