Anda di halaman 1dari 24

SQL SERVER 6.

5
Chapter-10/1
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
OBJECTIVE
Distributed Data Overview
SCOPE
Distributed data
Implementing distributed data
Distributed Transaction Coordinator
Replication
Remote stored procedures
Bulk Copy Program
Distributing data on WWW
SQL SERVER 6.5
Chapter-10/2
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
IMPLEMENTING DISTRIBUTED DATA
The goal of distributed data is to make data more readily available to the users. With the
distributed data, component partitions of data are distributed over various nodes of the
network.
Depending on the specific update and retrieval traffic, distributing data can significantly
enhance overall performance.
In many situations it is necessary to distribute data across a geographically dispersed
client/server environment. Distributing data does not in any way reduce the need to
ensure consistency, concurrency, adequate performance and security of data.
In fact, it makes it more difficult to provide these capabilities. For example, when
multiple copies of same information exist, there is always the possibility that they might
contradict each other.
The challenge of RDBMS is to provide these capabilities in a multiserver environment
while maintaining performance and security.
The challenge to the implementor/administrator is to determine the method of distributing
the information in the multiserver environment.
Some of the methods which SQL Server uses to implement the distributed data are:
Distribution Transaction Coordinator
Replication
Remote stored procedures
Bulk Copy Program
Distributed Data on World Wide Web
SQL SERVER 6.5
Chapter-10/3
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
DISTRIBUTED TRANSACTIONS
Understanding Distributed Transactions
Many times more than one source of information need to be updated and transaction
technologies need to be applied to the update to ensure that a transaction is valid
before it occurs and all or none of the changes are made against the database table.
The transactions that span more than one server i.e. update data on two or more
network-connected computer systems are referred as Distributed Transactions.
Distributed transactions must be self-contained, autonomous or able to be reversed
without affecting other processes. .
Microsoft Distributed Transaction Coordinator (MS DTC) is a tool that provides the
utility functions that help manage the process.
It addresses the challenges of processing transactions over a distributed set of
software components that exist on a single computer or on a system of networked
computers.
MS DTC is fully integrated with Microsoft SQL Server and provides a transaction
manager in SQL Enterprise Manager at each computer, which is MS DTC user
interface, that manages distributed transactions.
With MS DTC, running transactions and their status can be viewed and transaction
changes can be forced manually.
Two Phase commit
MS DTC utilizes two-phase commit in its implementation
Two phase commit allows an application to coordinate updates among multiple
servers.
This implementation treats transactions on separate SQL Servers as a single
transaction.
It requires one SQL Server to be the commit server which serves as a record keeper
that helps the application determine whether to commit or to rollback transaction.
This commit process runs as a separate process from the transaction.
The role of commit service is to provide a single place where the commit / rollback
status is maintained.
The SQL Servers communicate with the commit service only if a failure happen
during the two-phase commit.
Thus, the two-phase commit guarantees that either all the databases on the
participating servers are updated or that none of them are updated.
First phase
After all servers participating in the distributed transaction have been updated, the
two phase commit begins when all servers agree that they are ready to commit.
SQL SERVER 6.5
Chapter-10/4
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
Second Phase
All servers commit.
A COMMIT TRANSACTION statement is executed to all servers.
Application tells the commit server that the transaction is complete, and connections are
closed. If any errors occur between phases one and two, all servers coordinate with the
commit server to see if the transaction should be canceled or committed.
Processing of Distributed Transactions by DTC
The participants in distributed transactions include:
Application : Application requests the transactions. It can be SQL Server or a client
application.
Transaction Manager : It is responsible for overall coordination of the transactions. This
role is played by MS DTC, installed on either a client or server within the network.
Resource Managers : These contain the resources being operated on. They are the
computers the databases reside on.
Step 1:
MS DTC, SQL Server and client applications communicate by means of methods-
functions that control transaction objects.

An application begins a transaction by calling the transaction manager with a
BeginDistributedTransaction method.

This method creates a transaction object that represents the transaction.
The application then includes the transaction object with execution request to the
resource managers.

When the resource manager receives an execution request with a new transaction
object, the resource, manager enlists in the transaction by calling the transaction
manager.

As the transaction progresses, the transaction manager keeps track of each of the
resource managers enlisted in the transaction.
SQL SERVER 6.5
Chapter-10/5
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
DIAGRAM
Step 2:
The resource manager performs the transactions request.(insert, delete or update).
The resource manager keeps information to allow it to either undo or redo the
transactions work on those resources.
Step 3:
When the application complete the transactions work it calls the MS DTC
CommitTransaction method.
MS DTC goes through a two-phase commit protocol with all enlisted resource
managers. It ensures that all managers commit the transaction or all abort it.
In first phase, MS DTC asks each resource manager if it is prepared to commit. If all
resource managers are prepared, then in second phase it broadcasts the commit
message.
If a failure occurs, resource manager is not sure about the outcome of the transaction.
It then keeps locks on the data that is modified by the transaction.
Once the transaction has successfully committed, the resource managers and the
transaction manager will ensure that the transactions effects are durable.
Application Transaction
Manager
Transaction Object
Begin Transaction
Application Transaction
Manager
Resource Manager
enlist
enlist
SQL SERVER 6.5
Chapter-10/6
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
Using Distributed Transactions
Server must be instructed to participate in a distributed transaction and MS DTC should
be informed be informed of the participation.
SQL Servers participation in an MS DTC transaction can be started by either of the
following:
By a client application calling APIs from DB-Library or ODBC. SQL Server acts as a
resource manager.
By using BEGIN DISTRIBUTED TRANS statement. SQL Server uses remote stored
procedures to specify the transaction work on different database on different servers..
The COMMIT TRANSACTION marks the end of the distributed transaction.
Syntax:
BEGIN DISTRIBUTED TRAN [tran_name]
EXAMPLE
BEGIN DISTRIBUTED TRAN
EXEC sp_addlogin user1
EXEC server1.master.sa.spaddlogin user1
COMMIT TRAN
OUTPUT:
SQL Server login name user1 is either added to both the local and remote server or none.
SQL SERVER 6.5
Chapter-10/7
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
REPLICATION
Replication overview
Replication is provided as an integral element of Microsoft SQL Server 6.5.
It allows automatic distribution of read-only copies of transactional data from a single
source server to one or more destination servers at one or more remote locations.
This brings with it the ability to synchronize information sources for multiple domains
even in the cases where they are physically separated by distance and poor
communication links.
At its most fundamental level, replication recognizes changes made to the database and
sends these changes to the remote system or systems for their use.
There are two basic replication models:
(a) Tight consistency
(b) Loose consistency
Microsoft has employed loose integration in its replication model.
It allows a time lag between the moment when original data is altered and when the
replicated copies of that data are updated i.e. the information will flow across the system
of servers not in real-time but as quickly as it can.
Tight consistency Vs Loose consistency
Tight consistency Loose consistency
1. Guarantees that all copies will be
constantly identical to the original
Does not guarantee that all copies will be
identical to the original.
2. Requires high speed LAN Supports LANs, WANs, fast and slow
communication links.
3. Reduces database availability Better database availability.
4. Less scaleable model than Loose
Consistency.
More scaleable than tight consistency.
Few ways where replication might be used:
To maintain several decision support servers, removing the report generation and ad-
hoc query impact from the source server.
SQL SERVER 6.5
Chapter-10/8
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
To distribute and update read-only copies of changes from a master price list on a
source server to several regional servers supporting field offices.
To distribute location specific data from a centralized master table.
To support order processing during a communications outage.
Server roles in replication
Publication server
When data is made available for replication, it is published. The server that provides
the information to other systems i.e. the server that contains the source databases and
makes data available for replication is called publisher or publication server.
The information provided to the other systems by the publisher is called publication.
Publication consists of articles, the basic unit of replication. Articles are the items
provided to the other systems, which are discrete pieces of information that range
from the entire contents of the database to a single row or the result of a query.
Subscription server
A server requesting for the publication is called a subscription server or subscriber.
The subscriber can be Microsoft SQL Server, Microsoft Access database or some
ODBC database whereas the publisher must be Microsoft SQL Server.
Distribution server
Receives data from the publisher, stores in the distribution database, and then
forwards it to the appropriate subscription servers.
Replicated data moves in only one direction - from publication server to subscription
servers.
Each server can act as both a subscriber and a publisher.
EXAMPLE
If server A publishes Publication 1, and server B publishes Publication 2, Server A could act as a
publication server (of Publication 1) and a subscription server ( to Publication 2).
In most cases, the publication server and the distribution server are combined in one
computer.
Types of replication models
There are four distinct replication models
SQL SERVER 6.5
Chapter-10/9
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
Central publisher
DIAGRAM
One server is defined as the publisher.
It distributes data to any number of servers that are setup as subscribers.
This is default option for replication in SQL Server.
Publication server is the primary owner or source of all replicated data.
Subscription sites treat replicated data as read-only.
Example - This scenario might be used to distribute master data, lists or reports from a
central server to any number of subscription server.
Central Publisher using a remote distributor
DIAGRAM

Publisher /
Distributor
Subscribers
Publisher Distributor
Subscribers
SQL SERVER 6.5
Chapter-10/10
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
A separate distribution server is established to store the data before it is published to
the subscribing server.
Example - Useful when publication server is a heavily used online transaction processing
server and should be freed from the distribution role because of performance and storage
considerations.
Central subscriber
DIAGRAM

Multiple publishers replicate data to a single subscriber.
It addresses the need for consolidating data at a centralized site and providing the
local site with an overview of all the data.
Multiple publishers are writing to the same subscription table, hence precautions must
be taken to ensure that all data remain in synchronization.
Example - Useful for rolling up inventory data from a number of servers at local
warehouses into a central subscriber at headquarters.
Multiple publishers of one table
DIAGRAM
Distributor /
Publisher
Distributor /
Publisher
Distributor /
Publisher
Distributor /
Publisher
Subscriber
Publisher / Distributor /
Subscriber
Publisher / Distributor /
Subscriber
SQL SERVER 6.5
Chapter-10/11
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
Multiple publication servers and multiple subscription servers each potentially play a
dual role.
It is closest to the implementation to fully distributed data processing.
Each server controls its own data and has a view of the other data.
Example - Useful for maintaining common information at regionally dispersed centres.
Guidelines for implementing replication
It is important to replicate only the necessary data and not the whole database.
If sites are read only and only need specific information from the database, replication
can be used.
If sites must have update capabilities, do not consider replication.
The database server that serves as the central point for data modification is the best
candidate for distributing information.
CLASSROOM EXERCISES

1. What is the difference between a distributed and a replicated
information?
2. What could be the advantages and disadvantages of using the
loose consistency model?
Publisher / Distributor /
Subscriber
SQL SERVER 6.5
Chapter-10/12
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
REMOTE STORED PROCEDURES
Remote stored procedure allows a client who is connected to one SQL Server to
execute a stored procedure on another SQL Server without establishing a connection
to the second server.
The server to which the client is connected accepts the request and sends the request
to the remote server on the clients behalf.
The remote server processes the request and returns any results to the original server,
which in turn passes those results to the client.
Requests can be initiated by the clients on the server.
Syntax:
EXEC server_name.dbname.owner.stored_procedure_name
Remote execution does not require a client to open the connection to the remote
server.
Client executes the stored procedure using EXEC statement as part of normal batch
submitted to the local server.
The name of the remote server must be included.
EXAMPLE
EXEC sp_who
EXEC remote_server.master.sa.sp_who
Both stored procedures are returned to the client in a identical manner.
Server to server connection information must be available to both the servers before
the connection can be opened.
This information is stored in master database, and is added using system stored
procedure.
System stored procedures used in server administration are:
sp_addremotelogin
sp_addserver
sp_dropremotelogin
sp_dropserver
sp_helpremotelogin
sp_helpserver
sp_remoteoption
sp_serveroption
SQL SERVER 6.5
Chapter-10/13
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
Implementing remote stored procedures
A server must be willing to participate in this network. Remote Access configuration
option is turned on to enable participation.
Each server must store both its own name as the local server and the name of the
server in the sysservers table.
The login id that the local server uses when opening the connection must exist on the
remote server.
SQL SERVER 6.5
Chapter-10/14
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
BULK COPY PROGRAM
Command line utilities are executable programs which are rum from a command prompt.
Bulk Copy Program (BCP) is a command line utility that copies SQL Server data to or
from an Operating System file in a user specified format.
The utility can be used to transfer:
large amount of data to and from SQL Server through an intermediate file.
data into SQL Server database from another program usually another DBMS.
data for use with other programs for example spreadsheets. Data is moved from SQL
Server into an Operating System file or onto a diskette, from there, the other program
can import data.
Guidelines for copying data
The table structures of the source and destination tables need not be identical.
Data copied into SQL Server is appended to existing contents of table.
Data copied to a file overwrites any previous contents of file.
Permissions for copying data
To copy data from an Operating System file into a table, a user must have INSERT
permission on the table.
To copy a table or a view to an Operating System file, the user must have SELECT
permission on
table or view being copied
sysobjects
syscolumns
sysindexes
For transferring data to and from SQL Server, following information is provided
The name of the database
Name of the table or view
name of the Operating System file or diskette drive
The direction of transfer (in/out)
In addition , other parameters like length, datatype and terminator of the column can also
be specified.
SQL SERVER 6.5
Chapter-10/15
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
EXAMPLE
bcp library..member out mem_out /n /Sservername /Usa /Ppassword
Copies the member table from library database to mem_out file.
out option copies to a file from the database table or view.
/n uses the database datatypes as default (does not prompt for each field).
Server name, sa and password specify the name of the server and the login to connect to transfer data.
Note: See Appendix C for details
Copying tables with or without indexes
Tables without indexes Tables with indexes
bcp utility is fastest if the database table
has no indexes,or constraints that use
indexes.
bcp utility is slow if database table has
indexes, or constraints that use indexes.
For fast bcp, sp_dboption system stored
procedure is used to set the select
into/bulkcopy option for that database
true.
sp_dboption databasename, select
into/bulkcopy, true
For a table that has indexes,or constraints
that use indexes, there is no need to set
the option.
Fast bcp does not log data insert in
transaction log.
Inserts into the tables with indexes are
always logged.
By default, the select into/bulkcopy option is off.
Integrity issues
Any defaults defined for the columns,and datatypes in the table are always enforced
when data is copied into a table.
Rules and triggers are not enforced so as to load the data at maximum speed.
bcp out is static i.e. any updates made to the data during the bcp processes will not
be reflected in the bcp data file.
bcp in is dynamic i.e. all rows of a batch become available after the batch has
finished.
SQL SERVER 6.5
Chapter-10/16
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
CLASSROOM EXERCISES
1. Why bcp defaults to slow bcp?
2. How can you make sure that no one else can access the database while
exporting the data?
SQL SERVER 6.5
Chapter-10/17
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
DISTRIBUTING DATA ON THE WORLD WIDE WEB
World Wide Web (WWW) pages
World Wide Web is one of the most visible Internet services. It presents information
textually and graphically and turns Internet into an information resource and marketing
tool.
The (HyperText Markup Language) HTML files, also known as web pages, are resources
for displaying information on World Wide Web.
HTML commands are added to the document file using a text editor, or using a
specialised web page editor like Microsoft Word Internet Assistant. These commands are
used to present and format the simple text, tables and database query results.
These HTML pages can be viewed by using any HTML ( or web page) browser.
Distribution models
There are two ways in which data can be distributed on the Internet.
(a) Data push model
(b) Data pull model
Data push model
A web page is built and posted on the Internet.
The web users accessing the information through the Web Browser can not
manipulate the data, since it is read-only.
DIAGRAM
SQL
Server
Internet Server
Web Browser
HTML
files
Browser
reviews data
SQL SERVER 6.5
Chapter-10/18
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
Data pull model
A client opens a web page posted on the Internet server and interacts with the page.
Internet server queries SQL Server for the requested information.
The information is pulled, a new web page is generated and send to client.
DIAGRAM
The SQL Server Web Assistant
The SQL Server Web Assistant, an easy-to-use interface, assists in the process of
publishing Microsoft SQL Server data on the World Wide Web. This tool is used to
implement the data push model.
It allows to define a set of data (a query) which is automatically merged into an HTML
document and pushed to the Internet Server.
The pages can be generated on one-time basis or as a regularly scheduled SQL Server
task. A web page can also be updated using a trigger when the supporting data changes.
In order to use SQL Server Web Assistant, one must have:
(a) CREATE PROCEDURE privileges in the database.
(b) SELECT privileges on the columns accessed by the query.
Options for creating a web page
After connecting to the SQL Server Web Assistant from the SQL Server 6.5 program
group, certain information is supplied and selections are made for other options in a
series of dialog boxes.
SQL Server
returns data
Internet Server
requesting data from
SQL Server
Web
Browser
SQL
Server
Query
Receiving
data
Internet
Server
SQL SERVER 6.5
Chapter-10/19
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
Providing login
An appropriate login id is provided to access tables and databases that need to be used in
providing content for the required web page.
Selecting the contents for the web page (Specifying the query)
Queries in SQL Server Web Assistant can be built using one of the three options shown
by the Query dialog box:
Queries from a graphical database/table hierarchy - Allows to use the point-and-click
interface and indicate the tables and other items to be included in the query that will
be used to generate the page.
Free form query option - Allows to specify the database and directly enter (typing in)
the query
Query by using an existing stored procedure.

The query page asks you to select how to apply the query and then to type the query.
Scheduling
Creation of web pages can be scheduled as follows:
Scheduling option Description
Now Immediate, one time execution
Later One time execution for a specified date and time.
When data changes Automatic triggered execution. Changes to certain
tables and columns cause the web page to be updated.
On certain days of the week Execution at a day and time, for example, Wednesday
at noon.
On a regular basis Execution every n minutes, hours, days or weeks for
example every 5 minutes.
File options
The file name and path of the web page is created.
This is either on local or remote computer.
An optional template file can be used as a template for the web page. The template
can contain any static text, URL links or other features.
The title of the web page is specified and a title for the results set is also specified.
One or more URL links can be specified or a data query can be specified that returns
the formatting and presentation of the data with the additional HTML pages.
SQL SERVER 6.5
Chapter-10/20
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
Formatting options
HTML offers a limited variety of fonts and formatting options.
HTML heading styles to be used for the titles of the web page and data sections can
be specified.
Timestamp and column headers can be included.
If a template file is used, several of these options will already be determined.
After an HTML page is generated, it can be viewed by any web browser.
DIAGRAM
Implementation of data pull model
Web users can initiate dynamic queries from an HTML document to a SQL Server,
through the Microsoft Internet Information Server (IIS).

IIS provides the software necessary to publish information on Internet.

Integration of SQL Server with IIS, including its built-in Internet Database
Connector (IDC) which is an Internet Server API, modifies and retrieves the
information from the SQL Server as well as create new web pages and send these
back to the Web users.
DIAGRAM
Web
Browser
IDC
Internet
Server
SQL
Server
Query
IIS requesting
data from SQL
S
SQL Server
returns data
Receiving
data
SQL
Server
( Web
Assistant)
Internet Server
Web Browser
HTML
files
Browser
reviews data
IIS
SQL SERVER 6.5
Chapter-10/21
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
The IIS provides access to the ODBC layer with the use of IDC.
IDC acts as a go between for system, providing the interface between HTML and
how the information is queried at database level.
An HTX file is returned to the user, which indicates how the resulting data is
displayed.
DIAGRAM
Thus, there are three components:
Initial source HTML document containing form fields and other options.
The IDC file for making and carrying out the database commands.
The HTX file for presenting the information returned.
Building form to prompt for values
A form is presented to the user which allows them to select what information they
need.
Forms can be created which allow input that can be used to form the SQL statements
that are passed to the data source.
Incoming HTTP request
IDC document
The IDC source indicates the ODBC
DSN, SQL statement and applicable
parameters to use to retrieve
information.
Internet Information Server
ODBC
ODBC datasource
HTX document
The HTML extension file indicates
how the output data should be
provided to the users browser.
To browser for display to the user
SQL SERVER 6.5
Chapter-10/22
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
A sample HTML file (form.htm)
<HTML>
<HEAD>
<TITLE>
Demonstration form
</TITLE>
</HEAD>
<h1>sample form for database access</h1>
<FORM METHOD=POST ACTION=/scripts/form1.idc>
Enter name to find : <INPUT NAME=au_lname>
<p>
<INPUT TYPE=SUBMIT VALUE=Run query>
</FORM>
</BODY>
</HTML>
In this example, form1.idc is called and passed parameters. Notice, that the variable name
au_lname is used to reference the value provided by the user. Thus, a user is allowed to
type a name that can be used to search the authors table in the pubs database.
The browser opens a new URL on the server with the following specification:
http://scripts/form1.idc?au_lname=<name>
Building server query source file
When the URL is accessed, the server will run the indicated IDC file.
IDC file contains the SQL statements and directives necessary to carry out the
commands needed.
A sample IDC file (form1.idc)
Data source: web sql
Username: sa
Template: form1.htx
SQLstatement:
+SELECT au_lname, phone, address from authors
+ where au_lname like %au_lname%%
The user will not see this file or output from it directly. The IDC calls the Template
indicated and passes in the results to be returned as a Web page.
Notice, that the variable is placed between percentages.
The data source indicated in the IDC relates to the ODBC data source established with
the ODBC manager.
SQL SERVER 6.5
Chapter-10/23
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
After the values have been retrieved that you want to display, the results can be moved on
to the results set source files.
Building Results Source file
The HTML extension files with the extension .HTX are referenced in the Template
entry in the IDC file.
These files need to be created to dictate how the information is presented.
SQL SERVER 6.5
Chapter-10/24
Copyright Tata Infotech Ltd. : Copying of this document in part or full is not permitted without express authority.
SUMMARY
There are various methods which SQL Server uses to implement the Distributed Data.
Distributed Transaction Coordinator - Tool that provides the utility functions that help
manage the transactions that span more than one server.
Replication - Allows automatic distribution of read-only copies of transactional data from
a single source server to one or more destination servers at one or more remote locations.
There are three server roles:
Publication server
Subscription server
Distribution server
Types of replication models:
Central publisher
Central publisher using a remote distributor
Central subscriber
Multiple publishers of one table
Remote stored procedures - Allow a client connected to one SQL server to execute a
stored procedure on another SQL Server without establishing a connection to the second
server.
Bulk Copy Program - A command line utility that copies SQL Server data to or from an
Operating System file in a user specified format.
Distributing data on WWW - Two types of models
Data push model- Implemented by SQL Server Web Assistant.
Data pull model - Through IIS.

Anda mungkin juga menyukai