SQL updating one column

fallenapples

In Runtime
Messages
386
Location
Toronto, Ontario
I am trying to update the one names column in my SQL table. I want to change Robert to John. When I do it says doesn't not recognize column 'Robert' I tried reversing it and the message is the same. Do I need to use two columns?


Code:
UPDATE contacts SET name=John WHERE name=Robert
 
Are you wrapping the values in quotes?

Edit: Also, you should really be searching on an ID rather than a text string (slower).

Less chance of conflicts / modifying data you didn't want modified as well.

How is your Contacts table structured?
 
Last edited:
It is structured as followed:
name
phone_mobile
phone_work
email
relation_id

follows: So I should be using the column names and relation_id? And the values aren't in quotes.

I used UPDATE contacts SET name='John' WHERE name='Robert';

It worked. I just have to remember the syntax is imperative.

So when updating in this case the new value comes after SET and the old value comes after WHERE, correct?
 
Last edited:
So when updating in this case the new value comes after SET and the old value comes after WHERE, correct?

Yes.

What is your 'relation_id' column?

I'd structure it as "contact_id" being a unique/primary key, and your WHERE clauses would reference that when needing to make a change.

So, for example, assuming you know the ID of the contact record you want to update:
UPDATE contacts SET name='Jim' WHERE contact_id=4
 
For a very small table, the statement
UPDATE contacts SET name='John' WHERE name='Robert';
works fine but what if there are many contacts with the name of John and you only want to change one of them to Robert?
That's why carnagex is suggesting you have some kind of unique key to id each record.
 
For a very small table, the statement
UPDATE contacts SET name='John' WHERE name='Robert';
works fine but what if there are many contacts with the name of John and you only want to change one of them to Robert?
That's why carnagex is suggesting you have some kind of unique key to id each record.

Yeah, that :p. I suppose I didn't explain it very well.
 
Back
Top Bottom