创建表时出错

时间:2014-04-14 08:27:06

标签: mysql database

这是我的陈述,我在Customer_T上收到错误。错误说明:

"01:22:05   DROP TABLE customer_t   Error Code: 1051. Unknown table 'energyefficient.customer_t'    0.016 sec"

CREATE TABLE Customer_t
             (CustomerID   INT               NOT NULL,
              Name         VARCHAR(45)       NOT NULL,
              Address      VARCHAR(256)              ,
              Email        VARCHAR(100)              ,    
              Phone        VARCHAR(16)               ,
CONSTRAINT PK_CustomerID PRIMARY KEY (CustomerID));


CREATE TABLE Order_t
          (OrderID            INT        NOT NULL,
               OrderDate          DATE           NULL,
               CustomerID         INT        NOT NULL,
CONSTRAINT PK_OrderID PRIMARY KEY (OrderID),
CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES customer_t(CustomerID));

CREATE TABLE Equipment
             (EquipmentID         INT        NOT NULL,
              EquipmentType       VARBINARY(12)      ,
              YearOfManufacture   INT                ,
              Cost                DECIMAL(9,2)       ,
          Maker               VARCHAR(45)        ,
          Model               VARCHAR(45)        ,
          CustomerID          INT                ,
CONSTRAINT EquipmentID_PK PRIMARY KEY (EquipmentID),
CONSTRAINT CustomerID_FK FOREIGN KEY (CustomerID) REFERENCES Customer_t(CustomerID));

CREATE TABLE Order_Line_t
              (OrderLineID        INT        NOT NULL,
              OrderID             INT        NOT NULL,
              EquipmentID         INT        NOT NULL,
              OrderLineCost       DECIMAL(9,2)       ,
CONSTRAINT OrderLineID_PK PRIMARY KEY (OrderLineID),
CONSTRAINT OrderID_FK1    FOREIGN KEY (OrderID) REFERENCES Order_t(OrderID),
CONSTRAINT EquipmentID_FK2 FOREIGN KEY (EquipmentID) REFERENCES Equipment_t(EquipmentID));

CREATE TABLE MaintenanceSchedule_t
             (MaintenanceID       INT         NOT NULL,
              MaintenanceType     VARCHAR(45) NOT NULL,
              Schedule_Date            DATE        NOT NULL,
              EquipmentID         INT         NOT NULL,
              ServiceID           INT                 ,
CONSTRAINT MaintenanceID_PK PRIMARY KEY (MaintenanceID),
CONSTRAINT EquipmentID_FK3 FOREIGN KEY (EquipmentID) REFERENCES Equipment(EquipmentID));

CREATE TABLE Service
             (ServiceID           INT          NOT NULL,
              EstimatedCost       DECIMAL(9,2) NOT NULL,
              Status              VARCHAR(16)  NOT NULL,
              ServiceDate         DATE                 ,
              EquipmentID         INT          NOT NULL,
              EmployeeID          INT          NOT NULL,
              ActualCost          DECIMAL(9,2)         ,
              ServiceType         VARCHAR(45)  NOT NULL,
              Notes               VARCHAR(2000)        ,

CONSTRAINT ServiceID_PK PRIMARY KEY (ServiceID),
CONSTRAINT EquipmentID_FK FOREIGN KEY (EquipmentID)  REFERENCES Equipment(EquipmentID));

CREATE TABLE Employee_t
             (EmployeeID          INT          NOT NULL,
              AnnualSalary        DECIMAL(9,2)         ,
              Name                VARCHAR(45)  NOT NULL,
              DOB                 DATE                 ,
              POSITION            VARCHAR(45)          ,
CONSTRAINT EmployeeID_PK PRIMARY KEY (EmployeeID));  

2 个答案:

答案 0 :(得分:0)

尝试使用:

设置foreign_key_checks = 0; drop table energyefficient.customer_t; set foreign_key_checks = 1;

有时需要将外键检查设置为0;

由于

答案 1 :(得分:0)

可能性1

您的表创建脚本没有任何错误。

根据显示的错误消息:

"01:22:05   DROP TABLE customer_t   
Error Code: 1051. Unknown table 'energyefficient.customer_t'    0.016 sec"

您正在尝试将表customer_t放入名为energyefficient的数据库中。

可能的原因是您在其他数据库中创建了表并尝试从其他地方删除。

可能性2

您可以修改drop table以包含if exists子句,以便在不存在此类表的情况下,以静默方式禁止并忽略错误,但会显示警告。

示例

DROP TABLE IF EXISTS customer_t;

如果您首先删除表,请将全局变量foreign_key_checks更改为false并运行删除。

set foreign_key_checks = 0;
DROP TABLE IF EXISTS customer_t;
-- other drop statements here.
set foreign_key_checks = 1; -- reset to default