0% found this document useful (0 votes)
7 views5 pages

backup_sql

The document outlines a series of SQL Server operations including creating a backup device, performing full and differential backups, and managing transactions on the AdventureWorks2008R2 database. It details the steps to adjust product prices based on conditions, delete records, and restore the database to specific points in time. Additionally, it includes instructions for handling transaction log backups and restoring the database after deletions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

backup_sql

The document outlines a series of SQL Server operations including creating a backup device, performing full and differential backups, and managing transactions on the AdventureWorks2008R2 database. It details the steps to adjust product prices based on conditions, delete records, and restore the database to specific points in time. Additionally, it includes instructions for handling transaction log backups and restoring the database after deletions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

--Module 8

--1. Trong SQL Server, tạo thiết bị backup có tên adv2008back lưu trong thư mục T:\
backup\adv2008back.bak
exec sp_addumpdevice 'disk','adv2008back','D:\backup\adv2008back.bak'
--2. Attach CSDL AdventureWorks2008, chọn mode recovery cho CSDL này là full, rồi
thực hiện full backup vào thiết bị backup vừa tạo
alter database AdventureWorks2008R2
set recovery full
backup database AdventureWorks2008R2
to adv2008back
with description = 'AdventureWorks2008R2 FULL backup 1st' --FILE 1
go
--3. Mở CSDL AdventureWorks2008, tạo một transaction giảm giá tất cả mặt hàng xe
đạp trong bảng Product
xuống $15 nếu tổng trị giá các mặt hàng xe đạp không thấp hơn 60%.
select * from [Production].[ProductCategory]
select * from [Production].[ProductSubcategory]
where [ProductcategoryID] = 1
select * from [Production].[Product]
where [ProductSubcategoryID] in(select [ProductSubcategoryID] from [Production].
[ProductSubcategory]
where [ProductcategoryID] = 1)
begin tran
declare @tongtienxe money, @tong money
set @tongtienxe = (select sum([ListPrice]) from [Production].[Product]
where [ProductSubcategoryID] in(select
[ProductSubcategoryID] from [Production].[ProductSubcategory]
where [ProductcategoryID] = 1))
set @tong = (select sum([ListPrice]) from [Production].[Product])
if(@tongtienxe/@tong)>0.9
begin
update [Production].[Product]
set [ListPrice] = [ListPrice]-15
where [ProductSubcategoryID] in(select [ProductSubcategoryID] from [Production].
[ProductSubcategory]
where [ProductcategoryID] = 1)
commit tran
end
else
rollback tran
go
--4. Thực hiện các backup sau cho CSDL AdventureWorks2008, tất cả backup đều lưu
vào thiết bị backup vừa tạo
a. Tạo 1 differential backup
backup database AdventureWorks2008R2
to adv2008back
with differential, description = 'AdventureWorks2008R2 differential backup 1st'--FILE 2
go
b. Tạo 1 transaction log backup
backup log AdventureWorks2008R2
to adv2008back
with description = 'AdventureWorks2008R2 backup log 1st'--FILE 3
go
--5. (Lưu ý ở bước 7 thì CSDL AdventureWorks2008 sẽ bị xóa. Hãy lên kế hoạch phục
hồi cơ sở dữ liệu cho các hoạt động trong câu 5, 6).
--Xóa mọi bản ghi trong bảng Person.EmailAddress, tạo 1 transaction log backup
select * from Person.EmailAddress
delete from Person.EmailAddress
backup log AdventureWorks2008R2
to adv2008back
with description = 'AdventureWorks2008R2 backup log 2nd' --FILE4
go
--6. Thực hiện lệnh:
--a. Bổ sung thêm 1 số phone mới cho nhân viên có mã số business là 10000 như sau:
--INSERT INTO Person.PersonPhone VALUES (10000,'123-456-7890',1,GETDATE())
INSERT INTO Person.PersonPhone VALUES (10000,'123-456-7890',1,GETDATE())
--b. Sau đó tạo 1 differential backup cho AdventureWorks2008 và lưu vào thiết bị
backup vừa tạo.
backup database AdventureWorks2008R2
to adv2008back
with differential, description = 'AdventureWorks2008R2 differential backup 2nd' -- FILE 5
go
--c. Chú ý giờ hệ thống của máy.
--Đợi 1 phút sau, xóa bảng Sales.ShoppingCartItem
select * from Sales.ShoppingCartItem

drop table Sales.ShoppingCartItem

--7. Xóa CSDL AdventureWorks2008


use master
drop database AdventureWorks2008R2
--8. Để khôi phục lại CSDL:
-- kiểm tra FILE, để biết RESTORE
restore headeronly
from disk = 'D:\backup\adv2008back.bak'
--a. Như lúc ban đầu (trước câu 3) thì phải restore thế nào?
restore database [AdventureWorks2008R2]
from [adv2008back]
with FILE = 7, RECOVERY
go
select * from [Production].[Product]
--b. Ở tình trạng giá xe đạp đã được cập nhật và bảng Person.EmailAddress vẫn còn
nguyên chưa bị xóa (trước câu 5) thì cần phải restore thế nào?
use master
drop database AdventureWorks2008R2
restore database [AdventureWorks2008R2]
from [adv2008back]
with FILE = 7, NORECOVERY
restore database [AdventureWorks2008R2]
from [adv2008back]
with FILE = 8, NORECOVERY
restore log [AdventureWorks2008R2]
from [adv2008back]
with FILE = 9, RECOVERY
select * from [Production].[Product]
where [ProductSubcategoryID] in(select [ProductSubcategoryID] from [Production].
[ProductSubcategory]
where [ProductcategoryID] = 1)

select * from [Person].[EmailAddress]


--c. Đến thời điểm đã được chú ý trong câu 6c thì thực hiện việc restore lại CSDL
AdventureWorks2008 ra sao?
use master
drop database AdventureWorks2008R2
restore log [AdventureWorks2008R2]
from [adv2008back]
with FILE = 10, NORECOVERY, STOPAT ='May 17, 2019 12:27 PM'
go

You might also like