SQL Presentation 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

SEQUENCES

What are sequences?


■ Sequences are nothing but database objects.
■ A sequence is a set of integers 1, 2, 3, ... that are generated in order
on demand.
■ Sequences are user created database objects, that can be shared by
multiple users to generate unique integers.
■ A typical use of sequence is to create primary key values which are
unique for each row.
■ The sequence is generated and incremented (or decremented) by an
internal Oracle routine
■ The value generated can have a maximum of 38 digits.
Structure of sequences

■ CREATE SEQUENCE sequence


■ [INCREMENT BY n]
■ [START WITH n]
■ [{MAXVALUE n | NOMAXVALUE}]
■ [{MINVALUE n | NOMINVALUE}]
■ [{CYCLE | NOCYCLE}]
■ [{CACHE n | NOCACHE}];
■ INCREMENT BY -
Specifies the interval between sequence numbers. It can be any positive or
negative value but not zero. If this clause is omitted, the default value is 1
■ MAXVALUE And MINVALUE -
Specifies the maximum or minimum value that a sequence can generate.
■ START WITH -
Specifies the first sequence number to be generated. The default for an ascending
sequence is the sequence minimum value (1) and for a descending sequence, it is
the maximum value (-1).
■ CYCLE:
Specifies that the sequence continues to generate repeat values after reaching
either its maximum value.
■ CACHE:
Specifies how many values to generate in advance and to keep in memory for faster
access. Minimum value is two for this option.
■ CREATE SEQUENCE dept_deptid_seq
■ INCREMENT BY 10
■ START WITH 120
■ MAXVALUE 9999
■ NOCACHE
■ NOCYCLE;
Auto Increment
■ The simplest way in MySQL to use sequences is to define a column as AUTO_INCREMENT.
■ Ex. Creating table
■ CREATE TABLE INSECT
■ (
■ id INT UNSIGNED NOT NULL AUTO_INCREMENT,
■ PRIMARY KEY (id),
■ name VARCHAR(30) NOT NULL, # type of insect
■ date DATE NOT NULL, # date collected
■ origin VARCHAR(30) NOT NULL # where collected
■ );

■ INSERT INTO INSECT (id,name,date,origin)


VALUES
(NULL,'housefly','2001-09-10','kitchen'),
(NULL,'millipede','2001-09-10','driveway'),
(NULL,'grasshopper','2001-09-10','front yard');
Table

id name date origin

1 Ram 10-04-2010 IND

2 Arun 13-05-2014 IND

3 Kiran 17-01-2002 IND

You might also like