Constraints
Types of Constraints
NOT NULL
CREATE TABLE table_nn (
id SERIAL PRIMARY KEY,
tag text NOT NULL
);
INSERT INTO table_nn ( tag )
VALUES ('TAG 1'), ('TAG 2'), ('TAG 3'), ('');
-- NULL value wont be accepted
INSERT INTO table_nn ( tag ) VALUES (NULL);
-- empty string aren't NULL values
INSERT INTO table_nn ( tag ) VALUES ('');
-- adding another column TEXT;
ALTER TABLE table_nn
ADD COLUMN is_enable TEXT;
-- Updating values in new column to ''
-- so that NULL constrain can be added
UPDATE table_nn
SET is_enable = '' WHERE is_enable IS NULL;
-- adding null constraint on table
ALTER TABLE public.table_nn
ALTER COLUMN is_enable SET NOT NULL;UNIQUE
Adding Unique Constraint on column
DEFAULT CONSTRAINT
Primary Key
Composite Primary Key
Foreign Key
CHECK Constraint
Example
Last updated