-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path44_Database_Snapshot.sql
More file actions
278 lines (237 loc) · 6.3 KB
/
44_Database_Snapshot.sql
File metadata and controls
278 lines (237 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**************************************************************
* SQL Server 2022 Database Snapshot Tutorial
* Description: This script demonstrates how to work with database
* snapshots in SQL Server 2022. It covers:
* - Creating a database snapshot
* - Querying data from a snapshot
* - Comparing data between a snapshot and source database
* - Reverting to a snapshot (database restore)
* - Managing and dropping snapshots
**************************************************************/
-------------------------------------------------
-- Region: 1. Setup and Initial Database Creation
-------------------------------------------------
USE master;
GO
/*
Create a test database with sample data.
We'll create snapshots of this database.
*/
IF DB_ID('TestDB') IS NOT NULL
DROP DATABASE TestDB;
GO
CREATE DATABASE TestDB;
GO
USE TestDB;
GO
/*
Create a sample table and insert test data.
*/
CREATE TABLE dbo.Employees
(
EmployeeID INT PRIMARY KEY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
Salary DECIMAL(10, 2)
);
GO
INSERT INTO dbo.Employees (EmployeeID, FirstName, LastName, Salary)
VALUES
(1, 'John', 'Doe', 50000.00),
(2, 'Jane', 'Smith', 60000.00),
(3, 'Bob', 'Johnson', 55000.00);
GO
-------------------------------------------------
-- Region: 2. Creating Database Snapshots
-------------------------------------------------
USE master;
GO
/*
Create a database snapshot of TestDB.
The snapshot is a read-only, point-in-time view of the database.
Note: You must specify the file path for each database file.
*/
CREATE DATABASE TestDB_Snapshot_1
ON
(
NAME = TestDB,
FILENAME = 'E:\SQL_Data\TestDB_Snapshot_1.ss'
)
AS SNAPSHOT OF TestDB;
GO
/*
Verify that the snapshot was created successfully.
*/
SELECT name, database_id, source_database_id, create_date, state_desc
FROM sys.databases
WHERE source_database_id IS NOT NULL;
GO
-------------------------------------------------
-- Region: 3. Querying Data from a Snapshot
-------------------------------------------------
/*
Query data from the database snapshot.
Snapshots are read-only, so you can only perform SELECT operations.
*/
USE TestDB_Snapshot_1;
GO
SELECT * FROM dbo.Employees;
GO
-------------------------------------------------
-- Region: 4. Modifying Data in the Source Database
-------------------------------------------------
/*
Make changes to the data in the source database.
This will create divergence between the source and snapshot.
*/
USE TestDB;
GO
-- Update an existing employee
UPDATE dbo.Employees
SET Salary = 52000.00
WHERE EmployeeID = 1;
GO
-- Add a new employee
INSERT INTO dbo.Employees (EmployeeID, FirstName, LastName, Salary)
VALUES (4, 'Sarah', 'Williams', 65000.00);
GO
-- Delete an employee
DELETE FROM dbo.Employees
WHERE EmployeeID = 3;
GO
-- Verify changes in the source database
SELECT * FROM dbo.Employees;
GO
-------------------------------------------------
-- Region: 5. Comparing Source Database and Snapshot
-------------------------------------------------
/*
Compare data between the source database and the snapshot.
The snapshot will still show the original data before changes.
*/
-- Query from source database
SELECT 'Source Database' AS Source, * FROM TestDB.dbo.Employees;
GO
-- Query from snapshot
SELECT 'Database Snapshot' AS Source, * FROM TestDB_Snapshot_1.dbo.Employees;
GO
-------------------------------------------------
-- Region: 6. Taking a New Snapshot
-------------------------------------------------
USE master;
GO
/*
Create a new snapshot to capture the current state of the database.
*/
CREATE DATABASE TestDB_Snapshot_2
ON
(
NAME = TestDB,
FILENAME = 'E:\SQL_Data\TestDB_Snapshot_2.ss'
)
AS SNAPSHOT OF TestDB;
GO
/*
List all available snapshots for TestDB.
*/
SELECT name, database_id, source_database_id, create_date, state_desc
FROM sys.databases
WHERE source_database_id = DB_ID('TestDB');
GO
-------------------------------------------------
-- Region: 7. Reverting to a Snapshot (Database Restore)
-------------------------------------------------
USE master;
GO
/*
Restore the database from a snapshot.
Note:
1. You must close all connections to both the source database and the snapshot.
2. The source database must be in SINGLE_USER mode.
3. After restoration, the snapshot will be automatically dropped.
*/
-- First, set the database to SINGLE_USER mode to close connections
ALTER DATABASE TestDB
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
-- Restore the database from the snapshot
RESTORE DATABASE TestDB
FROM DATABASE_SNAPSHOT = 'TestDB_Snapshot_1';
GO
-- Set the database back to MULTI_USER mode
ALTER DATABASE TestDB
SET MULTI_USER;
GO
/*
Verify that the database was restored from the snapshot.
The data should be back to its original state.
*/
USE TestDB;
GO
SELECT * FROM dbo.Employees;
GO
-------------------------------------------------
-- Region: 8. Managing and Dropping Snapshots
-------------------------------------------------
USE master;
GO
/*
Drop database snapshots when they are no longer needed.
*/
DROP DATABASE TestDB_Snapshot_2;
GO
/*
Verify that the snapshot was dropped.
*/
SELECT name, database_id, source_database_id, create_date
FROM sys.databases
WHERE source_database_id = DB_ID('TestDB');
GO
-------------------------------------------------
-- Region: 9. Multiple Snapshots for Point-in-Time Views
-------------------------------------------------
USE TestDB;
GO
/*
Let's make additional changes to demonstrate multiple snapshots.
*/
UPDATE dbo.Employees
SET Salary = Salary * 1.1; -- 10% salary increase
GO
-- Create another snapshot
USE master;
GO
CREATE DATABASE TestDB_Snapshot_3
ON
(
NAME = TestDB,
FILENAME = 'E:\SQL_Data\TestDB_Snapshot_3.ss'
)
AS SNAPSHOT OF TestDB;
GO
/*
View available snapshots with their creation dates.
*/
SELECT name, create_date
FROM sys.databases
WHERE source_database_id = DB_ID('TestDB')
ORDER BY create_date;
GO
-------------------------------------------------
-- Region: 10. Cleanup
-------------------------------------------------
USE master;
GO
/*
Clean up all resources by dropping snapshots and the test database.
*/
-- Uncomment the following lines to clean up resources:
/*
DROP DATABASE TestDB_Snapshot_1;
GO
DROP DATABASE TestDB_Snapshot_3;
GO
DROP DATABASE TestDB;
GO
*/