当allow null为true时,为什么默认值不插入列? [英] Why default value is not inserting in column when allow null is true?
问题描述
我有一个表地址,其中有一个名为faxno的列。允许为null并将默认值设置为0.当我们使用数据集以编程方式插入记录时,我们将faxno值设为Null。但不是0。
我不明白为什么会发生这种情况?
是否有可能插入默认值而不是Null?
这是因为只有当你没有插入任何数据时才会应用默认值那一栏。
让我们看看下表。这里FaxNo列有一个默认约束,默认值设置为0.
CREATE TABLE 地址
(
ID INT IDENTITY ( 1 , 1 ),
[名称] VARCHAR ( 100 ),
FaxNo VARCHAR ( 100 ) DEFAULT 0
)
将一些数据插入地址表。在第一个插入语句中,我们在FaxNo列中插入NULL,在第二个插入语句中,我们只插入名称。
INSERT INTO 地址([名称],FaxNo) VALUES (' Bob', NULL )
INSERT INTO 地址([名称]) VALUES (' James')
< pre lang =SQL> SELECT * FROM 地址
查询地址表将给出以下输出。
1 Bob NULL
2 James 0
Set该表不允许空值,并且在插入时未赋值时将采用默认值。
你可以这样尝试
创建 表 tbDefault(id int ,name numeric ( 10 , 2 )约束 ct_default 默认 0 。< span class =code-digit> 0 )
insert into tbDefault (id)值( 1 )
选择 * 来自 tbDefault
我希望这会对你有帮助。
I have a table address in which a column called "faxno" which allows null and has default value as 0.When we insert a record programmatically using dataset we are getting the faxno value as "Null" but not as "0".
I didn''t understand why this is happening?
Is there any possiblity to insert default value instead of Null?
This is because the default value will be applied only when you don''t insert any data into that column.
Lets look at the below table. Here FaxNo column has a default constraint with default value set to 0.
CREATE TABLE Address ( ID INT IDENTITY(1,1), [Name] VARCHAR(100), FaxNo VARCHAR(100) DEFAULT 0 )
Insert some data into the Address table. In the first insert statement we are inserting NULL into FaxNo column and in second insert statement we are only inserting the Name.
INSERT INTO Address ([Name], FaxNo) VALUES ('Bob', NULL) INSERT INTO Address ([Name]) VALUES ('James')
SELECT * FROM Address
Querying the address table will give the following output.
1 Bob NULL 2 James 0
Hi,
Set the table not to allow nulls and it will take the default value when a value is not assigned on insert.
u can try like that
create table tbDefault(id int,name numeric(10,2) constraint ct_default default 0.0 ) insert into tbDefault(id) values(1) select * from tbDefault
i hope this will help u.
这篇关于当allow null为true时,为什么默认值不插入列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!