I.
Introduction to Microsoft SQL Server
Microsoft SQL Server is a relational database management system (RDBMS)
developed by Microsoft. It supports a wide range of transaction processing, business
intelligence, and analytics applications in corporate IT environments. Key features
include:
Scalability and Performance: Efficiently handles large volumes of data and
high transaction rates.
Security: Provides robust security features to protect data.
Integration: Seamlessly integrates with other Microsoft products and
services.
Tools and Services: Includes tools for data management, development, and
business intelligence.
Software Installation
Step 1: Download SQL Server
1. Go to the Microsoft SQL Server download page.
2. Choose the edition you want to install (e.g., Developer, Express).
SQL Server Downloads | Microsoft
3. Download the installer.
Step 2: Run the Installer
1. Open the downloaded installer file.
2. Choose the installation type:
o Basic: Quick setup with default settings.
o Custom: Allows you to choose specific features and settings.
o Download Media: Downloads the installation files for offline
installation.
Step 3: Follow the Installation Wizard
Page 1 of 8
1. License Terms: Accept the license terms and click “Next.”
2. Feature Selection: Select the features you want to install (e.g., Database
Engine Services, SQL Server Replication).
3. Instance Configuration: Choose the instance type (default or named
instance).
4. Server Configuration: Set up the SQL Server services and specify the
authentication mode (Windows Authentication or Mixed Mode).
5. Database Engine Configuration: Configure the database engine, including
data directories and memory settings.
6. Installation Progress: Monitor the installation progress and wait for it to
complete.
Step 4: Install SQL Server Management Studio (SSMS)
Here’s a step-by-step guide to installing SQL Server Management Studio (SSMS):
1. Go to the SSMS download page:
o Visit the SQL Server Management Studio (SSMS) download page 1.
2. Download and run the SSMS installer:
o Click on the download link for the latest version of SSMS.
o Once the download is complete, locate the installer file (e.g., SSMS-
Setup-ENU.exe) in your downloads folder.
o Double-click the installer file to run it.
3. Follow the installation wizard to complete the setup:
o The installation wizard will guide you through the setup process.
Accept the license terms and click “Install”.
o The installer will proceed to install SSMS. This may take a few minutes.
o Once the installation is complete, click “Close” to exit the wizard.
Page 2 of 8
After installation, you can launch SSMS from the Start menu or by searching for
“SQL Server Management Studio”.
Step 5: Connect to SQL Server
1. Open SQL Server Management Studio.
2. In the “Connect to Server” dialog, enter the server name and authentication
details.
3. Click “Connect” to access your SQL Server instance.
1. Open SQL Server Management Studio:
o You can find SSMS in the Start menu or by searching for “SQL Server
Management Studio”.
2. In the “Connect to Server” dialog, enter the server name and
authentication details:
o Server name: Enter the name of your SQL Server instance. This could
be localhost for a local instance, or a network name/IP address for a
remote server.
o Authentication: Choose the authentication method:
Windows Authentication: Uses your Windows credentials.
SQL Server Authentication: Requires a username and
password.
3. Click “Connect” to access your SQL Server instance:
o Once you’ve entered the necessary details, click the “Connect” button
to establish a connection to your SQL Server instance.
Page 3 of 8
II. Transaction Management Lab Material
1. Create Database and Tables
1.1 Create Database
First, create a database named TransactionManagement.
CREATE DATABASE TransactionManagement;
GO
1.2 Create Tables
1. Accounts Table
CREATE TABLE Accounts (
AccountID INT PRIMARY KEY,
AccountHolder NVARCHAR(100),
Balance DECIMAL(18, 2)
);
2. Inventory Table
CREATE TABLE Inventory (
ItemID INT PRIMARY KEY,
ItemName NVARCHAR(100),
Quantity INT
);
3. Orders Table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE
);
Page 4 of 8
4. Customers Table
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name NVARCHAR(100),
Age INT
);
2. Basic Syntax of Transactions
2.1 What are Transactions
A transaction is a unit of work performed on a database. It is a
sequence of operations that are executed as a single logical unit of
work. A transaction must be atomic, consistent, isolated, and durable
(ACID properties).
2.2 Beginning Transaction
To start a transaction, use the BEGIN TRANSACTION statement. This
marks the starting point of an explicit, local transaction.
Example:
BEGIN TRANSACTION;
-- Your SQL statements here
2.3 Committing or Rolling Back
To save the changes made during the transaction, use the COMMIT
statement. To undo the changes, use the ROLLBACK statement.
Example:
Page 5 of 8
BEGIN TRANSACTION;
-- Insert a new record
INSERT INTO Customers (Name, Age) VALUES ('Alice', 30);
-- Check if the insertion was successful
IF @@ERROR = 0
COMMIT TRANSACTION;
ELSE
ROLLBACK TRANSACTION;BEGIN
2.4 Creating Named Transactions
You can name a transaction using the SET TRANSACTION statement.
This is useful for managing complex transactions.
Example:
SET TRANSACTION 'OrderProcessing';
BEGIN TRANSACTION 'OrderProcessing';
-- Your SQL statements here
COMMIT TRANSACTION 'OrderProcessing';
More Examples
Example 1: Transfer Money Between Accounts
This example demonstrates a transaction that transfers money
between two bank accounts.
SQL Code:
Page 6 of 8
BEGIN TRANSACTION;
-- Deduct amount from source account
UPDATE Accounts SET Balance = Balance - 100 WHERE AccountID
= 1;
-- Add amount to destination account
UPDATE Accounts SET Balance = Balance + 100 WHERE AccountID
= 2;
-- Check for errors
IF @@ERROR = 0
COMMIT TRANSACTION;
ELSE
ROLLBACK TRANSACTION;
Example 2: Order Processing
This example shows how to process an order, update inventory, and
handle errors.
SQL Code:
BEGIN TRANSACTION 'OrderProcessing';
-- Deduct items from inventory
UPDATE Inventory SET Quantity = Quantity - 10 WHERE ItemID
= 101;
-- Insert order details
INSERT INTO Orders (OrderID, CustomerID, OrderDate) VALUES
(123, 1, GETDATE());
-- Check for errors
IF @@ERROR = 0
COMMIT TRANSACTION 'OrderProcessing';
Page 7 of 8
ELSE
ROLLBACK TRANSACTION 'OrderProcessing';
Page 8 of 8