tags:

views:

28

answers:

2

Not sure how to properly go about doing this.

I have my users table with about 500,000 already existing rows.

I'm creating a playlist feature for my music community, so I have a table called playlists. I'd like to give each of the 500,00 users a default playlist called Favorites.

What is the best way to do this?

Should I call the users table and loop through the data? Upon each loop, I insert the USERID into the playlists table? Is there a more efficient process?

+1  A: 

You should just be able to run the query

UPDATE table_reference
SET column = 'Favorites'

There's no WHERE clause so it will apply it to everything.

Edit

If you plan on having the default value be 'Favorites' you can set the default value for the field so that you don't run into this problem again.

Aaron Hathaway
A: 

I'm assuming you allow users to have multiple playlists, and that users can not share a common playlist (i.e. not a many to many, so only one user can have the same PlayList)

This means that you'll need to duplicate the Favourites PlayList record for each user, something like:

INSERT into PlayLists(UserId, PlayListName)
SELECT u.ID, 'Favorites' FROM Users u

You probably also have a many to many table PlayListSong or similar which links songs to the playlist

This would also mean that you need to copy all the links as well - if you can show a bit more about your table design, SO members can help you there as well.

nonnb