]trigger DISABLE Enabling Triggers Use the following command to enable a trigger: ALTER TRIGGER [ schema.. Oracle8: Database Administration 14-33 ......Getting Constraint and Trigger Inf
Trang 1Oracle8: 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;
Trang 214-28 Oracle8: Database Administration
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
• 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;
Trang 3Oracle8: 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
Trang 414-30 Oracle8: Database Administration
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;
Trang 5Oracle8: 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 Copyright Oracle Corporation, 1998 All rights reserved. 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
Trang 614-32 Oracle8: Database Administration
The following table shows the columns in DBA_CONSTRAINTS view that
are not self-evident
6> AND c.owner = cc.owner
7> AND c.constraint_name = cc.constraint_name
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
Trang 7Oracle8: 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",
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.
Trang 814-34 Oracle8: Database Administration
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
Trang 9Oracle8: 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
Trang 1014-36 Oracle8: Database Administration
Summary
14-21 Copyright Oracle Corporation, 1998 All rights reserved.
Summary
• Implementing constraints and triggers
• Using appropriate strategy for creating
and maintaining constraints
Trang 11Oracle8: Database Administration 14-37
Summary
Quick 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
Trang 1214-38 Oracle8: Database Administration
Trang 13
15
Using Clusters and Index-Organized Tables
Trang 1415-2 Oracle8: Database Administration
Trang 15Oracle8: 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
Trang 1615-4 Oracle8: Database Administration
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
Trang 17Oracle8: 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
Trang 1815-6 Oracle8: Database Administration
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
Trang 19Oracle8: Database Administration 15-7
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
Hash function
Trang 2015-8 Oracle8: Database Administration
• 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
Trang 21Oracle8: 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);
Trang 2215-10 Oracle8: Database Administration
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 ] )
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
Trang 23Oracle8: 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
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