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

  • Home
  • SEARCH
  • 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 8156051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:50:19+00:00 2026-06-06T16:50:19+00:00

I have a tasks table with a priority column, which has a unique constraint.

  • 0

I have a “tasks” table with a priority column, which has a unique constraint.

I’m trying to swap the priority value of two rows, but I keep violating the constraint. I saw this statement somewhere in a similar situation, but it wasn’t with MySQL.

UPDATE tasks 
SET priority = 
CASE
    WHEN priority=2 THEN 3 
    WHEN priority=3 THEN 2 
END 

WHERE priority IN (2,3);

This will lead to the error:

Error Code: 1062. Duplicate entry '3' for key 'priority_UNIQUE'

Is it possible to accomplish this in MySQL without using bogus values and multiple queries?

EDIT:

Here’s the table structure:

CREATE TABLE `tasks` (
  `id` int(11) NOT NULL,
  `name` varchar(200) DEFAULT NULL,
  `priority` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `priority_UNIQUE` (`priority`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
  • 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-06T16:50:20+00:00Added an answer on June 6, 2026 at 4:50 pm

    Is it possible to accomplish this in MySQL without using bogus values and multiple queries?

    No. (none that I can think of).

    The problem is how MySQL processes updates. MySQL (in difference with other DBMS that implement UPDATE properly), processes updates in a broken manner. It enforces checking of UNIQUE (and other) constraints after every single row update and not – as it should be doing – after the whole UPDATE statement completes. That’s why you don’t have this issue with (most) other DBMS.

    For some updates (like increasing all or some ids, id=id+1), this can be solved by using – another non-standard feature – an ORDER BY in the update.

    For swapping the values from two rows, that trick can’t help. You’ll have to use NULL or a bogus value (that doesn’t exist but is allowed in your column) and 2 or 3 statements.

    You could also temporarily remove the unique constraint but I don’t think that’s a good idea really.


    So, if the unique column is a signed integer and there are no negative values, you can use 2 statements wrapped up in a transaction:

    START TRANSACTION ;
        UPDATE tasks 
        SET priority = 
          CASE
            WHEN priority = 2 THEN -3 
            WHEN priority = 3 THEN -2 
          END 
        WHERE priority IN (2,3) ;
    
        UPDATE tasks 
        SET priority = - priority
        WHERE priority IN (-2,-3) ;
    COMMIT ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I have a table of tasks this table has a foreign key which
I have a table called TPM_TASKS which contain all tasks, as well as a
We have two tables - Tasks and TasksUsers (users assigned to task). Task has
I have two tables (Tasks and Timeentries), which are connected by a foreign key
I have tasks table with 3 fields: date priority (0,1,2) done (0,1) What I
We have a (currently InnoDB) table which contains roughly 500,000 rows. This represents a
Ok so I have a table Tasks -- TaskId (unique autoinc primary) ChildOf (Contains
I have Tasks table with two navigation properties - VersionReported and VersionResolved, both stored
I have a table called Tasks. This table has the following fields ID TypeID
Suppose I have a table 'Tasks' with a DATETIME column approve_time . I have

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.