PDA

View Full Version : Change a set of values in a column


jmassey09
11-06-2009, 07:49 PM
I have a column in a table that has the values 1 2 3 4 and 5. I need to change all entries with the value 5 in that column to value 6. what is the script to do this? I am working in PHPMyAdmin. Thanks!

Old Pedant
11-06-2009, 08:38 PM
UPDATE tableName
SET columnName = 6
WHERE columnName = 5

bazz
11-07-2009, 02:43 AM
curiosity...

Are those values PK's anywhere? if so, and you are using MySQL 5.0 or higher you could use a constraint on the FKs. If your app allows it to be set to 'on update cascade' then you would only need to change the PK directly in the table where it is a PK.

I don't know enough about your db and Old Pedant's answer should work on this basis:

if (constraints not set) { OK;
} else {
value of 6 must be in the parent table or you'll get a error #150 because an FK constraint fails.
}


I hope that makes sense.

bazz

Old Pedant
11-07-2009, 08:27 PM
Don't see how they could be PK's, Bazz.

He said "I need to change all entries with the value 5 in that column to value 6..."

By definition, a Primary Key has to be unique, so he could only have one with the value 5. Granted, it's possible he has a Composite Primary Key, but if he doesn't know how to change a column's value, I rate it as unlikely that he knows how to create a Composite Key.