user2320010
user2320010

Reputation: 95

SQL table won't build

I am getting an error saying there is a syntax error in this code,

CREATE TABLE IF NOT EXISTS `reportcategorys` (
  `categoryName` varchar(100) NOT NULL,
  `subCategoryName` varchar(500) NOT NULL,
  PRIMARY KEY (`categoryName`),
  FOREIGN KEY (`subCategoryName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;




CREATE TABLE IF NOT EXISTS `reportsubcategorys` (
  `subCategoryName` varchar(500) NOT NULL,
  `categoryName` varchar(100) NOT NULL,
  PRIMARY KEY (`subCategoryName`),
  FOREIGN KEY `Category_Name` (`categoryName`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

I can't see the syntax error

Upvotes: 0

Views: 32

Answers (2)

M.chaudhry
M.chaudhry

Reputation: 651

while describing Foreign key you need to write like

FOREIGN KEY(subCategoryName) REFERENCES reportcategory(categoryName)

for more information check this

https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

Upvotes: 1

duffymo
duffymo

Reputation: 308938

What does that FOREIGN KEY point to? No table that I can see.

The syntax ought to look like

FOREIGN KEY(subCategoryName) REFERENCES reportcategory(categoryName)

Substitute the table and its primary key as needed.

Table name ought to be singular: reportCategory, not reportCategories. (Style preference, not syntax.)

Upvotes: 3

Related Questions