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

Assigning ID's to Multiple Values

Hello All!

I was wondering is there a way to mass update tables made in a database so that you assign one instance of a particular value with an ID. Specifically, is there a way to do this using SQL.

Lets say I had a database with two tables and each table has a name column. Am I able to update both tables' name column where if a specific name pops up i can give it the same ID value in both tables. (I know I could join the tables based on name but i'd rather join on an int rather than a string value).

Any info would be greatly appreciated. Thank you!

1 Answer

The ID is always generated automatically in a production environment. I'm assuming here the ID is the table's primary key. It needs to be unique or everything breaks. What happens if the same name exists twice in the table? Then you end up with two identical primary keys in your table and a corrupted database. That's why you have ID columns as primary key at all. Trying to overwrite the ID column is a very, very bad idea.

But technically you can just update the ID column like any other column using an update statement.