Professional Documents
Culture Documents
Objectives
Define the purpose of a sequence and state how it can
be used in a database
Explain why gaps may appear in the integers
generated by a sequence
Use the CREATE SEQUENCE command to create a
sequence
Call and use sequence values
Identify which options cannot be changed by the
ALTER SEQUENCE command
Delete a sequence
Create indexes with the CREATE INDEX command
Explain the main index structures: B-tree and bitmap
Dr. Chen, Oracle Database System (Oracle)
Objectives (continued)
Verify index use with the explain plan
Introduce variations on conventional indexes,
including a function-based index and an index
organized table
Verify index existence via the data dictionary
Rename an index with the ALTER INDEX
command
Remove an index using the DROP INDEX
command
Create and remove a public synonym
Dr. Chen, Oracle Database System (Oracle)
Database Objects
An object is anything that has a name and
defined structure
Includes:
Table stores data
Sequence generates sequential integers
Index allows users to quickly locate specific
records
Synonym alias for other database objects
Sequences
Used for internal control purposes by
providing sequential integers for auditing
Used to generate unique value for primary
key column
Surrogate key = no correlation with actual row
contents
Creating a Sequence
Use the CREATE SEQUENCE command
Various intervals are allowed Default: 1
You can specify the starting number Default: 1
CREATE SEQUENCE sequencename
[INCREASE BY value]
[START WITH value]
[{MAXVALUE value | MAXVALUE}]
[{MINVALUE value | MINVALUE}]
[{CYCLE | NOCYLE}]
[{ORDER | NOORDER}]
[{CACHE value | NOCACHE}];
Figure 6-1 Syntax of the CREATE SEQUENCE command
Dr. Chen, Oracle Database System (Oracle)
Figure 6-3
10
11
Figure 6-5
SELECT SEQUENCE_NAME
FROM USER_SEQUENCES;
SEQUENCE_NAME
-----------------------------ORDERS_ORDER#_SEQ
Dr. Chen, Oracle Database System (Oracle)
12
Figure 6-6
13
Figure 6-7
14
15
16
17
18
19
Checking
Values on
Sequences
Why
1041?
20
Removing a Sequence
Use the DROP SEQUENCE command to delete
a sequence
Previous values generated are not affected by
removing a sequence from a database
21
22
Indexes
Query Optimization
An index stores frequently referenced values and
ROWIDs
Can be based on one column, multiple columns,
functions, or expressions
23
B-Tree Index
24
25
26
For example:
SELECT index_name FROM user_indexes;
INDEX_NAME
INDEX_NAME
----------------------------------------------------------ACCTBONUS_AMID_PK
ENROLLMENT_PK
BOOKAUTHOR_PK
COURSE_SECTION_CSEC_ID_PK
ORDERITEMS_PK
COURSE_COURSE_ID_PK
BOOKS_ISBN_PK
TERM_TERM_ID_PK
BOOKS_COST_IDX
STUDENT_S_ID_PK
AUTHOR_AUTHORID_PK
SYS_IL0000110103C00010$$
PUBLISHER_PUBID_PK
FACULTY_F_ID_PK
ORDERS_ORDER#_PK
PUBLISHER3_PUBID_PK
CUSTOMERS_ZIP_DESC_IDX
PUBLISHER2_PUBID_PK
CUSTOMERS_ZIP_IDX
REPCONTRACTS_PK
CUSTOMERS_CUSTOMER#_PK
BOOKSTORES_ID_PK
EMPLOYEES_EMPNO_PK
BOOKSTORES_NAME_UK
PT_CHARG_PATIENTNO_ITEMCODE_PK
STOREREPS_ID_PK
ITEM_ITEM_CODE_PK
ROOM_ROOM_NO_PK
32 rows selected.
ACCOMODATION_ACCOM_ID_PK
DOCTOR_PHYSCIAN_ID__PK
What specific information is displayed
PATIENT_PATIENT_NO_PK
on the output?
LOCATION_LOC_ID_PK
Dr. Chen, Oracle Database System (Oracle)
Answer: pk
Since they are all implicitly indexed.
27
28
29
Title
PubDate
PubID
Cost
VARCHAR2(10)
VARCHAR2(30)
DATE
NUMBER(2)
NUMBER(5,2) NUMBER(5,2)
Retail
Discount
Category
NUMBER(4,2)
VARCHAR2(12)
BOOKS
30
Optimizing Query
Processing
TITLE
COST CATEGORY
------------------------------ ---------- -----------BUILDING A CAR WITH TOOTHPICKS
37.8 CHILDREN
DATABASE IMPLEMENTATION
31.4 COMPUTER
HOLY GRAIL OF ORACLE
47.25 COMPUTER
HANDCRANKED COMPUTERS
21.8 COMPUTER
E-BUSINESS THE EASY WAY
37.9 COMPUTER
PAINLESS CHILD-REARING
48 FAMILY LIFE
SHORTEST POEMS
21.85 LITERATURE
7 rows selected.
Dr. Chen, Oracle Database System (Oracle)
31
33
TITLE
COST CATEGORY
------------------------------ ---------- -----------BUILDING A CAR WITH TOOTHPICKS
37.8 CHILDREN
DATABASE IMPLEMENTATION
31.4 COMPUTER
HOLY GRAIL OF ORACLE
47.25 COMPUTER
HANDCRANKED COMPUTERS
21.8 COMPUTER
E-BUSINESS THE EASY WAY
37.9 COMPUTER
PAINLESS CHILD-REARING
48 FAMILY LIFE
SHORTEST POEMS
21.85 LITERATURE
7 rows selected.
ON books (cost);
Index created.
Dr. Chen, Oracle Database System (Oracle)
34
TITLE
COST CATEGORY
------------------------------ ---------- -----------HANDCRANKED COMPUTERS
21.8 COMPUTER
SHORTEST POEMS
21.85 LITERATURE
DATABASE IMPLEMENTATION
31.4 COMPUTER
BUILDING A CAR WITH TOOTHPICKS
37.8 CHILDREN
E-BUSINESS THE EASY WAY
37.9 COMPUTER
HOLY GRAIL OF ORACLE
47.25 COMPUTER
PAINLESS CHILD-REARING
48 FAMILY LIFE
7 rows selected.
35
Before INDEX.
ISBN
---------4981341710
8843172113
3957136468
1915762492
9959789321
2491748320
2147428890
TITLE
COST CATEGORY
------------------------------ ---------- -----------BUILDING A CAR WITH TOOTHPICKS
37.8 CHILDREN
DATABASE IMPLEMENTATION
31.4 COMPUTER
HOLY GRAIL OF ORACLE
47.25 COMPUTER
HANDCRANKED COMPUTERS
21.8 COMPUTER
E-BUSINESS THE EASY WAY
37.9 COMPUTER
PAINLESS CHILD-REARING
48 FAMILY LIFE
SHORTEST POEMS
21.85 LITERATURE
7 rows selected.
After INDEX.
ISBN
---------1915762492
2147428890
8843172113
4981341710
9959789321
3957136468
2491748320
TITLE
COST CATEGORY
------------------------------ ---------- -----------HANDCRANKED COMPUTERS
21.8 COMPUTER
SHORTEST POEMS
21.85 LITERATURE
DATABASE IMPLEMENTATION
31.4 COMPUTER
BUILDING A CAR WITH TOOTHPICKS
37.8 CHILDREN
E-BUSINESS THE EASY WAY
37.9 COMPUTER
HOLY GRAIL OF ORACLE
47.25 COMPUTER
PAINLESS CHILD-REARING
48 FAMILY LIFE
7 rows selected.
36
37
TITLE
COST CATEGORY
------------------------------ ---------- -----------HANDCRANKED COMPUTERS
21.8 COMPUTER
SHORTEST POEMS
21.85 LITERATURE
DATABASE IMPLEMENTATION
31.4 COMPUTER
BUILDING A CAR WITH TOOTHPICKS
37.8 CHILDREN
E-BUSINESS THE EASY WAY
37.9 COMPUTER
HOLY GRAIL OF ORACLE
47.25 COMPUTER
PAINLESS CHILD-REARING
48 FAMILY LIFE
7 rows selected.
38
39
40
41
Your Turn
Practice the following examples (from the
text)
42
43
Figure 6-20 View the explain plan indicating a full table scan
Dr. Chen, Oracle Database System (Oracle)
44
Figure 6-21 View the explain plan indicating a full table scan
45
UNIQUE INDEX
46
47
48
49
50
51
Summary
52
Summary (continued)
The ALTER SEQUENCE command is used to modify an existing
sequence; the only settings that cant be modified are the START
WITH option and any option that would be invalid because of
previously generated values
The DUAL table is helpful for testing sequence value generation
The DROP SEQUENCE command deletes an existing sequence
An index can be created to speed up the query process
DML operations are always slower when indexes exist
Oracle 11g creates an index for PRIMARY KEY and UNIQUE
constraints automatically
An explicit index is created with the CREATE INDEX command
An index can be used by Oracle 11g automatically if a query
criterion or sort operation is based on a column or an expression
used to create the index
Dr. Chen, Oracle Database System (Oracle)
53
Summary (continued)
The two main structures for indexes are B-tree and bitmap
The explain plan can verify whether an index is used in a
query
Function-based indexes are used to index an expression or
the use of functions on a column or columns
An index organized table is a table stored in a B-tree structure
to combine the index and table into one database object
Information about an index can be retrieved from the
USER_INDEXES and USER_IND_COLUMNS views
An index can be dropped with the DROP INDEX command
An index can be renamed with the ALTER INDEX command
Dr. Chen, Oracle Database System (Oracle)
54
Summary (continued)
Except for a name change, an index cant be modified; it
must be deleted and then re-created
A synonym provides a permanent alias for a database object
A public synonym is available to any database user
A private synonym is available only to the user who created it
A synonym is created by using the CREATE SYNONYM
command
A synonym is deleted by using the DROP SYNONYM
command
Only a user with DBA privileges can drop a public synonym
Dr. Chen, Oracle Database System (Oracle)
55
END OF CHAPTER 6
56