BruceyBandit
BruceyBandit

Reputation: 4334

I get an MYSQL error #1064 when add foreign key constraint

I keep getting this sql error

"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Option (OptionId)' at line 1"

when I try and add a foreign key to the OptionId field from the Question Table to the OptionId(pk) field in the Option field. I don't get wy I keep getting the error because I don't see what is wrong with it.

Below is the foreign key constraint using ALTER TABLE:

ALTER TABLE Question ADD CONSTRAINT FK_OptionId FOREIGN KEY (OptionId) REFERENCES Option (OptionId)

Table names and syntax are correct, I made sure by double checking.

Why is it not working?

Upvotes: 1

Views: 4372

Answers (1)

Mark Byers
Mark Byers

Reputation: 839234

option is a reserved word in MySQL and must be surrounded by backticks.

ALTER TABLE Question
    ADD CONSTRAINT FK_OptionId FOREIGN KEY (OptionId)
    REFERENCES `Option` (OptionId)

Upvotes: 4

Related Questions