postgresql 插入默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14602992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:46:29  来源:igfitidea点击:

insert DEFAULT values

postgresqlinsertdefaultdefault-value

提问by Borys

Is there ANY difference between those two statements?:

这两个陈述之间有什么区别吗?:

INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets');

and:

和:

INSERT INTO distributors (dname) VALUES ('XYZ Widgets');

I mean is there at least one reason to use one or another pattern in some particular situation or is it completely the same? did is a serial column.

我的意思是在某些特定情况下至少有一个理由使用一种或另一种模式还是完全相同?did 是一个串行列。

回答by Pablo Santa Cruz

It's exactly the same thing. No need to pick one instead of the other.

这是完全一样的事情。无需选择一个而不是另一个。

Usually defaultkeyword is handy when you have computer-generated code. It makes life easier to just use every single column in the insert clause and just use defaultwhen you don't have a specific value for certain column.

default当您拥有计算机生成的代码时,通常关键字很方便。仅使用插入子句中的每一列并仅default在您没有特定列的特定值时使用,这使生活更轻松。

Other than that, as I said, it's the same.

除此之外,正如我所说,它是一样的。

回答by Hanky Panky

INSERT INTO distributors (dname) VALUES ('XYZ Widgets');

This is fine, you do not need to specify a field if you want its default value being saved, provided a default is set.

这很好,如果您希望保存默认值,则无需指定字段,前提是设置了默认值。