Help Center > > Developer Guide> SQL Reference> SQL Syntax> ALTER SEQUENCE

ALTER SEQUENCE

Updated at: Jul 15, 2020 GMT+08:00

Function

ALTER SEQUENCE modifies the parameters of an existing sequence.

Precautions

  • You must be the owner of the sequence to use ALTER SEQUENCE.
  • In the current version, you can modify only the owner, home column, and the maximum value. To modify other parameters, delete the sequence and create it again. Then, use the Setval function to restore original parameter values.
  • ALTER SEQUENCE MAXVALUE cannot be used in transactions, functions, and stored procedures.
  • After the maximum value of a sequence is changed, the cache of the sequence in all sessions is cleared.
  • ALTER SEQUENCE blocks the invocation of nextval, setval, currval, and lastval.

Syntax

Change the maximum value or home column of the sequence.

1
2
3
ALTER SEQUENCE [ IF EXISTS ] name 
    [ MAXVALUE maxvalue | NO MAXVALUE | NOMAXVALUE ]
    [ OWNED BY { table_name.column_name | NONE } ] ;

Change the owner of a sequence.

1
ALTER SEQUENCE [ IF EXISTS ] name OWNER TO new_owner;

Parameter Description

  • name

    Specifies the sequence name to be changed.

  • IF EXISTS

    Sends a notification instead of an error when you are modifying a non-existing sequence.

  • MAXVALUE maxvalue | NO MAXVALUE

    Maximum value of a sequence. If NO MAXVALUE is declared, the default value of the ascending sequence is 263-1, and that of the descending sequence is -1. NOMAXVALUE is equivalent to NO MAXVALUE.

  • OWNED BY

    Associates a sequence with a specified column included in a table. In this way, the sequence will be deleted when you delete its associated field or the table where the field belongs.

    If the sequence has been associated with another table before you use this parameter, the new association will overwrite the old one.

    The associated table and sequence must be owned by the same user and in the same schema.

    If OWNED BY NONE is used, existing associations will be deleted.

  • new_owner

    Specifies the user name of the new owner. To change the owner, you must also be a direct or indirect member of the new role, and this role must have CREATE permission on the sequence's schema.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
-- Create a sequence named serial that starts from 101 and increases in ascending order:
CREATE SEQUENCE serial START 101;

-- Change the maximum value of serial to 200:
ALTER SEQUENCE serial MAXVALUE 200;

-- Create a table, and specify default values for the sequence:
CREATE TABLE T1(C1 bigint default nextval('serial'));

-- Change the owning column of the serial sequence to T1.C1:
ALTER SEQUENCE serial OWNED BY T1.C1;

-- Delete the sequence.
DROP SEQUENCE serial cascade;
DROP TABLE T1;

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel