SQL 11 DB Objects
SQL 11 DB Objects
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1
Objectives
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 2
Database Objects
Object Description
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 3
What Is a Sequence?
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 4
The CREATE SEQUENCE Statement
© 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
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
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
© 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.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 10
Modifying a Sequence
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
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?
© 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]...);
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
© 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.
© 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
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
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