This chapter applies to only to constraints on tables. Constraints on views do not help maintain data integrity or have associated indexes. Instead, they enable query rewrites on queries involving views, thereby improving performance when using materialized views and other data warehousing features. Show
See Also:
Topics: 12.1 Enforcing Business Rules with ConstraintsWhenever possible, enforce business rules with constraints. Constraints have the advantage of speed: Oracle Database can check that all the data in a table obeys a constraint faster than application code can do the equivalent checking. creates a table of departments, a table of employees, a constraint to enforce the rule that all values in the department table are unique, and a constraint to enforce the rule that every employee must work for a valid department. Example 12-1 Enforcing Business Rules with Constraints Create table of departments: DROP TABLE dept_tab; CREATE TABLE dept_tab ( deptname VARCHAR2(20), deptno INTEGER ); Create table of employees: DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER ); Create constraint to enforce rule that all values in department table are unique: ALTER TABLE dept_tab ADD PRIMARY KEY (deptno); Create constraint to enforce rule that every employee must work for a valid department: ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno); Now, whenever you insert an employee record into CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /4, Oracle Database checks that its CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /5 value appears in CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /6. Suppose that instead of using a constraint to enforce the rule that every employee must work for a valid department, you use a trigger that queries CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /6 to check that it contains the CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /5 value of the employee record to be inserted into CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /4. Because the query uses consistent read (CR), it might miss uncommitted changes from other transactions. See Also:
12.2 Enforcing Business Rules with Both Constraints and Application CodeEnforcing business rules with both constraints and application code is recommended when application code can determine that data values are invalid without querying tables. The application code can provide immediate feedback to the user and reduce the load on the database by preventing attempts to insert invalid data into tables. For , assume that was run and then this column was added to the table CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /4: empgender VARCHAR2(1) The only valid values for Gender must be M or F. 1 are Gender must be M or F. 2 and Gender must be M or F. 3. When someone tries to insert a row into CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /4 or update the value of CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /4. Gender must be M or F. 1, application code can determine whether the new value for CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /4. Gender must be M or F. 1 is valid without querying a table. If the value is invalid, the application code can notify the user instead of trying to insert the invalid value, as in .Example 12-2 Enforcing Business Rules with Both Constraints and Application Code CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; / Result: Gender must be M or F. 12.3 Creating Indexes for Use with ConstraintsWhen a unique or primary key constraint is enabled, Oracle Database creates an index automatically, but Oracle recommends that you create these indexes explicitly. If you want to use an index with a foreign key constraint, then you must create the index explicitly. When a constraint can use an existing index, Oracle Database does not create an index for that constraint. Note that:
See Also:
12.4 When to Use NOT NULL ConstraintsBy default, a column can contain a DESCRIBE DEPARTMENTS;1 value. To ensure that the column never contains a DESCRIBE DEPARTMENTS;1 value, use the DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraint. Use a DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraint in both of these situations:
uses the SQL*Plus command INSERT INTO DEPARTMENTS ( DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID ) VALUES (NULL, 'Sales', 200, 1700);9 to show which columns of the CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /3 table have DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraints, and then shows what happens if you try to insert DESCRIBE DEPARTMENTS;1 values in columns that have DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraints. Example 12-3 Inserting NULL Values into Columns with NOT NULL Constraints DESCRIBE DEPARTMENTS; Result: Name Null? Type ----------------------------------------- -------- ------------ DEPARTMENT_ID NOT NULL NUMBER(4) DEPARTMENT_NAME NOT NULL VARCHAR2(30) MANAGER_ID NUMBER(6) LOCATION_ID NUMBER(4) Try to insert DESCRIBE DEPARTMENTS;1 into DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );07 column: INSERT INTO DEPARTMENTS ( DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID ) VALUES (NULL, 'Sales', 200, 1700); Result: DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );0 Omitting a value for a column that cannot be DESCRIBE DEPARTMENTS;1 is the same as assigning it the value DESCRIBE DEPARTMENTS;1: DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );1 Result: DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );2 You can prevent the preceding error by giving DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );07 a non- DESCRIBE DEPARTMENTS;1 default value. You can combine DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraints with other constraints to further restrict the values allowed in specific columns. For example, the combination of DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 constraints forces the input of values in the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 key, eliminating the possibility that data in a new conflicts with data in an existing row. See Also:
12.5 When to Use Default Column ValuesWhen an DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );20 statement does not specify a value for a specific column, that column receives its default value. By default, that default value is DESCRIBE DEPARTMENTS;1. You can change the default value when you define the column while creating the table or when you alter the column using the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statement. Note: Giving a column a non- DESCRIBE DEPARTMENTS;1 default value does not ensure that the value of the column will never have the value DESCRIBE DEPARTMENTS;1, as the DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraint does. Use a default column value in these situations:
See Also:
12.6 Choosing a Primary Key for a Table (PRIMARY KEY Constraint)The primary key of a table uniquely identifies each row and ensures that no duplicate rows exist (and typically, this is its only purpose). Therefore, a primary key value cannot be DESCRIBE DEPARTMENTS;1. A table can have at most one primary key, but that key can have multiple columns (that is, it can be a composite key). To designate a primary key, use the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraint. Whenever practical, choose as the primary key a single column whose values are generated by a sequence. The second-best choice for a primary key is a single column whose values are all of the following:
Minimize your use of composite primary keys, whose values are long and cannot be generated by a sequence. See Also:
12.7 When to Use UNIQUE ConstraintsUse a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 constraint (which designates a unique key) on any column or combination of columns (except the primary key) where duplicate non- DESCRIBE DEPARTMENTS;1 values are not allowed. For example: Unique KeyPrimary Key Employee Social Security Number Employee number Truck license plate number Truck number Customer phone number (country code column, area code column, and local phone number column) Customer number Department name column and location column Department number shows a table with a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 constraint, a row that violates the constraint, and a row that satisfies it. Figure 12-1 Rows That Violate and Satisfy a UNIQUE Constraint Description of "Figure 12-1 Rows That Violate and Satisfy a UNIQUE Constraint" See Also:
12.8 Enforcing Referential Integrity with FOREIGN KEY ConstraintsWhen two tables share one or more columns, you use can use a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraint to enforce referential integrity—that is, to ensure that the shared columns always have the same values in both tables. Note: A DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraint is also called a referential integrity constraint, and its DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );59 is DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );60 in the static data dictionary views DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );61. Designate one table as the referenced or parent table and the other as the dependent or child table. In the parent table, define either a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 or DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 constraint on the shared columns. In the child table, define a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraint on the shared columns. The shared columns now comprise a foreign key. Defining additional constraints on the foreign key affects the parent-child relationship. shows a foreign key defined on the department number. It guarantees that every value in this column must match a value in the primary key of the department table. This constraint prevents erroneous department numbers from getting into the employee table. shows parent and child tables that share one column, a row that violates the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraint, and a row that satisfies it. Figure 12-2 Rows That Violate and Satisfy a FOREIGN KEY Constraint Description of "Figure 12-2 Rows That Violate and Satisfy a FOREIGN KEY Constraint" Topics: See Also:
12.8.1 FOREIGN KEY Constraints and NULL ValuesForeign keys allow key values that are all DESCRIBE DEPARTMENTS;1, even if there are no matching DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 or DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 keys.
12.8.2 Defining Relationships Between Parent and Child TablesSeveral relationships between parent and child tables can be determined by the other types of constraints defined on the foreign key in the child table. No Constraints on the Foreign Key When no other constraints are defined on the foreign key, any number of rows in the child table can reference the same parent key value. This model allows nulls in the foreign key. This model establishes a one-to-many relationship between the parent and foreign keys that allows undetermined values (nulls) in the foreign key. An example of such a relationship is shown in between the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );88 and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );89 tables. Each department (parent key) has many employees (foreign key), and some employees might not be in a department (nulls in the foreign key). NOT NULL Constraint on the Foreign Key When nulls are not allowed in a foreign key, each row in the child table must explicitly reference a value in the parent key because nulls are not allowed in the foreign key. Any number of rows in the child table can reference the same parent key value, so this model establishes a one-to-many relationship between the parent and foreign keys. However, each row in the child table must have a reference to a parent key value; the absence of a value (a null) in the foreign key is not allowed. The same example in the previous section illustrates such a relationship. However, in this case, employees must have a reference to a specific department. UNIQUE Constraint on the Foreign Key When a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 constraint is defined on the foreign key, only one row in the child table can reference a given parent key value. This model allows nulls in the foreign key. This model establishes a one-to-one relationship between the parent and foreign keys that allows undetermined values (nulls) in the foreign key. For example, assume that the employee table had a column named DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );91, referring to an employee membership number in the company insurance plan. Also, a table named DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );92 has a primary key named DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );91, and other columns of the table keep respective information relating to an employee insurance policy. The DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );91 in the employee table must be both a foreign key and a unique key:
UNIQUE and NOT NULL Constraints on the Foreign Key When both DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 and DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraints are defined on the foreign key, only one row in the child table can reference a given parent key value, and because DESCRIBE DEPARTMENTS;1 values are not allowed in the foreign key, each row in the child table must explicitly reference a value in the parent key. This model establishes a one-to-one relationship between the parent and foreign keys that does not allow undetermined values (nulls) in the foreign key. If you expand the previous example by adding a DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraint on the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );91 column of the employee table, in addition to guaranteeing that each employee has a unique membership number, you also ensure that no undetermined values (nulls) are allowed in the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );91 column of the employee table. 12.8.3 Rules for Multiple FOREIGN KEY ConstraintsOracle Database allows a column to be referenced by multiple DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraints; there is no limit on the number of dependent keys. This situation might be present if a single column is part of two different composite foreign keys. 12.8.4 Deferring Constraint ChecksWhen Oracle Database checks a constraint, it signals an error if the constraint is not satisfied. To defer checking constraints until the end of the current transaction, use the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);09 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);10 statement. Note: You cannot use the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);09 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);10 statement inside a trigger. When deferring constraint checks:
In , the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 keys of the table ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);26 are created ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);18 and then deferred. Example 12-4 Deferring Constraint Checks DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );8 Query: DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );9 Result: ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);0 Update: ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);1 Result: ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);2 Query: ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);3 Result: ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);4 The ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);09 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);10 applies only to the current transaction, and its setting lasts for the duration of the transaction, or until another ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);09 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);10 statement resets the mode. The DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);33 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);09 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);10 statement applies only for the current session. The defaults specified when you create a constraint remain while the constraint exists. See Also: Oracle Database SQL Language Reference for more information about the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);09 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);10 statement 12.9 Minimizing Space and Time Overhead for Indexes Associated with ConstraintsWhen you create a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 or DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 key, Oracle Database checks to see if an existing index enforces uniqueness for the constraint. If there is no such index, the database creates one. When Oracle Database uses a unique index to enforce a constraint, and constraints associated with the unique index are dropped or disabled, the index is dropped. To preserve the statistics associated with the index (which would take a long time to re-create), specify the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);40 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);41 clause on the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);42 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);43 statement. While enabled foreign keys reference a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 or DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 key, you cannot disable or drop the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 or DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 key constraint or the index. Note: DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 keys with deferrable constraints must all use nonunique indexes. To use existing indexes when creating unique and primary key constraints, include ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);50 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);41 in the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);43 clause. See Also: Oracle Database SQL Language Reference for more details and examples of integrity constraints 12.10 Guidelines for Indexing Foreign KeysIndex foreign keys unless the matching unique or primary key is never updated or deleted. See Also: Oracle Database Concepts for more information about indexing foreign keys 12.11 Referential Integrity in a Distributed DatabaseThe declaration of a referential constraint cannot specify a foreign key that references a primary or unique key of a remote table. However, you can maintain parent/child table relationships across nodes using triggers. See Also: Oracle Database PL/SQL Language Reference for more information about triggers that enforce referential integrity Note: If you decide to define referential integrity across the nodes of a distributed database using triggers, be aware that network failures can make both the parent table and the child table inaccessible. For example, assume that the child table is in the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);53 database, and the parent table is in the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);54 database. If the network connection between the two databases fails, then some data manipulation language (DML) statements against the child table (those that insert rows or update a foreign key value) cannot proceed, because the referential integrity triggers must have access to the parent table in the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);54 database. 12.12 When to Use CHECK ConstraintsUse DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraints when you must enforce integrity rules based on logical expressions, such as comparisons. Never use DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraints when any of the other types of constraints can provide the necessary checking. See Also: Examples of DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraints include:
12.12.1 Restrictions on CHECK ConstraintsA DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint requires that a condition be true or unknown for every row of the table. If a statement causes the condition to evaluate to false, then the statement is rolled back. The condition of a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint has these limitations:
See Also:
12.12.2 Designing CHECK ConstraintsWhen using DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraints, remember that a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint is violated only if the condition evaluates to false; true and unknown values (such as comparisons with nulls) do not violate a check condition. Ensure that any DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint that you define is specific enough to enforce the rule. For example, consider this DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint: ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);5 At first glance, this rule may be interpreted as "do not allow a row in the employee table unless the employee salary is greater than zero or the employee commission is greater than or equal to zero." But if a row is inserted with a null salary, that row does not violate the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint, regardless of whether the commission value is valid, because the entire check condition is evaluated as unknown. In this case, you can prevent such violations by placing DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraints on both the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);85 and ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);86 columns. Note: If you are not sure when unknown values result in DESCRIBE DEPARTMENTS;1 conditions, review the truth tables for the logical conditions in Oracle Database SQL Language Reference 12.12.3 Rules for Multiple CHECK ConstraintsA single column can have multiple DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraints that reference the column in its definition. There is no limit to the number of DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraints that can be defined that reference a column. The order in which the constraints are evaluated is not defined, so be careful not to rely on the order or to define multiple constraints that conflict with each other. 12.12.4 Choosing Between CHECK and NOT NULL ConstraintsAccording to the ANSI/ISO standard, a DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraint is an example of a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint, where the condition is: ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);6 Therefore, you can write DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraints for a single column using either a DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraint or a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint. The DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 constraint is easier to use than the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint. In the case where a composite key can allow only all DESCRIBE DEPARTMENTS;1 or all non- DESCRIBE DEPARTMENTS;1 values, you must use a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint. For example, this DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraint allows a key value in the composite key made up of columns ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);05 and ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);06 to contain either all nulls or all values: ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);7 12.13 Examples of Defining Constraintsand show how to create simple constraints during the prototype phase of your database design. In these examples, each constraint is given a name. Naming the constraints prevents the database from creating multiple copies of the same constraint, with different system-generated names, if the data definition language (DDL) statement runs multiple times. creates tables and their constraints at the same time, using the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );40 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statement. Example 12-5 Defining Constraints with the CREATE TABLE Statement ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);8 creates constraints for existing tables, using the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statement. You cannot create a validated constraint on a table if the table contains rows that violate the constraint. Example 12-6 Defining Constraints with the ALTER TABLE Statement ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);9 See Also: Oracle Database Administrator's Guide for information about creating and maintaining constraints for a large production database 12.13.1 Privileges Needed to Define ConstraintsIf you have the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );40 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 or DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );40 ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);14 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 system privilege, then you can define constraints on the tables that you create. If you have the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);14 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 system privilege, then you can define constraints on any existing table. If you have the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 object privilege for a specific table, then you can define constraints on that table. DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraints require that the table owner has either the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);23 ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);24 system privilege or a quota for the tablespace that contains the associated index. You can define DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraints if the parent table or view is in your schema or you have the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);27 privilege on the columns of the referenced key in the parent table or view. See Also: 12.13.2 Naming ConstraintsAssign names to constraints DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1, DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16, DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48, DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48, and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 using the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);43 option of the constraint clause. This name must be unique among the constraints that you own. If you do not specify a constraint name, one is assigned automatically by Oracle Database. Choosing your own name makes error messages for constraint violations more understandable, and prevents the creation of duplicate constraints with different names if the SQL statements are run more than once. See the previous examples of the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );40 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statements for examples of the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);43 option of the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);42 clause. The name of each constraint is included with other information about the constraint in the data dictionary. See Also: "" for examples of static data dictionary views 12.14 Enabling and Disabling ConstraintsThis section explains the mechanisms and procedures for manually enabling and disabling constraints. enabled constraint. When a constraint is enabled, the corresponding rule is enforced on the data values in the associated columns. The definition of the constraint is stored in the data dictionary. disabled constraint. When a constraint is disabled, the corresponding rule is not enforced. The definition of the constraint is still stored in the data dictionary. An integrity constraint represents an assertion about the data in a database. This assertion is always true when the constraint is enabled. The assertion might not be true when the constraint is disabled, because data that violates the integrity constraint can be in the database. Topics: 12.14.1 Why Disable Constraints?During day-to-day operations, keep constraints enabled. In certain situations, temporarily disabling the constraints of a table makes sense for performance reasons. For example:
Temporarily turning off constraints can speed up these operations. 12.14.2 Creating Enabled Constraints (Default)When you define an integrity constraint (using either DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );40 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 or DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23), Oracle Database enables the constraint by default. For code clarity, you can explicitly enable the constraint by including the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);47 clause in its definition, as in . Example 12-7 Creating Enabled Constraints ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);0 Include the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);47 clause when defining a constraint for a table to be populated a row at a time by individual transactions. This ensures that data is always consistent, and reduces the performance overhead of each DML statement. An DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statement that tries to enable an integrity constraint fails if an existing row of the table violates the integrity constraint. The statement rolls back and the constraint definition is neither stored nor enabled. See Also: , for more information about rows that violate constraints 12.14.3 Creating Disabled ConstraintsYou define and disable an integrity constraint (using either DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );40 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 or DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23), by including the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);55 clause in its definition, as in . Example 12-8 Creating Disabled Constraints ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);1 Include the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);55 clause when defining a constraint for a table to have large amounts of data inserted before anybody else accesses it, particularly if you must cleanse data after inserting it, or must fill empty columns with sequence numbers or parent/child relationships. An DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statement that defines and disables a constraint never fails, because its rule is not enforced. 12.14.4 Enabling Existing ConstraintsAfter you have cleansed the data and filled the empty columns, you can enable constraints that were disabled during data insertion. To enable an existing constraint, use the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statement with the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);47 clause, as in . Example 12-9 Enabling Existing Constraints ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);2 An DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statement that attempts to enable an integrity constraint fails if any of the table rows violate the integrity constraint. The statement is rolled back and the constraint is not enabled. See Also: , for more information about rows that violate constraints 12.14.5 Disabling Existing ConstraintsIf you must perform a large insert or update when a table contains data, you can temporarily disable constraints to improve performance of the bulk operation. To disable an existing constraint, use the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statement with the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);55 clause, as in . Example 12-10 Disabling Existing Constraints ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);3 12.14.6 Guidelines for Enabling and Disabling Key ConstraintsWhen enabling or disabling DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16, DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48, and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraints, be aware of several important issues and prerequisites. DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 key and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraints are usually managed by the database administrator. See Also: Oracle Database Administrator's Guide and 12.14.7 Fixing Constraint ExceptionsIf a row of a table disobeys an integrity constraint, then this row is in violation of the constraint and is called an exception to the constraint. If any exceptions exist, then the constraint cannot be enabled. The rows that violate the constraint must be updated or deleted before the constraint can be enabled. You can identify exceptions for a specific integrity constraint as you try to enable the constraint. See Also: , for more information about this procedure When you try to create or enable a constraint, and the statement fails because integrity constraint exceptions exist, the statement is rolled back. You cannot enable the constraint until all exceptions are either updated or deleted. To determine which rows violate the integrity constraint, include the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);75 option in the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);47 clause of a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );40 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 or DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statement. See Also: Oracle Database Administrator's Guide for more information about responding to constraint exceptions 12.15 Modifying ConstraintsStarting with Oracle8i, you can modify an existing constraint with the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);81 ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);43 clause, as in . See Also: Oracle Database SQL Language Reference for information about the parameters you can modify Example 12-11 Modifying Constraints ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);4 12.16 Renaming ConstraintsOne property of a constraint that you can modify is its name. Situations in which you would rename a constraint include:
shows how to find the system-generated name of a constraint and change it. Example 12-12 Renaming a Constraint ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);5 Query: ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);6 Result (system-generated name of constraint name varies): ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);7 Rename constraint from name reported in preceding query to ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);83: ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);8 Query: ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);6 Result: empgender VARCHAR2(1)0 12.17 Dropping ConstraintsYou can drop a constraint using the ALTER TABLE dept_tab ADD PRIMARY KEY (deptno);42 clause of the DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );22 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );23 statement. Situations in which you would drop a constraint include:
To drop a constraint and all other integrity constraints that depend on it, specify ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);87. Example 12-13 Dropping Constraints empgender VARCHAR2(1)1 When dropping DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16, DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48, and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraints, be aware of several important issues and prerequisites. DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraints are usually managed by the database administrator. See Also:
12.18 Managing FOREIGN KEY ConstraintsDROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraints enforce relationships between columns in different tables. Therefore, they cannot be enabled if the constraint of the referenced primary or unique key is not present or not enabled. 12.18.1 Data Types and Names for Foreign Key ColumnsYou must use the same data type for corresponding columns in the dependent and referenced tables. The column names need not match. 12.18.2 Limit on Columns in Composite Foreign KeysBecause foreign keys reference primary and unique keys of the parent table, and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );47 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );16 key constraints are enforced using indexes, composite foreign keys are limited to 32 columns. 12.18.3 Foreign Key References Primary Key by DefaultIf the column list is not included in the ALTER TABLE emp_tab ADD FOREIGN KEY (deptno) REFERENCES dept_tab(deptno);27 option when defining a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraint (single column or composite), then Oracle Database assumes that you intend to reference the primary key of the specified table. Alternatively, you can explicitly specify the column(s) to reference in the parent table within parentheses. Oracle Database automatically checks to verify that this column list references a primary or unique key of the parent table. If it does not, then an informative error is returned. 12.18.4 Privileges Required to Create FOREIGN KEY ConstraintsTo create a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraint, the creator of the constraint must have privileged access to the parent and child tables.
In both cases, necessary privileges cannot be obtained through a role; they must be explicitly granted to the creator of the constraint. These restrictions allow:
12.18.5 Choosing How Foreign Keys Enforce Referential IntegrityOracle Database allows different types of referential integrity actions to be enforced, as specified with the definition of a DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );55 DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );48 constraint:
12.19 Viewing Information About ConstraintsTo find the names of constraints, what columns they affect, and other information to help you manage them, query the static data dictionary views DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );61 and empgender VARCHAR2(1)45, as in . See Also: Oracle Database Reference for information about DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );61 and empgender VARCHAR2(1)45 Example 12-14 Viewing Information About Constraints empgender VARCHAR2(1)5 List accessible constraints in DeptTab and EmpTab: empgender VARCHAR2(1)6 Result: empgender VARCHAR2(1)7 Distinguish between DESCRIBE DEPARTMENTS;3 DESCRIBE DEPARTMENTS;1 and DROP TABLE emp_tab; CREATE TABLE emp_tab ( empname VARCHAR2(80), empno INTEGER, deptno INTEGER );74 constraints in empgender VARCHAR2(1)51 and empgender VARCHAR2(1)52: empgender VARCHAR2(1)8 Result: empgender VARCHAR2(1)9 For empgender VARCHAR2(1)51 and empgender VARCHAR2(1)52, list columns that constitute constraints: CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /0 Result: CREATE OR REPLACE PROCEDURE add_employee ( e_name emp_tab.empname%TYPE, e_gender emp_tab.empgender%TYPE, e_number emp_tab.empno%TYPE, e_dept emp_tab.deptno%TYPE ) AUTHID DEFINER IS BEGIN IF UPPER(e_gender) IN ('M','F') THEN INSERT INTO emp_tab VALUES (e_name, e_gender, e_number, e_dept); ELSE DBMS_OUTPUT.PUT_LINE('Gender must be M or F.'); END IF; END; / BEGIN add_employee ('Smith', 'H', 356, 20); END; /1 Note that:
|