'Update multipe records of table with composite primary key from select
My table has the following structure:
- 4 columns (columnA, columnB, columnC, columnD)
- With the 3 columns (columnA, columnB, columnC) as the primary key.
How do I update multiple rows other than using a for-loop?
I'm looking for something like:
UPDATE table
SET columnC = 'something'
WHERE (columnA,columnB,columnC) IN (
SELECT columnA,columnB,columnC
FROM table
WHERE columnD = 'somethingD'
);
Notice I'm trying to update a column that is part of primary key.**
Solution 1:[1]
UPDATE <tablename>
SET columnc = 'something'
WHERE columnd = 'somethingD';
This query should work if you have same value in columnD for multiple rows of the table, and yes you can update columns which are part of composite primary key until your updated value does not violate the unique constraint of the composite primary key.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RF1991 |
