Nodral Posted September 27, 2011 Share Posted September 27, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/247970-foreign-key-not-working/ Share on other sites More sharing options...
mikosiko Posted September 27, 2011 Share Posted September 27, 2011 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 ; Quote Link to comment https://forums.phpfreaks.com/topic/247970-foreign-key-not-working/#findComment-1273317 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.