0

I am trying to add foreign key constraints to two existing tables. I have gone through MySQL documentations and some other posts about this issue on Stack Overflow however error code 1215 comes up.

Can someone help me identify my issue/error.

Here are my two existing tables:

---Books

CREATE TABLE Books(

ISBN VARCHAR (13) NOT NULL,

title VARCHAR(60) NOT NULL,

pubDate DATE NOT NULL,

PubID INT NOT NULL,

cost DECIMAL (5,2) NOT NULL,

retail DECIMAL (5,2) NOT NULL,

discount DECIMAL (5,2) NOT NULL,

category ENUM('Fitness', 'Children', 'Computer', 'Cooking', 'Business', 'Literature') NOT NULL,

PRIMARY KEY(ISBN)

---Publishers

CREATE TABLE Publishers(

pubID INT NOT NULL,

name VARCHAR(60) NOT NULL,

contact VARCHAR(20),

phone VARCHAR(20)

);

Here is my Alter Statement

ALTER TABLE Books

ADD CONSTRAINT FK_pubID

FOREIGN KEY (pubID) REFERENCES Publishers(pubID);

I am aware that PubID has a capital P in the Books table and a lower pubID in the Publishers table, however I am still unsure.

Kamalesh M. Talaviya
  • 1,422
  • 1
  • 12
  • 26
  • Possible duplicate of [MySQL Error 1215: Cannot add foreign key constraint](https://stackoverflow.com/questions/16969060/mysql-error-1215-cannot-add-foreign-key-constraint) – Richardissimo May 20 '18 at 06:48
  • I guess you missed this bit 'In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. ' - https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html - add a key to pubid in publishers, – P.Salmon May 20 '18 at 07:33

0 Answers0