Code Mysql INSERT ON DUPLICATE KEY UPDATE (Read 394 times)

  • None of them knew they were robots.
  • PipPipPipPipPipPipPipPipPip
  • Group: Premium Member
  • Joined: Nov 5, 2006
  • Posts: 3242
Pretend I have a table like id, name, address


And I want to insert something into it, then now the table is like


1, john, john's address


Is there an way to, when I try to add another john to that table with a different address, instead making the table like


1, john, john's address
2, john, john's new address


it becomes like


1, john, john's new address  ? Is that what insert on duplicate key update is for?
Play Raimond Ex (if you haven't already)


I'll not TAKE ANYTHING you write like this seriously because it looks dumb
  • Avatar of dada
  • VILLAIN
  • PipPipPipPipPipPipPipPip
  • Group: Administrator
  • Joined: Dec 27, 2002
  • Posts: 5531
You need to do something like:
insert into `table` (
   name,
   address
) values (
   'john',
   'john\'s new address'
) on duplicate key update `name` = values(`name`), `address` = values(`address`);

Didn't test this though (and also it's MySQL specific)


edit: of course best way to do this is to name `name` the primary and ditch the ID.
  • None of them knew they were robots.
  • PipPipPipPipPipPipPipPipPip
  • Group: Premium Member
  • Joined: Nov 5, 2006
  • Posts: 3242
Thanks dada, I love you
Play Raimond Ex (if you haven't already)


I'll not TAKE ANYTHING you write like this seriously because it looks dumb
  • None of them knew they were robots.
  • PipPipPipPipPipPipPipPipPip
  • Group: Premium Member
  • Joined: Nov 5, 2006
  • Posts: 3242
But like, what if there is an ID I cannot ditch?
Play Raimond Ex (if you haven't already)


I'll not TAKE ANYTHING you write like this seriously because it looks dumb
  • Avatar of dada
  • VILLAIN
  • PipPipPipPipPipPipPipPip
  • Group: Administrator
  • Joined: Dec 27, 2002
  • Posts: 5531
Er, I'm not sure but what if you make name a unique key and then just not pass the ID in the insert query?
  • Avatar of Pulits
  • I'm a hairy, slutty and drunk Mexican!
  • PipPipPipPip
  • Group: Premium Member
  • Joined: Jun 16, 2002
  • Posts: 438
Normalization. Separate your tables into:

Employees (idE, name)
Addresses (idA, idE, address)

That way you'll only need to update the table addresses, the importance relies into having employers' id as a foreign key.
"I think EVERYONE here on GW has to have cranked one out over Pulits or Trujin before. How's it feel, guys?" - Christophomicus <--Feels great, btw.
  • Avatar of dada
  • VILLAIN
  • PipPipPipPipPipPipPipPip
  • Group: Administrator
  • Joined: Dec 27, 2002
  • Posts: 5531
Yeah that's a good idea, but since he apparently can't get rid of the ID I guess he's working with some system that others rely on and can't be modified very easily. He should do it if possible though.
  • Avatar of Pulits
  • I'm a hairy, slutty and drunk Mexican!
  • PipPipPipPip
  • Group: Premium Member
  • Joined: Jun 16, 2002
  • Posts: 438
If that's the case:

UPDATE employees
SET adress = newadress
WHERE id = '1'

There could be more elaborated answers with TRIGGERS and STORED PROCEDURES, I suppose it depends on the complexity of your dB. I'd go with SP, since it can receive parameters ('id', in this case).
"I think EVERYONE here on GW has to have cranked one out over Pulits or Trujin before. How's it feel, guys?" - Christophomicus <--Feels great, btw.
  • None of them knew they were robots.
  • PipPipPipPipPipPipPipPipPip
  • Group: Premium Member
  • Joined: Nov 5, 2006
  • Posts: 3242
Thanks, I used insert into blah blah blah ignore passing the ID every time I update a record and it quite works
Play Raimond Ex (if you haven't already)


I'll not TAKE ANYTHING you write like this seriously because it looks dumb