表不起作用:1215。无法添加外键约束

时间:2018-04-20 16:05:42

标签: mysql sql sql-server mysql-workbench

  

错误代码:1215。无法添加外键约束

(之前的问题是1217错误。) 我通过添加设置foreign_key_checks'来将其修复到半工作点。在drop table周围,唯一的问题是我在Orders create table中得到了这个错误。 (CREATE TABLE命令)我查看了Orders表中的外键,但是我没有发现任何错误。我检查了所有外键并且都正确对齐。



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');




1 个答案:

答案 0 :(得分:0)

  

首先执行没有外键引用的表。   发布,执行剩余的表。