0% found this document useful (0 votes)
14 views

SQL 11 DB Objects

This document discusses various database objects in SQL and how to work with sequences. It describes what sequences are and how they are used to automatically generate unique primary key values. It provides the syntax for creating sequences and explains the different sequence options. Examples are given for creating a sequence, verifying sequence values, and using NEXTVAL and CURRVAL pseudocolumns to retrieve the next value from a sequence and obtain the current value.

Uploaded by

misterfarhan0307
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

SQL 11 DB Objects

This document discusses various database objects in SQL and how to work with sequences. It describes what sequences are and how they are used to automatically generate unique primary key values. It provides the syntax for creating sequences and explains the different sequence options. Examples are given for creating a sequence, verifying sequence values, and using NEXTVAL and CURRVAL pseudocolumns to retrieve the next value from a sequence and obtain the current value.

Uploaded by

misterfarhan0307
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

(SQL)

Other Database Objects

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1
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

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 2
Database Objects

Object Description

Table Basic unit of storage; composed of rows


and columns

View Logically represents subsets of data from


one or more tables

Sequence Generates primary key values

Index Improves the performance of some queries

Synonym Alternative name for an object

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 3
What Is a Sequence?

– Automatically generates unique numbers


– Is a sharable object
– It can have max. value up to 28 digits.
– Is typically used to create a primary key value
– Replaces application code
– Speeds up the efficiency of accessing
sequence values when cached in memory

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 4
The CREATE SEQUENCE Statement

• Define a sequence to generate sequential


numbers automatically.
CREATE
CREATE SEQUENCE
SEQUENCE sequence
sequence
[INCREMENT
[INCREMENT BYBY n]
n]
[START
[START WITH
WITH n]
n]
[{MAXVALUE
[{MAXVALUE nn || NOMAXVALUE}]
NOMAXVALUE}]
[{MINVALUE
[{MINVALUE nn || NOMINVALUE}]
NOMINVALUE}]
[{CYCLE
[{CYCLE || NOCYCLE}]
NOCYCLE}]
[{CACHE
[{CACHE nn || NOCACHE}];
NOCACHE}];

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 5
The CREATE SEQUENCE Options
• START WITH: The starting value of the sequence. It can
be of 28 or lesser digits.
• MAXVALUE: The maximum value to be generated by
the sequence. Its default value is 1027 – 1 for an
ascending sequence and 1 for a descending sequence.
• MINVALUE: The minimum value to be generated by the
sequence. Its default value is 1 for an ascending
sequence and 1027 – 1 for a descending sequence.
• CYCLE / NOCYCLE: It indicates whether the sequence
will continue upon reaching its max/min value or not.
• CACHE: It is used for faster access. Its minimum value
is 2 and default value is 20.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 6
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>
SQL> CREATE
CREATE SEQUENCE
SEQUENCE dept_deptno
dept_deptno
INCREMENT
INCREMENT BY
BY 11
START
START WITH
WITH 91
91
MAXVALUE
MAXVALUE 100
100
NOCACHE
NOCACHE
NOCYCLE;
NOCYCLE;
Sequence
Sequence created.
created.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 7
Confirming Sequences

– Verify your sequence values in the


USER_SEQUENCES data dictionary table.

SQL>
SQL> SELECT
SELECT sequence_name,
sequence_name, min_value,
min_value, max_value,
max_value,
increment_by,
increment_by, last_number
last_number
FROM
FROM user_sequences;
user_sequences;
– The LAST_NUMBER column displays the next
available sequence number.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 8
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.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 9
Using a Sequence
• Insert a new department named “MARKETING”.

SQL>
SQL> INSERT
INSERT INTO
INTO dept(deptno,
dept(deptno, dname,
dname, loc)
loc)
VALUES
VALUES (dept_deptno.NEXTVAL,‘MARKETING');
(dept_deptno.NEXTVAL,‘MARKETING');
11 row
row created.
created.

• View the current value for the DEPT_DEPTNO sequence


SQL>
SQL> SELECT
SELECT dept_deptno.CURRVAL
dept_deptno.CURRVAL
FROM
FROM dual;
dual;
– Gaps in sequence values can occur when:
• A rollback occurs
• The system crashes
• A sequence is used in another table

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 10
Modifying a Sequence

• Change the increment value, maximum value,


minimum value, cycle option, or cache option.

SQL>
SQL> ALTER
ALTER SEQUENCE
SEQUENCE dept_deptno
dept_deptno
INCREMENT
INCREMENT BY
BY 11
MAXVALUE
MAXVALUE 999999
999999
NOCACHE
NOCACHE
NOCYCLE;
NOCYCLE;
Sequence
Sequence altered.
altered.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 11
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>
SQL> DROP
DROP SEQUENCE
SEQUENCE dept_deptno;
dept_deptno;
Sequence
Sequence dropped.
dropped.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 12
What Is an Index?
– 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

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 13
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.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 14
Creating an Index
• Create an index on one or more columns.

CREATE
CREATE [UNIQUE]INDEX
[UNIQUE]INDEX index
index
ON
ON table
table (column[,
(column[, column]...);
column]...);

– Improve the speed of query access on the


ENAME column in the EMP table.

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

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 15
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.
– One 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.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 16
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.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 17
Create index on emp table
SQL> CREATE UNIQUE INDEX emp_ename_idx
ON emp(ename);

SQL>
SQL> SELECT
SELECT empno,
empno, ename,
ename, sal
sal
FROM
FROM emp
emp
WHERE
WHERE ename
ename == ‘BLAKE’;
‘BLAKE’;

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 18
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,


ic.column_position col_pos,ix.uniqueness
FROM user_indexes ix, user_ind_columns ic
WHERE ic.index_name = ix.index_name
AND ic.table_name = 'EMP';

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 19
Removing an Index
– Remove an index from the data dictionary.

SQL>
SQL> DROP
DROP INDEX
INDEX index;
index;
– Remove the EMP_ENAME_IDX index from the
data dictionary.

SQL>
SQL> DROP
DROP INDEX
INDEX emp_ename_idx;
emp_ename_idx;
Index
Index dropped.
dropped.
– To drop an index, you must be the owner of the
index or have the DROP ANY INDEX privilege.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 20
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
CREATE [PUBLIC]
[PUBLIC] SYNONYM
SYNONYM synonym
synonym
FOR
FOR object;
object;

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 21
Creating and Removing Synonyms

– Create a shortened name for the


DEPT_SUM_VU view.

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

• Drop a synonym.

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

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 22

You might also like