Jump to content

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 ;

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.