无法在Mysql中创建表

时间:2013-05-07 08:20:43

标签: mysql

我有这些表,但只设法创建

  1. Insurance_Policy_Type
  2. Historical_Era
  3. 区域
  4. 参考
  5. 组织
  6. Geographic_Area
  7. 其他人正在给我错误,例如,

    "Cant create table" on Loan_Item,
    "Reference_Id dont exist" on Historical_Era_Reference
    "error 1064 (4200)" on Loan
    

    CREATE TABLE Insurance_Policy_Type (
    Insurance_Policy_Type_Id INT (4),
    Insurance_Policy_Type_Name VARCHAR (30),
    PRIMARY KEY (Insurance_Policy_Type_Id));
    
    
    CREATE TABLE Historical_Era (
    Historical_Era_Id INT (4),
    Historical_Era_Name VARCHAR (30),
    PRIMARY KEY (Historical_Era_Id));
    
    
    CREATE TABLE Reference (
    Reference_Id INT (4),
    Reference_Name VARCHAR (30),
    Author VARCHAR (30),
    PRIMARY KEY (Reference_Id));
    
    
    CREATE TABLE Region (
    Region_Id INT (4),
    Region_Name VARCHAR (30),
    PRIMARY KEY (Region_Id));
    
    CREATE TABLE Organisation (
    Organisation_Id INT (4),
    Organisation_Name VARCHAR (30),
    PRIMARY KEY (Organisation_Id));
    
    CREATE TABLE Items (
    Item_Id INT (4),
    Item_Name VARCHAR (30),
    Description VARCHAR (30),
    Insurance_Policy_Type_Id INT (4),
    Geo_Area_Id INT (4)
    PRIMARY KEY (Item_Id),
    FOREIGN KEY (Insurance_Policy_Type_Id) REFERENCES Insurance_Policy_Type(Insurance_Policy_Type_Id),
    FOREIGN KEY (Geo_Area_Id) REFERENCES Geographic_Area(Geo_Area_Id));
    
    CREATE TABLE GeoArea_HistEra (
    Geo_Area_Id INT (4),
    Historical_Era_Id INT (4),
    FOREIGN KEY (Geo_Area_Id) REFERENCES Geographical_Area(Geo_Area_Id)),
    FOREIGN KEY (Historical_Era_Id) REFERENCES Historical_Era(Historical_Era_Id));
    
    CREATE TABLE Historical_Era_Reference (
    References_Id INT (4),
    Historical_Era_Id INT (4),
    FOREIGN KEY (Reference_Id) REFERENCES Reference (Reference_Id),
    FOREIGN KEY (Historical_Era_Id) REFERENCES Historical_Era (Historical_Era_Id));
    
    
    CREATE TABLE Loan_Item (
    Item_Id INT (4),
    Loan_Id INT (4),
    FOREIGN KEY (Item_Id) REFERENCES Items(Item_Id),
    FOREIGN KEY (Loan_Id) REFERENCES Loan(Loan_Id));
    
    CREATE TABLE Geographic_Area (
    Geo_Area_Id INT (4),
    Geo_Area_Name VARCHAR (30),
    Region_Id INT (4),
    PRIMARY KEY (Geo_Area_Id),
    FOREIGN KEY (Region_Id) REFERENCES Region (Region_Id));
    
    CREATE TABLE Loan (
    Loan_Id INT (4),
    Item_Id INT (4)
    Organisation_Id INT(4)
    Loan_Start DAY,
    Loan_End DAY
    FOREIGN KEY (Organisation_Id) REFERENCES Organisation (Organisation_Id),
    FOREIGN KEY (Item_Id) REFERENCES Items (Item_Id));
    

2 个答案:

答案 0 :(得分:2)

首先尝试创建表,然后再添加外键。

答案 1 :(得分:0)

上面的查询存在很多问题。

  1. 您正在引用尚不存在的表:"FOREIGN KEY (Geo_Area_Id) REFERENCES Geographic_Area(Geo_Area_Id));"首先创建表,然后更新外键。

  2. 有很多语法错误 - 缺少逗号和双括号。我建议学习如何发现它们,而不是逐一指向它们,例如我建议使用IDE。我个人在MySQL Workbench中测试语句。

  3. 由于多种原因,查询可能会产生错误150。有关主键为FK in the first answer here的典型错误,请参阅非常好的摘要。