Questions tagged [sql-update]

An SQL UPDATE statement is used to change existing rows in a table.

An SQL UPDATE statement is used to change existing rows in a table.

The basic syntax is:

UPDATE table 
SET 
    Column1 = 'x',
    Column2 = 'y'
WHERE id=1

Tagging Recommendation

This tag should be used for general SQL programming language questions, in addition to tags for specific products. For example, questions about Microsoft SQL Server should use the tag, while questions regarding MySQL should use the tag. Tagging by product (including version, e.g , ) is the easiest way to know what functionality is available for the task at hand.

References

12009 questions
17
votes
3 answers

Mysql temporarily suppress unique index

I have a table with unique index on two columns, id_parent and sort_order to be precise +----+-----------+------------+-------------+-------------+-------------+ | id | id_parent | sort_order | some_data | other_data | more_data …
Nameless
  • 2,306
  • 4
  • 23
  • 28
17
votes
4 answers

SQL Updatable View with joined tables

I have a view that looks similar to this, SELECT dbo.Staff.StaffId, dbo.Staff.StaffName, dbo.StaffPreferences.filter_type FROM dbo.Staff LEFT OUTER JOIN dbo.StaffPreferences ON dbo.Staff.StaffId = dbo.StaffPreferences.StaffId I'm trying…
Red Taz
  • 4,159
  • 4
  • 38
  • 60
17
votes
3 answers

MySQL conditionally UPDATE rows' boolean column values based on a whitelist of ids

I'm trying to update a set of records (boolean fields) in a single query if possible. The input is coming from paginated radio controls, so a given POST will have the page's worth of IDs with a true or false value. I was trying to go this…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
17
votes
4 answers

Oracle: Updating a table column using ROWNUM in conjunction with ORDER BY clause

I want to populate a table column with a running integer number, so I'm thinking of using ROWNUM. However, I need to populate it based on the order of other columns, something like ORDER BY column1, column2. That is, unfortunately, not possible…
Lukman
  • 18,462
  • 6
  • 56
  • 66
17
votes
3 answers

In Django, how can I prevent a "Save with update_fields did not affect any rows." error?

I'm using Django and Python 3.7. I have this code article = get_article(id) ... article.label = label article.save(update_fields=["label"]) Sometimes I get the following error on my "save" line ... raise DatabaseError("Save with update_fields…
Dave
  • 15,639
  • 133
  • 442
  • 830
17
votes
1 answer

Is there a non-commercial alternative to Z.EntityFramework.Extensions?

Entity Framework can be very slow on mass insert/update/delete operations. Even the often suggested tweaks to turn off AutoDetectChanges and/or ValidateOnSaveEnabled does not always help. I have come across the Z.EntityFramework.Extensions on NuGet,…
Michael
  • 938
  • 1
  • 10
  • 34
17
votes
3 answers

MySQL: Update all rows in a table matching results of another query

I've written a query returning rows associating Customers and Salespeoeple. Note that the query joins several database tables. And note that not all customers have a salesperson. c_id c_name s_id s_name 24 microsoft 1 mike 27 …
Tommy O'Dell
  • 7,019
  • 13
  • 56
  • 69
17
votes
1 answer

Using return value from DELETE for UPDATE in Postgres

I need to update a table using a value deleted from another table. The situation is a comment vote scorekeeper similar to the one on SO. I'm using python to work the postgres, but that shouldn't make a difference. query=""" UPDATE comment SET…
JDong
  • 2,304
  • 3
  • 24
  • 42
17
votes
6 answers

UPDATE with ORDER BY

Need to "tie" UPDATE with ORDER BY. I'm trying to use cursors, but get the error: cursor "cursupd" doesn't specify a line, SQL state: 24000 Code: BEGIN; DECLARE cursUpd CURSOR FOR SELECT * FROM "table" WHERE "field" = 5760 AND "sequence" >= 0…
dedoki
  • 709
  • 4
  • 14
  • 24
17
votes
3 answers

Update multiple rows in a table from another table when condition exists

I have two tables. Table1 contains companies whose locations are georeferenced with lat/lng coordinates in a column called the_geom Table2 also contain the same companies from Table1, not georeferenced, along with hundreds of other companies…
John
  • 802
  • 2
  • 9
  • 19
16
votes
4 answers

modify the property value of JSON string stored in the Table Column

I have JSON string stored in my database column. I have to update that value in JSON string. Here Is my table. I want to update the state value inside it. Example: Name1 has State value KA so I want to update it to GJ. What I have Tried So…
always-a-learner
  • 3,671
  • 10
  • 41
  • 81
16
votes
4 answers

Redshift table update with join

I have 3 table t1, t2 and t3. t1 has 2 column-> id1, val1 t2 -> id2, val2 t3 -> id3, val3 If id1=id2 and id2 = id3 then I need to update val1 ad val3. But I have repeating id1 and each should have same val3 I am using update t1 inner…
js_248
  • 2,032
  • 4
  • 27
  • 38
16
votes
4 answers

Recovery after wrong MySQL update query?

I made a wrong update query in my table. I forgot to make an id field in the WHERE clause. So that updated all my rows. How to recover that? I didn't have a backup....
assaqqaf
  • 1,575
  • 4
  • 21
  • 38
16
votes
4 answers

CodeIgniter query: How to move a column value to another column in the same row and save the current time in the original column?

In my db table, I have two datetime columns: Last and Current. These column allow me to keep track of when someone last used a valid login to the service I am building up. Using CodeIgniter's active record, is it possible to update a row so that the…
chris
  • 36,115
  • 52
  • 143
  • 252
16
votes
1 answer

sqlalchemy bulk update performance problems

I need to increment values in a column periodically with data I receive in a file. The table has > 400000 rows. So far, all my attempts result in very poor performance. I have written an experiment that reflects my requirements: #create…
devboell
  • 1,180
  • 2
  • 16
  • 34