简体   繁体   中英

invalid ALTER TABLE option when trying to add fk

ALTER TABLE OtherCharges
(
    ADD FOREIGN KEY (BookingID) REFERENCES Bookings(BookingID)
);

Above is the code I have and the error I am getting is "invalid ALTER TABLE option" any help would be appreciated.

You might want to have a look at the SQL Reference . To add a foreign key you'd use

ALTER TABLE OTHERCHARGES
  ADD CONSTRAINT OTHERCHARGES_FK1
    FOREIGN KEY (BOOKING_ID) REFERENCES BOOKINGS(BOOKING_ID)
      ON DELETE NO ACTION;

Always a good idea to name your constraints something reasonable but simple. Also, for FK's always specify an ON DELETE action, even if it's NO ACTION - that way it's explicitly stated and easy to understand.

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