---
title: "CREATE, ALTER and DROP SERIAL"
description: "Generate unique sequence numbers with CoraDB serials: CREATE SERIAL options, NEXT_VALUE, CURRENT_VALUE, ALTER and DROP SERIAL."
url: "https://www.coradb.ai/manual/sql/schema/serial_stmt.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/schema/serial_stmt.md"
product: "CoraDB"
version: "1.0"
keywords: "create serial, alter serial, drop serial, sequence, next_value, current_value, nextval, currval, serial_next_value, serial_current_value, db_serial, coradb sequence"
---

# CREATE, ALTER and DROP SERIAL

A serial (a *sequence* in other databases) generates unique numbers, such as invoice or ticket numbers. Unlike an [AUTO_INCREMENT](https://www.coradb.ai/manual/sql/schema/table_stmt.html#auto-increment) column, a serial is an object of its own: it is not tied to a table and can feed several tables. Numbers are handed out safely to many sessions at once. Do not expect a serial to be free of gaps: cached numbers, for example, are lost when the server stops.

- Any user, including `PUBLIC`, can create a serial. Once it exists, every user can read its numbers.
- Only its owner and `DBA` can change or drop a serial. A serial owned by `PUBLIC` can be changed by everyone.

> **Try it yourself:**
> The examples run on the [e-commerce sample database](https://www.coradb.ai/manual/sample-database.html). Start corasql with `corasql --no-auto-commit -u dba e-commerce` and finish with `ROLLBACK;` to remove the serials again.


## CREATE SERIAL

```text
CREATE SERIAL serial_name
    [START WITH initial_value]
    [INCREMENT BY interval]
    [MINVALUE min | NOMINVALUE]
    [MAXVALUE max | NOMAXVALUE]
    [CYCLE | NOCYCLE]
    [CACHE cached_count | NOCACHE]
    [COMMENT 'comment']
```

- *serial_name*: see [Identifiers](https://www.coradb.ai/manual/sql/identifier.html).
- **START WITH**: the first number. Default: `MINVALUE` for an ascending serial, `MAXVALUE` for a descending one.
- **INCREMENT BY**: the step, any integer except 0 (default 1). A negative step makes a descending serial. Its absolute value must be smaller than `MAXVALUE - MINVALUE`.
- **MINVALUE** / **NOMINVALUE**: the lowest number. `NOMINVALUE` (the default) means 1 for an ascending serial and -10<sup>36</sup> for a descending one.
- **MAXVALUE** / **NOMAXVALUE**: the highest number. `NOMAXVALUE` (the default) means 10<sup>37</sup> for an ascending serial and -1 for a descending one.
- **CYCLE**: after the last number, start again from `MINVALUE` (ascending) or `MAXVALUE` (descending). With **NOCYCLE** (the default), asking for a number past the limit fails with `Next value exceeds MAX(MIN) value.`
- **CACHE** *n*: keep the next *n* numbers in memory, which is faster under heavy use. Numbers still in the cache when the server stops are lost, leaving a gap. **NOCACHE** (the default) writes every number to disk.
- **COMMENT**: a description of the serial.

All values can have up to 38 digits.

```sql
CREATE SERIAL invoice_seq
    START WITH 10000 INCREMENT BY 2 MAXVALUE 20000 CACHE 3
    COMMENT 'invoice numbers';

SELECT name, current_val, increment_val, max_val, min_val, cyclic, started, cached_num, comment
FROM db_serial WHERE name = 'invoice_seq';
```

| name | current_val | increment_val | max_val | min_val | cyclic | started | cached_num | comment |
|---|---:|---:|---:|---:|---:|---:|---:|---|
| invoice_seq | 10000 | 2 | 20000 | 10000 | 0 | 0 | 3 | invoice numbers |

The `db_serial` catalog view lists all serials. It is read-only; use `ALTER SERIAL` to change a serial.

## Using a serial

```text
serial_name.NEXT_VALUE      -- or serial_name.NEXTVAL
serial_name.CURRENT_VALUE   -- or serial_name.CURRVAL
```

- **NEXT_VALUE** advances the serial and returns the new number. The first call after `CREATE SERIAL` returns the start value.
- **CURRENT_VALUE** returns the current number without advancing.

```sql
CREATE TABLE invoice (invoice_no INT PRIMARY KEY, order_id INT NOT NULL);
INSERT INTO invoice VALUES (invoice_seq.NEXT_VALUE, 455);
INSERT INTO invoice VALUES (invoice_seq.NEXT_VALUE, 456);
INSERT INTO invoice VALUES (invoice_seq.NEXTVAL, 457);

SELECT * FROM invoice ORDER BY invoice_no;
```

| invoice_no | order_id |
|---:|---:|
| 10000 | 455 |
| 10002 | 456 |
| 10004 | 457 |

```sql
SELECT invoice_seq.CURRENT_VALUE;   -- 10004
```

Each reference to `NEXT_VALUE` advances the serial, even within one statement: `SELECT s.NEXT_VALUE, s.NEXT_VALUE` returns two different numbers.

### CYCLE

A countdown from 3 to 1 that starts over:

```sql
CREATE SERIAL countdown START WITH 3 INCREMENT BY -1 MINVALUE 1 MAXVALUE 3 CYCLE;
-- successive countdown.NEXT_VALUE calls return 3, 2, 1, 3, 2, ...
```


### SERIAL_CURRENT_VALUE and SERIAL_NEXT_VALUE

```text
SERIAL_CURRENT_VALUE(serial_name)
SERIAL_NEXT_VALUE(serial_name, count)
```

Both return `NUMERIC(38,0)`.

- `SERIAL_CURRENT_VALUE(s)` is the same as `s.CURRENT_VALUE`.
- `SERIAL_NEXT_VALUE(s, count)` reserves *count* numbers in one call and returns the **last** of them. `SERIAL_NEXT_VALUE(s, 1)` is the same as `s.NEXT_VALUE`.

Reserving a block of numbers at once is much faster than calling `NEXT_VALUE` repeatedly. The first reserved number is *last* − (*count* − 1) × *increment*:

```sql
CREATE SERIAL batch_seq START WITH 101;

SELECT SERIAL_CURRENT_VALUE(batch_seq);     -- 101
SELECT SERIAL_NEXT_VALUE(batch_seq, 10);    -- 110: numbers 101 to 110 are yours
SELECT SERIAL_NEXT_VALUE(batch_seq, 10);    -- 120: numbers 111 to 120
```

## ALTER SERIAL

```text
ALTER SERIAL serial_name
    [INCREMENT BY interval]
    [START WITH initial_value]
    [MINVALUE min | NOMINVALUE]
    [MAXVALUE max | NOMAXVALUE]
    [CYCLE | NOCYCLE]
    [CACHE cached_count | NOCACHE]
    [COMMENT 'comment']
```

The options have the same meaning as in `CREATE SERIAL`. After `START WITH`, the next `NEXT_VALUE` returns the new start value:

```sql
ALTER SERIAL batch_seq START WITH 500 INCREMENT BY 5;
SELECT batch_seq.NEXT_VALUE;                -- 500

ALTER SERIAL batch_seq CACHE 20;
ALTER SERIAL batch_seq NOCACHE COMMENT 'batches';
```

## DROP SERIAL

```text
DROP SERIAL [IF EXISTS] serial_name
```

```sql
DROP SERIAL IF EXISTS batch_seq;
```

## See also

- [AUTO_INCREMENT](https://www.coradb.ai/manual/sql/schema/table_stmt.html#auto-increment)
- [System catalog](https://www.coradb.ai/manual/sql/catalog.html): the `db_serial` view
