Jump to content

Foreign Key not working


Nodral

Recommended Posts

Hi all

 

I'm trying to set up a simple MySQL database and the first 3 tables will set up fine, but I cannot get a reference table to set up foreign keys.  See my SQL below, I'm getting the following error

 

#1064 - 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 'FOREIGN KEY (item) REFERENCES supermarket.items(item),

    FOREIGN KEY (cat_id) RE' at line 4

 

 

SQL

CREATE DATABASE `supermarket` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

 

CREATE TABLE `supermarket`.`log`

(`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

`item` INT NOT NULL,

`time` TIMESTAMP NOT NULL);

 

CREATE TABLE `supermarket`.`items`

(`item` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

`item_label` VARCHAR(255) NOT NULL);

 

CREATE TABLE `supermarket`.`category`

(`cat_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

`cat_label` VARCHAR(255) NOT NULL);

 

CREATE TABLE `supermarket`.`mapping`

(`item` INT NOT NULL,

`cat_id` INT NOT NULL)

 

FOREIGN KEY (item) REFERENCES supermarket.items(item),

FOREIGN KEY (cat_id) REFERENCES supermarket.category(cat_id);

Link to comment
https://forums.phpfreaks.com/topic/247970-foreign-key-not-working/
Share on other sites

simple syntax error....

 

 

CREATE TABLE `supermarket`.`mapping`

  (`item` INT NOT NULL,

  `cat_id` INT NOT NULL)  /// incorrect ")" usage here... replace it for ,

  FOREIGN KEY (item) REFERENCES supermarket.items(item),

  FOREIGN KEY (cat_id) REFERENCES supermarket.category(cat_id);  // add ) here before the ;

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.