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

Josh Jacobsen
Josh Jacobsen
2,445 Points

Trouble w/ Challenge Task in "Keys & Auto Incrementing Values" within "Joining Relational Data Between Tables in SQL"

Hi,

I'm having trouble writing the correct SQL for the following challenge task:

"Alter the "t_movies" table to add a foreign key called "fk_genre_id" and constrain it to reference the "t_genres" "pk_id"."

I've tried multiple SQL answers (below) and referenced both MySQL/WC3 documentation, can't seem to get it right:

1) ALTER TABLE t_movies ADD FOREIGN KEY fk_genre_id INTEGER, ADD CONSTRAINT FOREIGN KEY t_genres REFERENCES t_genres(pk_id) 2) ALTER TABLE t_movies ADD FOREIGN KEY fk_genre_id INTEGER NULL, ADD CONSTRAINT FOREIGN KEY t_genres REFERENCES t_genres(pk_id) 3) ALTER TABLE t_movies ADD COLUMN fk_genre_id INTEGER, ADD CONSTRAINT FOREIGN KEY t_genres REFERENCES t_genres(pk_id) 4) ALTER TABLE t_movies ADD COLUMN fk_genre_id INTEGER Null, ADD CONSTRAINT FOREIGN KEY t_genres REFERENCES t_genres(pk_id)

Error messages (matched w/ each of the above) are as follows: 1) -You're missing the 'fk_genre_id' column. -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 'INTEGER, ADD CONSTRAINT FOREIGN KEY t_genres REFERENCES t_genres(pk_id)' at line 1

2) -You're missing the 'fk_genre_id' column. -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 'INTEGER NULL, ADD CONSTRAINT FOREIGN KEY t_genres REFERENCES t_genres(pk_id)' at line

3) -You're missing the 'fk_genre_id' column. -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 'REFERENCES t_genres(pk_id)' at line 1

4) -You're missing the 'fk_genre_id' column. -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 'REFERENCES t_genres(pk_id)' at line 1

Assuming the answer's pretty simple but I'm still novice in my SQL abilities (outside of completing all Database Foundation work prior to this section).

Appreciate the help, thanks!

-Josh

1 Answer

Hi Josh, I also struggled with this challenge. The correct answer that i found via this answer in the forum is:

ALTER TABLE t_movies ADD COLUMN fk_genre_id INTEGER,
ADD CONSTRAINT FOREIGN KEY (fk_genre_id) REFERENCES t_genres(pk_id)

In my case I was missing the parenthesis around "fk_genre_id". It seems your answer (4) is closest to the correct answer and just needs to be updated after "FOREIGN KEY" and before "REFERENCES"!

Cheers, naano