Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Update values in primary key

I faced a problem during my work in ASP.NET.

I have three tables in SQL Server:

  1. userTbl
  2. Hobbies
  3. HobbiesForUser

I have in the web browser two pages:

  1. insert (create a new user with new hobbies)
  2. update (update an existing user) - At the login page the user enters a password and username and then log in, and all the user information is uploaded from the SQL server, then the user is change or not change is personal information and click the update button.

The HobbiesForUser table containing two fields and both together are the primary key's.

Example for rows in SQL Server:

row 1:  id = 1 hob_id = 3                                                                                                                
row 2:  id = 1 hob_id = 5
row 3:  id = 2 hob_id = 3
row 4:  id = 2 hob_id = 4
row 5:  id = 2 hob_id = 1

My question is that if the user update is hobbies and now he has just two hobbies, how can I manage it in the SQL Server? how can I update the table?

I thought at first that I need first to delete the rows and then to insert again the new values, but I faced in a problem because it is not so easy to delete a primary key's that have a constraint.

Thanks.

Expected results: if I have an existing user and is id = 1 and is hob_id are 2,3,4.

Now the user decides to update is information, and now is hob_id = 2,6.

My goal is after he clicks the update button in the SQL Server the HobbiesForUser will be like that:

id = 1
hob_id = 2,6

Comments