我第一次模拟了一个非常简单的数据库并尝试插入查询:
CREATE TABLE `product` (
`pid` varchar(255) NOT NULL,
`mfr` varchar(255) NULL,
`pnum` varchar(255) NOT NULL,
`ssku` varchar(255) NULL,
PRIMARY KEY (`pid`)
);
CREATE TABLE `detail` (
`pid` varchar(255) NOT NULL,
`sdesc` varchar(255) NULL,
`supplier` varchar(255) NULL,
PRIMARY KEY (`pid`)
);
CREATE TABLE `images` (
`pid` varchar(255) NOT NULL,
`url` varchar(255) NULL,
`alt` varchar(255) NULL,
`position` varchar(255) NULL,
PRIMARY KEY (`pid`)
);
ALTER TABLE `detail` ADD CONSTRAINT `fk_detail` FOREIGN KEY (`pid`) REFERENCES `product` ();
ALTER TABLE `images` ADD CONSTRAINT `pid` FOREIGN KEY () REFERENCES `product` ();
...但是我收到了这个错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') REFERENCES `product` ()' at line 1
我做错了什么?
答案 0 :(得分:2)
您必须将列名传递给REFERENCES
子句:
REFERENCES product(pid);
此外,您正在images
约束上创建一个空白外键:
FOREIGN KEY () REFERENCES `product` ();