标签:默认 编号 balance 维护 ddr 地址 for 检查 恢复
---恢复内容开始---
SQL Sever 基础以创建银行数据库bankDB为案例
1、创建数据库
1-1 创建文件夹用以存放数据库
1-2 创建建库bankDB
2、创建数据库


2-1、创建用户信息表 userInfo
| 
 字段名称  | 
 数据类型  | 
 说明  | 
| 
 customerID  | 
 int  | 
 客户编号,主键  | 
| 
 customerName  | 
 CHAR(8)  | 
 客户姓名  | 
| 
 PID  | 
 CHAR(18)  | 
 身份证号  | 
| 
 telephone  | 
 CHAR(13)  | 
 电话  | 
| 
 address  | 
 VARCHAR(50)  | 
 地址  | 
| 
 
  | 
 
  | 
1 use bankDB 2 create table userinfo --建表 3 4 ( 5 customerID int identity(1,1), --客户id列 数据类型整型,标识列从1开始每次加1 6 customerName char(8) NOT NULL, 7 PID char(18) NOT NULL, 8 telephone char(13) NOT NULL, 9 address varchar(50) 10 )
2-2、创建银行卡信息表 cardInfo
| 
 字段名称  | 
 数据类型  | 
 说明  | 
| 
 cardID  | 
 CHAR(19)  | 
 卡号  | 
| 
 curType  | 
 CHAR(5)  | 
 类型  | 
| 
 savingType  | 
 CHAR(18)  | 
 存款类型(存或取)  | 
| 
 openDate  | 
 DATETIME  | 
 开户时间  | 
| 
 openMoney  | 
 MONEY  | 
 开户金额  | 
| 
 balance  | 
 MONEY  | 
 帐户余额  | 
| 
 pass  | 
 CHAR(6)  | 
 密码  | 
| 
 IsReportLoss  | 
 BIT  | 
 是否挂失  | 
| 
 customerID  | 
 Int  | 
 客户帐号  | 
1 create table cardinfo 2 ( 3 cardID char(19) NOT NULL, 4 curType char(5) NOT NULL, 5 savingType char(8) NOT NULL, 6 openDate datetime NOT NULL, --开户时间 数据类型datetime 7 openMoney money NOT NULL, --开户金额 数据类型 money 8 balance money not null, 9 pass char(6) NOT NULL, 10 IsReportLoss bit, 11 customerID int NOT NULL 12 )
2-3、创建交易信息表 transInfo
| 
 字段名称  | 
 数据类型  | 
 说明  | 
| 
 transDate  | 
 DATETIME  | 
 交易日期  | 
| 
 transType  | 
 CHAR(4)  | 
 交易类型  | 
| 
 cardID  | 
 CHAR(19)  | 
 卡号  | 
| 
 transMoney  | 
 MONEY  | 
 交易金额  | 
| 
 remark  | 
 TEXT  | 
 备注  | 
  
1 create table transinfo 2 ( 3 transDate datetime NOT NULL, 4 transType char(4) NOT NULL, 5 cardID char(19) NOT NULL, 6 transMoney money NOT NULL, 7 remark text --备注长文本 8 )
3、加约束
加约束,对于你创建的表添加一些限制,我的建议约束和创建表分开,我是学前端的我认为html、css、js除了活动页都要写在一起便于维护。约束也是这个意思,如果你非想写在表内,出门右拐谢谢。还有有些约束前端也会写,但是你还是要写,你做的不是大前端啊部门又不一样,前端要是没想到某个约束怎么办,保险啊老哥,严谨。我是这样认为的,学理都要严谨。还有大小写都一样,刚学完JavaScript在学这个很不习惯。。
3-1 userInfo表的约束
customerID 顾客编号 自动编号(标识列),从开始,主键
customerName 开户名 必填
PID 身份证号 必填,只能是位或位,身份证号唯一约束
telephone 联系电话 必填,格式为xxxx-xxxxxxxx或手机号位
address 居住地址 可选输入
1 --建立用户表约束-- 2 --urseinfo表的约束-- 3 alter table userInfo 4 add constraint PK_customerID --设为主键 5 primary key (customerID) 6 alter table userInfo 7 add constraint ck_pid --检查身份证号是不是正确 8 check (pid like‘[1-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][0-9]‘or pid 9 like‘[1-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][0-9][0-9][0-9][X]‘) 10 alter table userInfo 11 add constraint CK_telephone --检查电话号是否正确 12 check (telephone like ‘[0-9][1-9][1-9][1-9]-[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]‘or telephone 13 like‘[1-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]‘)
3-2 cardInfo表的约束
cardID 卡号 必填,主健, 银行的卡号规则和电话号码一样,一般前位代表特殊含义,
如某总行某支行等。假定该行要求其营业厅的卡号格式为:3576 xxxx xxx开始
curType 货币 必填,默认为RMB
savingType 存款种类 活期/定活两便/定期
openDate 开户日期 必填,默认为系统当前日期
openMoney 开户金额 必填,不低于元
balance 余额 必填,不低于元,否则将销户
pass 密码 必填,位数字,默认为个
IsReportLoss 是否挂失 必填,是/否值,默认为”否”
customerID 顾客编号 必填,表示该卡对应的顾客编号,一位顾客可以办理多张卡
1 alter table cardinfo 2 add constraint ck_cardid ---检查卡号正经银行每种卡每种号都不一样 3 check(cardid like ‘1010 3576 [0-9][0-9][0-9][0-9] [0-9][0-9][0-9]‘) 4 alter table cardinfo 5 add constraint dk_curType --检查存款类型,虽然大部分都是人民币但是来个老哥要存美元不能按 default(‘RMB‘) for curType --照人民币的价值来存了 6 alter table cardinfo 7 add constraint ck_savingType --检查老哥要存什么样的有买理财的有存活期有存定期的 8 check(savingType=‘定期‘ or savingType=‘活期‘) 9 alter table cardinfo 10 add constraint dk_openDate 11 default(getdate()) for openDate --设置开户日默认就是当前日期 12 alter table cardinfo 13 add constraint ck_balance --查看余额 银行龟定 14 check(balance>=1) 15 alter table cardinfo 16 add constraint dk_pass --设置默认密码 17 default(‘888888‘) for pass 18 alter table cardinfo 19 add constraint dk_IsReportLoss--默认不挂失 20 default(0) for IsReportLoss 21 --这个约束写的绝对有毛病是我刚学写的现在不想改了凑活看吧
--找出自学刚开始找出错误要改正
3-3 transInfo表的约束
transType 必填,只能是存入/支取
cardID 卡号 必填,外健,可重复索引
transMoney 交易金额 必填,大于
transDate 交易日期 必填,默认为系统当前日期
remark 备注 可选输入,其他说明
1 alter table transinfo 2 add constraint CK_transType --现在交易方式真苛刻转账都不让 3 check (transType in (‘存入‘,‘支取‘)) 4 alter table transinfo 5 add constraint ck_transMoney --交易金额这个必须有啊你给我时候我取0元我都得给你跑一趟? 6 check(transMoney>0) 7 alter table transinfo 8 add constraint dk_transDate --默认日期,不能交易玩不知道哪天吧 9 default(getdate()) for transDate
4、 插入测试数据
1 --录入数据-- 2 insert into userinfo 3 select ‘张三‘,‘123456789012345‘,‘0134-67898978‘,‘北京海淀‘union 4 select ‘李四‘,‘234576875325688‘,‘0478-44443333‘,‘湖北武汉‘union 5 select ‘张清‘,‘476585775327987‘,‘0743-46575797‘,‘浙江宁波‘union 6 select ‘陶庆‘,‘234576874576890‘,‘0678-12457656‘,‘江西南昌‘union 7 select ‘王莉‘,‘257869785328078‘,‘0235-68743236‘,‘云南丽江‘ 8 select *from userinfo 9 insert into cardinfo 10 select ‘1010 3576 1234 567‘,‘RNB‘,‘活期‘,getdate(),30000,‘2‘,‘888888‘,‘1‘,‘1‘union 11 select ‘1010 3576 1235 567‘,‘RNB‘,‘定期‘,getdate(),5600,‘3‘,‘888888‘,‘1‘,‘2‘union 12 select ‘1010 3576 1236 567‘,‘RNB‘,‘活期‘,getdate(),6200,‘4‘,‘888888‘,‘2‘,‘3‘union 13 select ‘1010 3576 1237 567‘,‘RNB‘,‘定期‘,getdate(),53000,‘5‘,‘888888‘,‘1‘,‘4‘union 14 select ‘1010 3576 1238 567‘,‘RNB‘,‘活期‘,getdate(),4500,‘6‘,‘888888‘,‘2‘,‘5‘ 15 select *from cardinfo 16 --运行试试吧。 关于数据类型之类 善用搜索工具,
---恢复内容结束---
标签:默认 编号 balance 维护 ddr 地址 for 检查 恢复
原文地址:http://www.cnblogs.com/cxh1995/p/7094219.html