Question

I am creating a database for a DVD shop for a uni project and i am having some issues when creating a foreign key i am sure its something really stupid i am missing. So here is the problem i have so far created two tables and I'm now trying to create the third one that contains the primary key from the first two tables here is the SQL for the first two tables and the one I'm having the issue with. Thanks in advance for any help you can give me

CREATE TABLE CATEGORY (
CAT_ID INT NOT NULL AUTO_INCREMENT,
CATEGORY varchar(20) NOT NULL,
PRIMARY KEY (`CAT_ID`))

CREATE TABLE AGE_CERT (
AGE_ID INT NOT NULL AUTO_INCREMENT,
DVD_AGE varchar(3) NOT NULL,
PRIMARY KEY (`AGE_ID`))

CREATE TABLE DVD (
DVD_ID INT NOT NULL AUTO_INCREMENT,
DVD_NAME varchar(30) NOT NULL,
DVD_COST varchar (4) NOT NULL,
AGE_ID INT NOT NULL,
CAT_ID INT NOT NULL,
PRIMARY KEY (`DVD_ID`)
FOREIGN KEY (AGE_ID) references AGE_CERT (AGE_ID) 
FOREIGN KEY (CAT_ID) references CATEGORY (CAT_ID))
Was it helpful?

Solution

You just forgot 2 commas between the keys

CREATE TABLE DVD 
(
  DVD_ID INT NOT NULL AUTO_INCREMENT,
  DVD_NAME varchar(30) NOT NULL,
  DVD_COST varchar (4) NOT NULL,
  AGE_ID INT NOT NULL, 
  CAT_ID INT NOT NULL,
  PRIMARY KEY (`DVD_ID`), /* forgot the comma here */
  FOREIGN KEY (AGE_ID) references AGE_CERT (AGE_ID), /* forgot the comma here */
  FOREIGN KEY (CAT_ID) references CATEGORY (CAT_ID)
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top