eozzy
eozzy

Reputation: 68680

Error creating tables with foreign keys

I modelled a really simple DB for the first time and trying to insert the queries:

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

... but I get this error:

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

what am I doing wrong?

Upvotes: 0

Views: 75

Answers (1)

BenM
BenM

Reputation: 53198

You must pass the column name to the REFERENCES clause:

REFERENCES product(pid);

Also, you're creating a blank foreign key on the images constraint:

FOREIGN KEY () REFERENCES `product` ();

Upvotes: 2

Related Questions