0

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

  1. 保险_保单_类型
  2. 历史_时代
  3. 地区
  4. 参考
  5. 组织
  6. 地理_区域

其余的都给我错误,例如,

"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));
4

2 回答 2

2

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

于 2013-05-07T08:23:04.313 回答
0

上面的查询有很多问题。

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

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

  3. 由于多种原因,该查询可能会产生错误 150。请在此处的第一个答案中查看有关将主键作为 FK 的典型错误的非常好的摘要。

于 2013-05-07T08:28:33.990 回答