cancel
Showing results for 
Search instead for 
Did you mean: 

Nested HDB Sequences

0 Kudos

Hey guys, I am pretty new to db sql hdb and stuff. So let's say that in my db I have 3 entities. Contract, Item, Bill. Each Contract has an Item and each Item has a Bill. For this situation, as far as I know, it is recommended to use a sequence in order to generate the id and make sure it won't be a duplicate. My problem is if I should really use that or find another way because with sequence I have only 2 options (for the moment): 1. 1 sequence for Contract. For each contract, 1 sequence to count each Contract's Item and for each Item one sequence to count the Item's Bill. 2. Keep three sequences and every time I add a row, let's say I add an Item, I count how many items area already in the database and start the sequence from that number and increment it, which in my opinion cuts out all the charm of sequence (although I am very new to HDB and SQL and everything) Any suggestion is welcome. Thank you!

Accepted Solutions (1)

Accepted Solutions (1)

SergioG_TX
Active Contributor

hi Catalin, the first thing to understand the definition of a sequence.

1) it is for the purpose of keeping unique ordered (unique numbered) list

2) each sequence should be assigned to only one table

in your example, you have 3 tables and therefore there should be 3 sequences. There shouldn't be any collision on your records since each will update independently from each other. Does that make sense? the sequences can all start w the same seed (1) or with whatever default value you assign on its definition AND they will increment based on the increment by property. Hope this makes sense

Answers (0)