关于sql表(允许空值) [英] About sql table(Allow nulls)

查看:412
本文介绍了关于sql表(允许空值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建sql数据库表时我不允许nulls.But当我通过windows应用程序输入数据时。如果我没有给出值,则使用0作为数据。

我该怎么办? do.should我必须写那个编码。请帮助我。我找不到附加表/表定义图片的方法。

when I'm creating the sql database table I not allow nulls.But when I'm entering data through windows application .If i not give value it use 0 as data.
What should i do.should i have to write coding for that.please help me.I could not find a way attach table/table definition pictures.

推荐答案

如果你不允许在列中为空,然后当您插入新记录时,SQL必须执行以下两项操作之一,但不提供值:

1)投诉并拒绝插入但有异常。 />
2)使用列的默认值(如果已设置)。



数值的默认值为零(除非您在表中更改它)定义),但其他人不能分配默认值,因为没有合理的默认值可以应用。



在你的Windows应用程序中,如果你没有值,那么我建议你提供一个对你的Windows应用程序有意义的一个没有分配值:对于字符串列,这将是一个空字符串,对于数字我通常使用-1或最大值,或者我允许空值并检查它们!



检查您的表定义,并查看您允许的默认值。



BTW:您可以非常轻松地在此处发布表定义:只需转到SSMS,右键单击表,然后选择脚本表为...,创建要...,剪贴板。然后,您可以将其直接粘贴到您的问题中:

If you do not allow nulls in a column, then SQL has to do one of two things when you insert a new record, but don't supply a value:
1) Complain and reject the insert with an exception.
2) Use a default value for the column, if set.

Numeric values have a default of zero (unless you change that in your table definition), but others cannot be assigned a default value because there is no sensible default that can be applied.

In your windows application, if you do not have a value, then I would suggest that you supply one which makes sense to your windows application as a "no value assigned": for a string column, that would be an empty string, for numbers I generally use -1 or the maximum value, or I allow nulls and check for them!

Check your table definition, and see what default you have allowed.

BTW: You can post table definitions here very easily: just go to SSMS, right click the table, and select "Script Table as...", "CREATE To...", "Clipboard". You can then paste that directly into your question:
USE [Testing]
GO

/****** Object:  Table [dbo].[MyTable]    Script Date: 09/01/2013 09:08:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[MyTable](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](50) NULL,
	[Model] [varchar](50) NULL,
 CONSTRAINT [PK_MyTable_1] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


这篇关于关于sql表(允许空值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆