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

Databases

Rasbin Rijal
PLUS
Rasbin Rijal
Courses Plus Student 10,864 Points

Keeping Constraints and Referencing Task3

No idea how to reference these 2 items in the question.

Please provide some idea. Edited post for link : https://teamtreehouse.com/library/database-foundations/joining-relational-data-between-tables-in-sql/keys-and-autoincrementing-values-2

Rasbin Rijal
Rasbin Rijal
Courses Plus Student 10,864 Points

Sorry, I tried again with new post but my code and link is not attached. It only shows category as Database.

I did copy paste below : Challenge Task 3 of 3

Alter the "t_movies" table to add a foreign key column called "fk_genre_id" and constrain it to reference the "t_genres" "pk_id". Type in your command below, then press Enter.

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

Rasbin Rijal
Rasbin Rijal
Courses Plus Student 10,864 Points

Here is the link. Sorry code attach thing did not work.

https://teamtreehouse.com/library/database-foundations/joining-relational-data-between-tables-in-sql/keys-and-autoincrementing-values-2

You can pass Task 1 with this code : CREATE TABLE t_genres (pk_id INT(6) AUTO_INCREMENT PRIMARY KEY, uk_name VARCHAR(45) NOT NULL UNIQUE KEY);

You can pass Task 2 with this code : ALTER TABLE t_movies ADD COLUMN pk_id INTEGER(5) AUTO_INCREMENT PRIMARY KEY FIRST;

Now, I am stuck in Task 3.

1 Answer

Steven Parker
Steven Parker
231,261 Points

:point_right: The challenge gives you a hint to look around 9:45 in the previous video. There you will see a good example of a very similar situation. Give that a try first.

But if that doesn't do it for you....




:warning: SPOILER ALERT

(Don't look until you give it a try!)




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

I had the same problem. I now know I missed the first set of brackets but I thought we had to write "ADD COLUMN" and I see you only wrote "ADD". Was I wrong in adding "COLUMN"?