Skill 1.1.5
Design and implement SEQUENCES
Key Concepts
CREATE SEQUENCE sets the data type, START WITH, INCREMENT BY, MINVALUE, MAXVALUE, CYCLE and CACHE. Values are obtained with NEXT VALUE FOR, which can appear in an INSERT, in a column DEFAULT or in a variable assignment, so an application can know the number before it inserts the row. sp_sequence_get_range reserves a block of values in one call, and one sequence can number rows in several tables, such as one invoice series for several invoice tables.
An IDENTITY column belongs to one table and generates its value during the insert. Both IDENTITY and sequences can leave gaps: values used by rolled-back transactions are not reused, and cached values are lost if the database restarts unexpectedly. Caching improves performance by reducing writes to metadata; NO CACHE minimizes gaps at the cost of speed. A sequence can be restarted with ALTER SEQUENCE ... RESTART WITH.