向表添加可为空的rowversion列 [英] Adding a nullable rowversion column to a table

查看:123
本文介绍了向表添加可为空的rowversion列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会简短而甜美的.我正在尝试将rowversion类型的列添加到现有表中.我的想法是,通过将其添加为NULL,不会对现有行加盖时间戳,但可惜它们是时间戳.如果是这种情况,那么在什么情况下该列会接受空值?

I'll keep this short and sweet. I am trying to add a column of type rowversion to an existing table. My thought was that by adding it as NULL, existing rows wouldn't be stamped with a timestamp, but alas they were. If that is the behavior, in what circumstances will the column ever admit a null value?

推荐答案

如果他们可以选择将其设置为可为空的类型,那么可为空和不可为空之间的功能区别是什么

if they give you the option of making it a nullable type, what is the functional difference between nullable vs non-nullable

在实践中,没有功能上的差异(但可能存在存储差异,请参见下文).您不能在rowversion列中插入NULL.

In practice there is no functional difference (but there could be storage difference, see below). You can't insert NULL into a rowversion column.

即使您为INSERT语句中的rowversion列指定了NULL,服务器也会插入生成的非null值.不管您如何声明rowversion列(NULLNOT NULL),它都将像这样工作.

Even if you specify NULL for the rowversion column in the INSERT statement, the server will insert the generated non-null value. And it will work like this regardless of how you declared the rowversion column (NULL or NOT NULL).

文档仅提及一次可空rowversion:/p>

The docs mention nullable rowversion only once:

不可为空的rowversion列在语义上等效于 binary(8)列.可为空的rowversion列在语义上 等同于varbinary(8)列.

A nonnullable rowversion column is semantically equivalent to a binary(8) column. A nullable rowversion column is semantically equivalent to a varbinary(8) column.

如果指定rowversion可为空,则它应该在磁盘上占据更多空间,以允许存储可能的NULL值(实际上是不会发生的).每个可为空的列都会产生开销,请参见:空"值进入SQL Server

If you specify rowversion nullable it should occupy more space on disk to allow storage of the possible NULL values (which in practice can't happen). Each nullable column incurs an overhead, see: How much size "Null" value takes in SQL Server

除了存储空值所需的空间外,还存在 具有可为空列的开销.

In addition to the space required to store a null value there is also an overhead for having a nullable column.

此外,varbinary(8)在磁盘上所占空间比binary(8)更多,以存储值的长度.

Besides, varbinary(8) takes more space on disk than binary(8) to store the length of value.

说了这么多,我试图创建两个表,每个表有10M行.一个具有可为空的rowversion列,第二个具有不可为空的rowversion列.在我的测试中,两个表占用的磁盘空间完全相同.我在SQL Server 2014 Express上进行了测试.

Having said all this, I tried to create two tables with 10M rows each. One with nullable rowversion column, second with non-nullable rowversion column. In my test both tables occupied exactly the same amount of disk space. I tested on SQL Server 2014 Express.

这篇关于向表添加可为空的rowversion列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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