码迷,mamicode.com
首页 > 数据库 > 详细

SqlServer基础语法(二)

时间:2016-08-30 09:30:56      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

先看一下腰实现的功能:

技术分享

一:创建数据库

/* 检查数据库是否存在,如果存在,删除此数据库 */

if exists(select * from sysdatabases where name=‘bankDB‘)
  drop database bankDB
 go
 /*创建数据库bankDB*/
 create database bankDB
 on
 (
   name=‘bankDB_data‘,
   filename=‘d:\bank\bankDB.mdf‘,
   size=10,
   --增长的速度
   filegrowth=15%
   )
   log on
   (
   --日志文件
   name=‘bankDB_log‘,
   filename=‘d:\bank\bankDB_log.ldf‘,
   size=5,
   filegrowth=15%
   )
   

 技术分享

技术分享

二:创建表的语句:

 
   /* 创建表*/
   
   use bankDB 
   go
   create table userInfo   --用户信息表
   (
    customerID int identity(1,1),
    customerName char(8) not null,
    PID  char(18) not null,
    telephone char(13) not null,
    address varchar(50)
    )
    
    go
    
    create table cardInfo  --银行卡信息表
    (
     cardID char(19) not null,
     curType char(5) not null,
     savingType char(8) not null,
     openDate datetime not null,
     openMoney money not null,
     balance money not null,
     pass char(6) not null,
     IsReportLoss bit not null,
     customerID  int not null
     )
     go
     
    create table  transInfo  --交易信息表
    ( 
     transDate  datetime not null,  
     transType  char(4) not null,
     cardID  char(19) not null,
     transMoney money not null,
     remark text
     )
     go
     
     

 三:对表添加约束

     /* 为userInfo表添加约束
      customerID(顾客编号):	自动编号(标识列),从1开始,主键
      PID(身份证号):  只能是18位或15位,身份证号唯一约束
       telephone(联系电话): 格式为xxxx-xxxxxxxx或手机号13位   
     */
     
     alter table userInfo
     add 
      constraint pk_customerID primary key(customerID),
      constraint chk_PID check(len(PID)=18 or len(PID)=15),
      constraint uq_PID unique(PID),
      constraint chk_telephone check(telephone like ‘[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  len(telephone)=13)
      go
     
     
     
     
     /*  cardInfo 表的约束
     cardID	卡号	必填,主健,银行的卡号规则和电话号码一样,一般前8位代表特殊含义,
       如某总行某支行等。假定该行要求其营业厅的卡号格式为:1010 3576 xxxx xxx开始,每4位号码后有空格,
       卡号一般是随机产生。
curType	货币种类	必填,默认为RMB
savingType	存款类型	活期/定活两便/定期
openDate	开户日期	必填,默认为系统当前日期
openMoney	开户金额	必填,不低于1元
balance	余额	必填,不低于1元,否则将销户
pass	密码	必填,6位数字,开户时默认为6个“8”
IsReportLoss	是否挂失	必填,是/否值,默认为”否”
customerID	顾客编号	外键,必填,表示该卡对应的顾客编号,一位顾客允许办理多张卡号
*/

alter table cardInfo
add
   constraint pk_cardID primary key(cardID),
   constraint ck_cardID check(cardID like ‘1010 3576 [0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9]‘),
   constraint df_curType default ‘RMB‘ for curType,
   constraint ck_savingType check(savingType in (‘活期‘,‘定活两便‘,‘定期‘)),
   constraint df_openDate default getdate() for OpenDate,
   constraint ck_openMoney  check(openMoney>=1),
   constraint ck_balance  check(balance>=1),
   constraint ck_pass check(pass like ‘[0-9][0-9][0-9][0-9][0-9][0-9]‘),
   constraint df_pass default ‘888888‘ for pass,
   constraint df_IsReportLoss default 0 for IsReportLoss,
   constraint fk_customerID foreign key(customerID) references userInfo(customerID)
go

/* transInfo表的约束
  transDate 	交易日期	必填,默认为系统当前日期
cardID	卡号	必填,外健,可重复索引
transType 	交易类型	必填,只能是存入/支取
transMoney	交易金额	必填,大于0
remark	备注	可选输入,其他说明
*/

alter table transInfo
add
  constraint  df_transDate default getdate() for transDate,
  constraint  ck_transType  check(transType in (‘存入‘,‘支取‘)),
  constraint  fk_cardid  foreign key(cardid) references  cardInfo(cardID),
  constraint  ck_transMoney  check(transMoney>0)
  

 四:插入数据:

/*
 张三开户, 身份证:123456789012345, 电话:010-67898978, 地址:北京海淀
   开户金额:1000  活期   卡号: 1010 3576 1234 5678
   
 李四开户, 身份证:321245678912345678, 电话:0478-44443333,
   开户金额:1  定期   卡号:1010 3576 1212 1134
  
 */
 
 insert into userInfo(customerName,PID,telephone,address)
 values(‘张三‘,‘123456789012345‘,‘0102-67898978‘,‘北京海淀‘)
 
 insert into cardInfo(cardID,savingType, openMoney, balance, customerID)
 values(‘1010 3576 1234 5678‘,‘活期‘, 1000,1000,3)
 
 
 
 
  insert into userInfo(customerName,PID,telephone)
 values(‘李四‘,‘321245678912345678‘,‘0478-44443333‘)
 
 insert into cardInfo(cardID,savingType, openMoney, balance, customerID)
 values(‘1010 3576 1212 1134‘,‘定期‘,1,1,2)
 
 select * from userInfo
 select * from cardInfo
 select * from transInfo
/*
   张三的卡号取款900元,李四的卡号存款5000元
   要求保存交易记录,以便客户查询和银行业务统计
*/

 --张三    
   /*  交易信息表插入交易信息 */
   insert into transInfo(transType,cardID,transMoney)
   values(‘支取‘,‘1010 3576 1234 5678‘,900)
   
  /*更新银行卡信息表中的现有余额*/
  update cardInfo 
  set balance=balance-900
  where cardID=‘1010 3576 1234 5678‘
  
  
--李四

    /*  交易信息表插入交易信息 */
    insert into transInfo(transType,cardID,transMoney)
    values(‘存入‘,‘1010 3576 1212 1134‘,5000)
    
   /*更新银行卡信息表中的现有余额*/
   update cardInfo 
   set balance=balance+5000
   where cardID=‘1010 3576 1212 1134‘

 五:常规业务操作:

/*---------修改密码-----*/
--1.张三(卡号为1010 3576 1234 5678)修改银行卡密码为123456
--2.李四(卡号为1010 3576 1212 1134)修改银行卡密码为123123
   update cardInfo 
   set pass=‘123456‘
   where cardID =‘1010 3576 1234 5678‘
   
   update  cardInfo 
   set pass =‘123123‘
   where cardID=‘1010 3576 1212 1134‘
   
   
   /*--------- 李四的卡号挂失 ---------*/
  update cardInfo 
  set IsReportLoss=1
  where cardID =‘1010 3576 1212 1134‘
  
  select * from cardInfo 
  

 六:一些比较复杂的操作:

/* 统计银行的资金流通余额和盈利结算*/
 --统计说明:存储代表资金流入,取款代表资金流出,假定存款利率为千分之三,贷款利率为千分之八
   
  declare @inMoney money
  declare @outMoney money
  declare @profit money
  select * from transInfo 
  select @inMoney=SUM(transMoney)  from transInfo  where transType=‘存入‘
  select @outMoney=SUM(transMoney) from transInfo where transType =‘支取‘
  print ‘银行流通金额总计为:‘+convert(varchar(20),@inMoney-@outMoney)+‘RMB‘
  set @profit=@outMoney*0.008-@inMoney*0.003
  print ‘盈利结算为:‘+convert(varchar(20),@profit)+‘RMB‘
  go
  

    DATEDIFF: 函数返回两个日期之间的天数。

SqlServer基础语法(二)

标签:

原文地址:http://www.cnblogs.com/sunliyuan/p/5820535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!