SQL:使用NULL值与缺省值 [英] SQL: Using NULL values vs. default values

查看:188
本文介绍了SQL:使用NULL值与缺省值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL中使用 NULL值 优点 em>默认值



PS。这里有很多类似的问题,但没有回答我的问题。

解决方案


数据库是系统
值,它占用
存储的一个字节,并指示值为
不存在,而不是空格或
零或任何其他默认值。在包含
NULL值的数据库中的
字段意味着
此单元格的内容在
查看时是未知的。允许$​​ b $ b NULL值的列也允许在
列中插入没有任何值的行
。有几个pro和
cons使用NULL值,而
则为默认值:



优点 >

NULL值没有数据
类型,因此可以插入到任何
数据结构和任何数据库
列。在另一个
手上的默认值需要指定它们的数据类型
,并且一个
列中的默认值可能在另一个
列中看起来相同,但它可能是不同的
类型。



NULL通常用于其中
值是可选的模式。这是一个方便的
方法省略
未知字段的数据输入,而不必
实现其他规则,如
在整数中存储负值
字段表示省略数据。



由于NULL值仅占用1
位的内存空间,因此在优化数据库时,它们可能是有用的

使用这些值比
比默认值高得多。
字符的8位和整数
16位。



虽然您的系统需求可能会随时间而改变,
类型与它们,NULL值总是
NULL所以没有必要更新
类型的数据。



为表模式分配不空
也可以帮助表验证,
在某种意义上说,带有Not
Null标准的列将需要一个值插入
。默认值不是
具有这些功能。



缺点



NULL值容易与
空字符串混淆,当选择
时,空字符串返回
a空值给用户。在这个意义上,默认的
值是混乱的,并且是
safer选项,除非默认值
被设置为空字符串。



如果在
数据库中允许NULL值,那么它们可能会导致设计者
额外的时间,并可以正常工作
make数据库逻辑更多的是
,特别是当有
a的时候,很多比较都是在
中的空值。



$ b b

资料来源:帮助


What are the pros and cons of using NULL values in SQL as opposed to default values?

PS. Many similar questions has been asked on here but none answer my question.

解决方案

A NULL value in databases is a system value that takes up one byte of storage and indicates that a value is not present as opposed to a space or zero or any other default value. The field in a database containing the NULL value means that the content of this cell is unknown at the time of looking at it. A column that allows NULL values also allows rows to be inserted with no values at all in that column. There are several pros and cons of using NULL values as opposed to default values:

Pros

NULL value does not have the data type, therefore can be inserted to any data structure and any database column. Default values, on the other hand, need to have their data type specified and a default value in one column might look the same in another column, but it might be of a different type.

NULL is often used in schemas where a value is optional. It is a convenient method for omitting data entry for unknown fields without having to implement additional rules, like storing negative values in an integer field to represent omitted data.

Since the NULL value takes up only 1 bit of memory space, they may be useful when optimising the database. Using those values is much more efficient than default values, e.g. character’s 8 bits and integer’s 16bits.

While your system requirements may change over time and the default value types with them, NULL value is always NULL so there is no need to update the type of data.

Assigning Not Null to table schemas can also help with table validation, in a sense that the column with Not Null criteria will require a value to be inserted. Default values do not have these capabilities.

Cons

NULL values are easily confused with empty character strings, which return a blank value to the user when selected. In this sense, default values are less confusing and are the safer option, unless the default value is set to the empty string.

If NULL values are allowed in the database, they may cause the designer some extra time and work as they can make the database logic more complicated, especially when there are a lot of comparisons to null values in place.

Source: Pro and cons

这篇关于SQL:使用NULL值与缺省值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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