Universal Database (netDB2) Introductory Guide


Working With Constraints

Constraints:
We can add constraints to the database created inorder to add limitations to the database. For example:

age should not proceed 70 years,
bank balance should be between 100 and 20,000

In such cases, one can put checks using constraints. The constraints can be put in a table or can be implemented using a "Alter" statement or set statement.

Example:
CREATE TABLE constraint_check
(ID SMALLINT NOT NULL,
NAME VARCHAR(9),
HIREDATE DATE,
SALARY DECIMAL(7,2),
PRIMARY KEY (ID),
CONSTRAINT YEARSAL CHECK (YEAR(HIREDATE) >= 1990 OR SALARY > 20000) ) 

This particular check implements that the year of hire should be greater than 1990 and also that the salary one is earning has to be 20,000 dollars.

Example:
ALTER TABLE school 
ADD CONSTRAINT age_check CHECK (age > 5)

This example checks the age of student, by adding a constraint to a table.