问题描述
我有一张这样的桌子:
create table1 (field1 int,
field2 int default 5557,
field3 int default 1337,
field4 int default 1337)
我想插入一行,其中包含 field2 和 field4 的默认值.
I want to insert a row which has the default values for field2 and field4.
我试过 insert into table1 values (5,null,10,null)
但它不起作用并且 ISNULL(field2,default)
不起作用也可以工作.
I've tried insert into table1 values (5,null,10,null)
but it doesn't work and ISNULL(field2,default)
doesn't work either.
当我插入一行时,如何告诉数据库使用该列的默认值?
How can I tell the database to use the default value for the column when I insert a row?
推荐答案
只是不要在插入语句中包含要为其使用默认值的列.例如:
Just don't include the columns that you want to use the default value for in your insert statement. For instance:
INSERT INTO table1 (field1, field3) VALUES (5, 10);
...将采用 field2
和 field4
的默认值,并将 5 分配给 field1
并将 10 分配给 field3.
...will take the default values for
field2
and field4
, and assign 5 to field1
and 10 to field3
.
这篇关于如何在 SQL 表中插入默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!