简体   繁体   中英

Alter Table With Foreign Key

I have a table named ITEM with fields ID, TYPE, DES, SIZE now i want to add a foreign key from another table AISLE which has field of AISLE_ID, X, Y, WIDTH, HEIGHT. Now i want the AISLE_ID to be the foreign key in the ITEM table. I used this statement but says the AISLE_ID is not recognized. This is the statement i used

ALTER table ITEM 
ADD FOREIGN KEY AISLE_ID(AISLE_ID)
REFERENCES AISLE(AISLE_ID)
ON DELETE NO ACTION
ON UPDATE CASCADE; 

Any Help, Thanks in advance.

Change your ALTER statement to be like below, since there is only matching column in ITEM per AISLE_ID definition type in AISLE ; consider making the ID column in ITEM table to be the foreign key

ALTER TABLE ITEM ADD CONSTRAINT fk_AISLE_ID 
FOREIGN KEY (ID) references AISLE(AISLE_ID)
ON DELETE NO ACTION
ON UPDATE CASCADE;

You can as well ALTER the ITEM table and add a column named AISLE_ID and then create a foreign key on it like

ALTER TABLE ITEM ADD  AISLE_ID INT NOT NULL;

ALTER TABLE ITEM ADD CONSTRAINT fk_AISLE_ID 
FOREIGN KEY (AISLE_ID) references AISLE(AISLE_ID)
ON DELETE NO ACTION
ON UPDATE CASCADE;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM