Chapter 1 Overview of Database Concepts

advertisement
Chapter 12
Additional Database Objects
Chapter 12
Introduction to Oracle9i: SQL
1
Database Objects
• Anything that has a name and defined
structure
• Includes:
– Sequence – generate sequential integers
– Index – quickly locate specific records
– Synonym – alias for other database objects
Chapter 12
Introduction to Oracle9i: SQL
2
Sequences
• Used for internal control purposes by
providing sequential integers for auditing
• Used to generate unique value for primary
key column – no correlation with actual row
contents
Chapter 12
Introduction to Oracle9i: SQL
3
CREATE SEQUENCE
Command
• Various intervals allowed – Default: 1
• Can specify starting number – Default: 1
Chapter 12
Introduction to Oracle9i: SQL
4
CREATE SEQUENCE
Command
• Can specify MINVALUE for decreasing
sequence, MAXVALUE for increasing
• Numbers can be reused if CYCLE specified
• ORDER clause for application cluster
environment
• Use CACHE to pre-generate integers –
Default: 20
Chapter 12
Introduction to Oracle9i: SQL
5
CREATE SEQUENCE
Command Example
Chapter 12
Introduction to Oracle9i: SQL
6
Verifying Sequence Values
Query USER_SEQUENCES data dictionary
view
Chapter 12
Introduction to Oracle9i: SQL
7
Using Sequence Values
• NEXTVAL – generates integer
• CURRVAL – contains last integer generated
by NEXTVAL
Chapter 12
Introduction to Oracle9i: SQL
8
Altering Sequence Definitions
• Use ALTER SEQUENCE command
• START WITH value cannot be altered –
drop sequence and re-create
• Changes cannot make current integers
invalid
Chapter 12
Introduction to Oracle9i: SQL
9
ALTER SEQUENCE Command
Example
Chapter 12
Introduction to Oracle9i: SQL
10
DROP SEQUENCE Command
Previous values generated are not affected
by removing a sequence from a database
Chapter 12
Introduction to Oracle9i: SQL
11
Indexes
• Stores frequently referenced value and row
ID (ROWID)
• Can be based on one column, multiple
columns, functions, or expressions
Chapter 12
Introduction to Oracle9i: SQL
12
Creating an Index
• Implicitly created by PRIMARY KEY and
UNIQUE constraints
• Explicitly created by CREATE INDEX
command
Chapter 12
Introduction to Oracle9i: SQL
13
CREATE INDEX Command
Example
Chapter 12
Introduction to Oracle9i: SQL
14
Verifying an Index
Indexes listed in USER_INDEXES view
Chapter 12
Introduction to Oracle9i: SQL
15
Removing an Index
Use DROP INDEX command
Chapter 12
Introduction to Oracle9i: SQL
16
Synonyms
• Serve as permanent aliases for database
objects
• Can be private or public
– Private synonyms are only available to user
who created them
– PUBLIC synonyms are available to all database
users
Chapter 12
Introduction to Oracle9i: SQL
17
CREATE SYNONYM Command
Syntax
Chapter 12
Introduction to Oracle9i: SQL
18
CREATE SYNONYM Command
Example
Chapter 12
Introduction to Oracle9i: SQL
19
Deleting a SYNONYM
• A private synonym can be deleted by owner
• A PUBLIC synonym can only be deleted by
a user with DBA privileges
Chapter 12
Introduction to Oracle9i: SQL
20
Data Dictionary
•
•
•
•
Stores information about database objects
Owned by user SYS
Cannot be directly accessed by users
Displays contents through data dictionary
views
Chapter 12
Introduction to Oracle9i: SQL
21
View Prefixes
Chapter 12
Introduction to Oracle9i: SQL
22
Download