简体   繁体   English

SQL Server事务复制和不同的主键

[英]SQL Server Transactional Replication, and different primary keys

With SQL Server 2005 and transactional replication, can I remove the primary key constraints on the subscriber, while leaving the primary key constraints on the publisher? 使用SQL Server 2005和事务复制,是否可以在订阅服务器上删除主键约束,而在发布服务器上保留主键约束?

Primary I want to do this because I want to cluster on different columns than the existing clustered constraints. Primary我想这样做是因为我想在与现有集群约束不同的列上集群。 I don't think I can convert a constraint from clustered to nonclustered without dropping it first, and replication is already occurring. 我认为我不能在不先删除约束的情况下将其从集群转换为非集群,并且复制已经在进行。

Why don't you leave the primary key and create additional non clustered indexes on the subscriber or will this not resolve your problem? 您为什么不离开主键并在订阅服务器上创建其他非聚集索引,还是不能解决您的问题? If the reason to have other columns indexed on the subscriber is performance then this should be a solution. 如果在订阅服务器上建立其他列索引的原因是性能,那么这应该是一个解决方案。

This might work for snapshot replication, but I'm not sure it would work with transactional replication. 这可能适用于快照复制,但是我不确定它是否适用于事务复制。 The sole requirement on a table to allow it to be replicated is the existence of a primary key to allow each row to be uniquely identified. 对表进行复制的唯一要求是存在主键,以允许唯一地标识每一行。

You could try pausing replication and then try dropping the primary key constraint, recreating it as a non-clustered PK and then un-pausing replication. 您可以尝试暂停复制,然后尝试删除主键约束,将其重新创建为非群集PK,然后取消暂停复制。 If SQL Server will not allow you to drop the PK, you'll find out before you do any damage. 如果SQL Server不允许您删除PK,则在进行任何损坏之前会先找到。

The alternative is to break replication and reinitialise it. 替代方法是中断复制并重新初始化它。

Either way, you'll want to do the change during a maintenance window. 无论哪种方式,您都需要在维护时段内进行更改。

Directly, for primary key its not possible. 直接地,对于主键是不可能的。 Other way round: When you set the transactional replication, when you select the articles for replication, you can select different properties like "check foreign key contstraint". 反之亦然:设置事务复制时,在选择要复制的项目时,可以选择其他属性,例如“检查外键约束”。 Set that property to false. 将该属性设置为false。 In db1 convert your primary key to a foreign key with some new table tb1 containing that as primary key. 在db1中,使用一些包含该表作为主键的新表tb1将主键转换为外键。 So, ultimately , on your replication db db2, the foreign key constraint wont be replicated and thus allowing you to do what you want. 因此,最终,在复制数据库db2上,将不会复制外键约束,从而使您可以执行所需的操作。

The basic of replication process is to maintain identical database organisation between different servers. 复制过程的基础是在不同服务器之间维护相同的数据库组织。

Your question here can be considered as asking if a replication process can be used to break this basic replication principle. 您在这里的问题可以被视为询问是否可以使用复制过程来破坏此基本复制原理。

So the answer is no, but I am still interested in the reasons that let you formulate this question. 因此,答案是否定的,但是我仍然对让您提出这个问题的原因感兴趣。 Shall I say that this "dual primary key" option was seen as a way to solve another problem? 我是否应该说这个“双主键”选项被视为解决另一个问题的方法? I think you should go back to this initial issue and try to find another way to solve it. 我认为您应该回到最初的问题,并尝试寻找另一种解决方法。

I did my own homework, and came to the conclusion that you can drop the constraints on the subscriber. 我做了自己的作业,得出的结论是,您可以消除对订户的限制。

I set up a simple transactional replication scenario, dropped the primary key on the subscriber, and then did some inserts, deletes and updates, and verified that the changes were replicated to the subscriber. 我设置了一个简单的事务复制方案,将主键放在了订阅服务器上,然后进行了一些插入,删除和更新,并验证了更改是否已复制到订阅服务器。

I guess I should have done that to begin with. 我想我应该先这样做。 I didn't know it was going to be that easy :). 我不知道那会那么容易:)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM