CREATE SEQUENCE¶
Creates a new sequence, which can be used for generating sequential, unique numbers.
Important
Snowflake does not guarantee generating sequence numbers without gaps. The generated numbers are not necessarily contiguous.
For more details, see Using Sequences.
This command supports the following variant:
- CREATE OR ALTER SEQUENCE: Creates a sequence if it doesn’t exist or alters an existing sequence.
Syntax¶
Variant syntax¶
CREATE OR ALTER SEQUENCE¶
Creates a new sequence if it doesn’t already exist, or transforms an existing sequence into the sequence defined in the statement. A CREATE OR ALTER SEQUENCE statement follows the syntax rules of a CREATE SEQUENCE statement and has the same limitations as an ALTER SEQUENCE statement.
For more information, see CREATE OR ALTER SEQUENCE usage notes.
Required parameters¶
nameSpecifies the identifier for the sequence; must be unique for the schema in which the sequence is created.
In addition, the identifier must start with an alphabetic character and cannot contain spaces or special characters unless the entire identifier string is enclosed in double quotes (e.g.
"My object"). Identifiers enclosed in double quotes are also case-sensitive.For more details about identifiers, see Identifier requirements.
Optional parameters¶
START [ WITH ] [ = ] initial_valueSpecifies the first value returned by the sequence. Supported values are any value that can be represented by a 64-bit two’s complement integer (from
-2^63to2^63 - 1).Default:
1INCREMENT [ BY ] [ = ] sequence_intervalSpecifies the step interval of the sequence:
- For positive sequence interval
n, the nextn-1values are reserved by each sequence call. - For negative sequence interval
-n, the nextn-1lower values are reserved by each sequence call.
Supported values are any non-zero value that can be represented by a 64-bit two’s complement integer.
Default:
1- For positive sequence interval
{ ORDER | NOORDER }Specifies whether or not the values are generated for the sequence in increasing or decreasing order.
-
ORDER specifies that the values generated for a sequence or auto-incremented column are in increasing order (or, if the interval is a negative value, in decreasing order).
For example, if a sequence or auto-incremented column has
START 1 INCREMENT 2, the generated values might be1,3,5,7,9, etc. -
NOORDER specifies that the values are not guaranteed to be in increasing order.
For example, if a sequence has
START 1 INCREMENT 2, the generated values might be1,3,101,5,103, etc.NOORDER can improve performance when multiple INSERT operations are performed concurrently (for example, when multiple clients are executing multiple INSERT statements).
Default: The NOORDER_SEQUENCE_AS_DEFAULT parameter determines which property is set by default.
-
COMMENT = 'string_literal'Specifies a comment for the sequence.
Default: No value
Access control requirements¶
A role used to execute this operation must have the following privileges at a minimum:
| Privilege | Object | Notes |
|---|---|---|
| CREATE SEQUENCE | Schema | |
| OWNERSHIP | Sequence | Required to execute a CREATE OR ALTER SEQUENCE statement for an existing sequence. OWNERSHIP is a special privilege on an object that is automatically granted to the role that created the object, but can also be transferred using the GRANT OWNERSHIP command to a different role by the owning role (or any role with the MANAGE GRANTS privilege). Note that in a managed access schema, only the schema owner (i.e. the role with the OWNERSHIP privilege on the schema) or a role with the MANAGE GRANTS privilege can grant or revoke privileges on objects in the schema, including future grants. |
Operating on an object in a schema requires at least one privilege on the parent database and at least one privilege on the parent schema.
For instructions on creating a custom role with a specified set of privileges, see Creating custom roles.
For general information about roles and privilege grants for performing SQL actions on securable objects, see Overview of Access Control.
Usage notes¶
-
The first/initial value for a sequence cannot be changed after the sequence is created.
-
A sequence does not necessarily produce a gap-free sequence. Values increase (until the limit is reached) and are unique, but are not necessarily contiguous. For more information, including the upper and lower limits, see Sequence Semantics.
-
Regarding metadata:
Attention
Customers should ensure that no personal data (other than for a User object), sensitive data, export-controlled data, or other regulated data is entered as metadata when using the Snowflake service. For more information, see Metadata fields in Snowflake.
- The OR REPLACE and IF NOT EXISTS clauses are mutually exclusive. They can’t both be used in the same statement.
-
CREATE OR REPLACE <object> statements are atomic. That is, when an object is replaced, the old object is deleted and the new object is created in a single transaction.
CREATE OR ALTER SEQUENCE usage notes¶
- All limitations of the ALTER SEQUENCE command apply.
- The initial value (
START) of a sequence can’t be changed after the sequence is created. - Setting or unsetting a tag is not supported; however, existing tags are not altered by a CREATE OR ALTER SEQUENCE statement and remain unchanged.
Examples¶
Here is a simple example of using sequences:
Run the same query again; note how the sequence numbers change:
Now use the sequence while inserting into a table:
Create a sequence that increments by 5 rather than by 1:
Run the same query again; note how the sequence numbers change. You might expect that the next set of sequence numbers would start 5 higher than the previous statement left off. However, the next sequence number starts 20 higher (5 * 4, where 5 is the size of the increment and 4 is the number of
NEXTVALoperations in the statement):
This example demonstrates that you can use a sequence as a default value for a column to provide unique identifiers for each row in a table:
This query shows that each row in the table has a distinct value:
More examples are available in Using Sequences.