Is this a valid approach to updating
I had a long form which includes multiple combo boxes and textboxes.
I am using this form for registration, also I'm using same kind of form for updating.
- I finished up the registration process using
INSERT
query. - For updates, I had one idea. Instead of using
UPDATE
query, should I remove (DELETE
) the record first and then inser开发者_高级运维t it again into the database, because since I already done the insertion process at first for registration, I can reuse the code.
Is there anything wrong with this approach?
There are scenarios (especially in bulk data movement jobs etc) where doing something like this might make sense.
But in your scenario, i really dont see any reason why you should do what you are suggesting.
There is no benefit that i can see from your approach other than saving you only a negligible amount of development time. On the cons, you application will execute 2 SQL statements (DELETE + INSERT) instead of 1 UPDATE everytime your user data gets updated. Depending on how your application works, you will also additionally need some other logic to keep track of changes to users profiles as things like dtCreated etc will not suffice.
I would just go with a simple UPDATE statement instead.
You can use REPLACE INTO
command which uses the same syntax. Behind the screen it will perform a DELETE
then an INSERT
. If you use InnoDB
tables and you have set up relations this can be very dangerous if you come to think that the REPLACE INTO
will work like UPDATE
. Result, bunch of deleted rows due to cascading rules.
You consider using DELETE
followed by INSERT
for updates because you have the insert-sql ready. Well you could use the UPDATE
like syntax for inserts. That way it is just a matter of using INSERT/UPDATE according to your task.
So instead of:
INSERT INTO products (name) VALUES ('iPhone');
.. you would use:
INSERT INTO products SET name='iPhone';
Which looks much the same as the update:
UPDATE products SET name='iPhone' WHERE id=1;
Almost the same.
精彩评论