How will you add a constraint on above DB2 table TAB1 for ages between 3 to 16 years?


Constraints are used to restrict the data inserted at a particular column. Constraints can be used in such a way that a value can only be inserted if it satisfies the condition given in constraints. We can give the below parameter during CREATE TABLE command to add a constraint.

CREATE TABLE DBSET1.TAB1
   (STUDENT_ID CHAR(10) NOT NULL,
   ENROLLMENT_ID CHAR(20) NOT NULL,
   NAME VARCHAR(50),
   AGE SMALLINT CONSTRAINT NUMBER CHECK
   (AGE >=3 AND AGE <=16)
   PRIMARY KEY (STUDENT_ID));

The CONSTRAINT NUMBER CHECK reserved words can be followed by the permissible range that a column can have. It can be accompanied by logical operators like AND, NOT, OR to adjust the value range.

Updated on: 11-Sep-2020

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements