[英]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
列( NULL
或NOT NULL
),它都会像这样工作。
The docs mention nullable rowversion
only once: 文档只提到一次可空的rowversion
:
A nonnullable
rowversion
column is semantically equivalent to abinary(8)
column. 不可空的rowversion
列在语义上等同于binary(8)
列。 A nullablerowversion
column is semantically equivalent to avarbinary(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.