CREATE TABLE user
(userid INT(10), uname VARCHAR(30) NOT NULL, phone_num VARCHAR(10) NOT NULL, car_plate VARCHAR(9) NOT NULL,
PRIMARY KEY (userid));
INSERT INTO user (userid, uname, phone_num, car_plate)
VALUES (576, 'alvin', '81005199', 'sgu1256t'), (877, 'dominic', '97972841', 'sga8814e'), (112, 'desmond', '97551488', 'slj8422y');
CREATE TABLE carpark
(carparkid INT (10), location VARCHAR(35) NOT NULL,
PRIMARY KEY (carparkid));
INSERT INTO carpark (carparkid, location)
VALUES ('001', '25 toa payoh lorong 8'), ('002', '45 burghley drive'), ('003', 'ang mo kio hub');
CREATE TABLE lot
(carparkid INT(10) NOT NULL, lot_num INT(3) NOT NULL, lot_status ENUM('empty', 'reserved', 'occupied') DEFAULT 'empty');
INSERT INTO lot (carparkid, lot_num)
VALUES(001, 001), (001, 002), (001, 003), (001, 004), (001, 005), (002, 001), (002, 002), (002, 003), (002, 004), (002, 005), (003, 001), (003, 002), (003, 003), (003, 004), (003, 005);
到目前为止,这是我的代码,但是我需要将停车场中的carparkid改为停车场。当我使用这段代码时:
ALTER TABLE lot
ADD CONSTRAINT FK_carparklot
FOREIGN KEY (carparkid) REFERENCES carpark(carparkid)
它工作正常,但当我使用它时:
ALTER TABLE lot
ADD CONSTRAINT FK_carparklot
FOREIGN KEY (carparkid) REFERENCES carpark(carparkid)
ON DELETE CASCADE
ON UPDATE SET DEFAULT;
它给了我错误:
ERROR 1005 (HY000): Can't create table `carparkful`.`#sql-3b64_3` (errno: 150 "Foreign key constraint is incorrectly formed")
答案 0 :(得分:0)
按照InnoDB和NDB引擎的设计/文档操作。
根据DOCS:SET DEFAULT:MySQL解析器识别此操作,但 InnoDB和NDB拒绝包含 {{1}的表定义}或ON DELETE SET DEFAULT
条款
这意味着不支持更新ON UPDATE SET DEFAULT
选项/参数