Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7804191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:50:00+00:00 2026-06-02T01:50:00+00:00

Given a table like this, where x and y have a unique constraint. id,x,y

  • 0

Given a table like this, where x and y have a unique constraint.

id,x,y
1, 1,1
2, 1,2
3, 2,3
4, 3,5
..

I want to increase the value x and y in a set of rows by a fixed amount with the UPDATE statement. Suppose I’m increasing them both by 1, it seems UPDATE follows id order and gives me an error after updating the row with 1 2, since it collides with the next row, 2 3, which hasn’t been updated to 3, 4 yet.

Googling around, I can’t find a way to force UPDATE to use an order. To do it in reverse would be enough for my application. I am also sure the set is consistent after the whole update.

Any solutions? Some way to force an order into the update, or any way to make it postpone the constraint check until it’s finished?

This is meant for a Django application, and it’s meant to be compatible with all supported databases. I know some databases have atomic transactions and this problem won’t happen, some have features to avoid this problem, but I need a strictly standard SQL solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:50:02+00:00Added an answer on June 2, 2026 at 1:50 am

    For PostgreSQL you can define the primary key constraint as “deferrable” and then it would be only evaluated at commit time.

    In PostgreSQL this would look like this:

    postgres=>create table foo (
    postgres(>    id integer not null,
    postgres(>     x integer,
    postgres(>     y integer
    postgres(>);
    CREATE TABLE
    postgres=>alter table foo add constraint pk_foo primary key (id) deferrable initially deferred;
    ALTER TABLE
    postgres=> insert into foo (id, x,y) values (1,1,1), (2,1,1), (3,1,1);
    INSERT 0 3
    postgres=> commit;
    COMMIT
    postgres=> update foo set id = id + 1;
    UPDATE 3
    postgres=> commit;
    COMMIT
    postgres=> select * from foo;
     id | x | y
    ----+---+---
      2 | 1 | 1
      3 | 1 | 1
      4 | 1 | 1
    (3 rows)
    postgres=>
    

    For Oracle this is not necessary as it will evaluate the UPDATE statement as a single atomic operation, so that works out of the box there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table storing millions of rows. It looks something like this: Table_Docs
I want to have a database table that keeps data with revision history (like
I have an SQL Table named samples, defined like this: sampledate (datetime, 24 records
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
I have a table in a MSSQL database that looks like this: Timestamp (datetime)
I have a table defined like this: CREATE TABLE mytable (id INT NOT NULL
Let's say I have SELECT name FROM table which gives me something like foo
Given a table row, I want to get the HTML out of the span
I am desiging a new table that will potentially have 200K rows. I would
When we can catch an exception like: Violation of UNIQUE KEY constraint 'IX_Product'. Cannot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.