Rectangle 27 0

mysql How to add a foreign key to an existing table?


ALTER TABLE ITEM
ADD CATEGORY_NAME VARCHAR(35) NOT NULL;

ALTER TABLE ITEM
ADD CONSTRAINT FK_CATEGORY_NAME
FOREIGN KEY (CATEGORY_NAME)
REFERENCES CATEGORY (CATEGORY_NAME);
ALTER TABLE ITEM
ADD CONSTRAINT FK_CATEGORY_NAME
FOREIGN KEY (SOME_OTHER_EXISTING_COLUMN)
REFERENCES CATEGORY (CATEGORY_NAME);

You need to add the CATEGORY_NAME column on ITEM TABLE, or map the foreign key to another existing column in ITEM.

Note