CREATE SEQUENCE

statementsupportedTables, views, streams, sequences

docs.snowflake.com ↗

Object: 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. - DEFINE SEQUENCE: Declarative definition of a sequence’s target state inside a DCM project.

Syntax

supportedpartialnot supportednot scored

CREATE [ OR REPLACE ] SEQUENCE [ IF NOT EXISTS ] <name>
  [ WITH ]
  [ START [ WITH ] [ = ] <initial_value> ]
  [ INCREMENT [ BY ] [ = ] <sequence_interval> ]
  [ { ORDER | NOORDER } ]
  [ COMMENT = '<string_literal>' ]

For more information, see CREATE OR ALTER SEQUENCE usage notes and CREATE OR ALTER <object>.

CREATE OR ALTER SEQUENCE <name>
  [ WITH ]
  [ START [ WITH ] [ = ] <initial_value> ]
  [ INCREMENT [ BY ] [ = ] <sequence_interval> ]
  [ { ORDER | NOORDER } ]
  [ COMMENT = '<string_literal>' ]

Parameters

NameRequiredDescription
namerequiredSpecifies 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 (for example, "My object"). Identifiers enclosed in double quotes are also case-sensitive. For more details about identifiers, see Identifier requirements.
START [ WITH ] [ = ] initial_valueoptionalSpecifies 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^63 to 2^63 - 1). Default: 1
INCREMENT [ BY ] [ = ] sequence_intervaloptionalSpecifies the step interval of the sequence: For positive sequence interval n, the next n-1 values are reserved by each sequence call. For negative sequence interval -n, the next n-1 lower 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
{ ORDER | NOORDER }optionalSpecifies 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 be 1, 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 be 1, 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'optionalSpecifies a comment for the sequence. Default: No value

Usage notes

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.

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.

See also

Referenced by