表不工作:1215。无法添加外键约束
Posted
技术标签:
【中文标题】表不工作:1215。无法添加外键约束【英文标题】:Table not working: 1215. Cannot add foreign key constraint 【发布时间】:2018-10-01 09:51:23 【问题描述】:错误代码:1215。无法添加外键约束
(之前的问题是 1217 错误。) 我通过在 drop 表周围添加“set foreign_key_checks”将其修复到半工作状态,唯一的问题是我在 Orders create 表中收到此错误。 (CREATE TABLE orders) 我查看了 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');
【问题讨论】:
请为您正在使用的concrete RBDMS 添加一个标签 .... “我不断收到相同的错误,即 1215,同时警告我 courseproject.product、courseproject.manufacturer 等” 请完整引用所有实际错误,而不仅仅是对它们的这种模糊的解释。 我纠正了一些错误,但即使插入也是错误的。 Demo。例如:表顺序错误、列错误、列名重复两次、自引用FK等 它是使用 FK 的创建表的顺序 - 您在创建之前引用了制造商和 dc_location。 与问题无关,但整数邮政编码会给您带来一些痛苦。即使是相当简单的美国 5 位数邮政编码也包含以前导零开头的代码,这些代码将被丢弃。走出美国,你会喜欢字母数字代码、破折号代码,你有什么。 【参考方案1】:首先执行没有外键引用的表。 发布,执行剩余的表。
【讨论】:
它工作了一会儿,但现在我收到“错误代码:1217。无法删除或更新父行:外键约束失败”。 你试图执行哪个语句? 我正在执行整个代码;我检查了所有外键,但似乎找不到问题。我重新组织了表,因此在创建之前没有调用任何内容,并仔细检查了外键以确保它们调用了正确的东西,它们就是。以上是关于表不工作:1215。无法添加外键约束的主要内容,如果未能解决你的问题,请参考以下文章
创建外键时出错:MySQL 错误 1215:无法添加外键约束 [重复]