tags:

views:

80

answers:

2

Why I am not able to update the column based on a condition which is not the primary key.

I am trying to update the constituencies table where name matches a specific criterial as shown below but the below queries shows an error


Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table constituencies set city_id = '1' where constituencies.name = "East Delhi"' at line 1


update table constituencies set city_id = '1' where constituencies.name = "East Delhi";
update table constituencies set city_id = '1' where constituencies.name = "South Delhi";
update table constituencies set city_id = '1' where constituencies.name = "Delhi Sadar";
update table constituencies set city_id = '1' where constituencies.name = "Karol Bagh";
update table constituencies set city_id = '1' where constituencies.name = "New Delhi";
update table constituencies set city_id = '1' where constituencies.name = "Outer Delhi";
update table constituencies set city_id = '1' where constituencies.name = "North East Delhi";
update table constituencies set city_id = '1' where constituencies.name = "North West Delhi";
update table constituencies set city_id = '1' where constituencies.name = "West Delhi";

Is it necessary that the condition should be checked with a primary key only ?

Please throw some light on the above.

+10  A: 

Try

update constituencies set city_id = 1 where constituencies.name = "East Delhi";

You Don't need to write Table in your mysql Query.

Salil
:-) yeah this worked flawlessly... Thanks Salil.
Gaurav Sharma
+3  A: 

You don't need table keyword, as Salil posted. Also, take a look at function IN for your update condition - it could simplify your queries (as you're updating rows with same ID anyways).

Try:

UPDATE constituencies set city_id = '1' 
WHERE name IN 
(
  "East Delhi", "South Delhi", "Delhi Sadar", "Karol Bagh", "New Delhi", 
  "Outer Delhi", "North East Delhi", "North West Delhi", "West Delhi"
);
jimmy_keen
+1 for the IN comment, maybe you could rewrite it a bit
Unreason