Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Monday, February 19, 2024

SQL Server Partitioned view

What is partitioned view in SQL Server?

In SQL Server, a partitioned view is a virtual table that combines the results of multiple SELECT statements against different tables or partitions into a single, logical view. Each SELECT statement represents a partition of the view, and the combined result appears as a single unified view. Partitioned views are primarily used for horizontal partitioning, allowing you to distribute data across multiple tables based on a predefined criterion. 

Example of multiple tables


CREATE VIEW PartitionedView AS
SELECT * FROM Table1
UNION ALL
SELECT * FROM Table2
UNION ALL
...
SELECT * FROM TableN;

Example of table with different partitioning criteria

The SELECT statements in a partitioned view should include a WHERE clause that specifies the criteria for partitioning the data. Each SELECT statement corresponds to a partition based on this criterion.


CREATE VIEW DatePartitionedView AS
SELECT * FROM SalesData WHERE SaleDate < '2020-01-01'

UNION ALL

SELECT * FROM SalesData WHERE SaleDate >= '2020-01-01';

When should we use partitioned view?

Partitioned views are useful when dealing with large datasets that can be logically divided into partitions. Each partition can be stored in a separate table, and the partitioned view provides a seamless way to query the entire dataset.

Partitioned views can simplify certain administrative tasks, such as archiving or purging old data. We can create new tables to store data for a specific period and modify the partitioned view to include the new partition. Partitioned view is useful for management of temporal or spatial data.

What is local partitioned view in SQL Server?

Local partitioned view is created by joining different tables from same server.

What is distributed partitioned view in SQL Server?

Distributed view is created by joining different tables from different servers.

SQL Server View with Schema binding

Schema binding means binding the object with its schema. We know that a database object belongs to a schema. The default schema of the database object is dbo.

When a view is created it will have binding with the table which is used to create it. The table will belong to a schema. The table name must be written with its schema name else it will throw error.

When WITH SCHEMABINDING argument is passed during view creation then the table cannot be dropped unless its associated view is dropped. Note that dbo stands for schema of Employees table in the following example.

CREATE VIEW MyVwEmployees
with schemabinding
AS SELECT Id, first_name, last_name, salary
FROM dbo.Employees – dbo must be written
WHERE salary < 50000

Run the drop command to drop employees table.

drop table dbo.Employees

We find that it throws error message as given below.

Cannot DROP TABLE 'dbo.Employees' because it is being referenced by object 'MyVwEmployees'.

SQL Server View with Check Option

When a view is created then we can add WITH CHECK OPTION after the WHERE condition in SELECT query. If the condition fails then we cannot add row into the view. Look at the following example in this regard.

CREATE TABLE Employees
(
	Id int ,
	first_name varchar(10),
	last_name varchar(10),
	salary int
) 
GO

Create a view based on Employees table.

CREATE VIEW MyVwEmployees
AS SELECT Id, first_name, last_name, salary
FROM Employees
WHERE salary < 50000

Now insert a row into the view.

INSERT INTO MyVwEmployees VALUES(1, 'Akash','Verma',60000)

The row is inserted into table but view will not display the row. 

Now alter the view definition as given below.

ALTER VIEW MyVwEmployees
AS SELECT Id, first_name, last_name, salary
FROM Employees
WHERE salary < 50000
WITH CHECK OPTION

Insert a row as given below.

INSERT INTO MyVwEmployees VALUES(2, 'Rina','Gupta',60000)

The row will not be inserted and following error will be.

The attempted insert or update failed because the target view either specifies WITH CHECK OPTION or spans a view that specifies WITH CHECK OPTION and one or more rows resulting from the operation did not qualify under the CHECK OPTION constraint.

SQL Server Encryption of views

We can create a view with encryption so that its script cannot be deciphered.

CREATE VIEW vwEmpSalary
WITH ENCRYPTION 
AS
SELECT Id, first_name, last_name, salary 
FROM Employees

Now run the following stored procedure to see the text of view.

sp_helptext 'vwEmpSalary'

We get the following message

The text for object 'vwEmpSalary' is encrypted.

Alternatively, we can use the following query to get the script of a view if it is not encrypted.

SELECT id, ctext, text 
FROM SYSCOMMENTS
WHERE ID = OBJECT_ID('vwEmpSalary')

Since the view is encrypted, we get null values.

SQL Server Indexed View (Materialized View)

View does not occupy space unless it is a materialized view. When index is created on a view then that view is known as materialized view. It is created to improve query performance. The index on view can significantly improve query performance, especially for aggregations, joins, and complex queries. The index is automatically maintained by the database engine whenever the underlying data changes. Indexed view should not be created if the table from which view is created is highly transactional (i.e. a lot of action queries are associated with the table). In this case, synchronization between view and table will be very time consuming affair.

We need to use schema binding on the view to create index on the view. Schema binding implies that table cannot be dropped as it has binding with view. Look at the following example.

CREATE VIEW MyIndexedView
WITH SCHEMABINDING
AS
SELECT Column1, SUM(Column2) AS Total
FROM MyTable
GROUP BY Column1;

CREATE UNIQUE CLUSTERED INDEX IX_MyIndexedView ON MyIndexedView(Column1);

We can create additional non-clustered indexes on the view

Key Characteristics of Materialized View:

  • The data of materialized view is physically stored in database.
  • Indexed views can have a clustered index, improving query performance by providing a structured storage format.
  • The index is automatically maintained by the database engine whenever the underlying data changes.

SQL Server Standard view

A standard view, also known as a regular or non-indexed view, is a virtual table that is defined by a SELECT statement. It does not physically store data but provides a way to simplify queries and abstract the underlying table structures. Standard views are not materialized, meaning that the results of the view are generated dynamically based on the underlying data whenever the view is queried.

Key Characteristics of Standard view:

  • The view definition is stored in the database, but the data is not.
  • No physical storage is associated with the view itself.
  • Dynamic data: Each time the view is queried, the underlying SELECT statement is executed, and the results are generated on-the-fly.

SQL Server How to create a view

To create a view, we should keep the following points in mind.

  1. We can use SELECT query to create a view.
  2. We can create a view using JOIN operation in SELECT query.
  3. We cannot use any action query to create a view.
  4. We cannot use ORDER BY clause with SELECT query unless TOP argument is used with the query.

Example to create a view:

A view is always based on table. So, to create a view, we require table. For the example, we first of all create a table and will then create a view out of that table.

CREATE TABLE Employees
(
	Id int ,
	first_name varchar(10),
	last_name varchar(10),
	salary int,
	gender varchar(1),
	grade varchar(10),
	dob date
) 
GO

We create a view out of Employee table.

CREATE VIEW vwEmployees
AS
SELECT Id, first_name, last_name, gender
FROM Employees
GO

Note that salary, grade and date of birth are sensitive data and are not included in view.

Error during creating view

The ORDER BY clause is invalid in views, inline functions, derived tables, sub queries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified. You get error when you attempt to create view using ORDER BY clause as given below.


CREATE VIEW vwEmployees
AS
SELECT Id, first_name, last_name, gender
FROM Employees
ORDER BY gender
GO

The following script will run successfully as it includes TOP argument with SELECT clause.


CREATE VIEW vwEmployees
AS
SELECT Top 5 Id, first_name, last_name, gender
FROM Employees
ORDER BY gender
GO

SQL Server Advantages and Disadvantages of Views

Advantages of Creating Views

Views provide several advantages such as simplifying complex queries, enhancing security, and improving the overall management of a database. Some important advantages as given below:

Views provide a level of abstraction over the underlying tables. Users can work with views without knowing the details of the underlying table structures or JOIN conditions.

Views allows us to implement security measures by controlling access to specific columns or/and rows of table.

A view can be referenced in different queries, stored procedures, triggers etc. and thereby it facilitates code reusability.

Views allow us to encapsulate complex SQL logic and join operations into a single, easily referenced virtual table. This simplifies queries for end user. End user does not have to create such complex logic; he can directly use view.

Views help in modularizing code by encapsulating specific functionalities. This enhances code organization and readability, making it easier to manage and maintain the database.

Disadvantages of Creating Views

View cannot be passed parameter.

View does not allow ORDER BY clause without using Top argument with SELECT clause.

View cannot be created using temporary table.

SQL Server Differences between View and Table

Differences between View and Table

  • Storage: Table data is physically stored while view is a virtual table that points to table instead of storing any physical data.
  • Dependence: View is dependent upon table but not vice versa. When data in table is inserted, updated or deleted then view is affected but a table created out of a table is not affected because tables are independent database objects.
  • Security: View is created as interface to provide only that data to user that we want to display as per the permissions of the user.
  • Importance: Table is crucial entity in database but view is less important than table. Multiple views can be created from a table. Deleting table may severely impact the business but deleting view is not that much important.

SQL Server View

In SQL Server, a view is a virtual table that is based on the result of a SELECT query. It does not store data itself but rather represents a compiled SELECT query that can be referenced and used in other queries.

Facts about views

  1. A view is a database object based on compiled query created using one or more tables.
  2. A view is a virtual table because it references a table. A view can reference a table which is created by joining multiple tables.
  3. View does not occupy physical space unless it is a materialized view. It means that view contains the query definition for result set not the data of result set.

Why is view created?


View is created as interface to provide only that data to user that we want to display as per the permissions of the user. It is a kind of window to view specific columns or/and rows from table.

View provides a layer of abstraction over tables. Due to abstraction of views, users do not know about the underlying tables.

View provides security of data because of its layer of abstraction over table.

View allows us to simplify complex queries because view can be used in other queries.

View can be queried like tables and it therefore acts as data source. So it allows code reusability.

SQL Server Rowset functions

Rowset function is function that returns an object which can be used in place of a table in another SQL statement. There are five Rowset functions in SQL Server which are as follows.

  • CONTAINSTABLE
  • FREETEXTTABLE
  • OPENQUERY
  • OPENROWSET
  • OPENDATASOURCE

CONTAINSTABLE statement allows you to construct a virtual table from the result set of a complex Full Text Search. The syntax is slightly complicated. This function should be used on a table that is enabled for full text indexing. Full Text Searching means you are using 4G message for ji message based on farji search what the data you get that data is stored in Virtual Table using contents table function.

FREETEXTTABLE function is similar to ContainsTable function both generates virtual table based on Full Text Indexing information.

OPENQUERY function is used to use a query that returns rose on a linked server to return a virtual table. Open query function returns result set so after FROM clause you can use it as a table. The first parameter of this function is server name and second parameter is query.  The query returns result set from linked server given in the first parameter. Open query function is used in distributed transaction. 

SQL Server How to Create a Linked Server

You can create a linked server by using following steps.

OPENROWSET function is another way to get data from different servers. In SQL Server this function takes different parameters to get data from another server. 

  1. Provider name
  2. Data source i.e. Server name
  3. Username and Password
  4. Query

OPENROWSET functions should be used if you have not created linked server. For a particular data source this function will allow you to connect to a data source directly using OLEDB. 

OPENDATASOURCE function is another way to get result set from data source.  This function takes

  1. Provided name
  2. Connection string

It is more flexible than open row set. In the connection string provider, data source, user ID and password are separated by semi-colon.

SQL Server Difference between Local and Distributed Transactions

Difference between Local and Distributed Transactions

Local transaction means the transaction that makes changes in a single database. Distributed transaction means transaction that makes changes to more than one database. In case of distributed transaction the different databases should not be on the same server. The servers may be distributed across network.

In case of distributed transaction, commit is slightly complex. SQL Server uses a protocol called two-phase commit. In the first phase, SQL Server asks every database involved with distributed transaction to prepare the transaction. Individual database verifies that it can commit the transaction and set sides all the resources necessary to do so. When all the databases involved in the transaction responds to the SQL Server with okay message then second phase begins. Then, SQL Server sends message to every database to commit the transaction. On the other hand, in first phase, if there is any error in a database then that database will not send okay message and then SQL Server will send message to rollback to all databases.

Microsoft DTC

Microsoft SQL Server provides a separate service for distributed transaction known as distributed transaction coordinator DTC. This service is installed separately in SQL Server to carry out distributed transactions.

In case of distributed transaction, you should use BEGIN DISTRIBUTED TRANSACTION statement. 

SQL Server Transaction Programming

Transaction Programming

A simplified view of transaction programming is as follows.

  • Use DECLARE statement to set up all the local variables which will be used in the transaction statements.
  • Use BEGIN TRANSACTION clause to begin transaction.
  • After BEGIN TRANSACTION, write all the statements which will be executed as a unit of work for transaction.
  • Use system global variables @@error and @@transient in BEGIN block.
  • Store values of global variables in the declared variables.

Check the value of these system variables before executing COMMIT or ROLLBACK statement.

Some Tips about Transaction

When you make any change in data within a transaction then lock is acquired on the data. Locking data consumes additional resource. So, transaction consumes additional resources on SQL Server. You should never use transaction for simple single statement SQL Query. 

  • Do not use transaction when the user is browsing the data (read operation).
  • Use transaction in only that part of code where data is actually going to change.

SQL Server Transaction Statements

Different Transaction Statements

Now you look at practical steps to carry out transaction in SQL. There are four statements used to manage transactions which are as follows:

  • BEGIN TRANSACTION
  • COMMIT TRANSACTION
  • ROLLBACK TRANSACTION
  • SAVE TRANSACTION 

Two global variables @@ERROR and @@TRANCOUNT are important in the context of transactions.

  1. @@ERROR
  2. @@TRANCOUNT

A transaction may throw error. The @@ERROR variable stores information about the most recent error number. If there is no error then its value is 0. Its value is reset to 0 if statement executes successfully. So, it is important to save its value in some variable for later use.

The @@TRANCOUNT variable stores the number of transactions in the current connection. It is important if we are using nested transactions. 

BEGIN TRANSACTION statement

A transaction can be given a name. After BEGIN TRANSACTION clause we can write either transaction name or a variable which holds the transaction name. 

BEGIN TRANSACTION empTrans WITH MARK 'Description for logging transaction plus current time'

The WITH MARK clause is used to place some marker in the transaction log. Marker is basically a description plus current time at which the transaction was carried out. This is basically used to use restore command to restore the database to the point of time the marker is describing to recover from a problem.

Look at the following log file path.

If file is in use, you cannot open log file.

Nested transaction

At transaction can be nested. The outer transaction will control the inner transactions. It means that if the outer transaction is roll backed then inner transactions will be rolled back; no matter inner transactions are committed or roll backed. 

  • There is cascading effect of rollback of outer transaction upon its inner transactions.
  • We must commit or roll back inner transactions before committing or rolling back the outer transaction.
  • Committing inner transaction does not change the database. It merely passes this change to outer transaction.

Example of Nested Transaction

Look at the following nested transactions. T1 and T2 are two transactions where T2 is nested within T1. Note that outer transaction is still open when inner transaction is committed or roll backed. To count the total number of open transactions we use system global variable @@TRANCOUNT.

BEGIN TRANSACTION; -- T1
SELECT @@TRANCOUNT AS 'T1 Begins, Transactions Count'
    BEGIN TRANSACTION; -- T2
    SELECT @@TRANCOUNT AS 'T2 Begins, Transactions Count'
    COMMIT; -- T2 is committed, but T1 is still open
    SELECT @@TRANCOUNT AS 'T2 Ends, Transactions Count'
COMMIT; -- T1 is committed, no open transactions remaining
SELECT @@TRANCOUNT AS 'T1 Ends, Transactions Count'

Note that @@TRANCOUNT is a system function in T-SQL that returns the current nesting level of open transactions for the current session. It indicates how many transactions are currently active and provides a way to determine if the code is currently executing within a transaction. 

Example of Nested Transaction

Now we see a practical example of nested transaction. For this we use employees table.

CREATE TABLE dbo.Employees(
	Id int ,
	first_name varchar(10) ,
	last_name varchar(10) 
)
GO
BEGIN TRANSACTION
INSERT INTO Employees VALUES (1, 'Amit', 'Kumar')
INSERT INTO Employees VALUES (3, 'Ritu', 'Sinha') 
	BEGIN TRANSACTION
	INSERT INTO Employees VALUES (5, 'Rohit', 'Sinha')
	COMMIT TRANSACTION
ROLLBACK TRANSACTION
GO
SELECT * FROM Employees

In the above example, although inner transaction is committed, yet it is roll backed because outer transaction is roll backed.

Look at the following and find out the cause of error message “The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.


BEGIN TRANSACTION
INSERT INTO Employees VALUES (1, 'Amit', 'Kumar')
INSERT INTO Employees VALUES (3, 'Ritu', 'Sinha') 
	BEGIN TRANSACTION
	INSERT INTO Employees VALUES (5, 'Rohit', 'Sinha')
	ROLLBACK TRANSACTION
COMMIT TRANSACTION
GO

Corrected
BEGIN TRY
    BEGIN TRANSACTION;

    -- Outer transaction
    INSERT INTO Employees VALUES (1, 'Amit', 'Kumar');
    INSERT INTO Employees VALUES (3, 'Ritu', 'Sinha');

    -- Begin inner transaction
    BEGIN TRANSACTION;

    -- Inner transaction
    INSERT INTO Employees VALUES (5, 'Rohit', 'Sinha');

    -- Rollback inner transaction if needed (based on some condition)
    IF @@ERROR>0
        ROLLBACK TRANSACTION;
    ELSE
        -- Commit inner transaction
        COMMIT TRANSACTION;

    -- Commit outer transaction
    COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    -- Handle errors
    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;

    -- Optionally re-throw the error
    THROW;
END CATCH;

Suppose in your code there are a number of transactions then how can you commit one of them? 

COMMIT TRANSACTION statement

COMMIT TRANSACTION statement optionally uses transaction name after the commit transaction clause. Instead of literal name, we can also use variable name that is holding the transaction name after the COMMIT TRANSACTION. This will commit the transaction mentioned after the COMMIT TRANSACTION.

  • Naming the transaction name after the commit transaction clause is optional. It is just for readability purpose. 

BEGIN TRANSACTION empTrans WITH MARK 'Description for logging transaction'
update tblEmployee_Transaction
set Salary=9000
where Emp_id=2
update tblEmployee_Transaction
set Grade='Grade1'
where Emp_id=2
if @@TRANCOUNT>0
	rollback 
else
	commit 
go

ROLLBACK TRANSACTION statement

Rollback Transaction means cancelling all the statements of the transaction.

ROLLBACK TRANSACTION statement allows you to roll back the most recent BEGIN TRANSACTION.

Some facts about Rollback Transaction:

  • We can optionally write transaction name after rollback transaction.
  • ROLLBACK TRANSACTION will always rollback the most recent begin transaction statement.
  • ROLLBACK WORK always rollbacks the outermost transaction.

Syntax: ROLLBACK TRANSACTION clause is followed by either transaction name or a variable that holds the name of transaction or a variable that hold the save point or a save point name.

SAVE TRANSACTION statement

A single transaction can be divided into multiple save points so that transaction can be committed up to a save point.  We use save transaction statement followed by the save point name to create a save point inside a transaction. Look at the following pseudo code.

To commit to a particular save point and roll back statements after save point, we use rollback statement followed by save point name.  We can conditionally return to a particular save point by using case expression or if statement.

Save points can be created even within nested transactions. Look at the following example in this regard.

What will happen if two transactions are given same save point?

In this situation the final save point will be considered and earlier save points will be ignored. It means that when we roll back the transaction using this save point then the last save point will be used to rollback and all the statements after that save point will be roll backed.

SAVE TRANSACTION statement allows you partially commit transaction while still being able to rollback the rest of the transaction given after the save point.

When you save a transaction then it must be named. it means that after the SAVE TRANSACTION clause you must have to write its name. By using this name we can roll back the transaction later.

Already we have learnt that are transaction can be nested. To find out the number of nested transaction, you should use system global variable @@TRANCOUNT.

 

SQL Server ACID Properties of Transaction

ACID Properties

To understand transaction in depth, we must know all about its properties. We will look at ACID properties which are related to transaction. ACID is an acronym for Atomicity, Consistency, Isolation and Durability.  Now you look at each of them one by one. 

Atomicity means that a transaction is a unit of work.  It means that each statement of transaction will be considered as a component of the unit of work. If any statement fails then all statements will be rejected. In the above example, when salary is not updated then updated grade is cancelled. 

Now we understand about consistency.  If transaction is committed or rolls backed the state of database remains consistent. It means that if execution of any statement in transaction violates the rule or constraint then all statements of the transaction will be roll backed. In the above example, what if grade is updated without updating the salary. In this situation business rule will be violated and it will be in an inconsistent state. 

Now we look at the concept of isolation. Suppose that two users are performing some transactions on a table. Both the transactions will be independent and isolated to each other. The first transaction will not know anything about another transaction and vice versa. It means that one transaction will never see the intermediate state of another transaction. The concepts of isolation are important in the context of read and write operations upon data. 

Finally durability means when the transaction is committed it endures. The work performed by transaction is saved permanently and will be available even after rebooting the system.


SQL Server Types of Transaction

Types of Transaction

There are three types of transaction in SQL Server.

  1. Automatic transaction
  2. Explicit transaction
  3. Implicit transaction

 Automatic Transaction

In SQL Server, automatic transactions refer to the default behavior where individual SQL statements are treated as transactions and are automatically committed or rolled back based on success or failure of each statement. Look at the following example.

CREATE TABLE Employees
(
	Id int ,
	first_name varchar(10) ,
	last_name varchar(10)
) 
Go

Now insert a record into the table.

INSERT INTO Employees VALUES (1, 'Amit', 'Kumar')

The above statement will be automatically committed as the statement is without error. Now insert another record into the table. This will throw error and statement will be automatically roll backed.

INSERT INTO Employees VALUES ('Ritu', 'Sinha')

Note that we are not using commit or rollback statement but it is executed automatically.

Explicit Transaction

In SQL Server, explicit transaction is used to group multiple statements as a unit of transaction. Without this, each statement will be a transaction. We use BEGIN TRANSACTION statement explicitly to create a block of statements as transaction. Look at the following example.

BEGIN TRANSACTION
INSERT INTO Employees VALUES (1, 'Amit', 'Kumar')
INSERT INTO Employees VALUES ('Ritu', 'Sinha') --error
INSERT INTO Employees VALUES (5, 'Rohit', 'Sinha')
IF @@ERROR>0 
	Rollback TRANS
ELSE
	Commit TRANS

Note that @@ERROR global variable is used to count the number of errors in current session.

Implicit Transaction

In SQL Server, we use SET IMPLICIT_TRANSACTIONS ON statement to activate implicit transaction. By default it is not activated. In SQL Server, implicit transaction is used to group multiple statements as a unit of transaction without using BEGIN TRANSACTION statement. These statements will be grouped based on the COMMIT or ROLLBACK statement. All the statements before COMMIT or ROLLBACK statement will be part of the transaction. Note that unlike automatic transaction we need to use COMMIT or ROLLBACK statement.

To understand implicit transaction, we use the following steps.

TRUNCATE TABLE Employees
SET IMPLICIT_TRANSACTIONS on
INSERT INTO Employees VALUES (1, 'Amit', 'Kumar')
INSERT INTO Employees VALUES (2, 'Ritu', 'Sinha')
SELECT * FROM Employees

Run the above script. We find that SELECT query returns two rows. Add the following code.

ROLLBACK TRANSACTION
SELECT * FROM Employees 

Run the above script. We find that SELECT query returns no row.

How to know whether SET IMPLICIT_TRANSACTIONS is On or OFF in SQL Server?

In SQL Server, you can check whether the IMPLICIT_TRANSACTIONS setting is ON or OFF by querying the options column in the sys.dm_exec_sessions dynamic management view. The IMPLICIT_TRANSACTIONS setting determines whether implicit transactions are enabled or disabled for the current session.

SELECT 
    CASE 
		-- 2 in decimal is 10 in binary
        WHEN ( @@OPTIONS & 2 = 2 ) THEN 'ON' 
        ELSE 'OFF' 
    END AS ImplicitTransactionsStatus
FROM sys.dm_exec_sessions
WHERE session_id = @@SPID; -- @@SPID returns the current session ID

Continued in next post...

SQL Server Transaction

An Introduction to Transaction

In this post we will learn about transaction in SQL language. Transaction is fundamental concept in any modern database programming. Transaction is basically a unit of work where work implies one or more SQL statements for some operation. It means that all statements of transaction must succeed for success of work. If any statement in transaction throws error then all statements will be rolled back.

A SQL statement represents an action. A single statement or more than one statement can represent a transaction or unit of work. Transaction means commit all actions of a unit of work else rollback all actions.

Examples

  • Suppose that email ID of a person in bank is to be updated. To perform this operation, only one table i.e. personal details table of users will be updated.
  • Consider another example in which a person A transfers some money to person B. In this case two bank accounts or tables will be affected. The transaction will not be complete unless the account of A is debited and account of B is credited. There are two actions involved here; debiting account A and crediting account B.
  • Consider third example in which a sales person sells some product to a customer. In this case multiple tables will be affected e.g. product, customer, salesperson and order tables. Unless all these tables are not properly updated business status will not be okay. When all of these tables will be updated, transaction will be complete and business will reach from one consistent state to another consistent

Thus, how any actions together will represent a transaction is purely based on business requirement. Each action represents a SQL statement.

Definition: A collection of actions fulfilling a business requirement is called transaction.

We look at the following T-SQL example to understand transaction.

Example

First of all, we create a table for this example and insert some records into it.


use AppliedkDb
Go
Create table tblEmployee_Transaction
(
Emp_Id int
,Emp_Name varchar(10)
,Grade varchar(10)
,Salary int check(Salary<7000)
)
go
Insert into tblEmployee_Transaction values (1, 'Paul',  'Grade1', 6000)
Insert into tblEmployee_Transaction values  (2, 'Tom',   'Grade2', 2300)
Insert into tblEmployee_Transaction values  (3, 'Haris', 'Grade1', 5000)
go

Now table is ready to understand about transaction. By default, SQL Server database engine treats each SQL statement as a unit of work i.e. transaction. So, CREATE TABLE statement is a transaction. Inserting a row into table is a transaction. Similarly a UPDATE statement is a transaction.

update tblEmployee_Transaction
set Grade='Grade1',Salary=9000
where Emp_id=2
go
select * from tblEmployee_Transaction

In above example we use a single update statement to update both grade and salary of an employee. Both updating grade and salary together is taken as a transaction or unit of work in this example. In table definition, the salary constraint is that salary must be below 7000. So update statement to update salary to 9000 fails and the statement is roll backed. Neither grade nor salary is updated because of salary constraint violation.

NOTE: There are two possible cases for a transaction. Either it will be committed or roll backed. Commit means all the actions of transaction succeeds. If any action fails then transaction cannot be committed. Rollback means all the actions of transaction are cancelled.

Now we look at the following UPDATE situation.

update tblEmployee_Transaction
set Salary=9000
where Emp_id=2
update tblEmployee_Transaction
set Grade='Grade1'
where Emp_id=2
go
select * from tblEmployee_Transaction

In the above example, there are two update statements. Each statement represents a transaction. The transaction to update grade will succeed but the transaction to update salary will not succeed because of salary constraint violation.

Now the question is how to treat the two statements as a unit of single transaction. We use BEGIN TRANSACTION statement to create an explicit transaction. The statements following BEGIN TRANSACTION statement will be part of the transaction.

Syntax

Look at the following example.

BEGIN TRANSACTION 
Update tblEmployee_Transaction
set Salary=9000
where Emp_id=2
update tblEmployee_Transaction
set Grade='Grade1'
where Emp_id=2
go
select * from tblEmployee_Transaction

We have not used COMMIT TRANSACTION or COMMIT TRANSACTION statement at the end. So, the SQL engine takes the decision on its own. The first statement is roll backed but second statement is committed. So, we find that grade is updated while salary is not.

Note that COMMIT TRANSACTION does not guarantee that the transaction will be committed. A transaction is committed only when no constraint or rule is violated. For example, the following commit will not work because salary constraint is violated. You can rollback a transaction even if all its statements succeed.

BEGIN TRANSACTION 
update tblEmployee_Transaction
set Salary=9000
where Emp_id=2
COMMIT TRANSACTION
Go

Next look at the following example:

BEGIN TRANSACTION 
update tblEmployee_Transaction
set Salary=9000
where Emp_id=2
update tblEmployee_Transaction
set Grade='Grade1'
where Emp_id=2
If @@ERROR>0
	ROLLBACK TRANSACTION
Else
	COMMIT TRANSACTION
Go
Select * from tblEmployee_Transaction

SQL Server Locking In Databases

SQL Server is a multi user database server which allows multiple users to modify data at the same time. To protect data it provides locking mechanism. There are different locking mechanism such as pessimistic locking and optimistic locking.

To understand locking, we should understand how data is updated in database environment. When data is modified first of all it is written into transaction log file and then it is committed into data file. So, there are two step processes in data updating.

In case of pessimistic locking, lock is taken as soon as user begins modifying the data and lock is released after updating the data. It means that as soon as data is inserted into transaction log file, lock is taken and lock is released when entry is committed in data file. But in case of optimistic locking, lock is taken after data is inserted into transaction log file and lock is released when entry is committed in data file.

So, in case of pessimistic locking, for longer duration lock is taken compared to optimistic locking. It means that in case of pessimistic locking, no other user can access the data when another user is modifying the data. But in case of optimistic locking, other user can access the data when another user is modifying the data. The lock is taken when data is already modified but is not committed. In case of pessimistic locking lock is taken when data is neither modified nor committed.

In case of optimistic lock there is probability of write conflict when multiple users are modifying the data at the same time. The last user who has modified the data will override all modifications made by others. So in case of optimistic lock there is probability of dirty write.

continued...

SQL Server Database Design

Normalization

Normalization is basically for Data integrity offer database normalization provides four types of integrity. They are as follows.

  1. Entity integrity
  2. Domain integrity
  3. Referential integrity
  4. User defined integrity

Entity Integrity

Each entity represents a row in a table. Entity must be uniquely recognized in database. Primary key is used to uniquely identify an entity in a table. Violation of this rule will be against entity integrity.

What is composite primary key?

When more than one column are used in a table to create a primary key when it is called composite primary key.

What is natural primary key?

If there is data available in a table which can identify each no of the table uniquely then it will be called natural primary key but if primary key is artificial generated in a table it will be called surrogate primary key.

Domain Integrity

SQL Server provides a variety of tools for enforcing domain integrity for examples types user defined data types default constraint check constraints foreign key constraints all these are used to enforce domain integrity in a table.

Candidate key

Any number of columns in a table can be used as a primary key. All such combination of columns will be called candidate key. Out of this candidate keys we pick one of them as a primary key.

Surrogate key

Surrogate key is a unique identifier for rows in a table ordinary part of the table data.

First Normal Form in Database Design

Let us consider name property of a business entity called employee. We can first name middle name and last name together as name property. How we can separate them and create three properties for the employee. when we input a value in a column of a table state it must be atomic data otherwise it will violet the principle of First Normal Form for example an in employee table we can use Amit Kumar Gupta as value of name In name column but this is not atomic data. If you try to sort the table on the basis of surname then it will not be possible in this case because name field is going to hold multivalve data. The First Normal Form guides to use atomic data in a field or column of a table.

Remember that First Normal Form is a matter of judgment hard and fast rule to design a table.

Second normal formal: It means that each non primary key field of a table should depend only on primary key field of table and not on any non primary key field apart from table being in First Normal Form.

Identity column

Identity column is a column in a table for which SQL Server automatically provides values. When we define identity for a column then we set the initial value and stepping value for the Identity of the column. The initial value is known as seed value and stepping value is also called incremental value. Identity is a way to include surrogate key in a table.

Constraints on Table

Constraints are rules which are used to enforce Data integrity on a table. The Data integrity may be related to entity integrity, domain integrity or user defined integrity. Use different constraints such as unique, default, check, primary key, foreign key etc.

There are two ways to apply constraints.

  1. Column constant is applied on a column of a table.
  2. Table constant is applied at the table level.

What do you mean by cascading referential integrity in a database?

 continued...

Hot Topics