码迷,mamicode.com
首页 > 其他好文 > 详细

Identity column 和 not for replication

时间:2016-07-30 23:56:17      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

Identity column的值是SQL Server Engine自动生成的,具有唯一和递增的特性,默认情况下,用户不能显式插入数值。在Replication中,如果ID列需要被同步到其他Subscriber中,那么如何使两个table的ID 列数值保持一致? 答案是在创建table时,为ID 列 指定 not for replication 属性。当distribution agent 执行Insert 命令时,ID 列能够被显式赋值,并且ID列的标识值不会自增,跟普通的整数型column 的行为相同。

如果ID列没有指定 not for replication 属性,那么每次插入数据时,不管Insert 操作是失败还是成功,其ID列的标识值都会自增。

Syntax simplified

<column_definition> ::= 
column_name <data_type>
    [ COLLATE collation_name ] 
    [ NULL | NOT NULL ]
    [ 
    [ CONSTRAINT constraint_name ] DEFAULT constant_expression ]
    |IDENTITY [ ( seed ,increment )
] [ NOT FOR REPLICATION ]   ]

IDENTITY

Indicates that the new column is an identity column. When a new row is added to the table, the Database Engine provides a unique, incremental value for the column. Identity columns are typically used with PRIMARY KEY constraints to serve as the unique row identifier for the table.Only one identity column can be created per table. Bound defaults and DEFAULT constraints cannot be used with an identity column. Both the seed and increment or neither must be specified. If neither is specified, the default is (1,1).

NOT FOR REPLICATION

In the CREATE TABLE statement, the NOT FOR REPLICATION clause can be specified for the IDENTITY property, FOREIGN KEY constraints, and CHECK constraints. If this clause is specified for the IDENTITY property, values are not incremented in identity columns when replication agents perform inserts. If this clause is specified for a constraint, the constraint is not enforced when replication agents perform insert, update, or delete operations.

参考文档:

Replicate Identity Columns

When you assign an IDENTITY property to a column, Microsoft SQL Server automatically generates sequential numbers for new rows inserted in the table containing the identity column. Because identity columns might be included as a part of the primary key, it is important to avoid duplicate values in the identity columns. To use identity columns in a replication topology that has updates at more than one node, each node in the replication topology must use a different range of identity values, so that duplicates do not occur.

For example, the Publisher could be assigned the range 1-100, Subscriber A the range 101-200, and Subscriber B the range 201-300. If a row is inserted at the Publisher and the identity value is, for example, 65, that value is replicated to each Subscriber. When replication inserts data at each Subscriber, it does not increment the identity column value in the Subscriber table; instead, the literal value 65 is inserted. Only user inserts, but not replication agent inserts cause the identity column value to be incremented.

 

Identity column 和 not for replication

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/5078063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!