0% found this document useful (0 votes)
6 views9 pages

SQL Sequence

The document explains the concept of sequences in database systems, which are user-defined objects that generate unique numeric values on demand. It provides syntax for creating sequences, including options for starting values, increments, and cycling behavior, along with examples of creating sequences in both ascending and descending order. Additionally, it covers the use of synonyms in SQL, differentiating between public and private synonyms, and includes examples for creating and dropping them.

Uploaded by

wtf58778
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

SQL Sequence

The document explains the concept of sequences in database systems, which are user-defined objects that generate unique numeric values on demand. It provides syntax for creating sequences, including options for starting values, increments, and cycling behavior, along with examples of creating sequences in both ascending and descending order. Additionally, it covers the use of synonyms in SQL, differentiating between public and private synonyms, and includes examples for creating and dropping them.

Uploaded by

wtf58778
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SEQUENCE

SEQUENCE
Sequence is a set of integers 1, 2, 3, … that are generated and supported by some database
systems to produce unique values on demand.
 A sequence is a user defined schema bound object that generates a sequence of numeric
values.
 Sequences are frequently used in many databases because many applications require
each row in a table to contain a unique value and sequences provides an easy way to
generate them.
 The sequence of numeric values is generated in an ascending or descending order at
defined intervals and can be configured to restart when exceeds max_value.
Syntax:
CREATE SEQUENCE sequence_name
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;

sequence_name: Name of the sequence.

initial_value: starting value from where the sequence starts.


Initial_value should be greater than or equal to minimum value
and less than equal to maximum value.

increment_value: Value by which sequence will increment itself.


Increment_value can be positive or negative.

minimum_value: Minimum value of the sequence.


maximum_value: Maximum value of the sequence.

cycle: When sequence reaches its set limit it starts from beginning.

nocycle: An exception will be thrown if sequence exceeds its max_value.

Following is the sequence query creating sequence in ascending order.

1
SEQUENCE

Example 1:

CREATE SEQUENCE sequence_1


start with 1
increment by 1
minvalue 0
maxvalue 100
cycle;

Above query will create a sequence named sequence_1.Sequence will start from 1 and
will be incremented by 1 having maximum value 100. Sequence will repeat itself from
start value after exceeding 100.
To get the next value of the sequence, you use the NEXTVAL

2
SEQUENCE

To get the current value of the sequence, you use the CURRVAL

3
SEQUENCE

Example 2:
Following is the sequence query creating sequence in descending order.
CREATE SEQUENCE sequence_2
start with 100
increment by -1
minvalue 1
maxvalue 100
cycle;

4
SEQUENCE

Above query will create a sequence named sequence_2.Sequence will start from 100 and
should be less than or equal to maximum value and will be incremented by -1 having
minimum value 1.

Example to use sequence : create a table named students with columns as id and name.
CREATE TABLE students
(
ID number(3),
NAME char(10)
);

Now insert values into table

INSERT into students VALUES(sequence_1.nextval,'Ramesh');

5
SEQUENCE

INSERT into students VALUES(sequence_1.nextval,'Suresh');


where sequence_1.nextval will insert id’s in id column in a sequence as defined in
sequence_1.

DROP SEQUENCE:
Remove a Sequence.
Syntax:
DROP SEQUENCE sequence_name;
Example:
DROP SEQUENCE sequence_2;

6
SEQUENCE

Synonyms
SQL Synonyms is an alias for a table or a Schema object in a database. They are used to
protect client applications from the changes made to name or location of an object.

Public Synonyms

Public Synonyms are owned by PUBLIC schema in a database. Public synonyms can be
referenced by all users in the database. They are created by the application owner for
the tables and other objects such as procedures and packages so the users of the
application can see the objects. To create a PUBLIC Synonym, you have to use keyword
PUBLIC .

Example:

CREATE PUBLIC SYNONYM Stud_table for STUDENT;

7
SEQUENCE

Private Synonyms

Private Synonyms are used in a database schema to hide the true name of a table,
procedure, view or any other database object.

Private synonyms can be referenced only by the schema that owns the table or object.

Example:

CREATE SYNONYM Stud_table FOR STUDENT;

Drop a Synonym

Synonyms can be dropped using DROP Synonym command. If you are dropping a public
Synonym, you have to use the keyword public in the drop statement.

8
SEQUENCE

Example:

DROP PUBLIC Synonym Stud_table;

DROP Synonym Stud_table;

You might also like