简体   繁体   English

将可空的rowversion列添加到表中

[英]Adding a nullable rowversion column to a table

I'll keep this short and sweet. 我会保持这简短而甜蜜。 I am trying to add a column of type rowversion to an existing table. 我正在尝试将rowversion类型的列添加到现有表中。 My thought was that by adding it as NULL , existing rows wouldn't be stamped with a timestamp, but alas they were. 我的想法是,通过将其添加为NULL ,现有行不会标记时间戳,但它们是唉。 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 如果他们给你选择使它成为可空类型,那么可空与可空之间的功能区别是什么

In practice there is no functional difference (but there could be storage difference, see below). 在实践中没有功能差异(但可能存在存储差异,见下文)。 You can't insert NULL into a rowversion column. 您不能将NULL插入rowversion列。

Even if you specify NULL for the rowversion column in the INSERT statement, the server will insert the generated non-null value. 即使您在INSERT语句中为rowversion列指定NULL ,服务器也会插入生成的非null值。 And it will work like this regardless of how you declared the rowversion column ( NULL or NOT NULL ). 无论你如何声明rowversion列( NULLNOT NULL ),它都会像这样工作。


The docs mention nullable rowversion only once: 文档只提到一次可空的rowversion

A nonnullable rowversion column is semantically equivalent to a binary(8) column. 不可空的rowversion列在语义上等同于binary(8)列。 A nullable rowversion column is semantically equivalent to a varbinary(8) column. 可空的rowversion列在语义上等同于varbinary(8)列。

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). 如果指定rowversion nullable,它应占用磁盘上的更多空间,以允许存储可能的NULL值(实际上不会发生这种情况)。 Each nullable column incurs an overhead, see: How much size "Null" value takes in SQL Server 每个可空列都会产生开销,请参阅: SQL Server中“Null”值的大小

In addition to the space required to store a null value there is also an overhead for having a nullable column. 除了存储空值所需的空间之外,还存在具有可空列的开销。

Besides, varbinary(8) takes more space on disk than binary(8) to store the length of value. 此外, varbinary(8)在磁盘上占用的空间比binary(8)以存储值的长度。


Having said all this, I tried to create two tables with 10M rows each. 说完这一切后,我尝试创建两个表,每个表有10M行。 One with nullable rowversion column, second with non-nullable rowversion column. 一个具有可空的rowversion列,第二个具有非可空的rowversion列。 In my test both tables occupied exactly the same amount of disk space. 在我的测试中,两个表都占用了相同数量的磁盘空间。 I tested on SQL Server 2014 Express. 我在SQL Server 2014 Express上测试过。

Even if you set it as NULL, rowversion take a value: 即使您将其设置为NULL,rowversion也会获取一个值:

CREATE TABLE MyTest (myKey int PRIMARY KEY, myValue int);  
GO   
INSERT INTO MyTest (myKey, myValue) VALUES (1, 0);  
GO   
INSERT INTO MyTest (myKey, myValue) VALUES (2, 0);  
GO  
SELECT * FROM MyTest;
GO
ALTER TABLE MyTest ADD rv rowversion NULL;
GO
SELECT * FROM MyTest;
GO
DROP TABLE MyTest;

+-------+---------+------------+
| myKey | myValue | rv         |
+-------+---------+------------+
| 1     | 0       | 0000020331 |
+-------+---------+------------+
| 2     | 0       | 0000020332 |
+-------+---------+------------+

Check it here: http://rextester.com/ENELE48783 请在此处查看: http//rextester.com/ENELE48783

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向表中添加 rowversion 列并对现有数据进行排序 - Adding a rowversion column to a table and ordering existing data 为什么可以为空的 rowversion 列在语义上等同于 varbinary(8)? - Why is a nullable rowversion column is semantically equivalent to a varbinary(8)? SQL Server rowversion 不添加列 - SQL Server rowversion without adding a column 如何在已经具有rowversion列的表中存储rowversion值? - How do I store a rowversion values in a table that already has a rowversion column? 与表中的LastModifyOn(DateTime)列相比,使用rowversion(timestamp)有什么好处? - What is the benefit of using rowversion(timestamp) over LastModifyOn(DateTime) column in a table? SQL Server 插入包含“时间戳(行版本)”列的表 - SQL Server Insert Into Table containing a column “Timestamp (Rowversion)” 如果未提供列列表,则对具有RowVersion列的表执行INSERT操作需要一个空值。 为什么? - Doing INSERT on table with RowVersion column requires a null value if column list is not provided. Why? 在SQL Server中,有没有等效于rowversion的表? - Is there an equivalent of rowversion in SQL Server, but for a table? 使用 RowVersion 查询 SQL Server 表 - Query SQL Server table with RowVersion 向具有 rowversion/timestamp 列的表中插入大量行时出错 (SQL Server 2016) - Error when inserting large amount of rows to a table with rowversion/timestamp column (SQL Server 2016)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM