SlideShare a Scribd company logo
1 of 24
Copyright  Oracle Corporation, 1998. All rights reserved.
13
Other Database Objects
13-2 Copyright  Oracle Corporation, 1998. All rights reserved.
Objectives
After completing this lesson, you should
be able to do the following:
• Describe some database objects and
their uses
• Create, maintain, and use sequences
• Create and maintain indexes
• Create private and public synonyms
13-3 Copyright  Oracle Corporation, 1998. All rights reserved.
Database Objects
Description
Basic unit of storage; composed of rows
and columns
Logically represents subsets of data from
one or more tables
Generates primary key values
Improves the performance of some queries
Alternative name for an object
Object
Table
View
Sequence
Index
Synonym
13-4 Copyright  Oracle Corporation, 1998. All rights reserved.
What Is a Sequence?
• Automatically generates unique
numbers
• Is a sharable object
• Is typically used to create a primary key
value
• Replaces application code
• Speeds up the efficiency of accessing
sequence values when cached in
memory
13-5 Copyright  Oracle Corporation, 1998. All rights reserved.
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-6 Copyright  Oracle Corporation, 1998. All rights reserved.
Creating a Sequence
• Create a sequence named DEPT_DEPTNO
to be used for the primary key of the
DEPT table.
• Do not use the CYCLE option.
SQL> CREATE SEQUENCE dept_deptno
2 INCREMENT BY 1
3 START WITH 91
4 MAXVALUE 100
5 NOCACHE
6 NOCYCLE;
Sequence created.
13-7 Copyright  Oracle Corporation, 1998. All rights reserved.
Confirming Sequences
• Verify your sequence values in the
USER_SEQUENCES data dictionary
table.
• The LAST_NUMBER column displays
the next available sequence number.
SQL> SELECT sequence_name, min_value, max_value,
2 increment_by, last_number
3 FROM user_sequences;
13-8 Copyright  Oracle Corporation, 1998. All rights reserved.
NEXTVAL and CURRVAL
Pseudocolumns
• NEXTVAL returns the next available
sequence value.
It returns a unique value every time it is
referenced, even for different users.
• CURRVAL obtains the current sequence
value.
NEXTVAL must be issued for that
sequence before CURRVAL contains a
value.
13-10 Copyright  Oracle Corporation, 1998. All rights reserved.
Using a Sequence
• Insert a new department named
“MARKETING” in San Diego.
• View the current value for the
DEPT_DEPTNO sequence.
SQL> INSERT INTO dept(deptno, dname, loc)
2 VALUES (dept_deptno.NEXTVAL,
3 'MARKETING', 'SAN DIEGO');
1 row created.
SQL> SELECT dept_deptno.CURRVAL
2 FROM dual;
13-11 Copyright  Oracle Corporation, 1998. All rights reserved.
Using a Sequence
• Caching sequence values in memory
allows faster access to those values.
• Gaps in sequence values can occur when:
– A rollback occurs
– The system crashes
– A sequence is used in another table
• View the next available sequence, if it was
created with NOCACHE, by querying the
USER_SEQUENCES table.
13-12 Copyright  Oracle Corporation, 1998. All rights reserved.
Modifying 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-13 Copyright  Oracle Corporation, 1998. All rights reserved.
Guidelines for Modifying
a Sequence
• You must be the owner or have the
ALTER privilege for the sequence.
• Only future sequence numbers are
affected.
• The sequence must be dropped and
re-created to restart the sequence at a
different number.
• Some validation is performed.
13-14 Copyright  Oracle Corporation, 1998. All rights reserved.
Removing a Sequence
• Remove a sequence from the data
dictionary by using the DROP
SEQUENCE statement.
• Once removed, the sequence can no
longer be referenced.
SQL> DROP SEQUENCE dept_deptno;
Sequence dropped.
13-15 Copyright  Oracle Corporation, 1998. All rights reserved.
What Is an Index?
• Is a schema object
• Is used by the Oracle Server to speed
up the retrieval of rows by using a
pointer
• Can reduce disk I/O by using rapid path
access method to locate the data
quickly
• Is independent of the table it indexes
• Is used and maintained automatically by
the Oracle Server
13-16 Copyright  Oracle Corporation, 1998. All rights reserved.
How Are Indexes Created?
• Automatically: A unique index is created
automatically when you define a
PRIMARY KEY or UNIQUE constraint in
a table definition.
• Manually: Users can create nonunique
indexes on columns to speed up access
time to the rows.
13-17 Copyright  Oracle Corporation, 1998. All rights reserved.
Creating an Index
• Improve the speed of query access on
the ENAME column in the EMP table.
SQL> CREATE INDEX emp_ename_idx
2 ON emp(ename);
Index created.
CREATE INDEX index
ON table (column[, column]...);
• Create an index on one or more columns.
13-18 Copyright  Oracle Corporation, 1998. All rights reserved.
When to Create an Index
• The column is used frequently in the WHERE
clause or in a join condition.
• The column contains a wide range of values.
• The column contains a large number of null
values.
• Two or more columns are frequently used
together in a WHERE clause or a join
condition.
• The table is large and most queries are
expected to retrieve less than 2–4% of the
rows.
13-19 Copyright  Oracle Corporation, 1998. All rights reserved.
When Not to Create an Index
• The table is small.
• The columns are not often used as a
condition in the query.
• Most queries are expected to retrieve
more than 2–4% of the rows.
• The table is updated frequently.
13-20 Copyright  Oracle Corporation, 1998. All rights reserved.
Confirming Indexes
• The USER_INDEXES data dictionary view
contains the name of the index and its
uniqueness.
• The USER_IND_COLUMNS view contains
the index name, the table name, and the
column name.
SQL> SELECT ic.index_name, ic.column_name,
2 ic.column_position col_pos,ix.uniqueness
3 FROM user_indexes ix, user_ind_columns ic
4 WHERE ic.index_name = ix.index_name
5 AND ic.table_name = 'EMP';
13-21 Copyright  Oracle Corporation, 1998. All rights reserved.
Removing an Index
• Remove an index from the data dictionary.
• Remove the EMP_ENAME_IDX index from
the data dictionary.
• To drop an index, you must be the owner
of the index or have the DROP ANY INDEX
privilege.
SQL> DROP INDEX emp_ename_idx;
Index dropped.
SQL> DROP INDEX index;
13-22 Copyright  Oracle Corporation, 1998. All rights reserved.
Synonyms
Simplify access to objects by creating a
synonym (another name for an object).
• Refer to a table owned by another user.
• Shorten lengthy object names.
CREATE [PUBLIC] SYNONYM synonym
FOR object;
13-23 Copyright  Oracle Corporation, 1998. All rights reserved.
Creating and Removing Synonyms
SQL> CREATE SYNONYM d_sum
2 FOR dept_sum_vu;
Synonym Created.
SQL> DROP SYNONYM d_sum;
Synonym dropped.
• Create a shortened name for the
DEPT_SUM_VU view.
• Drop a synonym.
13-24 Copyright  Oracle Corporation, 1998. All rights reserved.
Summary
• Automatically generate sequence
numbers by using a sequence generator.
• View sequence information in the
USER_SEQUENCES data dictionary table.
• Create indexes to improve query retrieval
speed.
• View index information in the
USER_INDEXES dictionary table.
• Use synonyms to provide alternative
names for objects.
13-25 Copyright  Oracle Corporation, 1998. All rights reserved.
Practice Overview
• Creating sequences
• Using sequences
• Creating nonunique indexes
• Displaying data dictionary information
about sequences and indexes
• Dropping indexes

More Related Content

Similar to chap13.ppt

08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadatarehaniltifat
 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Salman Memon
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusChhom Karath
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...rehaniltifat
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tablessiavosh kaviani
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...TAISEEREISA
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptxKulbir4
 
Dan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New FeaturesDan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New FeaturesEmbarcadero Technologies
 
Using SQL Queries to Insert, Update, Delete, and View Data.ppt
Using SQL Queries to Insert, Update, Delete, and View Data.pptUsing SQL Queries to Insert, Update, Delete, and View Data.ppt
Using SQL Queries to Insert, Update, Delete, and View Data.pptMohammedJifar1
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slidesmetsarin
 

Similar to chap13.ppt (20)

Les11.ppt
Les11.pptLes11.ppt
Les11.ppt
 
Sql views
Sql viewsSql views
Sql views
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata
 
Les12
Les12Les12
Les12
 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*Plus
 
Sql lite android
Sql lite androidSql lite android
Sql lite android
 
Les09.ppt
Les09.pptLes09.ppt
Les09.ppt
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
 
Overview of Oracle database12c for developers
Overview of Oracle database12c for developersOverview of Oracle database12c for developers
Overview of Oracle database12c for developers
 
Oracle SQL Part1
Oracle SQL Part1Oracle SQL Part1
Oracle SQL Part1
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tables
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
 
Dan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New FeaturesDan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New Features
 
chapter03.ppt
chapter03.pptchapter03.ppt
chapter03.ppt
 
Using SQL Queries to Insert, Update, Delete, and View Data.ppt
Using SQL Queries to Insert, Update, Delete, and View Data.pptUsing SQL Queries to Insert, Update, Delete, and View Data.ppt
Using SQL Queries to Insert, Update, Delete, and View Data.ppt
 
21
2121
21
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 

Recently uploaded

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Recently uploaded (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

chap13.ppt

  • 1. Copyright  Oracle Corporation, 1998. All rights reserved. 13 Other Database Objects
  • 2. 13-2 Copyright  Oracle Corporation, 1998. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • Describe some database objects and their uses • Create, maintain, and use sequences • Create and maintain indexes • Create private and public synonyms
  • 3. 13-3 Copyright  Oracle Corporation, 1998. All rights reserved. Database Objects Description Basic unit of storage; composed of rows and columns Logically represents subsets of data from one or more tables Generates primary key values Improves the performance of some queries Alternative name for an object Object Table View Sequence Index Synonym
  • 4. 13-4 Copyright  Oracle Corporation, 1998. All rights reserved. What Is a Sequence? • Automatically generates unique numbers • Is a sharable object • Is typically used to create a primary key value • Replaces application code • Speeds up the efficiency of accessing sequence values when cached in memory
  • 5. 13-5 Copyright  Oracle Corporation, 1998. All rights reserved. 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}];
  • 6. 13-6 Copyright  Oracle Corporation, 1998. All rights reserved. Creating a Sequence • Create a sequence named DEPT_DEPTNO to be used for the primary key of the DEPT table. • Do not use the CYCLE option. SQL> CREATE SEQUENCE dept_deptno 2 INCREMENT BY 1 3 START WITH 91 4 MAXVALUE 100 5 NOCACHE 6 NOCYCLE; Sequence created.
  • 7. 13-7 Copyright  Oracle Corporation, 1998. All rights reserved. Confirming Sequences • Verify your sequence values in the USER_SEQUENCES data dictionary table. • The LAST_NUMBER column displays the next available sequence number. SQL> SELECT sequence_name, min_value, max_value, 2 increment_by, last_number 3 FROM user_sequences;
  • 8. 13-8 Copyright  Oracle Corporation, 1998. All rights reserved. NEXTVAL and CURRVAL Pseudocolumns • NEXTVAL returns the next available sequence value. It returns a unique value every time it is referenced, even for different users. • CURRVAL obtains the current sequence value. NEXTVAL must be issued for that sequence before CURRVAL contains a value.
  • 9. 13-10 Copyright  Oracle Corporation, 1998. All rights reserved. Using a Sequence • Insert a new department named “MARKETING” in San Diego. • View the current value for the DEPT_DEPTNO sequence. SQL> INSERT INTO dept(deptno, dname, loc) 2 VALUES (dept_deptno.NEXTVAL, 3 'MARKETING', 'SAN DIEGO'); 1 row created. SQL> SELECT dept_deptno.CURRVAL 2 FROM dual;
  • 10. 13-11 Copyright  Oracle Corporation, 1998. All rights reserved. Using a Sequence • Caching sequence values in memory allows faster access to those values. • Gaps in sequence values can occur when: – A rollback occurs – The system crashes – A sequence is used in another table • View the next available sequence, if it was created with NOCACHE, by querying the USER_SEQUENCES table.
  • 11. 13-12 Copyright  Oracle Corporation, 1998. All rights reserved. Modifying 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.
  • 12. 13-13 Copyright  Oracle Corporation, 1998. All rights reserved. Guidelines for Modifying a Sequence • You must be the owner or have the ALTER privilege for the sequence. • Only future sequence numbers are affected. • The sequence must be dropped and re-created to restart the sequence at a different number. • Some validation is performed.
  • 13. 13-14 Copyright  Oracle Corporation, 1998. All rights reserved. Removing a Sequence • Remove a sequence from the data dictionary by using the DROP SEQUENCE statement. • Once removed, the sequence can no longer be referenced. SQL> DROP SEQUENCE dept_deptno; Sequence dropped.
  • 14. 13-15 Copyright  Oracle Corporation, 1998. All rights reserved. What Is an Index? • Is a schema object • Is used by the Oracle Server to speed up the retrieval of rows by using a pointer • Can reduce disk I/O by using rapid path access method to locate the data quickly • Is independent of the table it indexes • Is used and maintained automatically by the Oracle Server
  • 15. 13-16 Copyright  Oracle Corporation, 1998. All rights reserved. How Are Indexes Created? • Automatically: A unique index is created automatically when you define a PRIMARY KEY or UNIQUE constraint in a table definition. • Manually: Users can create nonunique indexes on columns to speed up access time to the rows.
  • 16. 13-17 Copyright  Oracle Corporation, 1998. All rights reserved. Creating an Index • Improve the speed of query access on the ENAME column in the EMP table. SQL> CREATE INDEX emp_ename_idx 2 ON emp(ename); Index created. CREATE INDEX index ON table (column[, column]...); • Create an index on one or more columns.
  • 17. 13-18 Copyright  Oracle Corporation, 1998. All rights reserved. When to Create an Index • The column is used frequently in the WHERE clause or in a join condition. • The column contains a wide range of values. • The column contains a large number of null values. • Two or more columns are frequently used together in a WHERE clause or a join condition. • The table is large and most queries are expected to retrieve less than 2–4% of the rows.
  • 18. 13-19 Copyright  Oracle Corporation, 1998. All rights reserved. When Not to Create an Index • The table is small. • The columns are not often used as a condition in the query. • Most queries are expected to retrieve more than 2–4% of the rows. • The table is updated frequently.
  • 19. 13-20 Copyright  Oracle Corporation, 1998. All rights reserved. Confirming Indexes • The USER_INDEXES data dictionary view contains the name of the index and its uniqueness. • The USER_IND_COLUMNS view contains the index name, the table name, and the column name. SQL> SELECT ic.index_name, ic.column_name, 2 ic.column_position col_pos,ix.uniqueness 3 FROM user_indexes ix, user_ind_columns ic 4 WHERE ic.index_name = ix.index_name 5 AND ic.table_name = 'EMP';
  • 20. 13-21 Copyright  Oracle Corporation, 1998. All rights reserved. Removing an Index • Remove an index from the data dictionary. • Remove the EMP_ENAME_IDX index from the data dictionary. • To drop an index, you must be the owner of the index or have the DROP ANY INDEX privilege. SQL> DROP INDEX emp_ename_idx; Index dropped. SQL> DROP INDEX index;
  • 21. 13-22 Copyright  Oracle Corporation, 1998. All rights reserved. Synonyms Simplify access to objects by creating a synonym (another name for an object). • Refer to a table owned by another user. • Shorten lengthy object names. CREATE [PUBLIC] SYNONYM synonym FOR object;
  • 22. 13-23 Copyright  Oracle Corporation, 1998. All rights reserved. Creating and Removing Synonyms SQL> CREATE SYNONYM d_sum 2 FOR dept_sum_vu; Synonym Created. SQL> DROP SYNONYM d_sum; Synonym dropped. • Create a shortened name for the DEPT_SUM_VU view. • Drop a synonym.
  • 23. 13-24 Copyright  Oracle Corporation, 1998. All rights reserved. Summary • Automatically generate sequence numbers by using a sequence generator. • View sequence information in the USER_SEQUENCES data dictionary table. • Create indexes to improve query retrieval speed. • View index information in the USER_INDEXES dictionary table. • Use synonyms to provide alternative names for objects.
  • 24. 13-25 Copyright  Oracle Corporation, 1998. All rights reserved. Practice Overview • Creating sequences • Using sequences • Creating nonunique indexes • Displaying data dictionary information about sequences and indexes • Dropping indexes