Индекс страницы книги SQL с автоматической перезаписью - PullRequest
0 голосов
/ 20 ноября 2018

Я хочу достичь чего-то вроде Страниц книги .

Например: у меня есть книга с 100 страницами, каждая из страниц имеет уникальный приоритет / номер страницы, (1-100).

+-----+--------------------------------+------+
| id  |            content             | page |
+-----+--------------------------------+------+
| 1   | cover                          | 1    |
| 2   | prelude                        | 2    |
| ... | ...                            | ...  |
| 50  | and so he do rig a jig jig...  | 50   |
| 51  | and he come to the bus stop... | 51   |
| ... | ...                            | ...  |
| 100 | back-cover                     | 100  |
+-----+--------------------------------+------+

Давайте представим, что я вставляю новую страницу, но между страницей 50 и 51.Таким образом, приоритет вставленной страницы / номер страницы равен 51.

И поэтому, перезаписав верхний номер страницы, «СТАРАЯ» страница 51-100 будет 52-101.

+-----+--------------------------------+------+
| id  |            content             | page |
+-----+--------------------------------+------+
| 1   | cover                          | 1    |
| 2   | prelude                        | 2    |
| ... | ...                            | ...  |
| 50  | and so he do rig a jig jig...  | 50   |
| 101 | this is a new page             | 51   |
| 51  | and he come to the bus stop... | 52   |
| ... | ...                            | ...  |
| 100 | back-cover                     | 101  |
+-----+--------------------------------+------+

Я использую PostgreSQL, TypeORM, TypeScript.

1 Ответ

0 голосов
/ 20 ноября 2018
--Updates the table and sets each page value to itself + 1
--Under conditions: the page number is the same as the inserted record but the
--content is different (the original record with that page number)
--Or the page number is greater than the page number of the inserted record    
UPDATE Table
SET [page] = [page] + 1
WHERE ([page] >= insertedPageNumber AND content <> insertedPageContent) OR [page] > insertedPageNumber 
...