backup_sql
backup_sql
--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