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

MySQL query for adding primary key

In the Question : Alter the "t_movies" table to add an auto incrementing primary key called "pk_id" first.

My response is : alter table t_movies add pk_id first add primary key auto_increment;

other alter statement which I could think of are : alter table t_movies add primary key pk_id auto_increment;

None seem to work. Please help

3 Answers

I think that the only thing missing here is the INT data type for your pk_id.

Try: ALTER TABLE t_movies ADD pk_id INT PRIMARY KEY AUTO_INCREMENT FIRST;

I did not tested it, so I'm not sure if it is working.

Thank you Tomasz : Your solution worked, as it is I am facing problems in framing SQL.

Is it mandatory that "FIRST" has to be put at the end of the statement.

I mean will this statement be wrong :

alter table t_movies add pk_id int primary key first auto_increment;

Santiago Barrionuevo
Santiago Barrionuevo
14,176 Points

This works:

ALTER TABLE t_movies ADD COLUMN pk_id INT PRIMARY KEY AUTO_INCREMENT FIRST;

Thanks Tomasz!