SQL:使用 NULL 值与默认值 [英] SQL: Using NULL values vs. default values

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

问题描述

在 SQL 中使用 NULL 值<相比有什么优点缺点em>默认值?

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.

推荐答案

数据库中的 NULL 值是一个系统占用一字节的值存储并指示一个值是不存在而不是空间或零或任何其他默认值.这包含数据库中的字段NULL 值表示内容这个单元格在当时是未知的看着它.允许的列NULL 值还允许行根本没有插入任何值柱子.有几个优点和使用 NULL 值的缺点而不是默认值:

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:

优点

NULL 值没有数据类型,因此可以插入到任何数据结构和任何数据库柱子.默认值,另一方面手,需要有他们的数据类型指定和默认值合二为一列在另一个中可能看起来相同列,但它可能是不同的类型.

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 常用于模式中值是可选的.这是一个方便省略数据输入的方法未知领域,而不必实施额外的规则,比如以整数存储负值表示省略数据的字段.

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.

由于NULL值只占1一点内存空间,它们可能是在优化数据库时很有用.使用这些值要多得多比默认值有效,例如字符的 8 位和整数16 位.

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.

虽然您的系统要求可能随时间变化和默认值类型,NULL 值总是NULL 所以不需要更新数据类型.

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.

为表模式分配非空还可以帮助进行表格验证,从某种意义上说,带有 Not 的列空标准将需要一个值被插入.默认值不拥有这些能力.

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.

缺点

NULL 值很容易与空字符串,返回用户的空白值,当被选中.从这个意义上说,默认值不那么令人困惑,并且是更安全的选项,除非默认值设置为空字符串.

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.

如果 NULL 值被允许在数据库,它们可能会导致设计者一些额外的时间和工作,他们可以使数据库逻辑更复杂,尤其是当有与空值的大量比较地点.

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.

来源:利弊

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

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