PostgreSQL Alter table add check constraint to a table

Add check constraint to table

How to add check constraint to an existing table. Alter syntax and example.

Examples

Add check constraint to a table and all its children.

ALTER TABLE customers 
ADD CONSTRAINT ch_first_name 
CHECK (char_length(first_name) <= 20);

Add check constraint only to a table and not to its children.

ALTER TABLE customers 
ADD CONSTRAINT ch_first_name 
CHECK (char_length(first_name) <= 20) NO INHERIT;