Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools Database Foundations Joining Relational Data Between Tables in SQL Keys and Auto-Incrementing Values

Having trouble adding a constraint

ALTER TABLE movies add column genre_id INTEGER NULL ADD CONSTRAINT FOREIGN KEY (genre_id)REFERENCES genre(id);

This does not work. I get a response "Error Code: 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 'ADD CONSTRAINT FOREIGN KEY (genre_id)REFERENCES genre(id)' at line 1"

1 Answer

Add comma before adding a constraint. Right syntax will be:

ALTER TABLE movies  ADD COLUMN genre_id integer NULL, ADD CONSTRAINT FOREIGN KEY (genre_id) references genres(id)