我收到一个
错误代码 1215 无法添加外键约束。
当我FOREIGN KEY (product_id) REFERENCES products(product_id)
省略代码时,它似乎可以工作,但是当我添加它时,我收到了错误。
CREATE SCHEMA `my_web_db`;
USE my_web_db;
DROP TABLE IF EXISTS USERS;
DROP TABLE IF EXISTS DOWNLOADS;
DROP TABLE IF EXISTS PRODUCTS;
CREATE TABLE `my_web_db`.`USERS` (
`user_id` INT NOT NULL ,
`email_address` VARCHAR(100) NULL ,
`first_name` VARCHAR(45) NULL ,
`last_name` VARCHAR(45) NULL ,
PRIMARY KEY (`user_id`) )
DEFAULT CHARACTER SET = utf8;
CREATE TABLE `my_web_db`.`DOWNLOADS` (
`download_id` INT NOT NULL ,
`user_id` INT ,
`download_date` DATETIME NULL ,
`filename` VARCHAR(45) NULL ,
`product_id` INT NOT NULL,
PRIMARY KEY (`download_id`),
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
)
DEFAULT CHARACTER SET = utf8;
CREATE TABLE `my_web_db`.`PRODUCTS` (
`product_id` INT NOT NULL ,
`product_name` VARCHAR(45) NULL ,
PRIMARY KEY (`product_id`)
)
DEFAULT CHARACTER SET = utf8;