DB2: Constraints Cheat Sheet

Explanation of each constraint

  • NOT NULL
db2 create table <table_name>(col_name col_type not null,..)  
  • Unique constraints
db2 create table <tab_name>(<col> <col_type> not null unique, ...) 
  • Primary key
db2 create table <tab_name>( ,.., primary
key ()) 
  • Foreign key
db2 create table <tab_name>(<col> <col_type>,constraint 
<const_name> foreign key (<col_name>)  
                  reference <ref_table> (<ref_col>)  
  • Checking constraint
db2 create table                                                      
 (  
  primary key (),                                                       
  constraint  check (condition or condition)  
 )

Dropping the constraint

Dropping UNIQUE constraint

db2 alter table <tab_name> drop unique <const_name>

Dropping primary key

db2 alter table <tab_name> drop primary key 

Dropping check constraint

db2 alter table <tab_name> drop check <check_const_name>  

Dropping foreign key

db2 alter table <tab_name> drop foreigh key <foreign_key_name>  

Leave a Reply

Your email address will not be published. Required fields are marked *