`
lbxhappy
  • 浏览: 303385 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

创建数据库、表、主外键、各种约束、存储过程、视图、索引、事务使用、触发器、创建登录账号、数据库用户 Sql语句示例

阅读更多
  1. -----------创建数据库----------------  
  2. user master  
  3. go  
  4. if  exists (select * from sysdatabases where name= 'bankSystem' )  
  5.     drop database bankSystem  
  6. go  
  7. create database bankSystem  
  8. on primary  
  9. (  
  10.     name='bank_data' ,  
  11.     filename='D:\bank\bank_data.mdf' ,  
  12.     size=5,  
  13.     filegrowth=15%  
  14. )  
  15. log on  
  16. (  
  17.     name='bank_log' ,  
  18.     filename='D:\bank\bank_log.ldf' ,  
  19.     size=5,  
  20.     filegrowth=15%  
  21. )  
  22. go  
  23.   
  24. ----------------建表并添加约束----------------  
  25. use bankSystem  
  26. go  
  27. ---------------------表userInfo--------------  
  28. if  exists (select * from sysObjects where name= 'userInfo' )  
  29.     drop table userInfo  
  30. go  
  31. create table userInfo  
  32. (  
  33.     customerID int  identity(1,1) not  null ,  
  34.     customerName varchar(30) not null ,  
  35.     PID varchar(18) not null ,  
  36.     telephone varchar(13) not null ,  
  37.     address varchar(50)  
  38. )  
  39. go  
  40. alter table userInfo  
  41. add constraint PK_customID primary key (customerID)  
  42. alter table userInfo  
  43. add constraint UQ_pid unique (PID)  
  44. alter table userInfo  
  45. add constraint CK_pid check (pid like '4206[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'  or  
  46.                             pid like '4206[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' )  
  47.   
  48. alter table userInfo      
  49. add constraint CK_telephone check (telephone like '13[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'  or  
  50.                                     telephone like '%-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' )  
  51. ------------表cardInfo---------------  
  52. if  exists (select * from sysObjects where name= 'cardInfo' )  
  53.     drop table cardInfo  
  54. go  
  55. create table cardInfo  
  56. (  
  57.     cardID varchar(18) not null ,  
  58.     curType varchar(10) not null ,  
  59.     savingType varchar(10) not null ,  
  60.     openDate datetime not null ,  
  61.     openMoney money not null ,  
  62.     balance money not null ,  
  63.     pass varchar(6) not null ,  
  64.     IsReportLoss bit not null ,  
  65.     customerID int  not  null   
  66. )  
  67. go  
  68. alter table cardInfo  
  69. add constraint PK_cardID primary key (cardID)  
  70. alter table cardInfo  
  71. add constraint CK_cardID check (cardID like '1010 3576 [0-9][0-9][0-9][0-9] [0-9][0-9][0-9]' )  
  72. alter table cardInfo  
  73. add constraint DF_curType default  ( 'RMB' for  curType  
  74. alter table cardInfo  
  75. add constraint DF_openDate default  (getDate())  for  openDate  
  76. alter table cardInfo  
  77. add constraint CK_openMoney check (openMoney >=1)  
  78. alter table cardInfo  
  79. add constraint CK_balance check (balance>=1)  
  80. alter table cardInfo  
  81. add constraint CK_pass check (pass like '[0-9][0-9][0-9][0-9][0-9][0-9]' )  
  82. alter table cardInfo  
  83. add constraint DF_pass default  (888888)  for  pass  
  84. alter table cardInfo  
  85. add constraint DF_IsReportLoss default  (0)  for  IsReportLoss  
  86. alter table cardInfo  
  87. add constraint FK_customerID foreign key (customerID) references userInfo (customerID)  
  88. alter table cardInfo  
  89. add constraint CK_savingType check (savingType like '活期'  or savingType like  '定活两期'  or savingType like  '定期' )   
  90.   
  91. ---------------表transInfo-------------  
  92. if  exists (select * from sysObjects where name= 'transInfo' )  
  93.     drop table transInfo  
  94. go  
  95. create table transInfo  
  96. (  
  97.     transDate datetime not null ,  
  98.     cardID varchar(18) not null ,  
  99.     transType varchar(4) not null ,  
  100.     transMoney money not null ,  
  101.     remark text  
  102. )  
  103. go  
  104. alter table transInfo  
  105. add constraint DF_transDate default  (getDate())  for  transDate  
  106. alter table transInfo  
  107. add constraint FK_cardID foreign key (cardID) references cardInfo (cardID)  
  108. alter table transInfo  
  109. add constraint CK_transType check (transType like '存入'  or transType like  '支取' )  
  110. alter table transInfo  
  111. add constraint CK_transMoney check (transMoney >0)  
  112. go  
  113.   
  114. --------------插入数据----------------  
  115. insert into userInfo values ('张三' , '420656789012345' , '010-67898978' , '北京海淀' )  
  116. insert into userInfo values ('李四' , '420645678912345678' , '0478-44443333' , default )  
  117. insert into cardInfo values ('1010 3576 1212 113' , default , '定期' , '2007-10-10 11:54:36.812' ,1,1, default , default ,2)  
  118. insert into cardInfo values ('1010 3576 1234 567' , default , '活期' , '2007-10-10 11:58:45.352' ,1000,1000, default , default ,1)  
  119.   
  120. insert into transInfo (transDate,transType,cardID,transMoney) values ('2007-10-24 11:56:36.812' , '支取' , '1010 3576 1234 567' ,900)  
  121. update cardInfo set  balance=balance-900 where cardID= '1010 3576 1234 567'   
  122. insert into transInfo (transDate,transType,cardID,transMoney) values ('2007-10-24 11:56:54.245' , '存入' , '1010 3576 1212 113' ,5000)  
  123. update cardInfo set  balance=balance+5000 where cardID= '1010 3576 1212 113'   
  124.   
  125. ---------------修改密码--------------  
  126. update cardInfo set  pass= '123456'  where cardID= '1010 3576 1234 567'   
  127. update cardInfo set  pass= '123123'  where cardID= '1010 3576 1212 113'   
  128.   
  129. -------------是否挂失------------  
  130. update cardInfo set  IsReportLoss=1 where cardID= '1010 3576 1212 113'   
  131.   
  132. ---------------统计银行资金流通余额和盈利结算--------------  
  133. declare @inMoney money,@outMoney money,@sumMoney money,@rateEnd money   
  134. select @inMoney=sum(transMoney) from transInfo where transType='存入'   
  135. select @outMoney=sum(transMoney) from transInfo where transType='支取'   
  136. set  @sumMoney=@inMoney-@outMoney  
  137. set  @rateEnd=@outMoney*0.008-@inMoney*0.003  
  138. print '银行流余额总计为:' +convert(varchar(20),@sumMoney)+ 'RMB'   
  139. print '盈利结算结果为:' +convert(varchar(20),@rateEnd)+ 'RMB'   
  140.   
  141.   
  142. ---------------查询本周开户卡号----------------  
  143. select cardID from cardInfo where datepart(wk,openDate)=datepart(wk,getDate())  
  144.   
  145.   
  146. ----------------查询本月交易金额最高卡号------------  
  147. select * from userInfo where customerID in    
  148.         (select customerID from cardInfo where cardID in    
  149.             (select cardID from cardInfo where datepart(wk,openDate)=datepart(wk,getDate())))  
  150.   
  151. ----------------本月交易金额最大的卡号为-----------------  
  152. select cardID from transInfo where transMoney=(  
  153.         select max(transMoney) from transInfo where datepart(mm,transDate)=datepart(mm,getDate()))  
  154.   
  155. ---------------------挂失帐号客户信息----------------  
  156. select customerName as  客户姓名 telephone  as  电话 from userInfo where customerID  in   
  157.         (select customerID from cardInfo where isreportloss = 1)   
  158.   
  159. -------------------------催款提醒业务-------------  
  160. select customerName as  客户姓名,telephone  as  电话,balance  as  卡上余额 from userInfo inner join cardInfo   
  161.         on userInfo.customerID=cardInfo.customerID where userInfo.customerID in   
  162.         (select customerID from cardInfo where balance < 200)  
  163.   
  164. ------------创建索引--------------  
  165. if  exists (select * from sysindexes where name= 'IX_cardid' )  
  166.     drop index transInfo.IX_cardid  
  167. go  
  168. create nonclustered index IX_cardid   
  169.     on transInfo (cardID)  
  170.     with fillfactor=70  
  171. go  
  172.   
  173. select * from transInfo with(index=IX_cardid) where cardID ='1010 3576 1212 113'   
  174.   
  175. ------------创建视图-------------  
  176. if  exists (select * from sysobjects where name= 'view_userInfo' )  
  177.     drop view view_userInfo  
  178. go  
  179. create view view_userInfo  
  180.     as   
  181.         select 客户编号=customerID,开户姓名=customerName,身份证号=PID,电话=telephone,地址=address from userinfo  
  182. go  
  183. if  exists (select * from sysobjects where name= 'view_cardInfo' )  
  184.     drop view view_cardInfo  
  185. go  
  186. create view view_cardInfo  
  187.     as   
  188.         select 卡号=cardID,货币=curType,存款类型=savingType,开户日期=openDate,  
  189.                 余额=balance,密码=pass,是否挂失=isreportloss,客户编号=customerID from cardInfo  
  190. go  
  191. if  exists (select * from sysobjects where name= 'view_transInfo' )  
  192.     drop view view_transInfo  
  193. go  
  194. create view view_transInfo  
  195.     as   
  196.         select 交易日期=transDate,交易类型=transType,卡号=cardID,交易金额=transMoney,备注=remark  
  197.                 from transInfo  
  198. go  
  199. select * from view_userInfo  
  200. select * from view_cardInfo  
  201. select * from view_transInfo  
  202. go  
  203.   
  204. ---------------创建存储过程----------------  
  205. if  exists (select * from sysobjects where name= 'proc_getAndoutMoney' )  
  206.     drop proc proc_getAndoutMoney  
  207. go  
  208. create proc proc_getAndoutMoney  
  209.     @cardID varchar(19),  
  210.     @money money,  
  211.     @type char (4),  
  212.     @inputpass char (6)  
  213.     as   
  214.         if (@type= '支取' )  
  215.             begin  
  216.                 begin transaction  
  217.                 declare @error int   
  218.                 set  @error = 0  
  219.                 if (@inputpass=(select pass from cardInfo where cardID=@cardID))  
  220.                     begin  
  221.                         update cardInfo set  balance=balance-@money where cardID=@cardID  
  222.                         set  @error=@error+@@error  
  223.                         insert into transInfo values (getDate(),@cardID,@type,@money,default )  
  224.                         set  @error=@error+@@error  
  225.                     end  
  226.                 else   
  227.                     print '密码错误'   
  228.                 if  (@error<>0)  
  229.                     rollback transaction  
  230.                 else   
  231.                     commit transaction  
  232.             end  
  233.         else   
  234.             begin  
  235.                 update cardInfo set  balance=balance+@money where cardID=@cardID  
  236.                 insert into transInfo  values (getDate(),@cardID,@type,@money,default )   
  237.             end  
  238. go  
  239. exec proc_getAndoutMoney '1010 3576 1212 113' ,500, '存入' , '000000'   
  240. exec proc_getAndoutMoney '1010 3576 1234 567' ,300, '支取' , '123446'   
  241. select * from cardInfo where cardID='1010 3576 1212 113'   
  242.   
  243. -----------随机产生卡号----------  
  244. use bankSystem   
  245. go  
  246. if  exists (select * from sysobjects where name= 'proc_randCardID' )  
  247.     drop proc proc_randCardID  
  248. go  
  249. create proc proc_randCardID  
  250.     @randCardID varchar(18) output  
  251.     as   
  252.         declare @rand numeric(15,8)  
  253.         declare @tempStr varchar(16)  
  254.         select @rand=rand((datepart(mm,getDate())*100000+datepart(ss,getDate())*1000+datepart(ms,getDate())))  
  255.         set  @tempStr=convert(varchar(16),@rand)  
  256.         set  @randCardID= '1010 3576 ' +subString(@tempStr,3,4)+ ' ' +subString(@tempStr,7,3)  
  257. go  
  258. declare @mycardID varchar(18)  
  259. exec proc_randCardID @mycardID output  
  260. print '随机产生卡号为:' +@mycardID   
  261.   
  262. -------------开户存储过程--------------  
  263. set  nocount on  
  264. if  exists (select * from sysobjects where name= 'proc_openAccount' )  
  265.     drop proc proc_openAccount  
  266. go  
  267. create proc proc_openAccount  
  268.     @customerName varchar(8),  
  269.     @PID varchar(18),  
  270.     @telephone char (13),  
  271.     @openMoney money,  
  272.     @savingType char (8),  
  273.     @address varchar(50)=' '   
  274.     as   
  275.         declare @cardID varchar(18)  
  276.         declare @customerID int   
  277.         while (1=1)  
  278.             begin  
  279.                 exec proc_randCardID @cardID output   
  280.                 if  not exists (select * from cardInfo where cardID=@cardID)  
  281.                     break   
  282.                 else   
  283.                     continue   
  284.             end  
  285.         if  (@openMoney<1)  
  286.             return   
  287.         else   
  288.             begin  
  289.                 begin transaction  
  290.                     declare @error int   
  291.                     set  @error=0  
  292.                     insert into userInfo values (@customerName,@PID,@telephone,@address)  
  293.                     set  @error=@error+@@error  
  294.                     select @customerID=customerID from userInfo where customerName=@customerName  
  295.                     set  @error=@error+@@error  
  296.                     insert into cardInfo (cardID,savingType,openMoney,balance,customerID)  
  297.                             values (@cardID,@savingType,@openMoney,@openMoney,@customerID)  
  298.                     set  @error=@error+@@error  
  299.                 if (@error<>0)  
  300.                     rollback transaction  
  301.                 else   
  302.                     commit transaction  
  303.             end  
  304.         print '尊敬的客户,开户成功!系统为您产生的随机卡号为:' +@cardID+ ' 开户日期 ' +  
  305.                 convert(varchar(30),getDate(),111)+' 开户金额 ' +convert(varchar(10),@openMoney)  
  306. go  
  307. exec proc_openAccount '王五' , '420656889012678' , '2222-63598978' ,1000, '活期' , '河南新乡'   
  308.   
  309.   
  310. -----------------转账事务------------------  
  311. if  exists (select * from sysobjects where name= 'proc_transfer' )  
  312.     drop proc proc_transfer  
  313. go  
  314. create proc proc_transfer  
  315.     @card1 char (18),  
  316.     @card2 char (18),  
  317.     @outMoney money  
  318.     as   
  319.         begin transaction  
  320.             declare @error int   
  321.             set  @error=0  
  322.             update cardInfo set  balance=balance-@outMoney where cardID=@card1  
  323.             set  @error=@error+@@error  
  324.             insert into transInfo values (getDate(),@card1,'支取' ,@outMoney, default )  
  325.             set  @error=@error+@@error  
  326.             update cardInfo set  balance=balance+@outMoney where cardID=@card2  
  327.             set  @error=@error+@@error  
  328.             insert into transInfo values (getDate(),@card2,'存入' ,@outMoney, default )  
  329.             set  @error=@error+@@error  
  330.             if  (@error<>0)  
  331.                 rollback transaction  
  332.             else   
  333.                 commit transaction  
  334. go  
  335. exec proc_transfer '1010 3576 1212 113' , '1010 3576 1234 567' ,2000  
  336. select * from transInfo where cardID='1010 3576 1212 113'      
  337. select * from transInfo where cardID='1010 3576 1234 567'          
  338.           
  339. -------------------创建登录帐号和数据库用户---------------------  
  340. exec sp_addlogin 'sysAdmin' , '1234'   
  341. exec sp_grantdbaccess 'sysAdmin' , 'sysAdminBankUser'   
  342. grant insert,update,delete,select on userInfo,cardInfo,transInfo to sysAdminBankUser 


补上触发器:

  1. --update触发器  
  2. set  nocount on  
  3. if  exists (select * from sysobjects where name = 'trig_ppr_Update' )  
  4.     drop trigger trig_ppr_Update  
  5. go  
  6. create trigger trig_ppr_Update  
  7.     on PlanPropertyRelation   
  8.     --with encryption --加密  
  9.         for  update  
  10.             as   
  11.                 declare @Plans_Id int ,@currentId  int ,@childId  int ,@childNewLevel  int   
  12.                 declare @oldFatherId int ,@newFatherId  int   
  13.                 select @oldFatherId=PPR_ProExtend_ID from deleted  
  14.                 select @newFatherId=PPR_ProExtend_ID from inserted  
  15.                 if (@oldFatherId=@newFatherId)  
  16.                     return ;  
  17.                 else   
  18.                 begin                     
  19.                     select @Plans_Id=Plans_ID,@childId=Property_ID,@currentId=PlanPropertyRelation_ID from inserted  
  20.                     delete from PropertyValuesRelation where PlanPropertyRelation_ID=@currentId  
  21.                 end  
  22. go  
  23.   
  24. --删除触发器,删除前触发  
  25. set  nocount on  
  26. if  exists (select * from sysobjects where name = 'trig_ppr_BeforeDelete' )  
  27.     drop trigger trig_ppr_BeforeDelete  
  28. go  
  29. create trigger trig_ppr_BeforeDelete  
  30.     on PlanPropertyRelation   
  31.     --with encryption --加密  
  32.         INSTEAD OF delete  
  33.             as   
  34.             declare @oldId int ,@error  int   
  35.             select @oldId=PlanPropertyRelation_ID from deleted  
  36.             delete from PropertyValuesRelation where PlanPropertyRelation_ID=@oldId  
  37.             delete from PlanPropertyRelation where PlanPropertyRelation_ID=@oldId  
  38. go 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics