I new to DB/Hibernate and found code:
@SequenceGenerator(name = "entSeq", allocationSize = 5, sequenceName = "CODE_SEQ") ... @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "entSeq")
which set sequence for primary key.
Why was sequences used for values of primary key? Which goals was addressed:
- increase performance
- add constraints, some checks
- limit possible value range of integer values of ID, why to do so??
- why to start counting from 1?
I read about syntax and usage in:
- http://msdn.microsoft.com/en-us/library/ff878091.aspx
- http://www.techonthenet.com/oracle/sequences.php
but doesn't found answer for my question.
UPDATE:
I enjoyed reading:
- http://www.oracle.com/technetwork/products/rdb/0307-sequences-130053.pdf Guide to Using SQL: Sequence Number Generator
where shown that there is problem in DB theory how to get unique ID for primary keys. That mean that I can make insert into table without providing value for primary key from my own:
INSERT INTO suppliers (supplier_id, supplier_name) VALUES (supplier_seq.nextval, 'Kraft Foods');
But I expect that this feature must be present in all DB without forcing me to supply primary key values...
Do I think right?
UPDATE2:
Answer for why use START WITH:
This clause can be useful when adding sequences to existing databases. When an older scheme was in use by the application and has already consumed some values from the legal range this clause can be used to skip those consumed values. MINVALUE and MAXVALUE are used to specify the legal range but START WITH would initiate the sequence usage within that range so that previously generated values would not reappear.
UPDATE3: *sequences* provide http://en.wikipedia.org/wiki/Surrogate_key