Когда я импортирую файл в phpmyadmin, я получаю следующую ошибку:
# 1005 - Невозможно создать таблицу `test_db``part_order` (errno: 150" Ограничение внешнего ключанеправильно сформировано ")
Я проверял синтаксис много раз, но не вижу, что не так.Я пытался использовать INDEX
и CONSTRAINT
, но безуспешно.Любая помощь будет оценена.
CREATE TABLE IF NOT EXISTS `all_products` (
`product_id` int(10) NOT NULL AUTO_INCREMENT,
`product_name` VARCHAR(100) NOT NULL,
`product_price` decimal(10,2) NOT NULL,
`product_description` VARCHAR(1000) NOT NULL,
PRIMARY KEY(`product_id`, `product_name`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
CREATE TABLE IF NOT EXISTS `product_color` (
`color_id` int(10) NOT NULL AUTO_INCREMENT,
`product_id` INT(10) NOT NULL,
`color_name` VARCHAR(100) NOT NULL,
PRIMARY KEY(`color_id`, `color_name`),
FOREIGN KEY(`product_id`) REFERENCES all_products(`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
CREATE TABLE IF NOT EXISTS `users` (
`user_id` INT(10) NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(100) NOT NULL,
`last_name` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`password` VARCHAR(200) NOT NULL,
`address` VARCHAR(200) NOT NULL,
`city` VARCHAR(100) NOT NULL,
`post_code` VARCHAR(8) NOT NULL,
PRIMARY KEY(`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
CREATE TABLE IF NOT EXISTS `part_order` (
`order_number` INT(10) NOT NULL ,
`product_name` VARCHAR(100) NOT NULL,
`color_name` VARCHAR(100) NOT NULL,
`qty` INT(10) NOT NULL,
PRIMARY KEY(`order_number`),
INDEX(`order_number`),
CONSTRAINT FOREIGN KEY(`product_name`) REFERENCES
all_products(`product_name`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY(`color_name`) REFERENCES
product_color(`color_name`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
CREATE TABLE IF NOT EXISTS `orders` (
`order_number` INT(10) NOT NULL,
`user_id` INT(10) NOT NULL,
Date DATETIME DEFAULT CURRENT_TIMESTAMP,
`total_cost` INT(10) NOT NULL,
`status` VARCHAR(50) NOT NULL,
PRIMARY KEY(`status`),
CONSTRAINT FOREIGN KEY(`order_number`) REFERENCES
part_order(`order_number`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES
users(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
)