0

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
);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
user2525364
  • 93
  • 2
  • 3
  • 12

3 Answers3

1

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)
);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • I don't understand the question in your comment. Perhaps you should ask another question (because it doesn't seem related to this one) with a good example of exactly what you are trying to accomplish. – Gordon Linoff Mar 20 '14 at 18:52
0
FOREIGN KEY (stockID) REFERENCES tblStock(stockID)

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

mseifert
  • 5,390
  • 9
  • 38
  • 100
0

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.