Best way to update rows in table - mysql

I have an interface where the user adds, edit or delete products, once I save the changes I go to the products table, delete everything by ID and add the products that the user just selected, is this a bad way? how could I better thank you

Yes! it's a bad way cause every time you perform two tasks instead of one. like if you want to update product you can update it directly using UPDATE query rather than delete and then insert
What will be the benefit of this UPDATE
Query will be easier
you can get result in single query
if you have id it will be the same rather than changing every time cause you insert new record every time
Ex: think that you are making an e-commerce website a user visits your website and added the product in his/her cart but doesn't check out at that time. it saved it for later meanwhile you change the details of your product so you removed old id and inserted the new one.
now what's happens when user check his/her cart it will not show the product or some sort of error will generated as you have created your e-commerce platform so using update is far more better then delete-insert process for editing the product
Syntax:
UPDATE TABLE_NAME SET COLUMNS_NAME=NEW_VALUE [where clause]
Ex:
UPDATE PRODUCT SET product_name='HELLO WORLD' where id=1

Related

MYSQL SUM and UPDATE between tables [duplicate]

I need to know about one thing in databases.
I have product table with it's total inventory as seen in image
If any one have any idea that how can i do that??
Please Share it
Thanks
What you're asking about isn't technically a "relationship" in the technical sense when referring to relational databases. Such relations are things like having a table that refers to the "key" of another table, for instance relating a table with a customer's address to another table with the customer's order. Anyway, that's beyond the scope of what you asked about so to answer your question, you can do that in the application code or a trigger.
Triggers are features of the database that does something when an INSERT, UPDATE, or DELETE happens.
Something like this should work okay with minor adjustments for table/column names:
UPDATE table2 SET inventory = inventory - NEW.qty where id = NEW.id_product;
Now that only covers an INSERT; you'll want to create another trigger for Event UPDATE and probably somehow handle returned inventory or cancelled orders as well, but that's probably something you'll handle at the application level rather than in a trigger.

How To Implement A Recent History Table

I have an app where you can select a person from a list to view more details. Now each time a person is selected to view more details, I want to record that in a recent history table. I'm not sure the best way to approach this to have say the latest 10 person selections.
I know I need to create the history table but am wondering if I should just do an insert for each person click and select only 10 results with the most recent dates or if I should worry about just updating only 10 records to keep the row count low?
Any input would be appreciated.
I would update, otherwise you keep adding more and more data which you are not going to use. Maybe you won't run into problems with this specific case (because people won't select tens of thousands of persons a day), but in general you should be careful with just adding data without cleaning it up.
That said, the first step is the same anyway:
Adding or updating the person
So, if someone selects 'Bob', I would try to update 'Bob' in the history and set his lastselecteddate to now. If that fails, then insert 'Bob', again with the current timestamp.
Cleaning up the older history
After that you can decide whether or not to clean up old history. You could delete all rows but the newest ten, or you can keep a hundred, or not clean them at all. Also, instead of cleaning them up right away, you could make a job that does this once every day.
If you clean up right away, you can decide to not do that when you updated a person. After all, if you didn't insert one, you shouldn't have to clean-up one.

Insert row into table when checkbox is checked

I have a MS Access db to handle our product pricing. One of my tables has all of our products in it with a yes/no, "checkbox", field field to identify if we bulk that product. I have a separate table that handles our bulk product pricing. Currently when I want to set up a bulk product I add it to our products table, check off the bulked so it removes it from the non-bulk price query, then add the product# to the bulk products table.
I'm pretty new to VBA but what I want is when the "Bulked" field is checked it takes the product code for the current row and insert it into the product code field in Bulked Products. Is that possible?
Yes, it is possible, you can run an update query in VBA, but I suggest that you do not do it. It will involve you checking whether the record already exists in the bulk table, if it does exists, if it matches, and rewinding if someone clicks by mistake. I do not believe you need two tables, you just need a field to indicate that the product is bulk.

Inserting data in MYSQL is placed after a recently deleted record

I have designed a page using PHP, JavaScript and MYSQL, where it holds id(Auto incremented) images, names and so on. every thing works fine, i can insert, update or delete the records with click on respective buttons. what i have noticed that once a record deleted and when i try to insert a new record, it gets placed exactly after recently deleted record. for example. if i have 18 records, i delete record # 14 and insert a new record which obviously will be record # 19, it will be place after deleted record which was #14. is there anyway to force the insert to place the new record at the end of the table (after last record #19)? i don't want to get into phpMyAdmin and use Alter table order by...
when using select i have no problem as i'm using ORDER BY.. so that it displays every thing as i want.
Appreciating all your help.
Fardin
By definition, SQL tables have no natural order. Relying on the natural order that some MySQL table managers provide is going to lead to very fragile code. Please consider sticking with the SQL standard and ordering your rows on retrieval.
You cannot control how MySQL controls the recycling of deleted-record space.
Here is a comment from MySQL that says, essentially, the same thing: http://forums.mysql.com/read.php?108,41767,41836.

MySQL table design

I have a table with products. Each product has a title and a price.
The products come in huge XML files, on a daily basis.
I store all of them in MySQL. But sometimes they have a wrong title. But i can't edit it, because they will be lost the next day (cronjob removes all products and inserts again).
What would be the best way to edit them? Save them in a different table and SELECT both tables at once? Whereas the table that contains the edited rows has precedence over the cronjob table.
What would be the best way to handle it, since there are 300.000+ products. Products might be (manually) edited via a CMS system.
Thanks!
Is there some sort of ID that remains constant? (productID) for example?
Can you edit the cronjob?
If both of the above is true; i'd edit the job to only add new records into the table; preventing writing over your updated values.
If there is a unique identifier for each product that remains constant over updates, you could make a table containing the product ID and the corrected title. Correcting a title would involve inserting a row into this table as well as updating the main table.
As the last step of the cron job, you can then update your main table of products from this one.
UPDATE FROM tblProduct p, tblProductCorrections pc
SET p.strTitle = pc.strCorrectedTitle
WHERE p.intId = pc.intProductId