sql server 复制

This article will show how triggers, indexes, constraints and foreign keys will replicate in snapshot and after snapshot in SQL Server replication.

本文将展示触发器,索引,约束和外键将如何在快照中以及快照在SQL Server复制中进行复制。

Merge replication is used to replicate data from a publisher to a subscriber database and vice versa. Recently someone at our workplace came to us saying that SQL Server replication was not working in DEV environment and when we check replication monitor merge agents were running without issues and everything was working as expected. We asked him what was the issue and he said he created a new trigger and it did not replicate to the subscribers. He was under the assumption that adding a table to merge replication will simply replicate all the objects that were created on top of the table.

合并复制用于将数据从发布者复制到订户数据库,反之亦然。 最近,我们工作场所的某个人来找我们,说SQL Server复制在DEV环境中不起作用,并且当我们检查复制监视器合并代理程序是否正常运行且一切都按预期运行时。 我们问他是什么问题,他说他创建了一个新触发器,并且没有复制到订户。 他假设添加表以合并复制将仅复制在表顶部创建的所有对象。

Triggers, Indexes, constraints and foreign keys created on a table show different behavior while applying snapshot and after applying snapshot.

在表上创建的触发器,索引,约束和外键在应用快照时和应用快照后显示不同的行为。

Let us create sample tables, indexes, triggers, constraints and foreign keys before adding it to replication and create them after generating snapshot as well.

让我们在将其添加到复制之前创建示例表,索引,触发器,约束和外键,并在生成快照后创建它们。

We’ll use the following databases.

我们将使用以下数据库。

  • Publisher DB: test_pub 发布者数据库:test_pub
  • Subscriber DB: test_sub 订户数据库:test_sub

We’ll use Merge replication as the type of SQL Server replication.

我们将使用合并复制作为SQL Server复制的类型。

快照前 (Before Snapshot)

In this example both subscriber database and publisher database are on the same server. To illustrate this, I created a sample table, triggers, non-clustered indexes and foreign keys on top of the tables in publisher database before adding the table to merge replication.

在此示例中,订户数据库和发布者数据库都在同一服务器上。 为了说明这一点,在添加表以合并复制之前,我在发布者数据库的表顶部创建了一个示例表,触发器,非聚集索引和外键。

CREATE TABLE PARENT1 ( ID INT CONSTRAINT PK_PARENT1 PRIMARY KEY )
CREATE TABLE PARENT2 ( ID INT CONSTRAINT PK_PARENT2 PRIMARY KEY )CREATE TABLE CHILD1 ( ID1 INT, ID2 INT , CONSTRAINT FK1_CHILD1 FOREIGN KEY ( ID1 ) REFERENCES PARENT1( ID ) )CREATE TABLE KEYCHECK (ID INT)

Below is the code to create one trigger, default Constraint, check constraint and non-clustered index on table PARENT1.

下面是在表PARENT1上创建一个触发器,默认约束,检查约束和非聚集索引的代码。

CREATE NONCLUSTERED INDEX [NonClusteredIndex1] ON [dbo].[PARENT1] ( [ID] ASC )
GOCREATE TRIGGER PARENT1_TRIG ON [dbo].[PARENT1]
FOR INSERT
NOT FOR REPLICATION
AS
BEGIN
PRINT 'Test'
End
GOALTER TABLE PARENT1
ADD CHECK (id<>10);
GO
ALTER TABLE PARENT1
ADD CONSTRAINT DF_PARENT1 DEFAULT 0 FOR ID;
GO

Now let us create SQL Server merge replication and add these tables to the merge publication.

现在,让我们创建SQL Server合并复制,并将这些表添加到合并发布中。

Login to server using SQL Server Management studio -> Navigate to the SQL Server replication folder -> click on Local publications -> right click -> click on New Publication. Please refer to the below image.

使用SQL Server Management Studio登录到服务器->导航到SQL Server复制文件夹->单击本地发布 ->右键单击->单击新建发布 。 请参考下图。

Click on Next -> select the publisher database. Click Next and select Merge replication.

单击下一步 ->选择发布者数据库。 单击下一步,然后选择合并复制。

Click Next and check 2008 or later -> Add the tables you created earlier. Please refer to the below image.

单击下一步,然后检查2008或更高版本 ->添加您之前创建的表。 请参考下图。

After adding the tables to replication, click on Article properties -> set properties of all table articles -> make sure you set below properties to true.

将表添加到复制后,单击文章属性 ->设置所有表文章的属性->确保将以下属性设置为true。

Click Next -> Next -> add filter if you want to replicate specific data to the subscribers or you can skip this step and click Next -> check create snapshot immediately and uncheck schedule the snapshot agent to run at following times.

如果要复制特定数据到订阅服务器,请单击下一步 -> 下一步 ->添加过滤器,或者可以跳过此步骤,然后单击下一步 ->选中立即创建快照,然后取消选中安排快照代理在以下时间运行。

Click Next and add the credentials you want to use for snapshot agent and connecting publisher -> Next -> Next -> and input publication name and finish.

单击下一步,然后添加要用于快照代理和连接发布者的凭据-> 下一步 -> 下一步 ->并输入发布名称和完成。

Right click on publication you just created, launch SQL Server replication monitor and make sure the snapshot is completed.

右键单击刚创建的发布,启动SQL Server复制监视器,并确保快照已完成。

Navigate to the publication you just created, right click and click on New subscription -> Next -> Next -> select the type of subscription you want (Pull or push) as per your need -> Next -> add the subscriber server and select the subscriber database. Add the logins to run merge agent, connect the publisher, distributor and the subscriber -> Next -> select agent schedule as per your need. In my case I selected Run continuously -> Initialize immediately -> select subscription type as per your need -> check create subscriber and click Next -> Finish.

导航到您刚刚创建的发布,右键单击并单击“ 新建订阅” ->“ 下一步” ->“ 下一步” ->根据需要选择想要的订阅类型(拉或推)->“ 下一步” ->“添加订阅服务器”并选择订户数据库。 添加登录以运行合并代理,根据需要连接发布者,分发者和订阅者-> 下一步 ->选择代理时间表。 就我而言,我选择了连续运行 -> 立即初始化 ->根据需要选择订阅类型->检查创建订阅者,然后单击下一步 -> 完成

Once the snapshot is applied go to the subscriber and check the objects we created on PARENT1 table.

应用快照后,转到订阅服务器并检查我们在PARENT1表上创建的对象。

We can see that default constraint, check constraint, non-clustered index and trigger got replicated to the subscriber as we set article properties true for above object types.

当我们将上述对象类型的商品属性设置为true时,我们可以看到默认约束,检查约束,非聚集索引和触发器已复制到订阅服务器。

快照后 (After snapshot )

Now the initial snapshot is applied on the subscriber and the data is being replicated bidirectionally.

现在,初始快照将应用到订阅服务器,并且数据将被双向复制。

Let us see the behavior of these objects by creating new non- clustered index, default constraint, check constraint, trigger and foreign key and check if they were replicated to the subscriber or not.

让我们通过创建新的非聚簇索引,默认约束,检查约束,触发器和外键,并检查是否将它们复制到订户来查看这些对象的行为。

触发条件: (Triggers:)

At publisher server, on the publisher database let us create new trigger on table PARENT1.

在发布服务器上,在发布服务器上,让我们在表PARENT1上创建新触发器。

CREATE TRIGGER PARENT1_TRIG2 ON PARENT1
FOR INSERT
NOT FOR REPLICATION
AS
BEGIN
PRINT 'test2'
END

Wait for replication to sync. Now go to the subscriber and check if the new trigger is created at the subscriber.

等待复制同步。 现在转到订阅服务器,并检查是否在订阅服务器上创建了新触发器。

The newly created trigger PARENT1_TRIG2 on table PARENT1 did not replicate.

在表PARENT1上新创建的触发器PARENT1_TRIG2没有复制。

At publisher server, on the publisher database let us modify the existing trigger PARENT1_TRIG which is on both publisher and subscriber.

在发布服务器上的发布服务器上,让我们修改发布服务器和订阅服务器上的现有触发器PARENT1_TRIG。

USE [Z_TEST1]
GOSET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GOALTER TRIGGER [dbo].[PARENT1_TRIG] ON [dbo].[PARENT1]
FOR INSERT
NOT FOR REPLICATION
AS
BEGIN
PRINT 'ALTER TEST'
End

Now wait for SQL Server replication sync and we can see “ALTER TRIGGER” got replicated to the subscriber.

现在等待SQL Server复制同步,我们可以看到“ ALTER TRIGGER”已复制到订阅服务器。

At the publisher server, on the publisher database let us drop the existing trigger PARENT1_TRIG on table “PARENT1”

在发布服务器上,在发布服务器上,让我们将现有触发器PARENT1_TRIG放在表“ PARENT1”上

USE [Z_TEST1]
GODROP TRIGGER PARENT1_TRIG

Wait for replication sync and on the subscriber we can see “DROP TRIGGER” did not replicate to the subscriber.

等待复制同步,在订阅服务器上,我们可以看到“ DROP TRIGGER”没有复制到订阅服务器。

Results

结果

  • Triggers which were created before snapshot got replicated to the subscribers while applying initial snapshot (Can turn off this in SQL Server replication settings) 在应用初始快照时将快照复制到订阅服务器之前创建的触发器(可以在SQL Server复制设置中将其关闭)
  • Triggers which were created after snapshot are not replicated and if you want them on the subscriber, please create them manually on the subscriber 快照后创建的触发器不会复制,如果要在订阅服务器上使用,请在订阅服务器上手动创建
  • Alter trigger will replicate if the trigger exists on the publisher and the subscriber 如果触发器存在于发布者和订阅者上,则将复制该触发器
  • Alter trigger does not replicate if the trigger is only on the publisher 如果触发器仅在发布者上,则该触发器不会复制
  • Drop trigger does not replicate. If you want to drop them on the subscriber, do it manually 删除触发器不会复制。 如果要将它们放在订阅服务器上,请手动进行

默认约束和检查约束: (Default and Check Constraints:)

At publisher server, on the publisher database let us create new default and check constraints on table PARENT2. Please refer to the below code.

在发布者服务器上,让我们在发布者数据库上创建新的默认值,并检查表PARENT2上的约束。 请参考下面的代码。

ALTER TABLE PARENT2
ADD CHECK (id<>10);
GOALTER TABLE PARENT2
ADD CONSTRAINT DF_PARENT2 DEFAULT 0 FOR ID;
GO

Wait for SQL Server replication sync. Now go to the subscriber and check for the newly created constraints. We can see the new constraints got replicated to the subscribers.

等待SQL Server复制同步。 现在转到订户并检查新创建的约束。 我们可以看到新的约束已复制到订户。

Now let us drop the existing constraints on table PARENT1 at publisher.

现在,让我们在发布服务器上将现有约束放到表PARENT1上。

USE [Z_TEST1]
GOALTER TABLE [dbo].[PARENT1] DROP CONSTRAINT [DF_PARENT1]
GOALTER TABLE [dbo].[PARENT1] DROP CONSTRAINT [CK__PARENT1__ID__3D9E16F4]
GO

Results

结果

  • The newly created default constraints are replicated to the subscribers 新创建的默认约束被复制到订户
  • The newly created check constraints are replicated to the subscribers 新创建的检查约束被复制到订户
  • Drop constraint replicated to the subscribers 删除约束复制到订户
  • Check and default constraints which were created before snapshot got replicated while applying initial snapshot (We can turn off this in article properties) 应用初始快照时在复制快照之前创建的检查约束和默认约束(我们可以在文章属性中关闭此约束)

非聚集索引: (Non-Clustered index:)

At the publisher, on the publisher database let us create a new index on table PARENT1. Please refer to below T-SQL script to create index and disable index.

在发布者上,在发布者数据库上,让我们在表PARENT1上创建一个新索引。 请参考下面的T-SQL脚本来创建索引和禁用索引。

USE [Z_TEST1]
GO
CREATE INDEX NonClusteredIndex2 ON PARENT1(ID)

After SQL Server replication sync go to the subscriber and check for newly created index. The new index which we created did not replicated.

SQL Server复制同步后,转到订阅服务器并检查新创建的索引。 我们创建的新索引未复制。

Let us disable the index on table PARENT1 at publisher database.

让我们在发布者数据库上禁用表PARENT1上的索引。

USE [Z_TEST1]
GO
ALTER INDEX NonClusteredIndex1 ON PARENT1 DISABLE;

Disable index did not replicate to the subscriber. We can check that from below query.

禁用索引未复制到订阅服务器。 我们可以从下面的查询中检查。

SELECT name,is_disabled FROM sys.indexes where name ='NonClusteredIndex1'

On the publisher database let us drop the existing index on table PARENT1.

在发布者数据库上,让我们将现有索引放在表PARENT1上。

USE [Z_TEST1]
GO
DROP INDEX [NonClusteredIndex1] ON [dbo].[PARENT1]
GO

Results

结果

  • Newly created index does not replicate to the subscriber 新创建的索引不会复制到订阅服务器
  • Disable index does not replicate to the subscriber 禁用索引不会复制到订阅服务器
  • Drop index does not replicate to the subscriber 删除索引不会复制到订户
  • Existing indexes before snapshot got replicated while applying the initial snapshot on the subscriber. (We can turn off this in article properties) 在订阅服务器上应用初始快照时,复制快照之前的现有索引。 (我们可以在文章属性中关闭此功能)

外键约束: (Foreign key Constraints:)

At the publisher server on the publisher database let us create a new foreign key on table CHILD1 which referrers to table PARENT2.

在发布者数据库上的发布者服务器上,让我们在表CHILD1上创建一个新外键,该外键引用表PARENT2。

ALTER TABLE CHILD1
ADD CONSTRAINT FK2_CHILD1
FOREIGN KEY (ID2) REFERENCES PARENT2(ID);

Wait for SQL Server replication sync. New foreign key got replicated to the subscriber.

等待SQL Server复制同步。 新的外键已复制到订户。

Let us drop the foreign key constraint on CHILD1 at publisher.

让我们在发布者处对CHILD1删除外键约束。

USE [Z_TEST1]
GO
ALTER TABLE [dbo].[CHILD1] DROP CONSTRAINT [FK1_CHILD1]
GO

Results

结果

  • Newly created foreign keys are replicated to the subscriber 新创建的外键复制到订户
  • Drop foreign key got replicated to the subscriber 删除外键已复制到订户
  • Foreign keys created before snapshot got replicated to the subscriber when initial snapshot is applied. (We can turn off this in article properties) 应用初始快照时,在快照复制到订阅服务器之前创建的外键。 (我们可以在文章属性中关闭此功能)

We have seen the behavior of triggers, foreign keys, indexes, default constraints and check constraints while applying snapshot and after applying snapshot as well during SQL Server replication.

我们已经看到了触发器,外键,索引,默认约束和检查约束的行为,它们在应用快照时以及在SQL Server复制期间应用快照后也是如此。

Based on the above results we must make deployment/upgrade scripts on replicated tables as SQL Server replication compatible.

基于以上结果,我们必须使复制表上的部署/升级脚本与SQL Server复制兼容。

For example, if we are creating a new trigger on a table which is being replicated already and want it to be on both publisher and subscriber, we must create them on both the publisher and the subscriber. If we are creating a new constraint we can just create it on the publisher.

例如,如果我们要在已经被复制的表上创建一个新触发器,并希望它同时在发布者和订阅者上,则必须在发布者和订阅者上都创建它们。 如果我们要创建新的约束,则可以在发布者上创建它。

We can also control the replication of these objects in snapshot by setting article properties while configuring the publication.

我们还可以通过在配置发布时设置文章属性来控制快照中这些对象的复制。

目录 (Table of contents)

SQL Server Replication with a table with more than 246 columns
Foreign key issues while applying a snapshot in SQL Server merge replication
SQL Server Replication (Merge) – What gets replicated and what doesn’t
SQL Server Replication (Merge) – Performance Issues in replicating schema changes
Merge SQL Server replication parameterized row filter issues
具有超过246列的表SQL Server复制
在SQL Server合并复制中应用快照时出现外键问题
SQL Server复制(合并)–复制什么,什么不复制
SQL Server复制(合并)–复制架构更改中的性能问题
合并SQL Server复制参数化的行筛选器问题

翻译自: https://www.sqlshack.com/sql-server-replication-merge-what-gets-replicated-and-what-doesnt/

sql server 复制

sql server 复制_SQL Server复制(合并)–复制什么,什么不复制相关推荐

  1. sql server 复制_SQL Server复制配置:点对点和合并复制

    sql server 复制 This article will cover SQL Server replication configuration including Peer to peer re ...

  2. sql server 面试_SQL Server复制面试问题与解答

    sql server 面试 In this article, we will discuss a number of interview questions that cover the SQL Se ...

  3. sql数据透视_SQL Server中的数据科学:取消数据透视

    sql数据透视 In this article, in the series, we'll discuss understanding and preparing data by using SQL ...

  4. sql server 别名_SQL Server别名概述

    sql server 别名 This article gives an overview of SQL Server Alias and its usage for connecting with S ...

  5. sql server 面试_SQL Server审核面试问题

    sql server 面试 In this article, we will discuss a number of common and important SQL Server Audit que ...

  6. 2008 r2 server sql 中文版补丁_SQL Server 2008 SP4 补丁

    SQL Server 2008 SP4 补丁对于客户而言,Microsoft SQL Server 2008 Service Pack 4 中的几个关键改进如下所示: 改进了从 SQL Server ...

  7. sql server 复制_SQL Server复制(合并)–复制架构更改中的性能问题

    sql server 复制 This article will review performance issues in replicating schema changes on tables in ...

  8. sql server 快照_SQL Server复制:配置快照和事务复制

    sql server 快照 In this article of the series on SQL Server replication, we will explore ways to confi ...

  9. sql server 跟踪_SQL Server作业性能–跟踪

    sql server 跟踪 描述 (Description) Keeping track of our SQL Server Agent jobs is a very effective way to ...

最新文章

  1. python中in_python中的min和in用代码实现
  2. pdo_mysql 必须 ×未安装_php pdo_mysql未安装问题解决方法_PHP教程 | 蒲公英网
  3. python标准词匹配_python匹配目标词
  4. debian 文件夹中文件大小_debian 记录用到的命令 文件夹 目录等操作
  5. 绿茶影视内容站群-明王优化版吸粉视频站群-轻量级CMS站群
  6. unittest学习记录
  7. java runtime ssh 后执行指令_酒后系列:被某厂面试官吊打后酒后整理的JVM干货
  8. python程序设计知识点汇总_Python入门知识点汇总
  9. [Jetty] jetty 内存调优
  10. python程序设计从基础到开发课后题答案夏敏捷_Python程序设计——从基础到开发(21世纪高等学校计算机应用技术规划教材)...
  11. 深度学习——词汇embedded、Beam Search
  12. 你认识细菌吗?一文带你读懂了解它
  13. Kotlin:对let,apply,run,also等作用域函数的理解
  14. 【矩阵论】8. 常用矩阵总结——镜面阵,正定阵
  15. Linux·工作队列
  16. android google map 标记,android,在googlemap上从位图添加标记
  17. 树莓派利用红外线实现CSI摄像头自动拍照
  18. 【每天学一点系列~】还在困惑数据结构(尤其是链表)里指针的看这里!!!
  19. CATTECATT 分析
  20. 基于MATLAB樽海鞘算法SSA(麻雀算法(十一)也简称SSA)的Eggholeer函数优化,测试函数的100种优化方法之十二

热门文章

  1. 能跳过节假日的闹钟_苏宁小Biu智能闹钟体验:聪明好看,别具风格的“小物件”...
  2. PostgreSql之在group by查询下拼接列字符串
  3. [ZJOI2008]骑士
  4. 十四、Hadoop学习笔记————Zookeeper概述与基本概念
  5. swift简介(东拼西凑,看看就的了)
  6. MSSQL OPTION语句详解
  7. Andrew Stankevich#39;s Contest (1)
  8. wdos相关问题解答
  9. 在变薄变厚的周而复始中前进的信息
  10. sqlhelper 下载 使用指南 代码 [收藏]