Here and here are some similar questions to this but not so same that I can apply them to my solution.
I need to delete a row and renumber rows (higher from deleted row) in document with single query. I get an answer here which is not in single query and for that it is not suitable for simple usage.
Here is my example table with all rows of all documents.
Working document will be 'brkalk' with number 2.
For example we will delete row 'brred' 3.
DROP TABLE IF EXISTS kalksad1;
CREATE TABLE kalksad1(
kalk_id int PRIMARY KEY,
brkalk integer,
brred integer,
description text);
INSERT INTO kalksad1 VALUES
(12, 2, 5, 'text index 12 doc 2 row 5'),
(26, 2, 1, 'text index 26 doc 2 row 1'),
(30, 2, 2, 'text index 30 doc 2 row 2'),
(32, 4, 1, 'text index 32 doc 4 row 1'),
(36, 1, 1, 'text index 36 doc 1 row 1'),
(37, 1, 2, 'text index 37 doc 1 row 2'),
(38, 5, 1, 'text index 38 doc 5 row 1'),
(39, 5, 2, 'text index 39 doc 5 row 2'),
(42, 2, 3, 'text index 42 doc 2 row 3'),
(43, 2, 4, 'text index 43 doc 2 row 4'),
(46, 3, 1, 'text index 46 doc 3 row 1'),
(47, 3, 2, 'text index 47 doc 3 row 2');
Of course I can easily delete a row with SQL:
DELETE FROM kalksad1 WHERE brkalk=2 AND brred=3
But for good functionality of my program it is needed that there will not be "holes" in row numbering inside one document.
After such deleting I have rows in column 'brred' numbered as 1, 2, 4, 5.
I would like to get query which will renumber rows 4 to 3 and 5 to 4 (actually renumber values in column 'brred') immediately after deletion of row 3 and if possible in single SQL command so I can apply it to all my documents and make function for such deleting.
Since more complicated things like swap and renumber was possible I thing that one may be possible too.
Please if someone can develop described query.