如何在 Postgresql 数据库中添加不允许为空的列? [英] How can I add a column that doesn't allow nulls in a Postgresql database?
本文介绍了如何在 Postgresql 数据库中添加不允许为空的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下查询(针对 Internet 进行了清理)向我的 Postgresql 数据库添加一个新的NOT NULL"列:
ALTER TABLE mytable ADD COLUMN mycolumn 字符变化(50) NOT NULL;
每次运行此查询时,都会收到以下错误消息:
<块引用>错误:列mycolumn"包含空值
我被难住了.我哪里出错了?
注意:我主要使用 pgAdmin III (1.8.4),但是当我从终端内运行 SQL 时,我收到了同样的错误.
解决方案
你必须设置一个默认值.
ALTER TABLE mytable ADD COLUMN mycolumn 字符变化(50) NOT NULL DEFAULT 'foo';...一些工作(根据需要设置实际值)...ALTER TABLE mytable ALTER COLUMN mycolumn DROP DEFAULT;
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 mycolumn character varying(50) NOT NULL;
Each time I run this query, I receive the following error message:
ERROR: column "mycolumn" contains null values
I'm stumped. Where am I going wrong?
NOTE: I'm using pgAdmin III (1.8.4) primarily, but I received the same error when I ran the SQL from within Terminal.
解决方案
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;
这篇关于如何在 Postgresql 数据库中添加不允许为空的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文