online [0 / 0]

  • min
  • max
  • close

user

status

points

logon

Search results

Sign in or register now!


Why not sign up for free and bring this tutorial to bring it to life. Using our Practice-Labs will make this tutorial much easier to follow and provide you with the server to complete the tasks.


What you get with the lab:


  • It's FREE, but if that didn't grab you carry on down the list
  • Access real working Microsoft servers and Cisco networking equipment (dependant on tutorial content)
  • Carry out the tasks outlined in this tutorial without the need for your own equipment
  • Equipment already has the base configurations so you can focus on the parts you need to
  • Make a mistake, no problem in our labs, simply reset the device and start over

More information:


  • Registered users get 60 mins of free lab time for free tutorials
  • Paying users get unlimited lab time in all free tutorials

For unregistered users click hereto register for an account

Already have an account, click hereto login and use a Practice-Lab with this tutorial


Dont want to register?


No problem see what your missing by viewing this tutorial on SQL Server 2008 Snapshots as a video.

Creating and reverting database Snapshots in Microsoft SQL Server 2008



Exercise 1 - Managing database snapshots


This exercise is taken from our 70-432 - Microsoft SQL Server 2008 Implementation and Maintenance course and starts our series of free demos from our paid courses. If you like this demo and want to get more involved with Microsoft SQL Server 2008 then our SQL Server courses are just for you.

Our SQL Courses have the following benefits


  • No time restrictions
  • Additional lab layouts
  • Dozens more tasks
  • Extremely cost effective

This is a modified sample from our 70-432 - Microsoft SQL Server 2008 Implementation and Maintenance course, it has been modified to enable it to flow correctly as a standalone module.


In this exercise you will learn how to create and populate a database using T-SQL commands. You will then further your T-SQL skills by creating a database snapshot, looking at the differences in the files between the active and snapshot databases, then finally modifying some data and restoring the snapshot. Please refer to the Microsoft website for more information on database snapshots at Microsoft Technet which will help you gain a further understanding of this technology.

In this exercise you will be required to Power On and connect to the following servers from your Practice-Lab application:

SQL Server 1

If this is the first time you are accessing a Practice-Lab why not take a few minutes to watch our video on getting started, or if you are still having difficulty connecting to your Practice-Lab device please refer to our help pages.

Create, Drop and Revert operations on snapshot database overview

A Database Snapshot is a read-only, static view of a database without any uncommitted transactions. The uncommitted transactions are rolled back to make the database snapshot transactionally consistent.

When you create a snapshot database, that database will be allocated to an empty sparse file . Whenever there are changes to the original data pages, the original pages are moved to the sparse file.

If you access the database snapshot, it will be read from the sparse file and from the original database data pages which have not changed.

Creating a database

Use the following steps to create a database using T-SQL statements, these statements will create the database then insert some data so that a snapshot can be made.

Step 1

If you haven't done so already, power on and connect to SQL Server 1. Ensure you login as administrator (leave the auto-login checkbox checked). Then open up SQL Management Studio and create a new query. To do this, double-click the SQL Server Management Icon on the desktop of SQL Server 1.

Next click the Connect button in the connect to server dialog once the management studio has loaded.

opening SQL server management studio

To create a new query, click the New Query button in the top left hand side of SQL Server Management Studio.

You should now have a screen similar to the below:

new query in ssms

The Query window allows you to type and execute SQL commands. To type a command, place your cursor in the large white space in the middle of the screen and type in the respective T-SQL command that you require. To execute the command hit the Execute button which has the red exclamation mark on it in the toolbar at the top of the screen.

Use the following T-SQL commands to create the database, make sure you clear the query window after each one (alternatively when you are more confident with SQL commands you can enter in a number of them at the same time to be executed sequentially for example you could create the new database, table and insert the data all in the same query!):


	USE master
    GO
first query in ssms using master

Remember after each command to hit execute!

These commands will create a database called MyFirstDatabase:


     CREATE DATABASE MyFirstDatabase
     GO
Creating a database using T-SQL commands

Next you want to select this database to run the next set of SQL commands on (in the screenshot below I have entered in the 3 of the statements below, Select the database, create the table and insert the data).

Creating a table and inserting data using T-SQL commands

	 USE MyFirstDatabase
     GO

Next we need to create a table in the database so that we can populate it with some data.


    CREATE TABLE MyFirstTable (ID INT, FirstName VARCHAR(128), LastName VARCHAR(128))
	GO 

Now lets populate the table with a few rows of data.


    INSERT INTO MyFirstTable VALUES(1, 'Rupert', 'Bear')
    INSERT INTO MyFirstTable VALUES(2, 'Postman', 'Pat')
    INSERT INTO MyFirstTable VALUES(3, ' Thomas', 'Tank-Engine')
    GO

Before continuing, lets have a look at the table data. To do this, we can expand in object explorer Databases > MyFirstDatabase > Tables then Right-click the table and select Select top 1000 rows

selecting rows in ssms

You can see the data we have entered in:

viewing selected rows in ssms

Finally we create the snapshot of the database using the following commands. Ensure you are back at your query window (or open a new query)


    CREATE DATABASE MySnapshot ON (Name ='MyFirstDatabase', FileName='c:\myfirstdatabase_snapshot.ss1') 
    AS SNAPSHOT OF MyFirstDatabase
    GO  
Creating a database snapshot using t-sql commands

Lets take a look at the differences in the two database files

MyFirstDatabase (Located under C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA):

The properties of the live database

Snapshot of MyFirstDatabase (Located under C :\):

Compare the snapshot database file versus the live database file

Notice the Size on disk properties of the two files!

Revert Operation

To be able to perform a revert operation; the user must have the RESTORE DATABASE permissions on the source database, this is not a problem for our example, but its something to keep in mind in a real environment.

Before you restore the snapshot, why not add some additional data, or modify the existing data in the table?

Use SSMS (SQL Server Management Studio) or a T-SQL command to modify some data, in this example I have chosen to use SSMS by right clicking on our table and selecting Edit top 200 rows you can then edit the cells rather like a spread sheet:

Editing rows in SQL Server Management Studio

As you can see I have modified all the rows:

Edited rows in SSMS

Step 1

To restore a database from a snapshot database use the following T-SQL statements (remember use a New Query or your existing query window):


	USE master
    GO 
    RESTORE DATABASE MyFirstDatabase FROM DATABASE_SNAPSHOT = 'MySnapshot'
    GO
Restoring a database snapshot using T-SQL commands

When you execute this command, you may see and error:

Restore error 5970

Make sure that you have closed your edit window before hand (in fact you may have to close and re-open SQL Server Management Studio)!

Succesful database snapshot restore in SQL Server 2008

You can see our updated rows have been reverted:

Our recovered rows

Also try

  • Why not try to use the Drop Database T-SQL command to drop the snapshot.

Summary

In this exercise learned how to create a database, add a table and some fictitious data. You then learned how to create a snapshot of this data before finally restoring the snapshot.

You also saw the differences in the file types between the live database and the snapshot of the database which is important to understand.

And finally

The above exercise gives you an insight to what you can achieve in a Practice-Lab. Being able to learn, discover and explore new topics and tools without the fear of compromising production environments.

If you are interested in more topics like the above then have a look at our 70-432 - Microsoft SQL Server 2008 Implementation and Maintenance Practice-Lab course.

Alternatively look at purchasing access to our Practice-Lab library that covers all of our supported technologies.

We recommend repeating these exercises as it will not only help build your confidence in the subject matter but will open your mind to the other capabilities. Do not feel afraid to experiment in our labs!

If you wish to comment on this Practice-Lab please send an email to Support@Practice-IT.co.uk with your feedback.