Tải bản đầy đủ (.pdf) (34 trang)

Oracle 8 Database Administration volume 2 instruction guide phần 3 doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (105.06 KB, 34 trang )

Oracle8: Database Administration 14-27


.
Maintaining Constraints and Triggers
By default, a trigger is enabled when created.
Disabling Triggers
Disabling a trigger may be necessary in the following conditions:
• An object that the trigger references is not available, but the DML
activity on the table cannot be stopped.
• To speed up a large data load on the table that has the trigger.
Use the following command to disable a trigger:
ALTER TRIGGER [ schema. ]trigger DISABLE
Enabling Triggers
Use the following command to enable a trigger:
ALTER TRIGGER [ schema. ]trigger ENABLE
What follows is the syntax for enabling or disabling all triggers in a table:
ALTER TABLE [ schema. ]table
{ DISABLE | ENABLE } ALL TRIGGERS
14-16
Copyright  Oracle Corporation, 1998. All rights reserved.
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 TABLE scott.employees
ENABLE ALL TRIGGERS;
14-28 Oracle8: Database Administration




.
Lesson 14: Maintaining Data Integrity
Constraints may be dropped when:
• They are no longer necessary.
• They need to be modified but because they cannot be modified directly,
they must be dropped and added.
Syntax
Use the following command to drop a constraint:
ALTER TABLE [ schema. ] table
DROP {CONSTRAINT constraint
| PRIMARY KEY
| UNIQUE ( column [, column ] ) }
[ CASCADE ]
Note
• Use the CASCADE option to drop any constraint that is referenced by a
foreign key.
• When a primary key or a unique constraint is dropped, any associated
unique index is also dropped.
• If a primary key or unique constraint is implemented using a nonunique
index, the associated index is not dropped, and must be dropped
manually if not required.
14-17
Copyright  Oracle Corporation, 1998. All rights reserved.
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;
DROP TABLE departments
CASCADE CONSTRAINTS;
Oracle8: Database Administration 14-29


.
Maintaining Constraints and Triggers
Dropping a Parent Table
Tables that are referenced by a foreign key can only be dropped if the
foreign key is dropped. This can be achieved using the following command:
DROP [ schema .] table
CASCADE CONSTRAINTS
This may be necessary if the parent table needs to be reorganized.
14-30 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
Use the following command to drop a trigger that is no longer required:
DROP TRIGGER [ schema. ] trigger
14-18
Copyright  Oracle Corporation, 1998. All rights reserved.
Dropping Triggers
DROP TRIGGER scott.audit_dept;
Oracle8: Database Administration 14-31


.
Getting Constraint and Trigger Information

Getting Constraint and Trigger Information
Constraints and Their Status
Use the following query to obtain the names, types, and status of all
constraints on SCOTT’s EMP table:
SVRMGR> SELECT constraint_name, constraint_type, deferrable,
2> deferred, validated
3> FROM dba_constraints
4> WHERE owner='SCOTT'
5> AND table_name='EMPLOYEES';
CONSTRAINT_NAME C DEFERRABLE DEFERRED VALIDATED
-
EMP_DEPT_FK R DEFERRABLE DEFERRED VALIDATED
EMP_PK P DEFERRABLE IMMEDIATE VALIDATED
SYS_C00565 C NOT DEFERRABLE IMMEDIATE VALIDATED
3 rows selected.
14
-
19
Co
p
y
ri
g
h
t



O
r

ac
l
e

Co
rp
o
r
at
i
o
n
,
1
998
. All ri
g
h
ts
r
ese
rv
ed
.
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
14-32 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
The following table shows the columns in DBA_CONSTRAINTS view that
are not self-evident.
Columns in Constraints
To get the columns in the constraints on SCOTT’s EMP table, use the
following query:
SVRMGR> SELECT c.constraint_name, c.constraint_type,
2> cc.column_name
3> FROM dba_constraints c, dba_cons_columns cc

4> WHERE c.owner='SCOTT'
5> AND c.table_name='EMPLOYEES'
6> AND c.owner = cc.owner
7> AND c.constraint_name = cc.constraint_name
8> ORDER BY cc.position;
CONSTRAINT_NAME C COLUMN_NAME
-
EMP_DEPT_FK R DEPTNO
EMP_PK P EMPNO
SYS_C00565 C LAST_NAME
3 rows selected.
Name Description
CONSTRAINT_TYPE The type of constraint is P if Primary key, U if Unique, R if
Foreign key, or C if Check constraint. NOT NULL
constraints are stored as check constraints.
SEARCH_CONDITION Shows the condition specified for a check constraint
R_OWNER
R_CONSTRAINT_NAME
Define the owner and name of the referenced constraint for
foreign keys
GENERATED Indicates whether the constraint name is system generated
(Valid values are ‘USER NAME’ and ‘GENERATED
NAME’.)
BAD Indicates that the constraint is to be rewritten to avoid such
situations as Year 2000 problems (This might happen
because earlier releases of Oracle allowed 2-digit years to
be specified in check constraints.)
LAST_CHANGE The date when the constraint was last enabled or disabled
Oracle8: Database Administration 14-33



.
Getting Constraint and Trigger Information
Finding Primary Key–Foreign Key Relationships
To find foreign keys on SCOTT’s EMP table and the parent constraints, use
the following query:
SVRMGR> SELECT c.constraint_name AS "Foreign Key",
2> p.constraint_name AS "Referenced Key",
3> p.constraint_type,
4> p.owner,
5> p.table_name
6> FROM dba_constraints c, dba_constraints p
7> WHERE c.owner='SCOTT'
8> AND c.table_name='EMPLOYEES'
9> AND c.constraint_type='R'
10> AND c.r_owner=p.owner
11> AND c.r_constraint_name = p.constraint_name;
Foreign Key Referenced Key C OWNER TABLE_NAME
-
EMP_DEPT_FK DEPT_PK P SCOTT DEPARTMENTS
1 row selected.
14-34 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
14-20
Copyright  Oracle Corporation, 1998. All rights reserved.
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
Oracle8: Database Administration 14-35


.
Getting Constraint and Trigger Information
Triggers and Trigger Columns
To find the names of all triggers on SCOTT’s EMP table, and the columns if
any, in the UPDATE OF clause, use the following query:
SVRMGR> SELECT t.owner, t.trigger_name, t.trigger_type,
2 t.triggering_event, t.status,

3 c.column_name, o.status as "VALIDITY"
4 FROM dba_triggers t, dba_trigger_cols c,
5 dba_objects o
6 WHERE t.owner = c.trigger_owner (+)
7 AND t.trigger_name = c.trigger_name (+)
8 AND t.owner = o.owner
9 AND t.trigger_name = o.object_name
10 AND o.object_type = 'TRIGGER'
11 AND t.table_owner = 'SCOTT'
12 AND t.table_name = 'EMPLOYEES';
OWNER RIGGER_NAME TRIGGER_TYPE TRIGGERING_EVENT STATUS

COLUMN_NAME VALIDITY

SCOTT EMP_CONV_LN BEFORE STATEMENT INSERT OR UPDATE ENABLED
LAST_NAME VALID
SCOTT T1 BEFORE STATEMENT UPDATE ENABLED
EMPNO INVALID
2 rows selected.
14-36 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
Summary
14-21
Copyright  Oracle Corporation, 1998. All rights reserved.
Summary
• Implementing constraints and triggers
• Using appropriate strategy for creating

and maintaining constraints
Oracle8: Database Administration 14-37


.
Summary
Quick Reference
Context Reference
Initialization parameters None
Dynamic performance views None
Data dictionary views DBA_CONSTRAINTS
DBA_CONS_COLUMNS
DBA_TRIGGERS
DBA_TRIGGER_COLS
Commands CREATE TABLE CONSTRAINT
ALTER TABLE ADD CONSTRAINT
EXCEPTIONS INTO
CREATE OR REPLACE TRIGGER
ALTER TABLE DISABLE CONSTRAINT
ALTER TABLE ENABLE NOVALIDATE
CONSTRAINT
ALTER TABLE ENABLE VALIDATE
CONSTRAINT EXCEPTIONS INTO
ALTER TRIGGER DISABLE
ALTER TABLE DISABLE ALL TRIGGERS
ALTER TRIGGER ENABLE
ALTER TABLE ENABLE ALL TRIGGERS
ALTER TRIGGER COMPILE
ALTER TABLE DROP CONSTRAINT
DROP TABLE CASCADE CONSTRAINTS

DROP TRIGGER
Packaged procedures and
functions
None
14-38 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity

15
Using Clusters and
Index-Organized Tables
15-2 Oracle8: Database Administration


.
Lesson 15: Using Clusters and Index-Organized Tables
Instructor Note
Topic Timing
Lecture 45 minutes
Practice 15 minutes
Total 60 minutes
Oracle8: Database Administration 15-3


.
Objectives
Objectives
15-2

Copyright  Oracle Corporation, 1998. All rights reserved.
Objectives
• Creating and maintaining clusters
• Using index-organized tables
• Retrieving information about clusters
and tables from the data dictionary
15-4 Oracle8: Database Administration


.
Lesson 15: Using Clusters and Index-Organized Tables
Overview
In a regular table, the user has very limited control over how row data is
distributed. When the table is first created, the rows are generally inserted
into a segment starting with the first block of the first extent. But once other
DML operations are carried out, several factors, such as the order of blocks
in the free list and row migration, make it difficult to influence the ordering
of row data within the table.
Clusters offer some degree of control over how rows are stored. When a
cluster is used, the Oracle server stores all rows that have the same key value
in the same block, if possible.
When index-organized tables are used, the user exercises greater control
over data ordering. Data in an index-organized table is in the order of the key
specified.
This lesson focuses on the use of these two means of storing data.
15-3
Copyright  Oracle Corporation, 1998. All rights reserved.
Distribution of Rows Within a
Table
Cluster Index-organized

table
Table
Random
Ordering of Rows
Grouped Ordered
Oracle8: Database Administration 15-5


.
Clusters
Clusters
A cluster can be used to store related sets of rows within the same Oracle
Server block. The slide shows an example from an order processing system,
where the order table, ORD, and the items in the order, ITEM, are stored in a
cluster.
Difference Between Clustered and Unclustered Tables
If they were stored as regular tables, ORD and ITEM are placed in different
segments. This means that the tables use their own set of blocks—a block
that is used to store a row from the ORD table does not contain data from the
ITEM table, and vice versa.
If the tables ORD and ITEM are stored in a cluster, they share the same
cluster segment. A block in this segment can store rows from both tables. If
a table is stored in a cluster, the cluster becomes the physical unit of storage
and the table is a logical entity
—that is, part of the cluster.
15-4
Copyright  Oracle Corporation, 1998. All rights reserved.
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
15-6 Oracle8: Database Administration


.
Lesson 15: Using Clusters and Index-Organized Tables
Cluster Characteristics
Clusters have the following characteristics:
• Clusters have a cluster key, which is used to identify the rows that need
to be stored together.
• The cluster key may consist of one or more columns.
• Tables in a cluster have columns that correspond to the cluster key.
• Clustering is a mechanism that is transparent to the applications using
the tables. Data in a clustered table can be manipulated as though it were
stored in a regular table.
• Updating one of the columns in the cluster key may entail physically
relocating the row.
• The cluster key is independent of the primary key. The tables in a cluster
can have a primary key, which may be the cluster key or a different set of
columns.
• Clusters are usually created to improve performance. Random access to
clustered data may be faster, but full table scans on clustered tables are
generally slower.
Oracle8: Database Administration 15-7


.
Clusters
There are two types of cluster:

• Index cluster
• Hash cluster
Index Cluster
An index cluster uses an index, known as a cluster index, to maintain the
data within the cluster.
• The cluster index must be available to store, access, or maintain data in
an index cluster.
• The cluster index is used to point to the block that contains the rows with
a given key value.
• The structure of a cluster index is similar to that of a normal index.
Although a normal index does not store a NULL key value, cluster
indexes store NULL keys. There is only one entry for each key value in
the cluster index. Therefore, they are likely to be smaller than a normal
index on the same set of key values.
15-5
Copyright  Oracle Corporation, 1998. All rights reserved.
Cluster Types
Index cluster Hash cluster
Hash function
15-8 Oracle8: Database Administration


.
Lesson 15: Using Clusters and Index-Organized Tables
• To store or retrieve rows from a cluster, the Oracle server uses the cluster
index to locate the first row that corresponds to the given key value and
then retrieves the rows for the given key.
• If several rows in an index cluster have the same cluster key, the cluster
key is not repeated for each row. In a table with a large number of rows
per key value, use of an index cluster may reduce the amount of space

needed to store data.
Hash Cluster
A hash cluster uses a function to calculate the location of a row. The hash
function uses the cluster key and can either be user defined or system
generated.
When a row is inserted into a table in a hash cluster:
• The hash key columns are used to compute a hash value.
• The row is stored based on the hash value.
The hash function is used to locate the row while retrieving the data from a
hashed table. Where applicable, a hash cluster can provide greater
performance gains than an index cluster.
Oracle8: Database Administration 15-9


.
Creating Clusters
Creating Clusters
15-6
Copyright  Oracle Corporation, 1998. All rights reserved.
1. Create a cluster.
2. Create a cluster index.
Creating Index Clusters
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);

15-7
Copyright  Oracle Corporation, 1998. All rights reserved.
Creating Index Clusters
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);
3. Create tables in the cluster.
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);
15-10 Oracle8: Database Administration


.
Lesson 15: Using Clusters and Index-Organized Tables
To create tables in an index cluster, follow these steps:
1 Create the cluster.
2 Create the cluster index.
3 Create tables in the cluster.
You can create tables in the cluster before creating the index. All three steps
must be completed before inserting data into the table.
Creating the Cluster
Use the following command to create a cluster:
CREATE CLUSTER [ schema. ] cluster (column dattype [, column
datatype ] )

[ PCTFREE integer ]
[ PCTUSED integer ]
[ INITRANS integer ]
[ MAXTRANS integer ]
[ SIZE integer [ K | M ] ]
[ storage-clause ]
[ TABLESPACE tablespace ]
[ INDEX ]
where: schema is the owner of the cluster
cluster is the name of the cluster
column is the name of a key column
data type is the data type and size of the key column
SIZE specifies the space required by all rows
corresponding to a key value in bytes,
kilobytes, or megabytes
INDEX specifies that it is an index cluster
Note
If SIZE is not defined, it defaults to the size of one block. The Oracle server
uses this value to estimate the maximum number of key values that can be
accommodated in each block of the cluster. In the example, SIZE is set to
200. For a block size of 2048, after allowing for the header, about 1900 bytes
are available in the block for storing data. Therefore, a block can
accommodate a maximum of 9 key values.
Oracle8: Database Administration 15-11


.
Creating Clusters
Creating the Cluster Index
Use the following command to create a cluster index:

CREATE INDEX [ schema. ] index
ON CLUSTER [ schema. ] cluster
[ PCTFREE integer ]
[ INITRANS integer ]
[ MAXTRANS integer ]
[ TABLESPACE tablespace ]
[ storage-clause ]
The key columns do not need to be specified for a cluster index because they
have already been defined while creating the cluster. Place the cluster index
in a tablespace different from that used for creating the cluster.
Creating Tables in the Cluster
Specify the cluster name to create tables in the cluster:
CREATE TABLE [schema.]table
( column_definition
[, column_definition ]
[, [CONSTRAINT constraint] out_of_line_constraint ]
)
CLUSTER [schema.]cluster (column [, column ] )
The CLUSTER clause specifies that the table must be placed in a cluster.
Note
A table that is placed in a cluster cannot have any physical attributes of its
own because it is not a segment by itself and is a part of the cluster.
15-12 Oracle8: Database Administration


.
Lesson 15: Using Clusters and Index-Organized Tables
To create a hash cluster and place tables in it, follow these steps:
1 Create the cluster.
2 Create the tables.

Syntax
The command for creating a hash cluster is similar to the command for
creating an index cluster:
CREATE CLUSTER [ schema. ] cluster (column datatype [, column
datatype ] )
HASHKEYS integer
[ HASH IS expression ]
[ PCTFREE integer ]
[ PCTUSED integer ]
[ INITRANS integer ]
[ MAXTRANS integer ]
[ SIZE integer [ K | M ] ]
[ storage-clause ]
[ TABLESPACE tablespace ]
15-8
Copyright  Oracle Corporation, 1998. All rights reserved.
1. Create a cluster.
2. Create tables in a cluster.
Creating Hash Clusters
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);
Oracle8: Database Administration 15-13



.
Creating Clusters
The HASHKEYS clause indicates that the cluster is a hash cluster. The
Oracle server uses the next higher prime number as the number of possible
cluster key values.
The optional clause HASH IS can be used to specify a user defined hash
function, expression.
OEM
1 Use Oracle Schema Manager.
2 Choose Object—>Create.
3 Select Cluster from the list of values and then choose OK.
4 Enter General, Storage, and Options information in the property sheet.
5 Choose Create.
Alternatively, select an existing cluster from the navigator and then choose
Object
—>Create Like to create a new cluster with the same key and storage
characteristics as an existing cluster.

×