View Full Version : Record Update/Edit

02-16-2007, 08:20 PM
I have a number of tables that are linked together. If a record record is created in the "main" table, atleast one record or more is created in the other tables.

My users have requested a edit function and I was wondering which of the below methods would be best.

1. Delete the main record and all record relating to it in all the other tables. Then creating new records for the tables with the new data.


2. Check if the the data has changed, and if it has either create a new record if one does not already exist or update if it already exists. Or if data did exist and no longer does delete the record.

For simplicity sake the first one was the option I would go with but I am not sure if it would create more headaches in the long run.

02-16-2007, 09:12 PM
Assuming all of your primary and foreign keys are "meaningless" fields, meaning they are not fields that the user attaches any meaning to and cannot change, then I'd stick with updating existing rows.

For example, if you used something like a last name for the primary key, then a user making a change to a last name is a royal headache to update and it's easier to just delete and insert again. You'll still have to code for duplicate key issues.

02-16-2007, 09:20 PM
I used auto inc fields for all my tables. All other fields have a chance for duplicate values.

I will do the update method then. Thanks for the input.

02-17-2007, 12:19 AM
if you use INNODB tables you can look into using ON CASCADE DELETE and thus deletion from your primary table will also remove those records from your secondary tables.