Mike
Mike

Reputation: 2900

Can Delete/Insert sometimes be better than Update for database records?

I have a webpage where people create an order for pipe.

The orders are stored in the following tables:

When you go to edit one of these, all of this information is put on a single page for editing.

When the user clicks Finish it seems to me that the only way handle this is to Delete all records for the old order and reinsert the new stuff. Something about deleting and reinserting seems like it would be bad practice to me so I would like to know if I am missing something.

Upvotes: 1

Views: 217

Answers (2)

timmz
timmz

Reputation: 2252

I faced the same problem in my website, my solution was getting the ID of the table when i populate the fields in a hidden input or in the session variable, so when something change, i only update my DB by giving the stored ID. hope it helps.

Upvotes: 1

Zzz
Zzz

Reputation: 3025

You should use UPDATE:

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

Be careful when updating records. If we had omitted the WHERE clause (or made a mistake in it), you could update all the records in the table instead of the ones you wanted to update.

One reason you might want to delete and insert a new record is to give that record a new primary key (if it is auto-incrementing). You should be careful about doing this, as it would mess up any foreign keys in other tables that use this primary key

Upvotes: 1

Related Questions