Swap unique indexed column values in database

前端 未结 12 891
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 06:36

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

相关标签:
12条回答
  • 2020-12-03 06:47

    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

    0 讨论(0)
  • 2020-12-03 06:50

    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.

    0 讨论(0)
  • 2020-12-03 06:52

    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......"

    0 讨论(0)
  • 2020-12-03 06:53

    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.

    0 讨论(0)
  • 2020-12-03 06:56

    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.

    0 讨论(0)
  • 2020-12-03 06:56

    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
    
    0 讨论(0)
提交回复
热议问题