create database mydb
use mydb
go
create table account(
id varchar(16),
name varchar(16),
balance float
)
go
select * from account
insert into account(id, name, balance) values('620101', 'liyong', 300)
insert into account(id, name, balance) values('620106', 'mali', 400)
--insert into account(id, name, balance) values('620009', 'chenying', 800)
insert into account(id, name, balance) values('646009', 'chenying', 800)
--delete from account where id = '620009'
go
update account set balance = balance - 1000 where id = '620101'
update account set balance = balance + 1000 where id = '620106'
--消息 547,級(jí)別 16,狀態(tài) 0,第 1 行
--UPDATE 語句與 CHECK 約束"CK_Blance"沖突。該沖突發(fā)生于數(shù)據(jù)庫"mydb",表"dbo.account", column 'balance'。
--語句已終止。
go
--alter table account
--alter COlumn balance int
go
alter table account
add constraint CK_Blance check(balance >= 0)
go
alter table account
drop constraint CK_Blance
--定一個(gè)事務(wù)
--從liyong扣錢往mali加錢
begin transaction
update account set balance = balance - 1000 where id = '620101'
if((select balance output from account where id = '620101') 0)
begin
PRINT('余額不足!');
ROLLBACK;
end
else
begin
update account set balance = balance + 1000 where id = '620106'
commit;
PRINT('轉(zhuǎn)賬成功!');
end
go
sp_help
--備份設(shè)備
sp_addumpdevice 'disk', 'xk_bak' ,'d:\xk_bak'
--備份數(shù)據(jù)庫
backup database mydb
to xk_bak
--還原數(shù)據(jù)庫
restore database mydb from disk = 'd:\xk_bak'
with replace; --覆蓋
以上這篇Sqlserver事務(wù)備份和還原的實(shí)例代碼(必看)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。