Constraints in SQL
Constraints in SQL
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;
alter table
table_name
ENABLE constraint
constraint_name;
alter table
table_name
DISABLE constraint
constraint_name;
Check Constraint
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;
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;
ALTER TABLE
cust_table
drop constraint
fk_cust_table_ref;
alter table
cust_table
add constraint
check_cust_types
CHECK
(cust_type IN
(
'yuppie',
'dink',
'guppie'
)
);
The following errors are associated with Oracle constraint alter commands:
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.
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.
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.
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.
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:
[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:
[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;