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

Oracle 8 Database Administration volume 2 instruction guide phần 2 pot

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 (169.38 KB, 34 trang )

Oracle8: Database Administration 13-25


.
Reorganizing Indexes
Analyze the index to perform the following:
• Check all the index blocks for block corruption. Note that this command
does not verify whether index entries correspond to data in the table.
• Populate the INDEX_STATS view with information about the index.
Syntax
ANALYZE INDEX [ schema.]index VALIDATE STRUCTURE
After running this command, query INDEX_STATS to obtain information
about the index as shown in the following example:
SVRMGR> SELECT blocks, pct_used, distinct_keys
2> lf_rows, del_lf_rows
3> FROM index_stats;
BLOCKS PCT_USED LF_ROWS DEL_LF_ROWS

25 11 14 0
1 row selected.
Reorganize the index if it has a high proportion of deleted rows—for
example, when the ratio of DEL_LF_ROWS to LF_ROWS exceeds 30%.
13-15
Copyright  Oracle Corporation, 1998. All rights reserved.
Checking Index Validity
ANALYZE INDEX scott.ord_region_id_idx
VALIDATE STRUCTURE;
INDEX_STATS
13-26 Oracle8: Database Administration



.
Lesson 13: Managing Indexes
Dropping Indexes
Indexes may need to be dropped in the following scenarios:
• An index that is no longer needed by applications can be removed.
• An index may be dropped prior to performing bulk loads. Dropping an
index prior to large data loads and recreating them after the load:
- Improves performance of the load
- Uses index space more efficiently
• Indexes that are used only periodically do not need to be maintained
unnecessarily, especially if they are based on volatile tables. This is
generally the case in an OLTP system, where ad hoc queries are
generated at year-end or quarter-end to gather information for review
meetings.
• An index may be marked INVALID when there is an instance failure
during certain types of operations such as loading. In this case the index
needs to be dropped and recreated.
• The index is corrupt.
13-16
Copyright  Oracle Corporation, 1998. All rights reserved.
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;
Oracle8: Database Administration 13-27



.
Dropping Indexes
Syntax
Use the following command to drop an index:
DROP INDEX [schema.] index
OEM
1 Use Oracle Schema Manager.
2 Expand the Indexes node.
3 Expand the username (or schema).
4 Select the index.
5 Choose Object—>Remove.
6 Choose Yes in the dialog box.
Note
An index cannot be dropped if it is used to implement an integrity constraint
that is enabled. Constraints are discussed in the lesson “Maintaining Data
Integrity.”
13-28 Oracle8: Database Administration


.
Lesson 13: Managing Indexes
Obtaining Index Information
The data dictionary views DBA_INDEXES and DBA_IND_COLUMNS
give the information on the indexes and the columns indexed.
Checking Indexes and Their Validity
Use the following command to verify the name, type, and status of the
indexes owned by the user SCOTT:
SVRMGR> SELECT index_name, tablespace_name, index_type,
2> uniqueness, status
3> FROM dba_indexes

4> WHERE owner='SCOTT';
INDEX_NAME TABLESPACE_NAME INDEX_TYPE UNIQUENES STATUS

EMP_LNAME_IDX INDX01 NORMAL NONUNIQUE VALID
ORD_ORD_NO_IDX INDX01 NORMAL UNIQUE VALID
ORD_REGION_ID_IDX INDX02 BITMAP NONUNIQUE VALID
3 rows selected.
13-17
Copyright  Oracle Corporation, 1998. All rights reserved.
Obtaining Index Information
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

Oracle8: Database Administration 13-29



.
Obtaining Index Information
The column INDEX_TYPE indicates whether the index is bitmap or normal.
Use the following query to list the names of all reverse key indexes:
SVRMGR> SELECT o.object_name
2> FROM dba_objects o
3> WHERE owner='SCOTT'
4> AND o.object_id IN (SELECT i.obj#
5> FROM ind$ i
6> WHERE BITAND(i.property,4) = 4);
OBJECT_NAME

ORD_ORD_NO_IDX
1 row selected.
Finding Columns in an Index
The following query lists all the indexes owned by the user SCOTT and
shows the tables and columns on which the indexes are built:
SVRMGR> SELECT index_name, table_owner, table_name, column_name
2> FROM dba_ind_columns
3> WHERE index_owner = 'SCOTT'
4> ORDER BY index_name, column_position;
INDEX_NAME TABLE_OWNER TABLE_NAME COLUMN_NAME

EMP_LNAME_IDX SCOTT EMP LAST_NAME
ORD_ORD_NO_IDX SCOTT ORD ORD_NO
ORD_REGION_ID_IDX SCOTT ORD REGION_ID
3 rows selected.
13-30 Oracle8: Database Administration



.
Lesson 13: Managing Indexes
Summary
13-18
Copyright  Oracle Corporation, 1998. All rights reserved.
Summary
• Creating different types of indexes
• Reorganizing indexes
Oracle8: Database Administration 13-31


.
Summary
Quick Reference
Context Reference
Initialization parameters CREATE_BITMAP_AREA_SIZE
Dynamic performance views None
Data dictionary tables/views DBA_INDEXES
DBA_IND_COLUMNS
DBA_OBJECTS
IND$
INDEX_STATS
Commands CREATE INDEX
CREATE UNIQUE INDEX
CREATE BITMAP INDEX
CREATE INDEX REVERSE
ALTER INDEX STORAGE
ALTER INDEX INITRANS MAXTRANS

ALTER INDEX ALLOCATE EXTENT
ALTER INDEX DEALLOCATE UNUSED
ALTER INDEX REBUILD
ALTER INDEX REBUILD REVERSE
ALTER INDEX REBUILD NOREVERSE
ANALYZE INDEX VALIDATE STRUCTURE
DROP INDEX
Packaged procedures and
functions
None
13-32 Oracle8: Database Administration


.
Lesson 13: Managing Indexes

14
Maintaining Data Integrity
14-2 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
Instructor Note
Topic Timing
Lecture 45 minutes
Practice 30 minutes
Total 75 minutes
Oracle8: Database Administration 14-3



.
Objectives
Objectives
14-2
Copyright  Oracle Corporation, 1998. All rights reserved.
Objectives
• Implementing data integrity constraints
and triggers
• Maintaining integrity constraints and
triggers
• Obtaining constraint and trigger
information from the data dictionary
14-4 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
Overview
Data integrity guarantees that data in a database adheres to business rules.
There are three primary ways in which data integrity can be maintained:
• Application code
• Database triggers
• Declarative integrity constraints
Mapping the business rules using one of the three methods is a design
decision made by the designer. The database administrator is primarily
concerned with implementing the methods chosen by the designer and
balancing the performance needs against integrity requirements.
Application code may be implemented either as stored procedures within the
database or as applications running on the client. This lesson focuses on the

use of database triggers and integrity constraints.
14-3
Copyright  Oracle Corporation, 1998. All rights reserved.
Data Integrity
Application
code
Integrity
constraint
Database
trigger
Table
Data
Oracle8: Database Administration 14-5


.
Overview
Database Triggers
Database triggers are PL/SQL programs that are executed when a certain
event such as an insert or an update of a column occurs on a table. Triggers
can be enabled or disabled—that is, they can be set to execute when the
event occurs, or they can be set not to execute even though they are defined.
Database triggers are usually created only to enforce a complex business
rule that cannot be defined as an integrity constraint.
Integrity Constraints
Integrity constraints are the preferred mechanism for enforcing business
rules because they:
• Provide improved performance
• Are easy to declare and modify—that is, they do not require extensive
coding

• Centralize rules
• Are flexible (enabled/disabled)
• Are fully documented in the data dictionary
The following sections explain the behavior of integrity constraints and
discuss how they are implemented by the Oracle server.
14-6 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
Integrity Constraints and Triggers
As shown in the slide, there are five types of declarative integrity constraints.
Although the NOT NULL and CHECK constraints do not directly require
DBA attention, the primary key, unique, and foreign key constraints must be
managed to ensure high availability and acceptable performance levels.
Instructor Note
A foreign key constraint can be defined to disable the deletion of a parent
row when child rows exist or the deletion of all the child rows when the
corresponding parent row is deleted.
14-4
Copyright  Oracle Corporation, 1998. All rights reserved.
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 table’s 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
Oracle8: Database Administration 14-7


.
Integrity Constraints and Triggers
An integrity constraint can be in one of the following states:
• Disabled
• Enabled novalidate or enforced
• Enabled validate
Disabled
A constraint that is disabled is not checked, even though the constraint
definition is still stored in the data dictionary. Data in the table as well as
new data that is entered or updated may not conform to the rules defined by
the constraint.
Enabled Novalidate (Enforced)
If a constraint is enabled novalidate, new data that violates the constraint
cannot be entered. However, the table may contain data that is invalid—that
is, data that violates the constraint. This is usually an intermediate stage that

ensures that all new data is checked before being accepted into the table.
14-5
Copyright  Oracle Corporation, 1998. All rights reserved.
Constraint States
Disabled Enabled
novalidate
Enabled
validate
Existing dataNew data
14-8 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
Enabled Validate
If a constraint is in this state, then all data in the table is guaranteed to adhere
to the constraint. In addition, this state prevents any invalid data from being
entered. This is the normal state of operation of a constraint for online
transaction processing.
When a constraint is enabled validate from a disabled state, the table is
locked and all data in the table is checked for conformance. This may cause
DML operations such as a data load to wait, so it may be advisable to move
from a disabled state to enable novalidate and then to move to enable
validate.
Oracle8: Database Administration 14-9


.
Integrity Constraints and Triggers
The point in a transaction at which a constraint is checked can be controlled

by defining the constraint appropriately.
Nondeferred or Immediate Constraints
Nondeferred constraints, also known as immediate constraints, are enforced
at the end of every DML statement. A constraint violation causes the
statement to be rolled back. If a constraint causes an action such as delete
cascade, the action is taken as part of the statement that caused it.
A constraint that is defined as nondeferrable cannot be modified to be
enforced at the end of a transaction.
Deferred Constraints
Deferred constraints are constraints that are checked only when a transaction
commits. If any constraint violations are detected at commit time, the entire
transaction is rolled back. These constraints are most useful when both the
parent and child rows in a foreign key relationship are entered at the same
time, as in the case of an order entry system, where the order and the items
in the order are entered at the same time.
14-6
Copyright  Oracle Corporation, 1998. All rights reserved.
Deferred Constraints
DML statement
Check nondeferred
constraints
COMMIT
Check deferred
constraints
14-10 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
For a constraint to be deferred, it must be defined as a deferrable constraint

at the time of creation. A constraint that is defined as deferrable can be
specified as one of the following:
• Initially immediate specifies that by default it should function as an
immediate constraint, unless explicitly set otherwise.
• Initially deferred specifies that by default the constraint should only be
enforced at the end of the transaction.
Although the default mode of enforcement of a deferrable constraint is
specified and stored in the data dictionary, applications can modify the
constraint to work as either deferred or immediate. This is accomplished by
using either an ALTER SESSION command or a SET CONSTRAINT
command as shown below:
ALTER SESSION
SET CONSTRAINT[S] =
{IMMEDIATE|DEFERRED|DEFAULT}
SET CONSTRAINT[S]
{constraint [, constraint ]
|ALL }
{IMMEDIATE|DEFERRED}
Oracle8: Database Administration 14-11


.
Integrity Constraints and Triggers
Primary and unique keys are enforced using indexes. You can control the
location and type of index that is used for enforcing these constraints.
Oracle Server follows this procedure to implement unique and primary key
constraints:
1 If the constraint is disabled, no indexes are needed.
2 If the constraint is enabled and the columns in the constraint form the
leading part of an index, the index will be used to enforce the constraint.

3 If the constraint is enabled and there is no index that uses the constraint
columns as a leading part of the index, an index with the same name as
the constraint is created using the following rules:
- If the key is deferrable, a nonunique index on the key column is
created.
- If the key is nondeferrable, a unique index is created.
14-7
Copyright  Oracle Corporation, 1998. All rights reserved.
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?

14-12 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
You need to consider several factors in maintaining tables that are in a
foreign key relationship.
DDL Involving Parent Table
• The foreign key must be dropped before dropping the parent table. Use
the following command to perform both actions using a single
statement:
DROP TABLE table CASCADE CONSTRAINTS
• The parent table cannot be truncated without dropping or disabling the
foreign key.
• The foreign key must be dropped before the tablespace containing the
parent is dropped. The following command can be used to achieve this:
DROP TABLESPACE tablespace INCLUDING CONTENTS
CASCADE CONSTRAINTS
14-8
Copyright  Oracle Corporation, 1998. All rights reserved.
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
Oracle8: Database Administration 14-13


.
Integrity Constraints and Triggers
DML on Tables in a Foreign Key Relationship
If the DELETE CASCADE option is not used when rows are deleted from
the parent table, Oracle Server needs to ensure that there are no rows in the
child table with the corresponding foreign key. Similarly, an update to the
parent key is permitted only when there are no child rows with the old key
value. If there is no index on the foreign key on the child table, the Oracle
server locks the child table and prevents changes to ensure referential
integrity. If there is an index on the table, the referential integrity is
maintained by locking the index entries and avoiding more restrictive locks
on the child table. If both tables need to be updated concurrently from
different transactions, create an index on the foreign key columns.
When data is inserted into or the foreign key column is updated in the child
table, the Oracle server checks the index on the parent table that is used for
enforcing the referenced key. Therefore, the operation succeeds only if the
tablespace containing the index is online. Note that the tablespace

containing the parent table does not need to be online to perform DML
operations on the child table.
Note
Besides the advantage mentioned above, creating indexes on foreign key
columns is recommended because it has other performance advantages.
14-14 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
Database triggers are PL/SQL programs that are implicitly executed when a
DML statement is executed on the associated table.
Trigger Types
There are twelve possible trigger combinations that can be defined on a table
based on the following three properties that can be specified for a trigger:
DML Statement
A trigger can be set to execute when INSERT, UPDATE, or DELETE, or
any combination of these statements, is executed on a table. When defining
an update trigger, if column names are specified, the trigger fires only when
the columns mentioned are updated.
Timing
A trigger can be defined to execute either before or after the triggering
statement.
Frequency
You can control the number of times a trigger is executed for a given DML
statement. A ROW trigger executes once for every row affected, while a
STATEMENT trigger fires only once for the statement regardless of the
number of rows affected by the trigger.
14-9
Copyright  Oracle Corporation, 1998. All rights reserved.

Database Triggers
Table
Trigger
DML action
Trigger types
• INSERT or UPDATE or DELETE
• BEFORE or AFTER
• ROW or STATEMENT
Oracle8: Database Administration 14-15


.
Integrity Constraints and Triggers
Trigger States
Triggers can be enabled or disabled. A disabled trigger does not execute
when the corresponding DML operation is carried out on the table. A trigger
that is enabled fires on execution of the corresponding DML command.
Triggers are event driven, so when a trigger is enabled, no action is carried
out on an existing data.
Creating a Trigger: Example
The following trigger uses the INITCAP function to convert an employee’s
last name before it is stored.
CREATE TRIGGER scott.emp_conv_ln
BEFORE INSERT OR UPDATE OF last_name ON scott.employees
FOR EACH ROW
BEGIN
:NEW.last_name := INITCAP(:NEW.last_name);
END;
Note
Triggers are discussed in detail in the course, PL/SQL Program Units.

14-16 Oracle8: Database Administration


.
Lesson 14: Maintaining Data Integrity
Implementing Constraints and Triggers
A constraint can be defined either when a table is created or when a table is
altered to add the constraint.
Syntax: In-Line Constraint
At the time the table is created, the constraint can be created in line using the
following syntax to define the column:
column datatype [CONSTRAINT constraint]
in_line_constraint
[defer_spec]
in_line_constraint :==
{[NOT] NULL
|PRIMARY KEY [USING INDEX index_clause]
|UNIQUE [USING INDEX index_clause]
|REFERENCES [schema.]table [(column)]
[ON DELETE CASCADE]
|CHECK (condition)
}
14-10
Copyright  Oracle Corporation, 1998. All rights reserved.
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;
Oracle8: Database Administration 14-17


.
Implementing Constraints and Triggers
defer_spec :==
[
NOT DEFERRABLE
|DEFERRABLE [INITIALLY {
IMMEDIATE|DEFERRED}]
]
[DISABLE|
ENABLE [VALIDATE|NOVALIDATE]]
where: CONSTRAINT identifies the integrity constraint by the
name constraint stored in data
dictionary
USING INDEX specifies that the parameters defined in
the index-clause should be used for the
index the Oracle server uses, to enforce
a unique or primary key constraint (The
name of the index is the same as the
name of the constraint.)
DEFERRABLE indicates that constraint checking can

be deferred until the end of the
transaction by using the SET
CONSTRAINT(S) command
NOT DEFERRABLE indicates that this constraint is checked
at the end of each DML statement
(A NOT DEFERRABLE constraint
cannot be deferred by sessions or
transactions. NOT DEFERRABLE is
the default.)
INITIALLY IMMEDIATE
indicates that at the start of every
transaction, the default is to check this
constraint at the end of every DML
statement (If no INITIALLY clause is
specified,INITIALLYIMMEDIATEis
the default.)
INITIALLY DEFERRED
implies that this constraint is
DEFERRABLE and specifies that by
default, the constraint is checked only
at the end of each transaction
DISABLE disables the integrity constraint
If an integrity constraint is disabled, the
Oracle server does not enforce it

×