
- •Objectivesj i
- •Database Objectsj
- •What IsIs a Sequence?
- •The CREATE SEQUENCE
- •Creatingi a Sequence
- •Confirmingi i Sequences
- •NEXTVAL and CURRVAL Pseudocolumnsl
- •Usingi a Sequence
- •Usingi a Sequence
- •Modifyingi i a Sequence
- •Guidelinesi li for Modifyingi i a Sequence
- •Removingi a Sequence
- •What IsIs an Index?I
- •How Are IndexesI Created?
- •Creatingi an IndexI
- •Guidelinesi li to Creatingi an IndexI
- •Guidelinesi li to Creatingi an IndexI
- •Confirmingi i IndexesI
- •Removingi an IndexI
- •Synonyms
- •Creatingi and Removingi Synonyms
- •Summary
- •Practicei Overviewi

13
Other Database Objects

Objectivesj i
After completing this lesson, you should be able to do the following:
•• Describeri some databaset objectsj ts and theirt ir uses
•• Create,r t , maintain,i t i , and use sequences
•• Creater te and maintaini t in indexesi
•• Creater te privateri te and publiclic synonyms
13-2

Database Objectsj
|
Object |
|
|
|
Description |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Table |
|
|
|
Basic unit of storage; composed of rows |
|
|
|
|
|
|
and columns |
|
|
|
|
|
|
|
|
|
View |
|
|
|
Logically represents subsets of data from |
|
|
|
|
|
|
one or more tables |
|
|
|
|
|
|
|
|
|
Sequence |
|
|
|
Generates primary key values |
|
|
|
|
|
|
|
|
|
Index |
|
|
|
Improves the performance of some queries |
|
|
|
|
|
|
|
|
|
Synonym |
|
|
|
Alternative name for an object |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13-3

What IsIs a Sequence?
•• Automaticallyt ti lly generatesr t uniquei numbersrs
•• IsIs a sharabler le objectj t
•• IsIs typicallyt i lly used toto creater te a primaryri ry key valuel
•• Replacesl applicationli ti code
•• Speeds up thet efficiencyffi i off accessingi sequence valuesl when cached inin memoryry
13-4

The CREATE SEQUENCE
Statement
Define a sequence to generate sequential numbers automatically
CREATE SEQUENCE sequence [INCREMENT BY n] [START WITH n]
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}]
[{CACHE n | NOCACHE}];
13-5

Creatingi a Sequence
••CreateCreateaasequencesequencenamednamedDEPTDEPT__DEPTNODEPTNOtotobebeusedusedforforthetheprimaryprimarykeykeyofofthethe
DEPTDEPTtabletable..
••DoDonotnotuseusethetheCYCLECYCLEoptionoption..
SQL> CREATE SEQUENCE dept_deptno
2 INCREMENT BY 1
3 START WITH 91
4MAXVALUE 100
5 |
NOCACHE |
6 |
NOCYCLE; |
Sequence created.
13-6

Confirmingi i Sequences
•• Verifyrify yourr sequence valuesl inin thet
USER_SEQUENCES datata dictionaryi ti ry tablet le..
|
|
|
|
|
|
SQL> |
SELECT |
sequence_name, min_value, max_value, |
|
|
2 |
|
increment_by, last_number |
|
|
3 |
FROM |
user_sequences; |
|
|
|
|
|
|
|
|
|
|
|
•• The LAST_NUMBER columnl displaysi l thet nextt availableil le sequence numberr..
13-7

NEXTVAL and CURRVAL Pseudocolumnsl
•• NEXTVAL returnsr t r thet nextt availableil le sequence valuel ..
ItIt returnsr t r a uniquei valuel everyry timeti itit isis referenced,r f r , even forf r differentiff r t usersrs..
•• CURRVAL obtainst i thet currentrr t sequence valuel ..
NEXTVAL mustt be issuedi forf r thatt t sequence beforef re CURRVAL containst i a valuel ..
13-8

Usingi a Sequence
•• InsertI rt a new departmentrt t named
“MARKETING”I inin San Diegoi ..
SQL> |
INSERT |
INTO |
dept(deptno, dname, loc) |
2 |
VALUES |
|
(dept_deptno.NEXTVAL, |
3 |
|
|
'MARKETING', 'SAN DIEGO'); |
1 row created.
•• Viewi thet currentrr t valuel forf r thet
DEPT_DEPTNO sequence..
|
|
|
|
|
|
SQL> |
SELECT |
dept_deptno.CURRVAL |
|
|
2 |
FROM |
dual; |
|
|
|
|
|
|
|
|
|
|
|
13-10

Usingi a Sequence
•• Cachingi sequence valuesl inin memoryry allowsll fasterf t r access toto thoset valuesl ..
•• Gaps inin sequence valuesl can occurr when::
–A rollbackr ll occursrs
–The systemt crashesr
–A sequence isis used inin anothert r tablet le
•• Viewi thet nextt availableil le sequence,, ifif itit was createdr t withith NOCACHE,, by queryingr i thet USER_SEQUENCES tablet le..
13-11

Modifyingi i a Sequence
Change the increment value, maximum value, minimum value, cycle option, or cache option.
SQL> ALTER SEQUENCE dept_deptno
2 |
INCREMENT BY 1 |
3 |
MAXVALUE 999999 |
4 |
NOCACHE |
5 |
NOCYCLE; |
Sequence altered.
13-12