Controlling Constraints, Identities, and Triggers with NOT FOR REPLICATION

本文详细介绍了 SQL Server 2008 R2 中的 NOT FOR REPLICATION 选项,该选项允许指定在复制过程中如何处理外键约束、检查约束、标识列和触发器等数据库对象。文章还提供了具体的使用场景和解决方法。
摘要由CSDN通过智能技术生成



SQL Server 2008 R2            

In some cases, it is desirable for user activity in a replication topology to be treated differently from agent activity. For example, if a row is inserted by a user at the Publisher and that insert satisfies a check constraint on the table, it might not be required to enforce the same constraint when the row is inserted by a replication agent at the Subscriber. The NOT FOR REPLICATION option allows you to specify that the following database objects are treated differently when a replication agent performs an operation:

  • Foreign key constraints

    The foreign key constraint is not enforced when a replication agent performs an insert, update, or delete operation.

  • Check constraints

    The check constraint is not enforced when a replication agent performs an insert, update, or delete operation.

  • Identity columns

    The identity column value is not incremented when a replication agent performs an insert operation.

  • Triggers

    The trigger is not executed when a replication agent performs an insert, update, or delete operation.

When a table is published, schema options control how objects are created in the subscription database. The default schema options differ by publication. When options are set to specify that foreign key constraints and check constraints are created in the subscription database, the NOT FOR REPLICATION option is set. The NOT FOR REPLICATION option is also set when replicating identity columns in merge publications and transactional publications that support updatable subscriptions. For more information about replicating identity columns, see Replicating Identity Columns.

In most cases the default settings are appropriate, but they can be changed if an application requires different behavior. The main area to consider is triggers. For example, if you define an insert trigger with the NOT FOR REPLICATION option set, all user inserts fire the trigger, but inserts from replication agents do not. Consider a trigger that inserts data into a tracking table: when the user inserts the row originally, it is appropriate for the trigger to fire and enter a row into the tracking table, but the trigger should not fire when that data is replicated to the Subscriber, because it would result in an unnecessary row being inserted in the tracking table.

To specify the NOT FOR REPLICATION option

The NOT FOR REPLICATION option can be specified in the following ways:

主要针对identity这种自增长的列,SQLserver 2008 主要决定改列的增长值是不是因为replication在源端往目标端插入式触发一次+1,如果设置了改参数的Indentity列就不会自动增长,而是仅仅将值复制过去。除非如下图中报错,在创建publication时将identity的 value设置成manual 或者auto(事实从实际脚本发现即使设置了这参数,也依然无法解决这个问题)


具体解决方法见参考微软一片实验的blog:

https://blogs.msdn.microsoft.com/apgcdsd/2012/04/24/not-for-replication/

其中提及:Not for Replication”属性也适用于发布方和订阅方的Check约束、外键约束和标识列-Identity Column上。比如,从发布方复制数据到订阅方时,设置订阅服务器上的Check约束为“Not for Replication”,来防止复制代理在复制数据时由于此约束而被限制。


实际工作中的一个初始化报错例子


检查正确环境的表结构(注意标黄部分):


报错环境的表结构(注意黄色部分并未设置 Not For Replication)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值