Loading...
Maintaining Data Integrity

Maintaining Data Integrity
1.Methods to Guarantee Data Integrity
.Application code
.Database triggers
.Declarative integrity constraints

2.Types of Constraints
.NOT NULL
.UNIQUE
.PRIMARY KEY
.FOREIGN KEY
.CHECK

3.Constraint States
An integrity constraint can be enable(data is checked as it is entered or updated) or disabled(data that dose not confirm to the constraint’s rule is prevented from being entered).
An integrity constraint can be in one of the following states:
.DISABLE NOVALIDATE:Old data and new data will not be checked.And the index will be dropped.
.DISABLE VALIDATE:Any modification of the constrained columns is not allowed.In addition,the index on the constraint is dropped and the constraint is disabled.
.ENABLE NOVALIDATE:new data(will be inserted into table) check, old data(original data in the table) not check.If it’s on primary key ,novalid will still check the old data.
Enabling constraints in the novalidated state is most useful in data warehouse configurations that are uploading valid OLTP data.

.ENABLE VALIDATE:Old data and new data will be checked.When a constraint changes to enable validate from disabled state,the table is loced and all data in the table
is checked for soformity.This may cause DML operations such as a data load to wait,so it is advisable to move first from a disabled state
to enable novalidate,and then to enable validate.

Note:Unique or primary key moves from the DISABLE state to ENABLE state and there’s no existing index,a unique index is created automatically.
Similarly,when a unique or primary key moves from ENABLE to DISABLE and it is enabled with a unique index,the unique index is dropped.

Moving a single constraint from ENABLE NOVALIDATE state to ENABLE VALIDATE state dose not block reads,writes or other DDL statements.

4.Constraint Checking

Constraint check road
DML statement->Check nondeferred constraints->COMMIT->Check deefrred constraints

Nodeferred (Immediate) Constraints
Enforced at the end of every DML statement.A constraint that is defined as nondeferrable cannot be modified to be enforced at the end of transaction.

Deferred Constraints
Deferred constraints are constraints that are checked only when a transaction is commited.

A constraint that is defined as deferred 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

5.Changing the Enforcement of Constraints
.SET CONSTRAINTS statement

1
2
3
4
ALTER SESSION
   SET CONSTRAINT | CONSTRAINT[S]
     {constraint |ALL}
     {IMMEDIATE|DEFERRED}

The SET CONSTRAINT mode lasts for duration of the transaction or until another SET CONSTRAINTS statement resets the mode.
The SET CONSTRAINT statement is disllowed inside triggers.

.ALTER SESSION statement

ALTER SESSION
   SET CONSTRAINT[S] =
    {IMMEDIATE|DEFERRED|DEFAULT}

The ALTER SESSION statement imply setting ALL deferrable constraints statmemt applies to a current session only.

6.Primary and Unique Key Enforcement
Primary and unique keys are enforced using indexes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
IF Key_enabled='Yes' THEN
   IF IS an INDEX availabe FOR USE?='Yes'    
      IF Constraint Deferrable?='Yes' THNE      
         USE existing INDEX;         
      ELSE        
        USE existing INDEX;        
      END IF;   
   ELSE
      IF Constraint deferrable?='Yes' THEN         
         CREATE nonunique INDEX;       
      ELSE       
        CREATE UNIQUE INDEX;        
      END IF;   
   END IF;
ELSE
Do NOT USE INDEX;
END IF

7.Foreign Key Considerations

--------------------------------------------------------------------------------------------------
Desired Action                         |Applicaton sulution                                       |
--------------------------------------------------------------------------------------------------
Drop parent table                      |Cascade constraints                                       |
--------------------------------------------------------------------------------------------------
Truncate parent table                  |Disable or drop foreign key                               |
--------------------------------------------------------------------------------------------------
Drop tablespace containing parent table|Use the CASCADE CONSTRAINTS clause                        |
--------------------------------------------------------------------------------------------------
Perform DML on child table             |Ensure the tablespace constaining the parent key is online|
--------------------------------------------------------------------------------------------------

.The foreign key must be dropped before dropping the parent table.

DROP TABLE table CASCADE CONSTRAINTS

.The parent table cannot be truncated whithout dropping or disabling the foreign key.
.The foreign must be dropped before tablespace containing the parent is dropped.

1
DROP TABLESPACE tablespace INCLUDING CONTENTS CASCADE CONSTRAINTS

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.
Oracle9i no longer requires a share lock on unindexed foreign keys when doing an update or
delete on the primary key. It still obtains the table-level share lock, but then releases it
immediately after obtaining it. If multiple primary keys are update or deleted, the lock is obtained
and released once per row.

8.Defining Constraings While Creating a Table
Syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
COLUMN datatype [CONSTRAINT constraint]
{[NOT] NULL
|UNIQUE [USING INDEX index_clause]
|PRIMARY KEY [USING INDEX index_clause]
|REFERENCES [schema.]TABLE [(COLUMN)]
[ON DELETE CASCADE]
|CHECK (condition)
}
constraint_state :==
[NOT DEFERRABLE|DEFERRABLE [INITIALLY
{IMMEDIATE|DEFERRED}]
]
[DISABLE|ENABLE [VALIDATE|NOVALIDATE]]

Defining a constraint from the type NOT NULL after creating a table is only possible with:

1
ALTER TABLE table_name MODIFY col_name CONSTRAINT constraint_name NOT NULL;

9.Defining Constraints After Creating a Table:Example

1
2
3
4
ALTER TABLE hr.employee
ADD (CONSTRAINT employee_dept_id_fk FOREIGN KEY(dept_id)
REFERENCES hr.department(id)
DEFERRABLE INITIALLY DEFERRED);

10.Guidelines for Defining Constraints
. Place indexes used for enforcing primary key and unique constraints in a tablespace different from that of the table.
. If data is frequently loaded in bulk into a table, it is preferable to disable the constraints,
perform the load, and then enable the constraints. If a unique index is used for enforcing a
primary key or unique constraint, this index needs to be dropped when the constraint is
disabled. Performance can be enhanced by using a nonunique index for enforcement of
primary key or unique constraints in such situations: either create the key as deferrable or
create the index before defining or enabling the key.
. If a table contains a self-referencing foreign key, use one of the following methods to load
data:
– Define or enable the foreign key after the initial load
– Define the constraint as a deferrable constraint
The second method is useful if data loads are performed frequently.

11.Using the EXCEPTIONS Table
.Create exception table use:$ORACLE_HOME/rdbms/admin/utlexcpt.sql
.Add constraint with exceptions into clause
eg:

1
2
ALTER TABLE table_name ADD CONSTRAINT constraint_name
UNIQUE(col_name);

You also can use the following statement to except the duplicated rows after data load.

1
2
3
ALTER TABLE table_name
ENABLE VALIDATE CONSTRAINT constraint_name
EXCEPTIONS INTO schema.exceptions;

12.Obtaining Constraint information
.DBA_CONSTRAINTS
.DBA_CONS_COLUMNS

Popularity: 31% [?]

Managing Indexes

1.Classification of Indexes
Logical
- Single column or concatenated
The maximum number of columns in a composite key index is 32.However,the combined size of all the columns cannot exceed roughly one-third of the data block size.
More…

Popularity: 31% [?]

Only sys user and owner can compile the synonym?

Oracle 中的同义词是schema 对象的一个别名。同义词分为公共(public)同义词和私有(private)同义词.公共同义词可以被所有数据库用户所访问,而私有同义词只能被owner和被授权用户访问。
More…

Popularity: 33% [?]

颈椎锻炼法

感觉自己的脖子酸已经有一段时间可,最近感觉脖颈特别累,估计是得了职业病了,唉,年纪不大,身体是越来越差了,真是太缺少锻炼了,而我这个人平时又没有什么活动,从学校毕业了到现近四年几乎没有锻炼过,生命在于运动,真是真理啊。从网上找了一些锻炼方法,记录供自己参考。
一:多游泳。因为游泳的时候头总是向上抬,颈部肌肉和腰肌都得到锻炼,而且人在水中没有任何负担,也不会对椎间盘造成任何的损伤,算得上是比较惬意的锻炼颈椎的方式。

二:日常保养
  平时要注意少低头,工作一两个小时至少休息一次,休息时头向后仰,或平卧,让颈椎得到休息。颈椎病患者要注意不要来回转头,更不能旋转颈椎,斜搬按摩。

三:做体操,以下推荐简单体操一款。

  A:基本姿势:每次做各项训练动作前,先自然站立,双目平视,双脚略分开,与肩同宽,双手自然下垂。全身放松。

  B:前俯后仰:双手叉腰,先抬头后仰,同时吸气,双眼望天,停留片刻;然后缓慢向前胸部位低头,同时呼气,双眼看地。做此动作时,要闭口,使下颌尽量紧贴前胸,停留片刻后,再上下反复做4次。

  C:左右旋转:双手叉腰,先将头部缓慢转向左侧,同时吸气于胸,让右侧颈部伸直后,停留片刻,再缓慢转向右侧,同时呼气,让左边颈部伸直后,停留片刻。这样反复交替做4次。

  D:提肩缩颈:做操前,先自然站立,双目平视,双脚略分开,与肩平行,双手自然下垂。动作时双肩慢慢提起,颈部尽量往下缩,停留片刻后,双肩慢慢放松地放下,头颈自然伸出,还原自然,然后再将双肩用力往下沉,头颈部向上拔伸,停留片刻后,双肩放松,并自然呼气。

  E:左右摆动:做操前,先自然站立,双目平视,双脚略分开,与肩平行,双手叉腰。动作时头部缓缓向左侧倾斜,使左耳贴于左肩,停留片刻后,头部返回中位;然后再向右肩倾斜,同样右耳要贴近右肩,停留片刻后,再回到中位。这样左右摆动反复做4次。

四:如何预防颈椎病

  预防颈椎病的发生,最重要的是,要改善坐姿,埋头苦干时,也可间断地做运动。此外也要注意:

  1、睡觉时不可俯着睡,枕头不可以过高、过硬或过平。

  2、避免和减少急性损伤,如避免抬重物等。

  3、改正不良姿势,减少劳损,每低头或仰头1—2小时,需要做颈部活动,以减轻肌肉紧张度。

Popularity: 38% [?]

Autotrace in SQLPLUS(From Tom)

Here is what I like to do to get autotrace working:

1
2
3
4
5
6
7
8
9
    * cd $oracle_home/rdbms/admin
    * log into sqlplus as system
    * run >: @utlxplan
    * run >: create public synonym plan_table for plan_table
    * run >: grant all on plan_table to public
    * exit sqlplus and cd $oracle_home/sqlplus/admin
    * log into sqlplus as SYS
    * run >: @plustrce
    * run >: grant plustrace to public

More…

Popularity: 29% [?]

Powered by ExtJS Theme flavored Wordpress.