You are on page 1of 4

Distributed Database using Linked Server

DISTRIBUTED DATABASE

AIM:

To develop and write SQL queries for a distributed database of BookStore at two sites B1 and B2. The
Bookstores are divided into two sites by their ZIP codes.

Distributed Database Design

Description:

A distributed database is a database in which storage devices are not all attached to a common
processing unit such as the CPU, controlled by a distributed database management system. (together
sometimes called a distributed database system). It may be stored in multiple computers, located in the
same physical location; or may be dispersed over a network of interconnected computers. Unlike
parallel systems, in which the processors are tightly coupled and constitute a single database system, a
distributed database system consists of loosely-coupled sites that share no physical components.

There are two principal approaches to store a relation r in a distributed database system:

A) Replication B) Fragmentation/Partitioning

A) Replication: In replication, the system maintains several identical replicas of the same relation r in
different sites.

Data is more available in this scheme.  Parallelism is increased when read request is served.

Increases overhead on update operations as each site containing the replica needed to be updated in
order to maintain consistency.

Multi-datacenter replication provides geographical diversity:

B) Fragmentation: The relation r is fragmented into several relations r1, r2, r3....rn in such a way that
the actual relation could be reconstructed from the fragments and then the fragments are scattered to
different locations. There are basically two schemes of fragmentation:

Horizontal fragmentation - splits the relation by assigning each tuple of r to one or more fragments. 
Vertical fragmentation - splits the relation by decomposing the schema R of relation r.

Implementing distributed databases using SQL Server 2019

Linked servers provide SQL Server the ability to access data from remote data sources. Using these
mechanisms, we can issue queries, perform data modifications and execute remote procedures.
Database Design:

1. Create horizontal partitioning table Books (Bookid, bookname, totalqty, price, topic) in 2
Bookstore Databases based on two zip codes and name it as B1 and B2 Databases
2. B1 in One server(Server Name :PGLAB22) and B2 in another server (Server Name :PGLAB22\
SQLEXPRESS)

Steps : It contains 3 Phases.

Phase 1:

1. Connect PGLAB22 Server.


2. Create the Database B1.
3. Create Books table in B1 database
4. Insert records for Books table in B1 database
5. Connect PGLAB22\SQLExpress Server.
6. Create the Database B2.
7. Create Books table in B2 database
8. Insert records for Books table in B2 database

Phase 2:

9. Select PGLAB22\SQLExpress Server -> Security-> login again.


10. Create New login and fill all the credentials

Login name :user 1

Password : SDNB

Confirm Password :SDNB

Choose Sql server authentication

11. Select user mapping and choose the database b2 and give the user name : user1
12. Then Click ok.
13. Go to database B2 and select security -> users-> user1
14. Rightclick user1 and click securables.
15. In securables click search -> select all objects and give ok.
16. Click Object types and select databases and ok
17. Browse the database B2 and select the permissions like select, update..

Phase 3 :

18. Connect PGLAb22 server


19. Go to server objects and create new linked server
20. Give Linked Server name : PGLAb22\SQLEXPRESS and server type : SQLserver
21. Then choose security in the menu and click add
local login : PGLAB22 Remote user : user1 and Remote Pwd : sdnb
22. Then select New query and connect with the database B!.

SQL Queries for Phase 1 :

create database B1;


use B1;
create table books(Bookid int, bookname varchar(30), totalqty int,
price int, topic varchar(50), zipcode int);
insert into books values(1,'C',50,200,'CS',600044);
insert into books values(2,'C++',50,210,'CS',600044);
insert into books values(3,'Ecommerce',50,220,'Commerce',600044);
insert into books values(4,'Accounting',50,230,'Commerce',600044);
insert into books values(5,'PHP',50,190,'CS',600044);

select * from books;

SQL Queries for Phase 2 :

create database B2;


use B2;
create table books(Bookid int, bookname varchar(30), totalqty int,
price int, topic varchar(50), zipcode int);
insert into books values(10,'C',50,200,'CS',600045);
insert into books values(11,'C++',50,210,'CS',600045);
insert into books values(12,'Ecommerce',50,220,'Commerce',600045);
insert into books values(13,'Accounting',50,230,'Commerce',600045);
insert into books values(14,'nanoscience',50,190,'biology',600045);

select * from books;

SQL Queries for Phase 3 :

use B1;
select * from [PGLAB22\SQLEXPRESS].B2.dbo.books
union
select * from [PGLAB22].B1.dbo.books;

--Create View for Merging the two partitions of Databases in B1 and B2

create view dbview as


(
select * from [PGLAB22\SQLEXPRESS].B2.dbo.books
union
select * from [PGLAB22].B1.dbo.books
)
Select * from dbview;
--Updation in Server2 from Server1 using Linked Server
update [PGLAB22].B1.dbo.books set totalqty=100 where bookid=2;

--Insertion in Server2 from Server1 using Linked Server

insert into [PGLAB22\SQLEXPRESS].B2.dbo.books


values(15,'organic',70,300,'chemistry',600045)

--Deletion in Server2 from Server1 using Linked Server


delete from [PGLAB22\SQLEXPRESS].B2.dbo.books where bookid=15;

You might also like