Home > Software engineering >  how to add not null constraint for a column after dropping it if the table contains data
how to add not null constraint for a column after dropping it if the table contains data

Time:11-23

I have dropped the not null constraint for a column in postgresql. Now i need to add the not null constraint back to the column. My table has data in it already . How can i do it?

This column was created: columnname TEXT NOT NULL

CodePudding user response:

As described in Pg docs you can do something like this

update table_name set columnname='fill null data' where columnname is null;
ALTER TABLE table_name ALTER COLUMN columnname SET NOT NULL;

ALTER TABLE table_name ALTER COLUMN columnname SET default ' ';

  • Related