I have carried out a lot of searching but unable to locate an answer to my query. What I am asking may not be possible however:
I have a series of tables but to simplify let us assume that I have the following
Article articleId SERIAL
ArticlePageNumber articleId /references Article.articleId/ pageNumber Int
This is fairly self explanatory however data thatwould be acceptable would be
Article
-------
articleiId
1
2
ArticlePageNumber
-----------------
articleiId pageNumber
1 1
1 2
1 3
1 4
1 5
1 6
2 1
2 3
2 5
2 2
2 4
An unacceptable table would be ArticlePageNumber
ArticlePageNumber
-----------------
articleiId pageNumber
1 1
1 1
1 2
1 3
1 4
1 5
2 1
2 3
2 5
2 4
I would like the default value for ArticlePageNumber.pageNumber to increment similar to the way that serial does however the next value would be dependant on the previous value (or 1 if none) for entries with that particular articleId.
I would prefer to do this as constraints and checks within SQL rather that code however I am guessing it might not be possible.