MySQL150: 外键约束的格式不正确
MySQL 150: Foreign key constraint is incorrectly formed
当我将文件导入 phpmyadmin 时出现此错误:
#1005 - Can't create table `test_db`.`part_order` (errno: 150
"Foreign key constraint is incorrectly formed")
我检查了很多次语法,但我看不出有什么问题。我尝试使用 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
)
MySQL 需要引用列(product_name
和 color_name
)上的索引。将 INDEX(product_name)
添加到 all_products
table 并将 INDEX(color_name)
添加到 product_color
table
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`),
INDEX(`product_name`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
来自docs:
MySQL requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan. In the
referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order. Such an
index is created on the referencing table automatically if it does not
exist. This index might be silently dropped later, if you create
another index that can be used to enforce the foreign key constraint.
index_name, if given, is used as described previously.
当我将文件导入 phpmyadmin 时出现此错误:
#1005 - Can't create table `test_db`.`part_order` (errno: 150 "Foreign key constraint is incorrectly formed")
我检查了很多次语法,但我看不出有什么问题。我尝试使用 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
)
MySQL 需要引用列(product_name
和 color_name
)上的索引。将 INDEX(product_name)
添加到 all_products
table 并将 INDEX(color_name)
添加到 product_color
table
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`),
INDEX(`product_name`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
来自docs:
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.