Link to home
Start Free TrialLog in
Avatar of Isaiah Melendez
Isaiah Melendez

asked on

Update Records in MySQL from Program Logic

Hello, Experts,

I have a MySQL question. I am a DB admin for all things SQL server and recently started doing full-stack development and using MySQL more.

I have a very basic table that contains three columns (to make the example simple):

------------------------------
| id | ro_number | sales| date |
------------------------------
1 | 2355 | 2000 | 2020-02-15
2 | 2366 | 3500 | 2020-02-16
...

id is int and current PK, set to Not Null, ro_number is also int allow for nulls, sales is dec(13,2).

I am developing a solution that will pull data from an api in a json response, I parse through the data, and prep for loading data in (new records).

What I am after is in the event I need to update historical data, specifically for a date or given dates and I want to update the data if it exists. Should I just do a delete statement for the given date and then pull down the data and insert in again with the new values? or should I do a conditional check?

What say you?

I appreciate your help and excuse my noviceness with all things MySQL.

-Isaiah
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Agree with Slightwv, you can specify the insert and on duplicate key update specifying which columns you want updated.
note the duplicate key works so long as you have defined unique indexies, or columns that have to be unique.,

In the example about it is unclear what the basis for "unique" or which parameter should be unique. besides ID. which commonly would be autoincrement...

is ro_number the unique identifier in the example.