-1

Let us say that we have an SQL database with a contacts table that has 5 fields; contactID, title, firstName, middleName, and lastName. On the front end we have a profile page with an update button.

Let us also say that we want to allow the user to update any given field without having to also enter data in the other fields (a last name change, for example.). Is there a 'simple' way to allow this?

My solution is to add a bit field to each property and add a series of condition statements to generate the UPDATE statement based on the bit field.

It is ugly and seems inefficient. Any advice?

1 Answers1

3

The best way is to keep data retrieved from database in all fields and update all the fields. If user changes the value, it will be updated to new value. Otherwise it will stay the same old value.

Suppose that you retrieved all the values from database and on click of edit button, they are editable(textboxes) with old value already present in them. Then write a query like

update contacts set title =@title, firstName=@firstName, middleName=@middleName, lastName=@lastName where contactID=@ContactID
Sailesh Babu Doppalapudi
  • 1,534
  • 1
  • 10
  • 22
  • So you are saying when the user goes to edit a single field in their profile the system will update the entire record [all fields] with data from the profile page? – DaedalusK71 Aug 24 '15 at 23:41
  • Yes. If he changes something, then data will be updated. Otherwise, updated data will be same as old data. – Sailesh Babu Doppalapudi Aug 24 '15 at 23:43
  • That seems inefficient to me. Why update fields that have not changed? Won't that use additional, and unnecessary, processing? – DaedalusK71 Aug 25 '15 at 00:06
  • But keeping a bit flag for all fields and writing if condition several times is a lot more unnecessary processing than updating. Just think of 100 fields. Can you place a bit for all the 100 fields and can you write 100 if statements to check that bit values? – Sailesh Babu Doppalapudi Aug 25 '15 at 00:08
  • I see what you mean. That makes sense. Thanks! – DaedalusK71 Aug 25 '15 at 00:15