gpt4 book ai didi

mysql - 由于外键错误而无法创建表

转载 作者:行者123 更新时间:2023-11-29 11:39:16 26 4
gpt4 key购买 nike

我正在尝试创建一个包含产品的数据库,并且创建了一个连接产品和商店的表,但是我的“CREATE TABLE”语句不起作用,我收到 1005 错误和 150 errno,我在不同的网站上搜索过,检查了案例、类型,但没有任何效果。

这是代码

CREATE TABLE IF NOT EXISTS `products` (
`barcode` bigint(20) NOT NULL,
`name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT 'Unnamed product',
`allerg` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT 'z',
`description` text CHARACTER SET utf8 COLLATE utf8_bin,
CONSTRAINT pk_products PRIMARY KEY (`barcode`)
)

CREATE TABLE IF NOT EXISTS `stores` (
`name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT 'Unnamed store',
`city` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT 'Not specified',
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
CONSTRAINT pk_stores PRIMARY KEY (`id`)
)

CREATE TABLE IF NOT EXISTS `contain` (
`barcode` bigint(20) NOT NULL,
`id` bigint(20) unsigned NOT NULL,
CONSTRAINT pk_barcode PRIMARY KEY (`barcode`,`id`),
CONSTRAINT fk_bc FOREIGN KEY (`barcode`)
REFERENCES `products`(`barcode`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_id FOREIGN KEY (`id`)
REFERENCES `stores`(`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

有人看到什么了吗?

谢谢。

最佳答案

CREATE TABLE IF NOT EXISTS `products` (
`barcode` bigint(20) NOT NULL,
`name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT 'Unnamed product',
`allerg` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT 'z',
`description` text CHARACTER SET utf8 COLLATE utf8_bin,
CONSTRAINT pk_products PRIMARY KEY (`barcode`)
);

CREATE TABLE IF NOT EXISTS `stores` (
`name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT 'Unnamed store',
`city` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT 'Not specified',
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
CONSTRAINT pk_stores PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `contain` (
`barcode` bigint(20) NOT NULL,
`id` bigint(20) unsigned NOT NULL,
CONSTRAINT pk_barcode PRIMARY KEY (`barcode`,`id`),
CONSTRAINT fk_bc FOREIGN KEY (`barcode`)
REFERENCES `products`(`barcode`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_id FOREIGN KEY (`id`)
REFERENCES `stores`(`id`) ON DELETE CASCADE ON UPDATE CASCADE
);

关于mysql - 由于外键错误而无法创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36088439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com