tags:

views:

249

answers:

3

Can databases (MySQL in particular, any SQL--MS, Oracle, Postgres--in general) do mass updates, and figure out on their own what the new value should be? Say for example I've got a database with information about a bunch of computers, and all of these computers have drives of various sizes--anywhere from 20 to 250 GB. Then one day we upgrade every single computer by adding a 120 GB hard drive. Is there a way to say something like

update computers set total_disk_space = (whatever that row's current total_disk_space is plus 120)
+2  A: 

Yeah:

update computers set total_disk_space = total_disk_space + 120;
Tom Leys
+10  A: 

For the entire Table then:

Update Computers 
Set Total_Disk_Space = Total_Disk_Space + 120;

If, you only want to update certain ones, then you'd need filters, for example:

Update Computers
Set Total_Disk_Space = Total_Disk_Space + 120
Where PurchaseDate BETWEEN '1/1/2008' AND GETDATE();
Stephen Wrighton
+2  A: 

In your example, if total_disk_space is an INT you can use:

UPDATE computers
SET total_disk_space = total_disk_space + 120;

I you're storing character data, then it will be far more interesting.

Matt Haley