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

后端 未结 8 1820
我在风中等你
我在风中等你 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:54

    Or, create a new table as temp with the extra column, copy the data to this new table while manipulating it as necessary to fill the non-nullable new column, and then swap the table via a two-step name change.

    Yes, it is more complicated, but you may need to do it this way if you don't want a big UPDATE on a live table.

提交回复
热议问题