0

I have these tables but only managed to create

  1. Insurance_Policy_Type
  2. Historical_Era
  3. Region
  4. Reference
  5. Organisation
  6. Geographic_Area

The rest of them are giving me errors such as,

"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));
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • what error message do you get? – John Woo May 07 '13 at 08:22
  • 1st create the table with out Foreign key. once you create the table alter the table for adding Foreign key. – Janny May 07 '13 at 08:26
  • First of all there is typo in the sql above. You have missed one "," in Items table after Geo_Area_Id that's why it is showing you error like this and as others are saying you can try adding constraints manually and make sure you are using InnoDB Engine. – ATR May 07 '13 at 08:39

2 Answers2

2

Try to create the tables first and add the Foreign Keys later.

Benvorth
  • 7,416
  • 8
  • 49
  • 70
  • 1
    *voted up* so you can place reactions in further cases. Welcome to SO. – Rob May 07 '13 at 08:28
  • Hey guys, thank you all for your contributions, they were really helpful. you even opened my eyes to concentrate. Most of the errors I had were because of typos, missing " , ", having extra " ) " and not declaring Primary key. I hop this will also help somebody. ~ THANKS – user2357469 May 07 '13 at 13:12
  • @user2357469 you can edit your answer pasting the final correct code that you came up with. Also, if my suggestions did help you, please accept my answer. – ılǝ May 13 '13 at 08:30
0

There are plenty of issues with the query above.

  1. You are making a reference to a table which doesn't exist yet: "FOREIGN KEY (Geo_Area_Id) REFERENCES Geographic_Area(Geo_Area_Id));" First create the table and then update the foreign keys.

  2. There are plenty of syntax errors - missing commas and double brackets. Instead of pointing them one by one, I'd suggest learning how to spot them, for example I suggest using an IDE. I personally test statements in MySQL Workbench.

  3. The query will likely produce an error 150, for a number of reasons. Please see a very good summary on typical errors with primary keys as FK in the first answer here.

Community
  • 1
  • 1
ılǝ
  • 3,440
  • 2
  • 33
  • 47