Вы находитесь на странице: 1из 7

Oracle "alter table" constraint

Syntax examples
Oracle Tips by Burleson Consulting

We have "alter table" syntax from Oracle to add data constraints in-place in this form:

alter table
   table_name
add constraint
   constraint_name;

We can also use "alter table" syntax to enable or disable constraints:

alter table
   table_name
ENABLE constraint
   constraint_name;

alter table
   table_name
DISABLE constraint
   constraint_name;

Check Constraint

We have details on the different types of constraints:

alter table
   table_name
add constraint
   check_constraint_name
   CHECK
   (check_column_name IN
      (
       'check_constraint1_value',
       'check_constraint2_value',
       'check_constraint3_value',
       'check_constraint4_value'
      )
   ) DISABLE|ENABLE;

Here are some examples of Oracle "alter table" syntax to add foreign key constraints.

alter table
   cust_table
add constraint
   fk_cust_name FOREIGN KEY (person_name)
references
   person_table (person_name)
initially deferred deferrable;

Here is an example of a multiple column foreign key constraint:

alter table
   cust_table
add constraint
   fk_cust_name FOREIGN KEY (person_name, person_gender)
references
   person_table (person_name, person_gender)
initially deferred deferrable;

Here is another example of Oracle "alter table" syntax to drop constraints.

ALTER TABLE
   cust_table
drop constraint
   fk_cust_table_ref;

Here we use Oracle "alter table" syntax to add a check constraint.

alter table
   cust_table
add constraint
   check_cust_types
   CHECK
   (cust_type IN
      (
       'yuppie',
       'dink',
       'guppie'
      )
   );

Oracle constraint errors

The following errors are associated with Oracle constraint alter commands:

 ORA-02290: check constraint (owner.constraintname) violated


 
 ORA-02291: integrity constraint (owner.constraintname) violated - parent
key not found
 
 ORA-02292:violated integrity constraint (owner.constraintname)- child
record found
Oracle Check Constraint tips
Oracle Tips by Burleson Consulting

Oracle Check Constraint

Oracle check constraint insures that updated or inserted values meet a specific condition.  The
Oracle check constraint check condition must return a TRUE or FALSE, much Like the WHERE
clause.  If the Oracle check constraint condition returns as TRUE when you use Oracle check
constraint, the value is accepted by the constraint. If Oracle check constraint returns the
condition as FALSE, the value is rejected. Below, we include an Oracle check constraint on the
editor_active column of the EDITOR table that insures the value is either Y or N.

To put it another way, Oracle check constraint validates incoming columns at row insert time.
With Oracle check constraint, rather than having an application verify that all occurrences of
REGION are North, South, East, or West, an Oracle CHECK constraint can be added to the table
definition to ensure the validity of the region column.

Here is an example of Oracle check constraint:

Oracle check constraint has some limitations.  For one, subqueries cannot be used within your
Oracle check constraints.  Also, an Oracle check constraint is able to reference another column.
Sysdate, currval, nextval, level, rowid, uid, user or userenv cannot be referenced with Oracle
check constraint.

Oracle check constraint cannot reference columns from other tables. There can be more than one
Oracle check constraint per column, however the values being checked with Oracle check
constraint must pass all Oracle check constraints on that column before being acceptable.  Oracle
check constraint can also be used to check multiple columns. 
Oracle check constraint does have some limitations in its ability to validate data.  If more than
one capable Oracle check constraint is needed, triggers must be implemented.

Oracle Tips by Burleson


Check Constraint

The job of the check constraint is to insure that updated or inserted values meet a specific
condition.  Like the WHERE clause, the check condition must return a TRUE or FALSE.  TRUE
and the value is accepted by the constraint, FALSE and the value is rejected.  In the EDITOR
table, we included a check constraint on the editor_active column that insures that the value is
either Y or N.

create table editor


(
  editor_key              varchar2(9) not null,
  editor_last_name       varchar2(40),
  editor_first_name      varchar2(30) not null,
  editor_hire_date       date,
  editor_active          char(1) 
  constraint active_ck check (editor_active in ('Y','N')),
  constraint ed_name_un unique
(editor_first_name,editor_last_name),
  constraint editor_pk primary key (editor_key)
    using index tablespace users
);

Check constraints are limited.  You cannot use subqueries within your check constraints.  A
check constraint  can reference another column, but it can only reference the value of the row
being checked. You cannot reference sysdate, currval, nextval, level, rowid, uid, user or userenv.

SQL> alter table editor


  2   add (constraint hire_ck check (editor_hire_date > (SYSDATE -30)));
 add (constraint hire_ck check (editor_hire_date > (SYSDATE -30)))
                                                    *
ERROR at line 2:
ORA-02436: date or system variable wrongly specified in CHECK constraint

You cannot reference another table’s columns.  You can have more than one check constraint on
a column, and the values being checked must pass all check constraint on that column before
being acceptable.  A check constraint can also check multiple columns. 

So, a check constraint is limited in its ability to validate data.  If you need a more capable check,
you must implement it as a trigger.  Triggers are beyond the scope of this book but are covered in
Easy Oracle PL/SQL and in the many PL/SQL books available.

Check constraint
A check constraint is a user-defined condition that must evaluate to TRUE or NULL
for a column value to be valid. For example, you can define a constraint to ensure
no employee is paid less than the minimum wage, say (salary > 1000). If the
condition is violated (after an Insert or Update), the entire transaction will be rolled
back.

[edit] Examples
Define check contraint in-line:

CREATE TABLE emp (empno NUMBER PRIMARY KEY,


ename VARCHAR2(20),
sal NUMBER(10,2) CHECK (sal between 1000 and 20000)
);

Define check contraint out-of-line:

CREATE TABLE emp (empno NUMBER PRIMARY KEY,


ename VARCHAR2(20),
sal NUMBER(10,2),
CONSTRAINT salcheck
CHECK (sal between 1000 and 20000)
);

Try to insert invalid data:

INSERT INTO emp VALUES (1, 'Frank', 10)


*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SALCHECK) violated

[edit] NOT NULL constraint


Columns in a table can be defined as NOT NULL to indicate that they may not
contain NULL values (a value must be entered). Example:

CREATE TABLE t1 (c1 NUMBER PRIMARY KEY, c2 DATE NOT NULL);

[edit] Comparisons
Any arithmetic expression containing a NULL always evaluates to NULL. For
example, 10 + NULL = NULL. In fact, all operators (except concatenation and the
DECODE function) return null when given a null operand.
[edit] Some invalid examples
A NULL is not equal to a NULL:

SELECT * FROM emp WHERE NULL = NULL;

A NULL cannot be "not equal" to a NULL either:

SELECT * FROM emp WHERE NULL <> NULL;

A NULL does not equal an empty string either:

SELECT * FROM emp WHERE NULL = '';

[edit] Valid examples


Select column values that are NULL:

SELECT * FROM emp WHERE comm IS NULL;

Select column values that are NOT NULL:

SELECT * FROM emp WHERE comm IS NOT NULL;

Change a column value to NULL:

UPDATE emp SET comm = NULL WHERE deptno = 20;

[edit] Sorting
In ascending order, NULL values will always be sorted last and thus appear after the
other data. In descending order NULL values will appear first. The sort order of
NULL values can be overridden using the NULLS FIRST/LAST clause.
Examples:
select * from emp order by sal desc NULLS FIRST;
select * from emp order by sal desc NULLS LAST;

Вам также может понравиться