You are on page 1of 108

Revision no.

: PPT/2K403/02
Schema Management in Oracle 9i
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
2
Types of SQL Statements in Oracle
System Control Statements
Session Control
Data Manipulation
Transaction Control
Data Definition
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
3
System Control Statements
The system control statement alter system is use to alter the
properties of running database instance.
Example, the alter system statement is use to modify certain
initialization parameters such as the shared pool component
of the SGA.
At present, the alter system command is the only system
control statement in Oracle.
Example
SQL> alter system kill session 25,9192;
Session killed
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
4
Session Control Statements
Session control statements dynamically alter the properties of
an individual users session.
Example, you can use alter session set sql trace = true statement
to trace your session SQL statement alone.
Common session control statements include the alter session
and set role commands.
Example
SQL> alter session set nls_date_format = MM-DD-YYYY
HH:MI:SS;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
5
Data Manipulation Statements
The DML statements are the statements that either query or
manipulate data in a table.
Four important DML Statements are :
Select, Insert, Update and Delete
Other DML statements are :
call, lock table, explain plan and merge.
Other DML statements facilitate the execution of the four basic
DML statements.
Example, the merge statement deals with conditional inserts
and deletes, and the lock table statement is used to prevent
other transactions from modifying the same data while a
transaction is still running.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
6
Transaction Control Statements
The transaction control statements are use to control the
changes made when you issue data manipulation SQL
statement such as insert, update and delete.
This rarely used statement denotes the start of transaction. Its
mostly used for specifying a particular rollback segment for use by
a transaction.
Set transaction
This statement allows flexibility in your transactions, helping you
set intermediate points in the transaction to which you can roll back
or undo your transactions.
Save point
When it follows one or more DML statements, this statement will
undo the statements made by the preceding statements.
Rollback
When it follows a set of DML statements, this statement will make
the changes permanent.
Commit
Definition Statement
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
7
Data Definition Statements
DDL statements enable you to define the database.
DDL statements define the structure of the various schema
objects in Oracle.
DDL statements can also alter the structure of the objects and
drop the objects from the database.
The following list presents some of the main uses of the DDL
statements.
Create, alter and drop tables, indexes, procedure, functions and
packages.
Creating and managing users, tablespaces of the database.
Granting and revoking privileges on objects to users.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
8
Oracle Schema Management
A schema is defined as a logical collection of objects, although
it is used mostly as a synonym for a user.
An accounting schema within a company database would have
all the tables and code pertaining to the accounting
department.
Type of schema objects:
Tables, Indexes, Views
Materialized views
Procedures, functions, and packages.
Object tables and object types.
Database triggers
Database Links
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
9
Whats the Dual Table?
The dual table belongs to the sys schema and is created
automatically when the data dictionary is created.
The dual table has one column called dummy and one row.
The dual table enables you to use the Oracle select command
to compute a constant expression.
The dual table serves as a catchall table for expression.
Example
SQL> select 9 * 234 from dual;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
10
Managing Tablespaces
When you create a database, Oracle creates the SYSTEM
tablespace.
All the dictionary objects are stored in this tablespace.
The data files you specify when you create the database are
assigned to the SYSTEM tablespace.
You can add more space to the SYSTEM tablespace after you
create the database by adding more data files or by increasing
the size of the data files.
The PL/SQL program units created in the database are also
stored in the SYSTEM tablespace.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
11
Managing Tablespaces(contd.)
Oracle allocates space to an object in a tablespace, it is
allocated in chunks of contiguous database blocks known as
extents.
Each object is allocated a segment, which has one or more
extents.
Oracle maintains the extent information such as extents free,
extent size, extents allocated, and so on either in the data
dictionary or in the tablespace itself.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
12
Managing Tablespaces(contd.)
It you store the extent management information in the
dictionary for a tablespace, that tablespace is called a
dictionary-managed tablespace.
Whenever an extent is allocated or freed, the information is
updated in the corresponding dictionary tables.
Such updates also generate undo information.
If the storage management information is in the tablespace
itself, it is called locally managed tablespace.
They manage extents by referring to the bitmaps kept in each
data file of a tablespace for all the block within that data file.
Each bit corresponds to a block or a group of blocks.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
13
Managing Tablespaces(contd.)
When an extent is allocated or freed fro reuse, Oracle changes
the bitmap values to show the new status of the blocks.
These changes do not generate rollback information because
they do not generate rollback information because they do not
update tables in the data dictionary.
To create the tablespace use the CREATE TABLESPACE
statement.
Use ALTER TABLESPACE statement to modify the tablespace.
Use DROP TABLESPACE statement to delete the tablespace.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
14
Creating a Tablespace
In dictionary-managed tablespaces, all extent information is
stored in the data dictionary.
This statement creates a tablespace name APP_data, the data
file specified is created with size of 100MB.
CREATE TABLESPACE app_data
DATAFILE /DISK4/app01.dbf SIZE 100M,
EXTENT MANAGEMENT DICTIONARY;
CREATE TABLESPACE app_data
DATAFILE /DISK4/app01.dbf SIZE 100M,
EXTENT MANAGEMENT DICTIONARY;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
15
Creating Tablespaces
The following statement creates a tablespace using all optional
clauses.
CREATE TABLESPACE app_data
DATAFILE /DISK4/app01.dbf SIZE 100M,
Default storage
( initial 256K next 256K minextents 2 pctincrease 0 Maxextents 4096)
Blocksize 4K
Minumum extent 256K
Logging
Online
Permanent
Extent Management Dictionary
Segment Space Management Manual
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
16
Creating Tablespaces( contd.)
DEFAULT STORAGE
Specifies the default storage parameters for new objects that are
created in the tablespace.
Specify defaults parameters within parentheses
No parameter is mandatory, but if you specify default the
DEFAULT STORAGE clause, you must specify at least one
parameter inside the parentheses.
BLOCKSIZE
Specifies the block size that is used for the objects created in the
tablespace.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
17
Creating Tablespaces( contd.)
INITIAL
Specifies the size of the objects first extent.
NEXT
Specifies the size of the segments next and successive extents.
The size is specified in bytes ( K or M )
The defualt value of Initial and Next is 5 database blocks.
The minimum value of Initial is 3 database block for locally
managed tablespaces and 2 database block for dictionary-
managed tablespaces.
Next is 1 database block.
Even if you specify sizes smaller than these values, Oracle
allocates the minimum sizes when creating segments in the
tablespace.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
18
Creating Tablespaces( contd.)
PCTINCREASE
Specifies how much the third and subsequent extents grow over
the preceding extent.
The default value is 50, means that each subsequent extent is 50
percent larger than the preceding extent.
The minimum value is 0, meaning all extents after the first are the
same size.
Example if the storage parameters are (INBITAIL 1M NEXT 2M
PCTINCREASE 0), the extent sizes are 1MB, 2MB, 2MB, 2MB, and
so on.
If PCTINCREASE is 50, the extent sizes are 1MB, 2MB, 3MB,
4.5MB, 6.75MB, and so on
The actual NEXT extent size is rounded to a multiple of the block
size.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
19
Creating Tablespaces( contd.)
MINEXTENTS
Specifies the total number of extents allocated to the segment at
the time of creation.
The default and minimum value is 1.
When you specify MINEXTENTS as more than 1, the extent sizes
are calculated based on NEXT and PCTINCREASE.
MAXEXTENTS
Specifies the maximum number of extents that can be allocated to
a segment.
You can specify an integer or UNLIMITED.
The minimum value is 1.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
20
Creating Tablespaces( contd.)
MINIMUM EXTENT
Specifies that the extent size are a multiple of size specified.
The INITIAL and NEXT extent sizes you specify should be a
multiple of MINIMUM EXTENT.
LOGGING
Specifies that the DDL operations and direct-load INSERT are
recorded in the redo log files.
LOGGING is default, an can be omitted.
When you specify NOLOGGING, data is modified with minimal
logging and hence the commands complete faster.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
21
Creating Tablespaces( contd.)
ONLINE
Specifies that the tablespace be created online or available as
soon as it is created.
Online is default and hence can be omitted.
PERMANENT
Specifies whether the tablespace is to be used to create
permanent objects such as tables, indexes.
PERMANENT is default.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
22
Creating Tablespaces( contd.)
EXTENT MANAGEMENT
Until Oracle 9i, dictionary managed tablespaces were the default.
In Oracle 9i, to create a dictionary-managed tablespace, you need
to explicitly specify the EXTENT MANAGEMENT DICTIONARY
clause.
If omitted, ORACLE creates the tablespace as locally managed.
SEGMENT SPACE MANAGEMENT
This clause is applicable only to locally managed tablespaces.
The valid values are MANUAL and AUTO.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
23
Locally Managed Tablespaces
Reduced recursive space management
Reduced contention on data dictionary tables
No rollback generated ???
No coalescing required
CREATE TABLESPACE user_data
DATAFILE /u1/user_data.dbf SIZE 500M
EXTENT MANAGEMENT LOCAL
UNIFORM SIZE 10M;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
24
Temporary Tablespace
Used for sort operations
Cannot contain any permanent objects
CREATE TABLESPACE sort
DATAFILE /DISK2/sort01.dbf SIZE 50M
MINIMUM EXTENT 1M
DEFAULT STORAGE (INITIAL 2M NEXT 2M MAXEXTENTS 500
PCTINCREASE 0)
TEMPORARY;
CREATE TABLESPACE sort
DATAFILE /DISK2/sort01.dbf SIZE 50M
MINIMUM EXTENT 1M
DEFAULT STORAGE (INITIAL 2M NEXT 2M MAXEXTENTS 500
PCTINCREASE 0)
TEMPORARY;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
25
Undo Tablespace
Oracle 9i can manage undo information automatically.
For automatic undo management, you must have one undo
tablespace.
The undo tablespace can be created using CREATE UNDO
TABLESPACE statement.
When creating undo tablespace, you specify only the EXTENT
MANAGEMENT LOCAL and DATAFILE clause.
Create undo tablespace undo_tbs
datafile /opt/oracle/mydb/undo_tbs01.dbf size 500M;
You can create undo tablespace when creating a database
using the UNDO TABLESPACE clause of the CREATE
DATABASE statement.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
26
Adding Data Files to a Tablespace
ALTER TABLESPACE app_data
ADD DATAFILE
/DISK5/app03.dbf SIZE 200M;
ALTER TABLESPACE app_data
ADD DATAFILE
/DISK5/app03.dbf SIZE 200M;
Tablespace APP_DATA
Tablespace APP_DATA
app03.dbf
2M
app02.dbf
1M
app01.dbf
1M
Example
Example
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
27
Enabling Automatic Extension of Data Files

Example
Example
ALTER TABLESPACE app_data
ADD DATAFILE
/DISK6/app04.dbf SIZE 200M
AUTOEXTEND ON NEXT 10M
MAXSIZE 500M;
ALTER TABLESPACE app_data
ADD DATAFILE
/DISK6/app04.dbf SIZE 200M
AUTOEXTEND ON NEXT 10M
MAXSIZE 500M;
Tablespace APP_ DATA Tablespace APP_ DATA
app04.dbf
2M
app03.dbf
2M
app01.dbf
1M
app02.dbf
1M
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
28
ALTER DATABASE DATAFILE /DISK5/app02.dbf RESIZE
200M;
Example
Changing the Size of Data Files Manually
1M
Tablespace APP_DATA Tablespace APP_DATA
app02.dbf
1M
app01.dbf
1M
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
29
Changing the Storage Settings
Example
ALTER TABLESPACE app_data
MINIMUM EXTENT 2M;
ALTER TABLESPACE app_data
DEFAULT STORAGE
(INITIAL 2M NEXT 2M
MAXEXTENTS 999);
ALTER TABLESPACE app_data
MINIMUM EXTENT 2M;
ALTER TABLESPACE app_data
DEFAULT STORAGE
(INITIAL 2M NEXT 2M
MAXEXTENTS 999);
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
30
OFFLINE Status
The tablespace that is offline is not available for data access.
The SYSTEM tablespace and any tablespace with active
rollback segments cannot be taken offline.
Example
ALTER TABLESPACE app_data OFFLINE;
ALTER TABLESPACE app_data OFFLINE;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
31
Moving Data Files: ALTER TABLESPACE
The tablespace APP_DATA must be offline.
The target data files must exist.
Example
ALTER TABLESPACE app_data RENAME DATAFILE
/DISK4/app01.dbf TO /DISK5/app01.dbf;
ALTER TABLESPACE app_data RENAME DATAFILE
/DISK4/app01.dbf TO /DISK5/app01.dbf;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
32
Moving Data Files: ALTER TABLESPACE ( contd. )
The database must be mounted.
The target data file must exist.
Example
ALTER DATABASE RENAME FILE
/DISK1/system01.dbf TO /DISK2/system01.dbf;
ALTER DATABASE RENAME FILE
/DISK1/system01.dbf TO /DISK2/system01.dbf;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
33
The READ-ONLY Tablespace Status
Example
The tablespace APP_DATA is only available for read
operations.
Objects can be dropped
ALTER TABLESPACE app_data READ ONLY;
ALTER TABLESPACE app_data READ ONLY;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
34
Making Tablespace Read-Only
The tablespace must be online.
No active transactions are allowed (prior to release 8i)
Oracle 8i and higher version allows current transaction to
complete.
The tablespace must not contain active rollback segments.
The tablespace must not currently be involved in an online
backup.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
35
Dropping Tablespaces
The following statement removes the APP_DATA tablespace
and all its contents.
Example
DROP TABLESPACE app_data
INCLUDING CONTENTS;
DROP TABLESPACE app_data
INCLUDING CONTENTS;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
36
Obtaining Tablespace Information
DBA_TABLESPACES (V$TABLESPACE)
TABLESPACE_NAME
NEXT_EXTENT
MAX_EXTENTS
PCT_INCREASE
MIN_EXTLEN
STATUS
CONTENTS
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
37
Oracle Managed Files
The Oracle 9i database enables you to use Oracle Managed
Files (OMF), which enable Oracle to manage the creation and
deletion of data files.
The db_create_file_dest and db_create_online_log_dest_n
parameter specifies the locaton for OMF data files.
OMF limits the file size to 100MB.
OMF files cant be used on raw devices.
All OMF files for data files have to be created in one directory.
Oracle itself recommends OMF files for small and test
databases.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
38
Views for Managing Tablespaces
DBA_DATA_FILES
DBA_TABLESPACES
DBA_FREE_SPACE
DBA_SEGMENTS
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
39
Obtaining Data File and Tablespace
Information from the Control File
V$DATAFILE
- TS#
- NAME
- FILE#
- RFILE#
- STATUS
- ENABLED
- BYTES
- CREATE_BYTES
V$TABLESPACE
- TS#
- NAME
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
40
Guidelines
Use multiple tablespaces.
Specify storage parameters for tablespaces.
Assign tablespace quotas to users.
Use locally managed tablespaces
Can have 1023 data files/tablespace
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
41
Creating and Managing Tables
Create table employee
( empno number(5) primary key,
ename varchar2(15) not null )
Tablespace finan_data01 ;
The employee table is created within the tablespace
finan_data01.
The finan_data01 tablespace is locally managed with segment
space management auto, hence the employee table inherits all
the default storage parameters from the finan_data01
tablespace.
The use of the locally managed tablespace frees you from
having to specify a large number of parameters at table
creation time.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
42
Creating and Managing Tables (contd.)
Specifying Explicit Storage Parameters for a Table
Create table employee
( empno number(5) primary key,
ename varchar2(15) not null )
Pctfree 10
Pctused 40
Tablespace users
Storage ( initial 50k next 50k maxextents 10 pctincrease 25 );
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
43
Creating and Managing Tables (contd.)
Altering Tables
Alter table emp storage ( initial 2m next extent 2m, minextent 2
maxextents 40);
Alter table emp move tablespace users;
Adding a Column to a table
Alter table emp add (retired char(1));
Dropping a Column from a table
Alter table emp drop (retired);
Renaming a table Column
Alter table emp rename column retired to non-active;
Renaming a table
Alter table employee rename to emp;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
44
Creating and Managing Tables (contd.)
Removing all the data from a table
To remove al the rows from a table, you need to use the truncate
command.
Truncate is a DDL command, and therefore it cant be undone by
using the rollback command.
Delete is a DML command and hence write all changes to the
rollback segments which takes longer time to execute.
Truncate table test;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
45
Creating and Managing Tables (contd.)
Creating a new table with the CTAS option
Create table employee_new as
select * from employees;
If the table has millions of rows then you can use parallel and
nologging options with CTAS.
The parallel option enables you to do your data loading in
parallel by several processes.
The nologging option instructs Oracle not to bother logging
the changes to the redo log files and rollback segments.
Create table employee_new
as select * from employees
parallel degree 4
nologging;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
46
Special Oracle Tables
Temporary Tables
Index-Organized Tables
External Tables
Partitioned Tables
Range Partitioning
Hash Partitioning
List Partitioning
Composite Partitioning
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
47
Clusters
Clusters is use to organize the storing of tables that have
common columns and are usually used together in the same
data block.
The goal is to reduce disk I/O and thereby increase access
speed when you join related tables.
Clusters will reduce the performance of the insert statements,
since more blocks are needed to store clustered tables data
since the data of multiple tables needs to be stored in each
block.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
48
Distribution of Rows Within a Table
Cluster Index-organized
table
Table
Random
Ordering of Rows
Grouped Ordered
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
49
Clusters
Clustered ORD and
ITEM tables
Cluster Key
(ORD_NO)
101 ORD_DT CUST_CD
05-JAN-97 R01
PROD QTY
A4102 20
A5675 19
W0824 10
102 ORD_DT CUST_CD
07-JAN-97 N45
PROD QTY
A2091 11
G7830 20
N9587 26
Unclustered ORD and
ITEM tables
ORD_NO PROD QTY ...
----- ------ ------
101 A4102 20
102 A2091 11
102 G7830 20
102 N9587 26
101 A5675 19
101 W0824 10
ORD_NO ORD_DT CUST_CD
------ ------ ------
101 05-JAN-97 R01
102 07-JAN-97 N45
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
50
Cluster Types
Index cluster Hash cluster
Hash function Hash function
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
51
Creating Index Clusters
Create a cluster.
Create a cluster index.
CREATE CLUSTER scott.ord_clu
(ord_no NUMBER(3))
SIZE 200 TABLESPACE DATA01
STORAGE(INITIAL 5M NEXT 5M PCTINCREASE 0);
CREATE CLUSTER scott.ord_clu
(ord_no NUMBER(3))
SIZE 200 TABLESPACE DATA01
STORAGE(INITIAL 5M NEXT 5M PCTINCREASE 0);
CREATE INDEX scott.ord_clu_idx
ON CLUSTER scott.ord_clu
TABLESPACE INDX01
STORAGE(INITIAL 1M NEXT 1M PCTINCREASE 0);
CREATE INDEX scott.ord_clu_idx
ON CLUSTER scott.ord_clu
TABLESPACE INDX01
STORAGE(INITIAL 1M NEXT 1M PCTINCREASE 0);
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
52
Creating Index Clusters
Create tables in the cluster.
CREATE TABLE scott.ord
(ord_no NUMBER(3) CONSTRAINT ord_pk PRIMARY KEY,
ord_dt DATE, cust_cd VARCHAR2(3))
CLUSTER scott.ord_clu(ord_no);
CREATE TABLE scott.ord
(ord_no NUMBER(3) CONSTRAINT ord_pk PRIMARY KEY,
ord_dt DATE, cust_cd VARCHAR2(3))
CLUSTER scott.ord_clu(ord_no);
CREATE TABLE scott.item
(ord_no NUMBER(3) CONSTRAINT item_ord_fk
REFERENCES scott.ord,
prod VARCHAR2(5), qty NUMBER(3),
CONSTRAINT item_pk PRIMARY KEY(ord_no,prod))
CLUSTER scott.ord_clu(ord_no);
CREATE TABLE scott.item
(ord_no NUMBER(3) CONSTRAINT item_ord_fk
REFERENCES scott.ord,
prod VARCHAR2(5), qty NUMBER(3),
CONSTRAINT item_pk PRIMARY KEY(ord_no,prod))
CLUSTER scott.ord_clu(ord_no);
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
53
Creating Hash Clusters
Create a cluster.

Create tables in a cluster.


CREATE CLUSTER scott.off_clu
(country VARCHAR2(2),postcode VARCHAR2(8))
SIZE 500 HASHKEYS 1000
TABLESPACE DATA01
STORAGE(INITIAL 5M NEXT 5M PCTINCREASE 0);
CREATE CLUSTER scott.off_clu
(country VARCHAR2(2),postcode VARCHAR2(8))
SIZE 500 HASHKEYS 1000
TABLESPACE DATA01
STORAGE(INITIAL 5M NEXT 5M PCTINCREASE 0);
CREATE TABLE scott.office(
office_cd NUMBER(3), cost_ctr NUMBER(3), country
VARCHAR2(2), postcode VARCHAR2(8))
CLUSTER scott.off_clu(country,postcode);
CREATE TABLE scott.office(
office_cd NUMBER(3), cost_ctr NUMBER(3), country
VARCHAR2(2), postcode VARCHAR2(8))
CLUSTER scott.off_clu(country,postcode);
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
54
Defining SIZE for Clusters
Defines space used by all the rows for a given key.
Used in both types of cluster to estimate:
The maximum number of key values per block for an index cluster
The exact number of key values per block for a hash cluster
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
55
Parameters Specific to Hash Clusters
HASHKEYS: Number of key values
HASH IS: Optional user-defined hash function
Preallocated blocks
Key 2
Key 1
Key 12
Key 11
Key 3
Overflow block
Key 22
Key 21
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
56
ALTER CLUSTER scott.ord_clu
SIZE 300 STORAGE (NEXT 2M);
ALTER CLUSTER scott.ord_clu
SIZE 300 STORAGE (NEXT 2M);
Altering Clusters
Change storage and block space usage parameters
Change SIZE for index clusters
Allocate and deallocate space
SIZE, HASH IS, or HASHKEYS cannot be altered for hash
clusters
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
57
DROP CLUSTER scott.ord_clu
INCLUDING TABLES;
DROP CLUSTER scott.ord_clu
INCLUDING TABLES;
DROP TABLE scott.ord;
DROP TABLE scott.item;
DROP CLUSTER scott.ord_clu;
DROP TABLE scott.ord;
DROP TABLE scott.item;
DROP CLUSTER scott.ord_clu;
Dropping Clusters
Use INCLUDING TABLES to drop tables and cluster
or drop tables before dropping cluster.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
58
Retrieving Cluster Information
DBA_CLU_COLUMNS
OWNER
CLUSTER_NAME
CLU_COLUMN_NAME
TABLE_NAME
TAB_COLUMN_NAME
DBA_CLU_COLUMNS
OWNER
CLUSTER_NAME
CLU_COLUMN_NAME
TABLE_NAME
TAB_COLUMN_NAME
DBA_CLUSTER_
HASH_EXPRESSIONS
OWNER
CLUSTER_NAME
HASH_EXPRESSION
DBA_CLUSTER_
HASH_EXPRESSIONS
OWNER
CLUSTER_NAME
HASH_EXPRESSION
DBA_CLUSTERS
OWNER
CLUSTER_NAME
TABLESPACE_NAME
KEY_SIZE
CLUSTER_TYPE
FUNCTION
HASHKEYS
DBA_CLUSTERS
OWNER
CLUSTER_NAME
TABLESPACE_NAME
KEY_SIZE
CLUSTER_TYPE
FUNCTION
HASHKEYS
DBA_TAB_COLUMNS
OWNER
TABLE_NAME
COLUMN_NAME
DATA_TYPE
DATA_LENGTH
DATA_PRECISION
DATA_SCALE
DBA_TAB_COLUMNS
OWNER
TABLE_NAME
COLUMN_NAME
DATA_TYPE
DATA_LENGTH
DATA_PRECISION
DATA_SCALE
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
59
Index-Organized Tables
Indexed access on table
ROWID
Accessing index-organized table
Non-key columns
Key column
Row header
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
60
Index-Organized Tables ( IOTs )
IOTs are somewhat of a hybrid, since they possess features of
both indexes and tables.
IOTs are tables in which the data is stored in the form of a B-
tree index structure.
IOTs contain the primary key and no indexed columns.
When you update the table, it is the index structure that really
gets updated.
Data access is much faster because you only have to perform
one I/O to access the index/table.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
61
Index-Organized Tables ( IOTs ) ( contd. )
The actual row data, and not merely the ROWID, is held in the
index leaf block along with the index leaf block along with the
indexed cloumn value.
IOTs are especially use for cases where you need to query
based on the values of the primary key.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
62
Creating Index-Organized Table
Create table employee_new
( employee_id number, dept_id number, name varchar2(30),
address varchar2(120), city varchar2(30), state char(2),
phone_number number, constraint pk_employee_new
primary_key (employee_id))
ORGANIZATION INDEX TABLESPACE EMPINDEX_01
PCTTHRESHOLD 25
Overflow tablespace overflow tables;
Create table employee_new
( employee_id number, dept_id number, name varchar2(30),
address varchar2(120), city varchar2(30), state char(2),
phone_number number, constraint pk_employee_new
primary_key (employee_id))
ORGANIZATION INDEX TABLESPACE EMPINDEX_01
PCTTHRESHOLD 25
Overflow tablespace overflow tables;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
63
Creating an Index-Organized Table ( contd )
The key phrase organization index indicates that this table is
an IOT rather than a regular B-tree table.
The pctthreshold keyword indicates the percentage of space
reserved in the index blocks for the IOT employee_new.
Any part of a row in the table that does not fir the 25
percentage threshold value in each data block would be saved
in an overflow area.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
64
Row Overflow
Segment = PK Name
Type = Index
Segment = SYS_IOT_OVER_n
Type=Table
Row bigger than
PCTTHRESHOLD
Block
Rows within PCTTHRESHOLD
IOT tablespace
Overflow tablespace
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
65
Retrieving IOT Information from Data
Dictionary
DBA_INDEXES
OWNER
TABLE_NAME
INDEX_NAME
INDEX_TYPE
PCT_THRESHOLD
INCLUDE_COLUMN
DBA_INDEXES
OWNER
TABLE_NAME
INDEX_NAME
INDEX_TYPE
PCT_THRESHOLD
INCLUDE_COLUMN
DBA_TABLES
OWNER
TABLE_NAME
IOT_TYPE
IOT_NAME
TABLESPACE_NAME
DBA_TABLES
OWNER
TABLE_NAME
IOT_TYPE
IOT_NAME
TABLESPACE_NAME
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
66
Index-Organized Tables Compared with
Regular Tables
Physical ROWIDs Logical ROWIDs
Uniquely identified by ROWID Uniquely identified by primary key
Unique contraints allowed Unique constraints not allowed
Can contain LONG and LOB data Cant contain LONG data
Allowed in table clusters Not allowed in table clusters
Small space requirements Larger space requirements
Fast data access Slow data access
IOTS REGULAR ORACLE TABLES
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
67
Oracle Indexes
Oracle Indexes provide speedy access to table rows by sorting
specified column values and looking up the rows pertaining to
the specified index row value.
Indexes enable you to find a row with a certain column value
without your having to look at more than a small fraction of the
total rows in a table.
If you dont have an index on table, you have to read all the
tables data to find what youre looking for.
Using an index involves a tradeoff between speedy retrieval of
query results and slower updates and insertions.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
68
Classification of Indexes
Logical
Single column or concatenated
Unique or nonunique
Physical
Partitioned or nonpartitioned
B-tree or bitmap
Normal or reverse key (B-tree only)
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
69
B-Tree Index
Index entry header
Key column length
Key column value
ROWID
Root
Branch
Leaf
Index entry
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
70
Reverse Key Index
KEY ROWID
EMPNO (BLOCK# ROW# FILE#)
----- -------------------
1257 0000000F.0002.0001
2877 0000000F.0006.0001
4567 0000000F.0004.0001
6657 0000000F.0003.0001
8967 0000000F.0005.0001
9637 0000000F.0001.0001
9947 0000000F.0000.0001
... ...
... ...
Index on EMP (EMPNO)
EMP table
EMPNO ENAME JOB ...
----- ----- --------
7499 ALLEN SALESMAN
7369 SMITH CLERK
7521 WARD SALESMAN ...
7566 JONES MANAGER
7654 MARTIN SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
... ... ... ...
... ... ... ...
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
71
Bitmap Index
<Blue, 10.0.3, 12.8.3, 1000100100010010100>
<Green, 10.0.3, 12.8.3, 0001010000100100000>
<Red, 10.0.3, 12.8.3, 0100000011000001001>
<Yellow, 10.0.3, 12.8.3, 0010001000001000010>
key key
start start
ROWID ROWID
end end
ROWID ROWID bitmap bitmap
Table
Index
Block 10
Block 11
Block 12
File 3
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
72
Comparing B-Tree and
Bitmap Indexes
B-tree
Suitable for high-cardinality
columns
Updates on keys relatively
inexpensive
Inefficient for queries
using OR predicates
Useful for OLTP
B-tree
Suitable for high-cardinality
columns
Updates on keys relatively
inexpensive
Inefficient for queries
using OR predicates
Useful for OLTP
Bitmap
Suitable for low-cardinality
columns
Updates to key columns very
expensive
Efficient for queries
using OR predicates
Useful for DSS
Bitmap
Suitable for low-cardinality
columns
Updates to key columns very
expensive
Efficient for queries
using OR predicates
Useful for DSS
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
73
Creating Normal B-Tree Indexes
CREATE INDEX scott.emp_lname_idx
ON scott.employees(last_name)
PCTFREE 30
STORAGE(INITIAL 200K NEXT 200K
PCTINCREASE 0 MAXEXTENTS 50)
TABLESPACE indx01;
CREATE INDEX scott.emp_lname_idx
ON scott.employees(last_name)
PCTFREE 30
STORAGE(INITIAL 200K NEXT 200K
PCTINCREASE 0 MAXEXTENTS 50)
TABLESPACE indx01;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
74
Creating Indexes: Guidelines
Balance query and DML needs
Place in separate tablespace
Use uniform extent sizes: multiples of five blocks or MINIMUM
EXTENT size for tablespace
Consider NOLOGGING for large indexes
Set high PCTFREE if new key values are likely to be within the
current range
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
75
Creating Reverse Key Indexes
CREATE UNIQUE INDEX scott.ord_ord_no_idx
ON scott.ord(ord_no) REVERSE
PCTFREE 30
STORAGE(INITIAL 200K NEXT 200K
PCTINCREASE 0 MAXEXTENTS 50)
TABLESPACE indx01;
CREATE UNIQUE INDEX scott.ord_ord_no_idx
ON scott.ord(ord_no) REVERSE
PCTFREE 30
STORAGE(INITIAL 200K NEXT 200K
PCTINCREASE 0 MAXEXTENTS 50)
TABLESPACE indx01;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
76
Creating Bitmap Indexes
CREATE BITMAP INDEX scott.ord_region_id_idx
ON scott.ord(region_id)
PCTFREE 30
STORAGE(INITIAL 200K NEXT 200K
PCTINCREASE 0 MAXEXTENTS 50)
TABLESPACE indx01;
CREATE BITMAP INDEX scott.ord_region_id_idx
ON scott.ord(region_id)
PCTFREE 30
STORAGE(INITIAL 200K NEXT 200K
PCTINCREASE 0 MAXEXTENTS 50)
TABLESPACE indx01;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
77
Changing Storage Parameters for Indexes
ALTER INDEX scott.emp_lname_idx
STORAGE(NEXT 400K
MAXEXTENTS 100);
ALTER INDEX scott.emp_lname_idx
STORAGE(NEXT 400K
MAXEXTENTS 100);
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
78
Allocating and Deallocating Index Space
ALTER INDEX scott.ord_region_id_idx
ALLOCATE EXTENT (SIZE 200K
DATAFILE /DISK6/indx01.dbf);
ALTER INDEX scott.ord_region_id_idx
ALLOCATE EXTENT (SIZE 200K
DATAFILE /DISK6/indx01.dbf);
ALTER INDEX scott.ord_ord_no_idx
DEALLOCATE UNUSED;
ALTER INDEX scott.ord_ord_no_idx
DEALLOCATE UNUSED;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
79
Rebuilding Indexes
Use this command to:
Move an index to a different tablespace
Improve space utilization by removing deleted entries
Change a reverse key index to a normal B-tree index and vice
versa
ALTER INDEX scott.ord_region_id_idx REBUILD
TABLESPACE indx02;
ALTER INDEX scott.ord_region_id_idx REBUILD
TABLESPACE indx02;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
80
Checking Index Validity
ANALYZE INDEX scott.ord_region_id_idx VALIDATE
STRUCTURE;
ANALYZE INDEX scott.ord_region_id_idx VALIDATE
STRUCTURE;
INDEX_STATS
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
81
Dropping Indexes
Drop and re-create an index before bulk loads.
Drop indexes that are infrequently needed and build them
when necessary.
Drop and recreate invalid indexes.
DROP INDEX scott.dept_dname_idx;
DROP INDEX scott.dept_dname_idx;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
82
Obtaining Index Information
DBA_INDEXES
OWNER
INDEX_NAME
INDEX_TYPE
TABLE_OWNER
TABLE_NAME
UNIQUENESS
TABLESPACE_NAME
LOGGING
STATUS
DBA_INDEXES
OWNER
INDEX_NAME
INDEX_TYPE
TABLE_OWNER
TABLE_NAME
UNIQUENESS
TABLESPACE_NAME
LOGGING
STATUS
DBA_IND_COLUMNS
INDEX_OWNER
INDEX_NAME
TABLE_OWNER
TABLE_NAME
COLUMN_NAME
COLUMN_POSITION
COLUMN_LENGTH
DBA_IND_COLUMNS
INDEX_OWNER
INDEX_NAME
TABLE_OWNER
TABLE_NAME
COLUMN_NAME
COLUMN_POSITION
COLUMN_LENGTH
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
83
Data Integrity
Application
code
Integrity
constraint
Database
trigger
Table
Data a
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
84
Types of Constraints
Constraint
NOT NULL
UNIQUE
PRIMARY
KEY
FOREIGN
KEY
CHECK
Description
Specifies that a column cannot contain
null values
Designates a column or combination of
columns as unique
Designates a column or combination of
columns as the tables primary key
Designates a column or combination of
columns as the foreign key in a
referential integrity constraint
Specifies a condition that each row of
the table must satisfy
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
85
Constraint States
Disabled Enabled
novalidate
Enabled
validate
Existing data New data
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
86
Deferred Constraints
Check nondeferred
constraints
COMMIT
Check deferred
constraints
DML statement
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
87
Primary/Unique Key Enforcement
Do not use
index
Create
unique index
Use existing
index
Is an index
available
for use?
Yes
No
No
Yes
Constraint
deferrable?
Create nonunique
index
Yes
No
Key enabled?
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
88
Foreign Key Considerations
To
Drop parent table
Truncate parent table
Drop tablespace
containing parent
table
Avoid locks on child
table while performing
DML on parent table
Perform DML on child
table
Perform
Cascade constraints
Disable/drop foreign key
Use CASCADE CONSTRAINTS
clause
Create index on foreign key
Ensure tablespace containing
parent key index online
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
89
Database Triggers
Trigger types
INSERT or UPDATE or DELETE
BEFORE or AFTER
ROW or STATEMENT
Table
Trigger
DML action
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
90
Defining Constraints While Creating a Table
CREATE TABLE scott.employees(
empno NUMBER(4)
CONSTRAINT emp_pk PRIMARY KEY
DEFERRABLE
USING INDEX
STORAGE(INITIAL 100K NEXT 100K)
TABLESPACE indx01,
last_name VARCHAR2(30)
CONSTRAINT emp_ln_nn NOT NULL,
deptno NUMBER(2))
TABLESPACE data01;
CREATE TABLE scott.employees(
empno NUMBER(4)
CONSTRAINT emp_pk PRIMARY KEY
DEFERRABLE
USING INDEX
STORAGE(INITIAL 100K NEXT 100K)
TABLESPACE indx01,
last_name VARCHAR2(30)
CONSTRAINT emp_ln_nn NOT NULL,
deptno NUMBER(2))
TABLESPACE data01;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
91
Guidelines for Defining Constraints
Primary and unique constraints:
Place indexes in a separate tablespace
Use nonunique indexes if bulk loads are frequent
Self-referencing foreign keys:
Define or enable foreign keys after initial load
Defer constraint checking
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
92
Disabling Constraints
Disable before bulk load, especially self-referencing foreign
keys.
Disable referencing foreign keys before disabling parent keys.
Unique indexes are dropped, but nonunique indexes are
retained.
ALTER TABLE scott.departments
DISABLE CONSTRAINT dept_pk CASCADE;
ALTER TABLE scott.departments
DISABLE CONSTRAINT dept_pk CASCADE;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
93
Enabling Constraints
No locks on table
Primary/unique keys
must use nonunique
indexes
Enable
NOVALIDATE
ALTER TABLE scott.departments
ENABLE NOVALIDATE CONSTRAINT dept_pk;
ALTER TABLE scott.departments
ENABLE NOVALIDATE CONSTRAINT dept_pk;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
94
Enabling Constraints ( contd. )
Locks table
Can use unique or nonunique
indexes
Needs valid table data
Enable
VALIDATE
ALTER TABLE scott.employees
ENABLE VALIDATE CONSTRAINT emp_dept_fk;
ALTER TABLE scott.employees
ENABLE VALIDATE CONSTRAINT emp_dept_fk;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
95
Disabling and Enabling Triggers
Use ALTER TRIGGER to disable or enable one trigger.
Use ALTER TABLE to disable or enable all triggers.
ALTER TRIGGER scott.emp_conv_ln
DISABLE;
ALTER TRIGGER scott.emp_conv_ln
DISABLE;
ALTER TABLE scott.employees
ENABLE ALL TRIGGERS;
ALTER TABLE scott.employees
ENABLE ALL TRIGGERS;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
96
Dropping Constraints
Drop constraints using this command:
Drop a table and any referencing foreign key using this
command:
ALTER TABLE scott.employees
DROP CONSTRAINT emp_ln_uk;
ALTER TABLE scott.employees
DROP CONSTRAINT emp_ln_uk;
DROP TABLE departments
CASCADE CONSTRAINTS;
DROP TABLE departments
CASCADE CONSTRAINTS;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
97
Dropping Triggers
DROP TRIGGER scott.audit_dept;
DROP TRIGGER scott.audit_dept;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
98
Getting Constraint Information
DBA_CONSTRAINTS
OWNER
CONSTRAINT_NAME
CONSTRAINT_TYPE
TABLE_NAME
SEARCH_CONDITION
R_OWNER
R_CONSTRAINT_NAME
DELETE_RULE
STATUS
DEFERRABLE
DEFERRED
VALIDATED
GENERATED
BAD
LAST_CHANGE
DBA_CONS_COLUMNS
OWNER
CONSTRAINT_NAME
TABLE_NAME
COLUMN_NAME
POSITION
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
99
Getting Information on Triggers
DBA_TRIGGERS
OWNER
TRIGGER_NAME
TRIGGER_TYPE
TRIGGERING_EVENT
TABLE_OWNER
TABLE_NAME
STATUS
DESCRIPTION
TRIGGER_BODY
DBA_TRIGGER_COLS
TRIGGER_OWNER
TRIGGER_NAME
TABLE_OWNER
TABLE_NAME
COLUMN_NAME
DBA_OBJECTS
OWNER
OBJECT_NAME
OBJECT_TYPE
STATUS
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
100
Using Synonyms
Synonyms are aliases for objects in the database and are used
mainly for security purposes.
Synonyms hide the underlying objects identity and can be
either private or public, with public synonyms being accessible
by all the users in the database.
Private synonyms are part of the individual users schema, and
access rights have to be individually granted to specific user
before they can use the private synonyms
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
101
Using Synonyms ( contd. )
Oracle9i synonyms can be created for tables, views,
materialized views, and stored code such as packages and
procedures.
Synonyms are very powerful from the point of view of allowing
users access to objects that do not lie within their schemas.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
102
Creating a Synonym
Creating a public synonym for hr.employees table
Creating a private synonym for hr.location table
SQL> Connect system/manager;
SQL> CREATE PUBLIC SYNONYM employees for
hr.employees;
SQL> Connect system/manager;
SQL> CREATE PUBLIC SYNONYM employees for
hr.employees;
SQL> Connect hr/hr;
SQL> CREATE SYNONYM addresses for hr.locations;
SQL> Connect hr/hr;
SQL> CREATE SYNONYM addresses for hr.locations;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
103
Drop a Synonym
Synonyms, both private and public, are dropped in the same
manner by using the drop synonym command.
To drop public synonym
Drop public synonym employees;
To drop private synonym
Drop synonym addresses;
DBA_SYNONYMS table gives the information to manage
synonyms for database objects.
Select synonym_name, table_name from dba_synonyms where
owner = HR;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
104
Sequences
Programmatically created sequence numbers would cause
Oracle to constantly lock and unlock records holding the
maximum value of those sequences, to ensure an orderly
incrementing of the sequence.
This locking would lead users to wait serially for the next value
of the sequence to be issued to their transactions.
Oracle sequences are generated automatically internally.
This automatic generation of sequences increases database
concurrency.
Sequence are commonly used to create unique number to
generate a unique primary key for a column.
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
105
Creating Sequence
There are tow pseudo-columns called currval and nextval that
is use to query sequence values.
The currval pseudo-column provides you the current value of
the sequence and the nextval pseudo-cloumn gets you the
new or next sequence number.
SQL> Create sequene employee_seq
2 start with 10000
3 Increment by 1
4 no maxvalue
5 no cycle;
SQL> Create sequene employee_seq
2 start with 10000
3 Increment by 1
4 no maxvalue
5 no cycle;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
106
Views
A view is a specific representation of a table or set of tables.
A view does not physically exist, like regular tables, as part
of a tablespace.
Since a view is the product of a query, only the view definition
is stored in the data dictionary.
SQL> create view my_employees as
2 select emplpoyee_id, first_name, last_name, salary
3 from employees where manager_id=122;
SQL> drop view my_employees;
SQL> create view my_employees as
2 select emplpoyee_id, first_name, last_name, salary
3 from employees where manager_id=122;
SQL> drop view my_employees;
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
107
Appendix A Brief Oracle9i SQL & PL/SQL Primer
(only SQL Portion)
Revision no.: PPT/2K403/02
CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
108
Design & Published by:
CMS Institute, Design & Development Centre, CMS House, Plot No. 91, Street No.7,
MIDC, Marol, Andheri (E), Mumbai 400093, Tel: 91-22-28216511, 28329198
Email: courseware.inst@cmail.cms.co.in
www.cmsinstitute.co.in

You might also like