Create a DB2 sequence

A DB2 sequence is a stored object that generates numeric values in ascending or descending order. It is ideal for coordinating unique keys across tables, applications, and insert paths without relying on a single table-specific identity definition.

Sequence creation
Progress0 of 0 lessons

Prerequisites and design

Confirm CREATEIN on the schema, USAGE/ALTER governance, data type, start value, increment, lower and upper bounds, cycle policy, and cache sizing. A sequence provides unique generated values, not necessarily gap-free values. Rollbacks, concurrent use, and cached values can leave gaps; never use it as a legal invoice-number promise unless business rules explicitly accept that behavior.

Choose BIGINT when growth could exceed INTEGER. Use a positive increment for ascending keys or negative for descending. NO CYCLE is safest for identifiers: it fails at the boundary instead of reusing values. CYCLE is appropriate only when reused values cannot collide with retained data.

Create and use a sequence

CREATE SEQUENCE defines the stored generator. NEXT VALUE FOR obtains a new value; PREVIOUS VALUE FOR returns the last value generated in the current application process where valid. A sequence reference can appear in many expression locations, including an INSERT values list.

sql
1
2
3
4
5
6
7
8
9
10
11
CREATE SEQUENCE SALES.ORDER_SEQ AS BIGINT START WITH 100000 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE CACHE 100; INSERT INTO SALES.ORDERS (ORDER_ID, CUSTOMER_ID) VALUES (NEXT VALUE FOR SALES.ORDER_SEQ, ?);

Cache, ordering, and concurrency

CACHE preallocates values for performance and reduces contention, but an outage or member change can make unused cached values disappear from the visible progression. NO CACHE minimizes this behavior but can cost throughput. Sequence values are generated for coordination, not timestamps or commit order.

Do not infer insert order or business event order from sequence numbers in data sharing or concurrent workloads. If strict event ordering matters, record a timestamp and transaction/business state explicitly. Evaluate cache size with expected rate, restart behavior, and the tolerance for gaps.

Verify and common errors

Verify with VALUES NEXT VALUE FOR schema.sequence, then insert through the real application path. Check catalogs, grants, dependencies, and boundary behavior in test. Grant USAGE or the appropriate privilege only to roles that should allocate values; broad allocation can make diagnostics difficult.

Common errors are missing schema authority, duplicate names, using a type or start value outside allowed bounds, reaching MAXVALUE under NO CYCLE, and using PREVIOUS VALUE FOR before NEXT VALUE FOR in the process. Altering a live sequence needs release coordination because applications may assume the old range or increment.

Explain It Like I'm Five

A sequence is a ticket dispenser. Every time an app asks, Db2 gives the next numbered ticket. The dispenser may keep a small stack ready in its pocket for speed, so a restart can make a few ticket numbers disappear. That is normal; the goal is unique tickets, not perfectly consecutive ones.

Exercises

  1. Create a BIGINT NO CYCLE sequence for an order key.
  2. Explain why rollback does not promise to return a sequence number.
  3. Choose a cache size for 10,000 inserts per hour and state the gap trade-off.
  4. Test MAXVALUE behavior in a small test sequence.
  5. Compare a sequence with an identity column.

Quiz

Test Your Knowledge

1. Does a sequence guarantee gap-free values?

  • No
  • Always
  • Only with CACHE
  • Only in SPUFI

2. What returns a new value?

  • NEXT VALUE FOR
  • PREVIOUS VALUE FOR
  • ALTER TABLE
  • DISPLAY DB

3. What does NO CYCLE do at the maximum?

  • Raises an error
  • Starts at one
  • Drops the sequence
  • Commits all work

Frequently Asked Questions