gpt4 book ai didi

mysql - 表不工作 : 1215. 无法添加外键约束

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

Error Code: 1215. Cannot add foreign key constraint

(之前的问题是 1217 错误。)我通过在删除表周围添加“setforeign_key_checks”将其修复到半工作状态,唯一的问题是我在订单创建表中收到此错误。 (创建表订单)我查看了订单表中的外键,但没有发现任何错误。我检查了所有外键,它们都正确对齐。

set foreign_key_checks = 0;

DROP TABLE IF EXISTS product;
DROP TABLE IF EXISTS manufacturer;
DROP TABLE IF EXISTS dc_location;
DROP TABLE IF EXISTS customer;
DROP TABLE IF EXISTS details;
DROP TABLE IF EXISTS shipping;
DROP TABLE IF EXISTS orders;

set foreign_key_checks = 1;

CREATE TABLE manufacturer
(

manufacturer_key INT NOT NULL,

manufacturer_name VARCHAR(20),

manufacturer_address VARCHAR(20),

manufacturer_city VARCHAR(10),

manufacturer_state VARCHAR(10),

manufacturer_postalcode INT NOT NULL,

manufacturer_country VARCHAR(10),

manufacturer_main_phone INT NOT NULL,

CONSTRAINT manufacturer_pk
PRIMARY KEY (manufacturer_key)

);

CREATE TABLE dc_location
(

dc_location_key INT NOT NULL,

dc_location_name VARCHAR(15),

dc_location_address VARCHAR(20),

dc_location_city VARCHAR(20),

dc_location_state VARCHAR(10),

dc_location_postalcode VARCHAR(10),

dc_location_country INT NOT NULL,

dc_location_country2 VARCHAR(10),

dc_location_main_phone INT NOT NULL,

CONSTRAINT dc_pk
PRIMARY KEY (dc_location_key)

);

CREATE TABLE customer
(

customer_key INT NOT NULL,

customer_name VARCHAR(15),

customer_email VARCHAR(20),

customer_address VARCHAR(20),

customer_city VARCHAR(10),

customer_state VARCHAR(10),

customer_postalcode INT NULL,

customer_country VARCHAR(10),

customer_main_phone INT NULL,

CONSTRAINT customer_pk
PRIMARY KEY (customer_key)

);


CREATE TABLE product
(

product_key INT NOT NULL,

product_name VARCHAR(15) NOT NULL,

product_desc_short VARCHAR(50) NOT NULL,

manufacturer_key INT NOT NULL,

dc_location_key INT NOT NULL,

CONSTRAINT product_pk
PRIMARY KEY (product_key),

CONSTRAINT product_fk_manufacturer
FOREIGN KEY (manufacturer_key) REFERENCES manufacturer (manufacturer_key),

CONSTRAINT product_fk_dc_location
FOREIGN KEY (dc_location_key) REFERENCES dc_location (dc_location_key)

);

CREATE TABLE details
(

order_key INT NOT NULL,

product_key INT NOT NULL,

product_name VARCHAR(15) NOT NULL,

product_desc_short VARCHAR(15) NOT NULL,

CONSTRAINT details_pk
PRIMARY KEY (order_key),

CONSTRAINT details_fk_product
FOREIGN KEY (product_key) REFERENCES product (product_key)

);

CREATE TABLE orders
(

customer_key INT NOT NULL,

order_key INT NOT NULL,

order_date DATE NOT NULL,

shipping_key INT NOT NULL,

CONSTRAINT orders_pk
PRIMARY KEY (customer_key),

CONSTRAINT orders_fk_customer
FOREIGN KEY (customer_key) REFERENCES customer (customer_key),

CONSTRAINT orders_fk_details
FOREIGN KEY (order_key) REFERENCES details (order_key)

);

CREATE TABLE shipping
(

shipping_key INT NOT NULL,

shipping_date DATE NOT NULL,

delivery_date DATE NOT NULL,

CONSTRAINT shipping_pk
PRIMARY KEY (shipping_key),

CONSTRAINT shipping_fk_shipping
FOREIGN KEY (shipping_key) REFERENCES orders (shipping_key)

);

INSERT INTO customer(customer_email) VALUES

('Bandshirt.com'),

('MetallycaBand.com'),

('FeMaidenRocks.com'),

('MetadethMetal.com'),

('Amazoon.com');

INSERT INTO product VALUES

(1, 'Style1', 'Metallyca', 'Short Sleeve', 'XS/S/M/L/XL/XXL'),

(2, 'Style2', 'Metallyca', 'Short Sleeve', 'XS/S/M/L/XL/XXL'),

(3, 'Style1', 'Metallyca', 'Long Sleeve', 'XS/S/M/L/XL/XXL'),

(4, 'Style2', 'Metallyca', 'Long Sleeve', 'XS/S/M/L/XL/XXL'),

(5, 'Style1', 'Fe Maiden', 'Short Sleeve', 'XS/S/M/L/XL/XXL'),

(6, 'Style2', 'Fe Maiden', 'Short Sleeve', 'XS/S/M/L/XL/XXL'),

(7, 'Style1', 'Fe Maiden', 'Long Sleeve', 'XS/S/M/L/XL/XXL'),

(8, 'Style2', 'Fe Maiden', 'Long Sleeve', 'XS/S/M/L/XL/XXL'),

(9, 'Style1', 'Metadeth', 'Short Sleeve', 'XS/S/M/L/XL/XXL'),

(10, 'Style2', 'Metadeth', 'Short Sleeve', 'XS/S/M/L/XL/XXL'),

(11, 'Style1', 'Metadeth', 'Long Sleeve', 'XS/S/M/L/XL/XXL'),

(12, 'Style2', 'Metadeth', 'Long Sleeve', 'XS/S/M/L/XL/XXL');

最佳答案

First execute the tables which doesn't have foreign key references. Post that, execute the remaining tables.

关于mysql - 表不工作 : 1215. 无法添加外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49945701/

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