0% found this document useful (0 votes)
19 views36 pages

Lecture 06 S1 2023

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)
19 views36 pages

Lecture 06 S1 2023

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/ 36

19 March 2023 1

INF 312
DATABASE DESIGN AND
DEVELOPMENT

Semester 1, 2023
Week 6 – Lecture 6

Additional Database Objects


19 March 2023 2

READINGS
• Casteel, Chapter 6 of
Oracle 12c textbook
• This chapter is important
for SQL skill development.
19 March 2023 3

Lecture Objectives
After completing this chapter, you should be able to do the
following:
• Define the purpose of a sequence and explain how it
can be used in a database
• Use the CREATE SEQUENCE command to create a
sequence
• Call and use sequence values, and identify which
options can’t be changed by the ALTER SEQUENCE
command
• Delete a sequence
19 March 2023 4

Lecture 6 Objectives
After completing this chapter, you should be able to do the
following:
• Create indexes with the CREATE INDEX command
• Explain the main index structures: B-tree and bitmap
• Rename an index with the ALTER INDEX command
• Remove an index with the DELETE INDEX command
• Create and remove a public synonym
Database Objects
• An object is anything that has a name and defined
structure

• Database objects in Oracle includes:


• Table – is a basic unit of data storage in an Oracle Database. Data
is stored in rows and columns.
• Sequence – generates sequential integers that organizations can
use to assist with internal controls or simply to serve as primary
keys for tables.
• Index – serves the same basic purpose as an index in a book,
allowing users to locate specific records quickly.
• Synonym – is a simpler name, like a nickname, given to an object
with a complex name or to provide an alternative name for
identifying database objects.
Oracle 11g: SQL 5
Sequences
• Used for internal control purposes by providing sequential
integers for auditing
• Used to generate unique value for primary key column
– Generates Surrogate key since there is no correlation with actual
row contents
• Can also be used to provide business and auditing
controls.
• Every organization should have some control
mechanisms to avoid problems with transaction auditing,
embezzlement, and accounting errors.
• Most organizations use sequential numbers to track
checks, purchase orders, invoices, and anything else
used to record financial events.
Oracle 11g: SQL 6
7

Creating a Sequence
• Use the CREATE SEQUENCE command
• Various intervals are allowed – Default: 1
• You can specify the starting number – Default:
1

• Note: Optional commands are shown in square


brackets. Curly brackets indicate that one of the two
options listed can be used, but not both.
Creating a Sequence (continued)
• Can specify MINVALUE for decreasing sequence
and MAXVALUE for increasing sequence
• Numbers can be reused if CYCLE is specified
• ORDER clause is used in application cluster
environments to return sequence values in the
same order as multiple requests are received.
• Use CACHE to pre-generate sequence values
and store in server memory for faster processing
– Default: 20 (generates 20 sequence values at
once and keep in memory for use)
Oracle 11g: SQL 8
Creating a Sequence Example

Oracle 11g: SQL 9


10

View available sequences


• Use USER_SEQUENCES data dictionary to
view existing sequences

Next Number to issue

Oracle 11g: SQL


11

Using Sequence Values


• You can access sequence values by using the two
pseudocolumns NEXTVAL and CURRVAL.
• Pseudocolumns are data associated with table data,
much like columns, but aren’t physical columns stored
in the database.
• The pseudocolumn NEXTVAL (NEXT VALUE)
is used to actually generate the sequence
value. In other words, it calls the sequence
object and requests the value of the next
number in the sequence.
• After a value is generated, it’s stored in the
CURRVAL (CURRENT VALUE) pseudocolumn
so that you can reference it again.
12

Using Sequence Values

• NEXTVAL – generates next sequence value

Oracle 11g: SQL


13

Using Values from Sequence


• CURRVAL – contains the latest sequence
value generated by NEXTVAL

Oracle 11g: SQL


Altering Sequence Definitions
• Use ALTER SEQUENCE command to change the
settings for a sequence
• START WITH value cannot be altered – drop the
sequence and re-create it
• Changes cannot be made that invalidates the current
sequence values

Oracle 11g: SQL 14


ALTER SEQUENCE Command Example

Oracle 11g: SQL 15


16

Removing a Sequence
• Use the DROP SEQUENCE command to
delete a sequence
• Previous sequence values are not affected
by removing a sequence from a database

Oracle 11g: SQL


17

Removing a Sequence (continued)

Oracle 11g: SQL


Database Indexes
• An index stores a map of column values and the ROWIDs
of matching table rows.
• A ROWID is the physical address of a table row.
• Much like an index at the end of a book.
• Indexes make data retrieval more efficient

Oracle 11g: SQL 18


B-Tree Index Organisation - Structure

• Find zip code: 90404 ?


• Four data block reads in this structure!

Oracle 11g: SQL 19


B-Tree Index - Structure (continued)
• An index is implicitly created by PRIMARY KEY and
UNIQUE constraints
• An index can be explicitly created using the CREATE
INDEX command

Oracle 11g: SQL 20


CREATE INDEX Command Examples

Oracle 11g: SQL 21


Bitmap Indexes - Structure
Used to
improve
queries on
columns that
have low
cardinality i.e.
small number
of distinct
values

Oracle 11g: SQL 22


Function-Based Indexes
Creating index
on profit to be
used as search
criterion

Creating index for NULL


values

Oracle 11g: SQL 23


24

Index Organized Tables (IOT)


• An IOT stores table contents in a B-tree index
structure combining the index and table in one
structure.
• Use the “ORGANIZATION INDEX” option in a
CREATE TABLE statement to build an IOT

Oracle 11g: SQL


25

Verifying an Index

• Use the USER_INDEXES data dictionary view


to determine that the index exists

Oracle 11g: SQL


26

Verifying an Index (continued)

Oracle 11g: SQL


27

Removing an Index
• Use the DROP INDEX command to remove
an index

Oracle 11g: SQL


Synonyms
• Synonyms serve as alternative names or aliases for
database objects such as a table or a sequence
• Used to simplify references to the database objects and to
enhance security due to the hiding of actual object name.
• Can be private or public
• PRIVATE synonyms are only available to the user who created
them
• PUBLIC synonyms are available to all database users

Oracle 11g: SQL 28


CREATE SYNONYM Command Syntax

Oracle 11g: SQL 29


CREATE SYNONYM Command

Oracle 11g: SQL 30


31

Deleting a SYNONYM
• A private synonym can be deleted by its
owner
• A PUBLIC synonym can only be deleted by a
user with DBA privileges

Oracle 11g: SQL


32

SUMMARY
• A sequence is created with the CREATE SEQUENCE
command.
• The ALTER SEQUENCE command is used to modify an
existing sequence. The only settings that can’t be
modified are the START WITH option and any option
that would be invalid because of previously generated
values.
• The DROP SEQUENCE command deletes an existing
sequence.
• Oracle 11g creates an index for PRIMARY KEY and
UNIQUE constraints automatically.
• An explicit index is created with the CREATE INDEX
command.
33

SUMMARY
• An index can be dropped with the DROP INDEX
command.
• An index can be renamed with the ALTER INDEX
command.
• A synonym is created by using the CREATE SYNONYM
command.
• A synonym is deleted by using the DROP SYNONYM
command.
34
Syntax Guide
35
Syntax Guide
19 March 2023 36

Tasks for this week…..


• Read Chapter 6 – Additional Database
Objects.
• Do all Chapter 6 – Review Questions.
• Do Online Quiz #5 – on Moodle.

You might also like