DbSchema Database Designer

DbSchema | SQL Server - How to Set Up and Use Replication?

Publish on DbSchema Blog >>>

SQL Server: How to Set Up and Use Replication in sqlcmd and DbSchema

SQL Server alt >

Table of Contents

  1. Introduction
  2. What is Replication?
  3. Purpose of Using Replication
  4. Permission and Restrictions on Using Replication
  5. Advantages and Limitations of Using Replication
  6. SQL Server Replication Terminologies
  7. Roles in SQL Server Replication
  8. Replication Types in SQL Server
  9. Setting Up and Using Replication in sqlcmd and DbSchema
  10. Conclusion
  11. References

1. Introduction

SQL Server Replication is a set of solutions that allows you to copy and distribute data and database objects from one database to another, and then synchronize them to maintain consistency. In this article, we will discuss in detail how to set up and use replication in sqlcmd and DbSchema.

2. What is Replication?

Replication is a set of technologies for copying and distributing data and database objects from one database to another and then synchronizing between databases to maintain consistency.

3. Purpose of Using Replication

The primary purpose of replication is to maintain database consistency across multiple databases, which can be beneficial in improving availability, reliability, and performance. Some specific use cases include reporting, offloading query processing, data warehousing, and integration with heterogeneous data sources.

4. Permission and Restrictions on Using Replication

Permissions for replication operations mostly depend on the role (Distributor, Publisher, Subscriber). However, it’s important to note that only members of the sysadmin fixed server role can enable a server to use replication.

Restrictions on replication vary depending on the type of replication (Snapshot, Transactional, Merge, Peer-to-peer). There may be restrictions on object types, database objects, schema options, etc.

5. Advantages and Limitations of Using Replication

Advantages:

  • Improved data availability and accessibility
  • Load balancing of read-intensive operations
  • Data warehousing and reporting
  • Integration with heterogeneous databases

Limitations:

  • Requires careful planning, setup, and maintenance
  • The complexity of conflict resolution in certain replication types
  • Potential data latency

6. SQL Server Replication Terminologies

Term Description
Article The basic unit of data transfer in replication. An article can be a table, view, stored procedure, etc.
Publication A collection of articles.
Filter A SQL WHERE clause used to restrict the data replicated to the Subscriber.

7. Roles in SQL Server Replication

Role Description
Distributor Responsible for storing the replication status data, metadata about publication, and data to be distributed.
Publisher The source of data, objects, and schema changes in replication.
Subscriber The recipient of the replicated data.

8. Replication Types in SQL Server

Snapshot Replication alt

  • Snapshot replication: Provides a simple and straightforward replication method by sending a snapshot of the entire data set to the subscribers.

    Transactional Replication alt

  • Transactional replication: Monitors changes on the publisher and applies them to the subscriber.

    Peer-to-Peer Replication alt

  • Peer-to-peer replication: Allows multiple nodes to work as publishers and subscribers, helping to provide a distributed data system.

    Merge Replication alt

  • Merge replication: Primarily designed for mobile applications or distributed server applications that have possible data conflicts.

9. Setting Up and Using Replication in sqlcmd and DbSchema

Setting Up and Using Replication in sqlcmd

To set up and use replication with sqlcmd, you would typically follow these steps:

  1. Setting Up Distributor: First, you must connect to the Publisher instance. The Publisher is the source of data, objects, and schema changes in replication.

    __bash sqlcmd -S <publisher_server> -d master -U sa -P <password> __

    Enable the server as a Distributor using the stored procedure sp_adddistributor.

    __sql EXEC sp_adddistributor @distributor = N'<publisher_server>', @password = N'<password>'; GO __

    After successful execution, the query will return:

    __bash Command(s) completed successfully. __

To know more about stored procedures you can read our article SQL Server-How to create a stored procedure?

  1. Creating Publication: Connect to the Publisher instance again:

    __``bash
    sqlcmd -S -d master -U sa -P

    
    Add a new publication using the stored procedure **sp_addpublication**.
    
    ```sql
    EXEC sp_addpublication 
    @publication = N'<publication_name>', 
    @sync_method = N'native', 
    @repl_freq = N'continuous', 
    @status = N'active';
    GO
    

    After successful execution, the query will return:

    Command(s) completed successfully.
    
  2. Setting Up Subscriber: Now, connect to the Subscriber instance. The Subscriber is the recipient of the replicated data.

    sqlcmd -S <subscriber_server> -d master -U sa -P <password>
    

    Add a new subscription using the stored procedure sp_addsubscription.

    EXEC sp_addsubscription 
    @publication = N'<publication_name>', 
    @subscriber = N'<subscriber_server>', 
    @destination_db = N'<subscription_database>', 
    @subscription_type = N'Push', 
    @sync_type = N'automatic', 
    @article = N'all', 
    @update_mode = N'read only';
    GO
    

    After successful execution, the query will return:

    Command(s) completed successfully.
    

After successfully completing these steps, your replication setup is ready. Changes to the publication database will now be replicated to the subscription database.

NOTE:

Please remember to replace , , , and with your specific details. Always test new scripts in a controlled environment before deploying them in production.

Viewing Replication Data in DbSchema

After setting up replication, you can view the replicated data in DbSchema.

  1. Connect to the Subscriber Database: Start DbSchema and create a new connection to your SQL Server. Enter the necessary details (server name, port, database name, user credentials, etc.) and connect to your database.

  2. View Replicated Data: Once you’ve connected to your Subscriber database, you can run SQL queries against the replicated data. Navigate to the “SQL Editor” tab in DbSchema. Here you can write and execute SQL queries.

    For example, if you have a replicated table Orders, you can use:

    SELECT * FROM Orders;
    
  3. View Query Results: The result of your query will be displayed in the Results panel in DbSchema. You can interact with this data, view it, and confirm that replication is functioning as expected.

Visually Manage SQL Server using DbSchema

DbSchema is a SQL Server client and visual designer. DbSchema has a free Community Edition, which can be downloaded here.

Key Features of DbSchema:

Following are the key features of DbSchema which distinguish it from other database GUI tools.

10. Conclusion

SQL Server Replication is an effective method for copying and distributing data across multiple databases. Despite some complexities and potential limitations, when planned and implemented correctly, it can significantly improve data availability and performance.

11. References

  1. Microsoft Docs: Replication
  2. Microsoft Docs: Configure Publishing and Distribution
  3. DbSchema: Documentation

Note: The code provided in this article is illustrative and may need to be adjusted to fit your specific environment. Always test new scripts in a controlled environment before deploying them in production.

Visually Manage Databases using DbSchema

DbSchema is a databases client and visual designer. DbSchema has a free Community Edition, which can be downloaded here.
DbSchema main features include:

DbSchema Designer alt >

Interactive Diagrams

Design tables, column and foreign keys directly in diagrams, by double-clicking them. Changes will be saved to the design model and, if DbSchema is connected to the database also into the database. More.


Connection Dialog alt >

Simple Connection Dialog

Choose the database location, the user and password, and simply get connected. Choose 'Edit Manually' into the JDBC URL combo to enter a custom URL. More.


Relational Data Explorer alt >

Relational Data Explorer

Explore data from multiple tables simultaneously, using foreign keys or virtual foreign keys. Double-click cells to edit the data. More.


Query Builder alt >

Query Builder

Create SQL Queries featuring JOINS, GROUP BY, ORDER BY just using the mouse. More.


SQL Query Editor alt >

SQL Query Editor

Edit and execute SQL Queries. The editor is autocompletion-enabled. More.


Schema Synchronization alt >

Design Schema in Team & Schema Deployment

DbSchema is using the design model, a copy of the schema structure, independent of the database.
The design model can be saved to file and shared in a team.
Connecting to another database you may compare the design model with the database, commit the differences or merge them in the design model. More.


Dark Theme alt >

Dark Theme

Configurable styles & dark theme. More.


Many features are available in the free Community edition.
The Pro edition adds capabilities to save the design to the model file, design schema in team and deploy the schema on multiple databases.


DbSchema can be downloaded for free. No registration is required.