70

I have a database table and one of the fields (not the primary key) is having a unique index on it. Now I want to swap values under this column for two rows. How could this be done? Two hacks I know are:

  1. Delete both rows and re-insert them.
  2. Update rows with some other value and swap and then update to actual value.

But I don't want to go for these as they do not seem to be the appropriate solution to the problem. Could anyone help me out?

Cartucho
  • 3,257
  • 2
  • 30
  • 55
Ramesh Soni
  • 15,867
  • 28
  • 93
  • 113

12 Answers12

39

The magic word is DEFERRABLE here:

DROP TABLE ztable CASCADE;
CREATE TABLE ztable
    ( id integer NOT NULL PRIMARY KEY
    , payload varchar
    );
INSERT INTO ztable(id,payload) VALUES (1,'one' ), (2,'two' ), (3,'three' );
SELECT * FROM ztable;


    -- This works, because there is no constraint
UPDATE ztable t1
SET payload=t2.payload
FROM ztable t2
WHERE t1.id IN (2,3)
AND t2.id IN (2,3)
AND t1.id <> t2.id
    ;
SELECT * FROM ztable;

ALTER TABLE ztable ADD CONSTRAINT OMG_WTF UNIQUE (payload)
    DEFERRABLE INITIALLY DEFERRED
    ;

    -- This should also work, because the constraint 
    -- is deferred until "commit time"
UPDATE ztable t1
SET payload=t2.payload
FROM ztable t2
WHERE t1.id IN (2,3)
AND t2.id IN (2,3)
AND t1.id <> t2.id
    ;
SELECT * FROM ztable;

RESULT:

DROP TABLE
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "ztable_pkey" for table "ztable"
CREATE TABLE
INSERT 0 3
 id | payload
----+---------
  1 | one
  2 | two
  3 | three
(3 rows)

UPDATE 2
 id | payload
----+---------
  1 | one
  2 | three
  3 | two
(3 rows)

NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index "omg_wtf" for table "ztable"
ALTER TABLE
UPDATE 2
 id | payload
----+---------
  1 | one
  2 | two
  3 | three
(3 rows)
wildplasser
  • 43,142
  • 8
  • 66
  • 109
  • @MarcoDemaio I don't know. I'm afraid not: since mysql doesn't allow updates from self-joins, I'd suppose it does dirty-reads. But you could try ... – wildplasser Dec 27 '12 at 18:41
  • 1
    This is not supported in MS SQL Server. – PhillipM Mar 25 '19 at 13:15
  • Maybe Microsoft does not support it. (sql-server is not one of the tags) Since you paid for it, you maybe could ask for it? – wildplasser Mar 25 '19 at 23:00
  • Mind that if you actually depend on these exceptions during your transaction, for example before you decide to publish something to another system within the transaction, you can't use this. – Sebastiaan van den Broek Nov 09 '21 at 08:29
17

I think you should go for solution 2. There is no 'swap' function in any SQL variant I know of.

If you need to do this regularly, I suggest solution 1, depending on how other parts of the software are using this data. You can have locking issues if you're not careful.

But in short: there is no other solution than the ones you provided.

Daan
  • 9,984
  • 2
  • 30
  • 36
6

There is another approach that works with SQL Server: use a temp table join to it in your UPDATE statement.

The problem is caused by having two rows with the same value at the same time, but if you update both rows at once (to their new, unique values), there is no constraint violation.

Pseudo-code:

-- setup initial data values:
insert into data_table(id, name) values(1, 'A')
insert into data_table(id, name) values(2, 'B')

-- create temp table that matches live table
select top 0 * into #tmp_data_table from data_table

-- insert records to be swapped
insert into #tmp_data_table(id, name) values(1, 'B')
insert into #tmp_data_table(id, name) values(2, 'A')

-- update both rows at once! No index violations!
update data_table set name = #tmp_data_table.name
from data_table join #tmp_data_table on (data_table.id = #tmp_data_table.id)

Thanks to Rich H for this technique. - Mark

Mark Gaulin
  • 156
  • 2
  • 2
  • 1
    Might be a bit old this one but I was trying to do a 're-ordering' page for my silverlight app as the client wanted to sort their reports by a specific order - I added a sort column but since it's a unique key I was having trouble updating it. I ended up using a table variable but the principle is the same (I don't like temp tables much to be honest!). Thanks for the idea :) – Charleh Jun 29 '12 at 00:18
6

Further to Andy Irving's answer

this worked for me (on SQL Server 2005) in a similar situation where I have a composite key and I need to swap a field which is part of the unique constraint.

key: pID, LNUM rec1: 10, 0 rec2: 10, 1 rec3: 10, 2

and I need to swap LNUM so that the result is

key: pID, LNUM rec1: 10, 1 rec2: 10, 2 rec3: 10, 0

the SQL needed:

UPDATE    DOCDATA    
SET       LNUM = CASE LNUM
              WHEN 0 THEN 1
              WHEN 1 THEN 2 
              WHEN 2 THEN 0 
          END
WHERE     (pID = 10) 
  AND     (LNUM IN (0, 1, 2))
5

Assuming you know the PK of the two rows you want to update... This works in SQL Server, can't speak for other products. SQL is (supposed to be) atomic at the statement level:

CREATE TABLE testing
(
    cola int NOT NULL,
    colb CHAR(1) NOT NULL
);

CREATE UNIQUE INDEX UIX_testing_a ON testing(colb);

INSERT INTO testing VALUES (1, 'b');
INSERT INTO testing VALUES (2, 'a');

SELECT * FROM testing;

UPDATE testing
SET colb = CASE cola WHEN 1 THEN 'a'
                WHEN 2 THEN 'b'
                END
WHERE cola IN (1,2);

SELECT * FROM testing;

so you will go from:

cola    colb
------------
1       b
2       a

to:

cola    colb
------------
1       a
2       b
takrl
  • 6,356
  • 3
  • 60
  • 69
Andy Irving
  • 2,657
  • 1
  • 14
  • 11
3

I have the same problem. Here's my proposed approach in PostgreSQL. In my case, my unique index is a sequence value, defining an explicit user-order on my rows. The user will shuffle rows around in a web-app, then submit the changes.

I'm planning to add a "before" trigger. In that trigger, whenever my unique index value is updated, I will look to see if any other row already holds my new value. If so, I will give them my old value, and effectively steal the value off them.

I'm hoping that PostgreSQL will allow me to do this shuffle in the before trigger.

I'll post back and let you know my mileage.

the.jxc
  • 3,373
  • 21
  • 21
3

I usually think of a value that absolutely no index in my table could have. Usually - for unique column values - it's really easy. For example, for values of column 'position' (information about the order of several elements) it's 0.

Then you can copy value A to a variable, update it with value B and then set value B from your variable. Two queries, I know no better solution though.

Lis
  • 555
  • 4
  • 26
3

I also think that #2 is the best bet, though I would be sure to wrap it in a transaction in case something goes wrong mid-update.

An alternative (since you asked) to updating the Unique Index values with different values would be to update all of the other values in the rows to that of the other row. Doing this means that you could leave the Unique Index values alone, and in the end, you end up with the data that you want. Be careful though, in case some other table references this table in a Foreign Key relationship, that all of the relationships in the DB remain intact.

Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
2

In SQL Server, the MERGE statement can update rows that would normally break a UNIQUE KEY/INDEX. (Just tested this because I was curious.)

However, you'd have to use a temp table/variable to supply MERGE w/ the necessary rows.

Ben Mosher
  • 13,251
  • 7
  • 69
  • 80
  • MERGE has other .. issues .. and the same can be done with a DELETE/UPDATE/INSERT cycle; if the UPDATE is done as a single statement (see UPDATE..JOIN) then it will not violate any PK/AK/UX constraints as the constraints only apply to the end result of the statement. So can MERGE be used *in place of* UPDATE..JOIN to solve this? Sure.. because that's about what it is internally. – user2864740 Nov 23 '19 at 02:02
2

For Oracle there is an option, DEFERRED, but you have to add it to your constraint.

SET CONSTRAINT emp_no_fk_par DEFERRED; 

To defer ALL constraints that are deferrable during the entire session, you can use the ALTER SESSION SET constraints=DEFERRED statement.

Source

TheBakker
  • 2,852
  • 2
  • 28
  • 49
1

Oracle has deferred integrity checking which solves exactly this, but it is not available in either SQL Server or MySQL.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
1

1) switch the ids for name

id    student 

1     Abbot   
2     Doris  
3     Emerson 
4     Green  
5     Jeames  

For the sample input, the output is:

id student

1     Doris   
2     Abbot   
3     Green   
4     Emerson 
5     Jeames  

"in case n number of rows how will manage......"