postgresql - 将布尔列添加到表设置默认值 [英] postgresql - add boolean column to table set default

查看:53
本文介绍了postgresql - 将布尔列添加到表设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是将列添加到默认值为 false

Is this proper postgresql syntax to add a column to a table with a default value of false

ALTER TABLE users
ADD "priv_user" BIT
ALTER priv_user SET DEFAULT '0'

谢谢!

推荐答案

ALTER TABLE users
  ADD COLUMN "priv_user" BOOLEAN DEFAULT FALSE;

也可以直接指定NOT NULL

you can also directly specify NOT NULL

ALTER TABLE users
  ADD COLUMN "priv_user" BOOLEAN NOT NULL DEFAULT FALSE;

更新:以下仅适用于 postgresql 11 之前的版本.

UPDATE: following is only true for versions before postgresql 11.

正如 Craig 在填充表格中提到的,将其拆分为多个步骤会更有效:

As Craig mentioned on filled tables it is more efficient to split it into steps:

ALTER TABLE users ADD COLUMN priv_user BOOLEAN;
UPDATE users SET priv_user = 'f';
ALTER TABLE users ALTER COLUMN priv_user SET NOT NULL;
ALTER TABLE users ALTER COLUMN priv_user SET DEFAULT FALSE;

这篇关于postgresql - 将布尔列添加到表设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆