首页 > 代码库 > Sqlserver事务备份和还原实例

Sqlserver事务备份和还原实例

 1 create database mydb
 2 use mydb
 3 go
 4 create table account(
 5     id varchar(16),
 6     name varchar(16),
 7     balance float
 8 )
 9 go
10 select * from account
11 
12 insert into account(id, name, balance) values(620101, liyong, 300)
13 insert into account(id, name, balance) values(620106, mali, 400)
14 --insert into account(id, name, balance) values(‘620009‘, ‘chenying‘, 800)
15 insert into account(id, name, balance) values(646009, chenying, 800)
16 --delete from account where id = ‘620009‘
17 go
18 update account set balance = balance - 1000 where id = 620101
19 update account set balance = balance + 1000 where id = 620106
20 --消息 547,级别 16,状态 0,第 1 行
21 --UPDATE 语句与 CHECK 约束"CK_Blance"冲突。该冲突发生于数据库"mydb",表"dbo.account", column ‘balance‘。
22 --语句已终止。
23 
24 go
25 --alter table account
26 --alter COlumn balance int
27 go
28 alter table account
29 add constraint CK_Blance check(balance >= 0)
30 go
31 alter table account
32 drop constraint CK_Blance
33 --定一个事务
34 --从liyong扣钱往mali加钱
35 begin transaction
36 update account set balance = balance - 1000 where id = 620101
37 if((select balance output from account where id = 620101) < 0)
38 begin
39 PRINT(余额不足!);
40 ROLLBACK;
41 end
42 else
43 begin
44     update account set balance = balance + 1000 where id = 620106
45     commit;
46     PRINT(转账成功!);
47 end
48 go
49 sp_help
50 --备份设备
51 sp_addumpdevice disk, xk_bak ,d:\xk_bak
52 --备份数据库
53 backup database mydb
54 to xk_bak
55 --还原数据库
56 restore database mydb from disk = d:\xk_bak
57 with replace; --覆盖

 

Sqlserver事务备份和还原实例