user2525364
user2525364

Reputation: 93

#1005 - Cant create table... (errno: 150)

I have all the primary keys and drop tables in the correct order.

I basically want stockID to be a foreign key in my refund table.

My schema looks like this...

CREATE TABLE refunds (
refundID SMALLINT AUTO_INCREMENT,
stockID SMALLINT,
refundDate DATE,
FOREIGN KEY (stockID) REFERENCES tblStock(stockID),
PRIMARY KEY (refundID)
);

CREATE TABLE tblStock (
stockID SMALLINT AUTO_INCREMENT,
stockName VARCHAR(60),
StockNumbers SMALLINT
);

Upvotes: 0

Views: 69

Answers (3)

Codhidharma
Codhidharma

Reputation: 36

You are referencing a table that doesn't exist. Create it first.

In addition, you will want to index the key you are referencing. Make sure any value being referenced in your foreign key actually exists.

Upvotes: 0

mseifert
mseifert

Reputation: 5670

FOREIGN KEY (stockID) REFERENCES tblStock(stockID)

Is referencing a table that doesn't yet exist. Create tblStock first.

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269513

When referencing another table for a foreign key reference, that table needs to already exist. And, the column being referenced should be a primary key. Try this:

CREATE TABLE tblStock (
  stockID SMALLINT AUTO_INCREMENT PRIMARY KEY,
  stockName VARCHAR(60),
  StockNumbers SMALLINT
);

CREATE TABLE refunds (
  refundID SMALLINT AUTO_INCREMENT,
  stockID SMALLINT,
  refundDate DATE,
  FOREIGN KEY (stockID) REFERENCES tblStock(stockID),
  PRIMARY KEY (refundID)
);

Upvotes: 1

Related Questions