sql workshop::lecture 13

24
13 13 Other Database Objects

Upload: umair-amjad

Post on 11-May-2015

131 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: SQL WORKSHOP::Lecture 13

1313

Other Database ObjectsOther Database Objects

Page 2: SQL WORKSHOP::Lecture 13

13-2

ObjectivesObjectives

After completing this lesson, you should After completing this lesson, you should be able to do the following: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

After completing this lesson, you should After completing this lesson, you should be able to do the following: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

Page 3: SQL WORKSHOP::Lecture 13

13-3

Database ObjectsDatabase 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

Page 4: SQL WORKSHOP::Lecture 13

13-4

What Is a Sequence?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

• 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

Page 5: SQL WORKSHOP::Lecture 13

13-5

The CREATE SEQUENCE Statement

The CREATE SEQUENCE Statement

Define a sequence to generate sequential Define a sequence to generate sequential numbers automatically.numbers automatically.Define a sequence to generate sequential Define a sequence to generate sequential numbers automatically.numbers automatically.

CREATE SEQUENCE sequence[INCREMENT BY n][START WITH n][{MAXVALUE n | NOMAXVALUE}][{MINVALUE n | NOMINVALUE}][{CYCLE | NOCYCLE}][{CACHE n | NOCACHE}];

CREATE SEQUENCE sequence[INCREMENT BY n][START WITH n][{MAXVALUE n | NOMAXVALUE}][{MINVALUE n | NOMINVALUE}][{CYCLE | NOCYCLE}][{CACHE n | NOCACHE}];

Page 6: SQL WORKSHOP::Lecture 13

13-6

Creating a SequenceCreating a Sequence

• Create a sequence named DEPT_DEPTNO to be used for the primary key of theDEPT table.

• Do not use the CYCLE option.

• Create a sequence named DEPT_DEPTNO to be used for the primary key of theDEPT 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.Sequence created.

SQL> CREATE SEQUENCE dept_deptno 2 INCREMENT BY 1 3 START WITH 91 4 MAXVALUE 100 5 NOCACHE 6 NOCYCLE;Sequence created.Sequence created.

Page 7: SQL WORKSHOP::Lecture 13

13-7

Confirming SequencesConfirming Sequences

• Verify your sequence values in the USER_SEQUENCES data dictionary table.

• The LAST_NUMBER column displays the next available sequence number.

• 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;

SQL> SELECT sequence_name, min_value, max_value, 2 increment_by, last_number 3 FROM user_sequences;

Page 8: SQL WORKSHOP::Lecture 13

13-8

NEXTVAL and CURRVAL Pseudocolumns

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.

• 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.

Page 9: SQL WORKSHOP::Lecture 13

13-10

Using a SequenceUsing a Sequence

• Insert a new department named “MARKETING” in San Diego.

• View the current value for the DEPT_DEPTNO 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.1 row created.

SQL> INSERT INTO dept(deptno, dname, loc) 2 VALUES (dept_deptno.NEXTVAL, 3 'MARKETING', 'SAN DIEGO');1 row created.1 row created.

SQL> SELECT dept_deptno.CURRVAL 2 FROM dual;

SQL> SELECT dept_deptno.CURRVAL 2 FROM dual;

Page 10: SQL WORKSHOP::Lecture 13

13-11

Using a SequenceUsing 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.

• 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.

Page 11: SQL WORKSHOP::Lecture 13

13-12

Modifying a SequenceModifying a Sequence

Change the increment value, maximum Change the increment value, maximum value, minimum value, cycle option, or value, minimum value, cycle option, or cache option.cache option.

Change the increment value, maximum Change the increment value, maximum value, minimum value, cycle option, or value, minimum value, cycle option, or cache option.cache option.

SQL> ALTER SEQUENCE dept_deptno 2 INCREMENT BY 1 3 MAXVALUE 999999 4 NOCACHE 5 NOCYCLE;Sequence altered.Sequence altered.

SQL> ALTER SEQUENCE dept_deptno 2 INCREMENT BY 1 3 MAXVALUE 999999 4 NOCACHE 5 NOCYCLE;Sequence altered.Sequence altered.

Page 12: SQL WORKSHOP::Lecture 13

13-13

Guidelines for Modifying a Sequence

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.

• 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.

Page 13: SQL WORKSHOP::Lecture 13

13-14

Removing a SequenceRemoving a Sequence

• Remove a sequence from the data dictionary by using the DROP SEQUENCE statement.

• Once removed, the sequence can no longer be referenced.

• 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.Sequence dropped.

SQL> DROP SEQUENCE dept_deptno;Sequence dropped.Sequence dropped.

Page 14: SQL WORKSHOP::Lecture 13

13-15

What Is an Index?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

• 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

Page 15: SQL WORKSHOP::Lecture 13

13-16

How Are Indexes Created?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.

• 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.

Page 16: SQL WORKSHOP::Lecture 13

13-17

Creating an IndexCreating an Index

• Improve the speed of query access on the ENAME column in the EMP table.• 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.Index created.

SQL> CREATE INDEX emp_ename_idx 2 ON emp(ename);Index created.Index created.

CREATE INDEX indexON table (column[, column]...);

CREATE INDEX indexON table (column[, column]...);

• Create an index on one or more columns.• Create an index on one or more columns.

Page 17: SQL WORKSHOP::Lecture 13

13-18

When to Create an IndexWhen 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.

• 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.

Page 18: SQL WORKSHOP::Lecture 13

13-19

When Not to Create an IndexWhen 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.

• 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.

Page 19: SQL WORKSHOP::Lecture 13

13-20

Confirming IndexesConfirming 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.

• 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';

Page 20: SQL WORKSHOP::Lecture 13

13-21

Removing an IndexRemoving 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.

• 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.Index dropped.

SQL> DROP INDEX emp_ename_idx;Index dropped.Index dropped.

SQL> DROP INDEX index;SQL> DROP INDEX index;

Page 21: SQL WORKSHOP::Lecture 13

13-22

SynonymsSynonyms

Simplify access to objects by creating a Simplify access to objects by creating a synonym (another name for an object).synonym (another name for an object).

• Refer to a table owned by another user.

• Shorten lengthy object names.

Simplify access to objects by creating a Simplify access to objects by creating a synonym (another name for an object).synonym (another name for an object).

• Refer to a table owned by another user.

• Shorten lengthy object names.

CREATE [PUBLIC] SYNONYM synonymFOR object;

CREATE [PUBLIC] SYNONYM synonymFOR object;

Page 22: SQL WORKSHOP::Lecture 13

13-23

Creating and Removing SynonymsCreating and Removing Synonyms

SQL> CREATE SYNONYM d_sum 2 FOR dept_sum_vu;Synonym Created.Synonym Created.

SQL> CREATE SYNONYM d_sum 2 FOR dept_sum_vu;Synonym Created.Synonym Created.

SQL> DROP SYNONYM d_sum;Synonym dropped.Synonym dropped.

SQL> DROP SYNONYM d_sum;Synonym dropped.Synonym dropped.

• Create a shortened name for the DEPT_SUM_VU view.• Create a shortened name for the

DEPT_SUM_VU view.

• Drop a synonym.• Drop a synonym.

Page 23: SQL WORKSHOP::Lecture 13

13-24

SummarySummary

• 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.

• 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.

Page 24: SQL WORKSHOP::Lecture 13

13-25

Practice OverviewPractice Overview

• Creating sequences

• Using sequences

• Creating nonunique indexes

• Displaying data dictionary information about sequences and indexes

• Dropping indexes

• Creating sequences

• Using sequences

• Creating nonunique indexes

• Displaying data dictionary information about sequences and indexes

• Dropping indexes