How do you swap unique indexed column values in database?
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;
cola colb
------------
1 b
2 a
cola colb
------------
1 a
2 b
Source: By Ramesh Soni as answer to the question
This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 4.0
Related code-snippets:
- Binary data in MySQL.
- What is a change to SQL Server tables?
- Flat file databases are generally stored in a computer hard drive. Flat file databases cannot be shared by any party with another.
- Decoding T-SQL CAST in C#/VB.NET.
- How do I upgrade my SQL Server database version?
- How do you iterate over a result set of data?
- If a.net Embedded Database runs on a network. Embedded Database can run off a network.
- The ability to edit a database by multiple users is essential.
- What is the best way to use a database in c#?
- How is database indexing used?
- How do I index database columns?
- If I had been caught in a SQL Injection, what would it have cost?
- What is the best way to handle multiple permission types?
- Convert HashBytes to VarChart?
- How do I divide a string so I can access item x?