How can I add a column that doesn't allow nulls in a Postgresql database?

后端 未结 8 1872
我在风中等你
我在风中等你 2020-12-07 09:08

I\'m adding a new, \"NOT NULL\" column to my Postgresql database using the following query (sanitized for the Internet):

ALTER TABLE mytable ADD COLUMN mycol         


        
8条回答
  •  醉话见心
    2020-12-07 09:48

    You have to set a default value.

    ALTER TABLE mytable ADD COLUMN mycolumn character varying(50) NOT NULL DEFAULT 'foo';
    
    ... some work (set real values as you want)...
    
    ALTER TABLE mytable ALTER COLUMN mycolumn DROP DEFAULT;
    

提交回复
热议问题