Reprints Online

Members Login
Username 
 
Password 
    Remember Me  
Post Info TOPIC: More about Column- and Table Constraints


Guru

Status: Offline
Posts: 651
Date:
More about Column- and Table Constraints
Permalink  
 


If a constraint is de~ned within the create table command or added using the alter table command, the constraint is automatically enabled. A constraint can be disabled using the command

alter table <table> disable

constraint <name> j primary key j unique[<column(s)>] [cascade];

To disable a primary key, one must disable all foreign key constraints that depend on this primary key. The clause cascade automatically disables foreign key constraints that depend on the (disabled) primary key.

Example: Disable the primary key of the table DEPT and disable the foreign key constraint in the table EMP:

alter table DEPT disable primary key cascade;

In order to enable an integrity constraint, the clause enable is used instead of disable. A constraint can only be enabled successfully if no tuple in the table violates the constraint. Otherwise an error message is displayed. Note that for enabling/disabling an integrity constraint it is important that you have named the constraints.

In order to identify those tuples that violate an integrity constraint whose activation failed, one can use the clause exceptions into EXCEPTIONS with the alter table statement. EXCEPTIONS is a table that stores information about violating tuples.3 Each tuple in this table is identi~ed by the attribute ROWID. Every tuple in a database has a pseudo-column ROWID that is used to identify tuples. Besides the rowid, the name of the table, the table owner as well as the name of the violated constraint are stored.

Example: Assume we want to add an integrity constraint to our table EMP which requires that each manager must earn more than 4000:

alter table EMP add constraint managersal

check(JOB != 'MANAGER' or SAL >= 4000) exceptions into EXCEPTIONS;

If the table EMP already contains tuples that violate the constraint, the constraint cannot be activated and information about violating tuples is automatically inserted into the table

EXCEPTIONS. Detailed information about the violating tuples can be obtained by joining the tables EMP and EXCEPTIONS, based on the join attribute ROWID:

select EMP.~, CONSTRAINT from EMP, EXCEPTIONS where EMP.ROWID = EXCEPTIONS.ROWID;

3Before this table can be used, it must be created using the SQL script utlexcept.sql which can be found in the directory $ORACLEHOME/rdbms/admin. Tuples contained in the query result now can be modi~ed (e.g., by increasing the salary of managers) such that adding the constraint can be performed successfully. Note that it is important to delete \old" violations from the relation EXCEPTIONS before it is used again.

If a table is used as a reference of a foreign key, this table can only be dropped using the command drop table <table> cascade constraints;. All other database objects that refer to this table (e.g., triggers, see Section 5.2) remain in the database system, but they are not valid.

Information about integrity constraints, their status (enabled, disabled) etc. is stored in the data dictionary, more precisely, in the tables USERCONSTRAINTS and USERCONSCONSTRAINTS.



About the Author

For more information about this article please visit www.nueva-design.com




__________________
Page 1 of 1  sorted by
 
Quick Reply

Please log in to post quick replies.

Tweet this page Post to Digg Post to Del.icio.us


Create your own FREE Forum
Report Abuse
Powered by ActiveBoard