SQL update on duplicate key update

二次信任 提交于 2019-12-24 13:15:25

问题


I have asked this question Grouping and update large database table, but I didn't get the answer for this.

I have a table: name, date, detail, no, and name, date, detail are together as PK.

Somehow I need to update detail, and it is possible that there are duplicate key. Thus I need to sum the no for the duplicate rows. ON DUPLICATE KEY UPDATE is only used for INSERT. So how to address this problem?


回答1:


First things first, that multi-column primary key is probably a bad idea; as you've found out, it makes it difficult to manipulate the individual fields. What you ought to do is add an autoincrement bigint column to that table, which will become your new primary key, and your three-column uniqueness constraint can be a unique index instead. It should perform better... but it'll also allow you to do the sort of manipulation you need, as well. It'll let you perform modifications but still let you identify the original rows by their integer index.

If you do that, your "one-time update" can now be done safely, as long as you don't mind creating some temporary tables to work with. Something like this:

Create a couple of temporary tables with the same schema, but without the unique three-column index - you can have a non-unique index, because it'll help the queries you're about to perform;

Copy the records you need to process into the first table (including the unique integer primary key);

Update all the detail columns you need to update in the temporary table;

Use INSERT ... SELECT with SUM and GROUP BY to merge those records into the second table;

INSERT INTO temp2 (...whatever...) SELECT ...whatever..., SUM(no) FROM temp1 GROUP BY ...whatever...

Finally, delete all the records in the temp1 table from the original table (using the integer primary key), and insert the records in the temp2 table into the original table.




回答2:


Bad design. you should use surrogate id primary key and make these fields a composite unique index. If you want to reference this a table later what will you have? 3 extra fields as a foreign key in another table and an extra big index. How will you update the the detail field? If you said it before it's a big table it means that PK index rebuild. Disable the constraint if possible and if its not referenced already. Make select distinct or group by from your source table and make your update using this select.


REPLACE EDIT:

  REPLACE INTO table(name,date,detail)
  select distinct name,date,(select distinict detail from table) from 
  table


来源:https://stackoverflow.com/questions/9072053/sql-update-on-duplicate-key-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!