在条件语句和 SET 中使用 DEFAULT 值

发布于 2025-01-06 06:41:46 字数 691 浏览 4 评论 0原文

DEFAULT 关键字处理有问题

示例:

 CREATE TABLE BAR(c1 int null,c2 int default 1 not null)

像这样执行 SQL:

 INSERT INTO BAR SET c1 = 1, c2 = COALESCE(null,DEFAULT)

结果为: 未找到“DEFAULT”列; SQL语句: 插入条形图集 c1 = 1,c2 = COALESCE(null,DEFAULT) [42122-161] 42S22/42122

同时:

INSERT INTO BAR (c1,c2) values (1,  DEFAULT)

好的!

但是,

 INSERT INTO BAR SET c1 = 1, c2  = DEFAULT

结果: 列“C2”不允许为 NULL; SQL语句: INSERT INTO BAR SET c1 = 1, c2 = DEFAULT [23502-161] 23502/23502

我认为这是一个错误。如果不是,有什么方法可以指定使用 默认值存在吗?

H2 1.3.161 (2011-10-28)

There is something wrong with DEFAULT keyword handling

Example:

 CREATE TABLE BAR(c1 int null,c2 int default 1 not null)

Executing SQL like this:

 INSERT INTO BAR SET c1 = 1, c2 = COALESCE(null,DEFAULT)

Results in:
Column "DEFAULT" not found; SQL statement:
INSERT INTO BAR SET c1 = 1, c2 = COALESCE(null,DEFAULT) [42122-161]
42S22/42122

At the same time:

INSERT INTO BAR (c1,c2) values (1,  DEFAULT)

Ok!

But,

 INSERT INTO BAR SET c1 = 1, c2  = DEFAULT

Result:
NULL not allowed for column "C2"; SQL statement:
INSERT INTO BAR SET c1 = 1, c2 = DEFAULT [23502-161] 23502/23502

I think it is a bug. If it is not, is any way to specify use of
default value exists?

H2 1.3.161 (2011-10-28)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗失的美好 2025-01-13 06:41:46

术语 DEFAULT 不是伪列或可以在公式中使用的表达式。 DEFAULT 用于而不表达式。我尝试了 MySQL,它的工作方式与 H2 相同。

drop table foo cascade;
CREATE TABLE FOO(c1 int null,c2 int default 1 not null);
INSERT INTO FOO SET c1 = 1, c2 = default; -- works
INSERT INTO FOO SET c1 = 1, c2 = default * 2; -- doesn't work

您可能想要的是使用参数化语句(对吗?),其中 NULL 被转换为默认值。 H2 确实支持这一点,但没有完整记录。您可以使用:

drop table foo cascade;
CREATE TABLE FOO(c1 int null,c2 int default 1 not null null_to_default);
INSERT INTO FOO SET c1 = 1, c2 = null;

The term DEFAULT isn't a pseudo-column or an expression that can be used in a formula. DEFAULT is used instead of an expression. I tried MySQL and it works in the same way as H2.

drop table foo cascade;
CREATE TABLE FOO(c1 int null,c2 int default 1 not null);
INSERT INTO FOO SET c1 = 1, c2 = default; -- works
INSERT INTO FOO SET c1 = 1, c2 = default * 2; -- doesn't work

What you probably want is use a parameterized statement (right?) where NULL is converted to default. H2 does support this, but it's not fully documented. You can use:

drop table foo cascade;
CREATE TABLE FOO(c1 int null,c2 int default 1 not null null_to_default);
INSERT INTO FOO SET c1 = 1, c2 = null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文