简体   繁体   中英

ERROR 1005 (HY000): Can't create table `carparkful`.`#sql-3b64_3` (errno: 150 “Foreign key constraint is incorrectly formed”)

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

Here are mine codes so far, but i need to alter carparkid in table lot foreign key to carpark. When i use this code:

ALTER TABLE lot
ADD CONSTRAINT FK_carparklot
FOREIGN KEY (carparkid) REFERENCES carpark(carparkid)

it works fine, but when i use this:

ALTER TABLE lot
ADD CONSTRAINT FK_carparklot
FOREIGN KEY (carparkid) REFERENCES carpark(carparkid)
ON DELETE CASCADE            
ON UPDATE SET DEFAULT;

it gives me the error:

ERROR 1005 (HY000): Can't create table `carparkful`.`#sql-3b64_3` (errno: 150 "Foreign key constraint is incorrectly formed")

Operating as designed/documented for InnoDB and NDB engines.

According to DOCS : SET DEFAULT: This action is recognized by the MySQL parser, but both InnoDB and NDB reject table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses

This implies On Update set default option/parameter is not supported

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM