收到MYSQL错误但我无法理解发生了什么

时间:2016-12-10 16:23:22

标签: mysql foreign-keys constraints

我有一个.sql文件,我用它来填充数据库中的表。以下是我正在使用的代码。

CREATE TABLE Worker (
   worker_id int NOT NULL AUTO_INCREMENT,
   fname varchar(15) NOT NULL,
   lname varchar(20) NOT NULL,


   primary key(worker_id)
   );


CREATE TABLE Customer (
  customer_id int NOT NULL Auto_Increment,
  primary key (customer_id)
   );

CREATE TABLE Orders (
 Orders_id int NOT NULL AUTO_INCREMENT,
 customer_id int NOT NULL,
 worker_id int NOT NULL,
 Orders_detail_id int NOT NULL,
 Orders_Placed date NOT NULL,
 Delivered_on date NOT NULL,
 Total varchar(8) NOT NULL,
 Ship_addr varchar(50) NOT NULL,
 Order_status varchar(35) NOT NULL,

 primary key(Orders_id),
 foreign key(customer_id) references Customer(customer_id),
 foreign key(worker_id) references Worker(worker_id),
 foreign key(Orders_detail_id) references orders_detail(Orders_detail_id)
 );

CREATE TABLE orders_detail (
   Orders_detail_id int NOT NULL Auto_Increment,
   Product_id int NOT NULL,
   Quantity varchar(4) NOT NULL,

   primary key (Orders_detail_id, Product_id)
   );

CREATE TABLE PRODUCT (
  Product_id int NOT NULL Auto_Increment,
  Prod_name varchar(20) NOT NULL,
  Prod_desc varchar(45) NOT NULL,
  Prod_price varchar(6) NOT NULL,
  Quantity_left varchar(4) NOT NULL,

  primary key (Product_id)
  );
  • 尝试运行该文件时,收到以下错误:

ERROR 1005 (HY000) at line 27: Can't create table 'houston.ORDERS' (errno: 150)

如果能得到关于我收到错误的原因的提示,我将不胜感激。

4 个答案:

答案 0 :(得分:1)

您正在orders表 - orders_detail引用foreign key(Orders_detail_id)之前创建orders_detail(Orders_detail_id)表,搜索此查询后创建的表orders_detail。< / p>

使用此代码,只需对其进行测试即可运行:

CREATE TABLE Worker (
   worker_id int NOT NULL AUTO_INCREMENT,
   fname varchar(15) NOT NULL,
   lname varchar(20) NOT NULL,

   primary key(worker_id)
);


CREATE TABLE Customer (
   customer_id int NOT NULL Auto_Increment,
   primary key (customer_id)
);

CREATE TABLE orders_detail (
   Orders_detail_id int NOT NULL Auto_Increment,
   Product_id int NOT NULL,
   Quantity varchar(4) NOT NULL,

   primary key (Orders_detail_id, Product_id)
);

CREATE TABLE Orders (
   Orders_id int NOT NULL AUTO_INCREMENT,
   customer_id int NOT NULL,
   worker_id int NOT NULL,
   Orders_detail_id int NOT NULL,
   Orders_Placed date NOT NULL,
   Delivered_on date NOT NULL,
   Total varchar(8) NOT NULL,
   Ship_addr varchar(50) NOT NULL,
   Order_status varchar(35) NOT NULL,

   primary key(Orders_id),
   foreign key(customer_id) references Customer(customer_id),
   foreign key(worker_id) references Worker(worker_id),
   foreign key(Orders_detail_id) references orders_detail(Orders_detail_id)
);

CREATE TABLE PRODUCT (
   Product_id int NOT NULL Auto_Increment,
   Prod_name varchar(20) NOT NULL,
   Prod_desc varchar(45) NOT NULL,
   Prod_price varchar(6) NOT NULL,
   Quantity_left varchar(4) NOT NULL,

   primary key (Product_id)
);

答案 1 :(得分:0)

这是因为您尝试将Orders中的外键约束添加到orders_detail。尝试在订单

之前移动orders_detail的创建表

答案 2 :(得分:0)

首先,您必须运行此sql。

SET FOREIGN_KEY_CHECKS=0;

并且,你运行你的sqls。

之后,运行此sql。

SET FOREIGN_KEY_CHECKS=1;

就是这样。

答案 3 :(得分:0)

foreign key(Orders_detail_id) references orders_detail(Orders_detail_id)搜索此查询后创建的表orders_detail

在脚本底部插入CREATE TABLE Orders查询,一切都应按预期工作:)