I have these two tables for this site I'm building (login and licks). I want to allow the user to save his favorite licks so this means I need a mapping table but it's not working. It works without the foreign key constraint but I want/need the foreign key constraint. I've done research and everyone says to create the mapping table as I am but it's not working. Can anyone tell me why this wont work? Thank you.
Table: Login
CREATE TABLE `login` (
`login_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
PRIMARY KEY (`login_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
Table: Licks
CREATE TABLE `licks` (
`lick_id` int(11) NOT NULL AUTO_INCREMENT,
`lick_name` varchar(50) DEFAULT NULL,
`lick_category` varchar(20) DEFAULT NULL,
`lick_html_pg` varchar(50) DEFAULT NULL,
PRIMARY KEY (`lick_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
Table login_licks (Not woking!)
CREATE TABLE login_lick (
login_id INT NOT NULL,
lick_id INT NOT NULL,
PRIMARY KEY (login_id, lick_id),
FOREIGN KEY login_id REFERENCES login (login_id),
FOREIGN KEY lick_id REFERENCES licks (lick_id)
);
You are missing parentheses in the foreign key definition:
CREATE TABLE login_lick (
login_id INT NOT NULL,
lick_id INT NOT NULL,
PRIMARY KEY (login_id, lick_id),
FOREIGN KEY (login_id) REFERENCES login (login_id),
FOREIGN KEY (lick_id) REFERENCES licks (lick_id)
);
Here is a SQL Fiddle.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.