2008-04-12
1 use master
2 go
3
4
5
6 if exists(select * from sysdatabases where name = 'bankDB')
7 drop database bankDB
8
9 go
10 exec xp_cmdshell 'mkdir F:\Sql\Bank',no_output
11 go
12
13 --创建数据库
14 create database bankDB
15 on primary
16 (
17 name = 'bankDB_data',
18 filename = 'F:\Sql\Bank\bankDB_data.mdf',--物理名
19 size = 5mb,
20 maxsize = 100mb,
21 filegrowth = 15%
22 )
23 log on
24 (
25 name = 'bankDB_ldf',
26 filename = 'F:\Sql\Bank\bankDB_data.ldf',
27 size = 5mb,
28 filegrowth = 5%
29 )
30
31 go
32
33 use bankDB
34 go
35 --用户信息表
36 create table userInfo
37 (
38 customerId int not null identity(1,1),--用户卡号
39 customerName varchar(10) not null,--用户名
40 pId varchar(18) not null,--身份证号
41 telephone varchar(13) not null,--电话
42 address varchar(100) --住址
43
44 )
45 go
46
47 alter table userInfo --主键约束
48 add constraint PK_customerId primary key(customerId)
49
50 alter table userInfo --唯一约束
51 add constraint UQ_pId unique (pId)
52
53 alter table userInfo --身份证的约束(15位或者18位,18位的最后一位可能是X)
54 add constraint ck_pId check(pId 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][0-9][0-9][0-9]' or
55 pId 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][0-9][0-9][0-9][0-9][0-9][0-9,x,X]')
56
57 alter table userInfo --电话号码的check约束,区号有3位的,也有4位的,电话号码有7位的或者8位的,手机号有13开头的,也有15开头的
58 add constraint CK_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]' or
59 telephone like '[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]' or
60 telephone like '[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]' or
61 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
62 telephone like '13[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'or
63 telephone like '15[0,8,9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
64 )
65
66 --银行卡信息表
67 create table cardInfo
68 (
69 cardID varchar(19) not null,--卡号
70 curType varchar(10) not null,--货币类型
71 savingType varchar(8) not null,--存款类型
72 openDate datetime not null,--开户日期
73 openMoney money not null, --开户金额
74 balance money not null,--余额
75 pass char(8) not null,--密码
76 IsReportLoss char(2) not null,--是否挂失
77 customerId int not null--用户编号,外键
78 )
79
80 go
81
82 --添加约束
83 alter table cardInfo --银行卡号的约束
84 add constraint ck_cardID check(cardID like '1010 3576 [0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9]')
85 alter table cardInfo --主键约束
86 add constraint pk_cardID primary key(cardID)
87 alter table cardInfo --货币类型默认为RMB
88 add constraint df_curType default ('RMB') for curType
89 alter table cardInfo --存款类型必须为'活期','定活两便','定期'
90 add constraint ck_savingType check(savingType in('活期','定活两便','定期'))
91 alter table cardInfo --存款类型默认为'活期'
92 add constraint df_savingType default('活期') for savingType
93 alter table cardInfo --开户日期默认为当前日期
94 add constraint df_openDate default (getdate()) for openDate
95 alter table cardInfo --开关日期必须小于等于当前时间
96 add constraint ck_openDate check(openDate <=getdate())
97 alter table cardInfo --开户金额必须大于等于1元
98 add constraint ck_openMoney check(openMoney >=1)
99 alter table cardInfo --开户金额默认为1
100 add constraint df_openMoney default(1) for openMoney
101 alter table cardInfo --余额必须大于1元
102 add constraint ck_balance check(balance >= 1)
103 alter table cardInfo --密码为6位数字
104 add constraint ck_pass check(pass like '[0-9][0-9][0-9][0-9][0-9][0-9]')
105 alter table cardInfo --是否挂失默认为否
106 add constraint df_isReportLoss default ('否') for isReportLoss
107 alter table cardInfo --是否挂失只能为'是'或者'否'
108 add constraint ck_isreportLoss check(isReportLoss = '是' or isReportLoss = '否')
109 alter table cardInfo --与用户表建立主外键关系
110 add constraint fk_customerID foreign key (customerId) references userInfo(customerId)
111 go
112
113 --创建交易表
114 create table transInfo
115 (
116 transDate datetime not null,--交易日期
117 cardId varchar(19) not null,--卡号
118 transType varchar(4) not null,--交易类型(存入和支取)
119 transMoney money not null,--交易金额
120 remark varchar(100)--备注
121 )
122 go
123 --创建约束
124 alter table transInfo --交易日期默认为当前时间
125 add constraint df_transDate default(getdate()) for transDate
126 alter table transInfo --交易日期必须小于或者等于当前日期
127 add constraint ck_transDate check(transDate <=getdate())
128 alter table transInfo --与卡号表建立主外键关系
129 add constraint fk_cardId foreign key (cardId) references cardInfo(cardId)
130 alter table transInfo --交易类型必须为 '存入' 或者 '支取'
131 add constraint ck_transType check(transType like '存入' or transType like '支取')
132
133 alter table transInfo --交易金额必须大于 等于 0
134 add constraint ck_transMoney check (transMoney > =0)
135
136
137
138
139
140
2 go
3
4
5
6 if exists(select * from sysdatabases where name = 'bankDB')
7 drop database bankDB
8
9 go
10 exec xp_cmdshell 'mkdir F:\Sql\Bank',no_output
11 go
12
13 --创建数据库
14 create database bankDB
15 on primary
16 (
17 name = 'bankDB_data',
18 filename = 'F:\Sql\Bank\bankDB_data.mdf',--物理名
19 size = 5mb,
20 maxsize = 100mb,
21 filegrowth = 15%
22 )
23 log on
24 (
25 name = 'bankDB_ldf',
26 filename = 'F:\Sql\Bank\bankDB_data.ldf',
27 size = 5mb,
28 filegrowth = 5%
29 )
30
31 go
32
33 use bankDB
34 go
35 --用户信息表
36 create table userInfo
37 (
38 customerId int not null identity(1,1),--用户卡号
39 customerName varchar(10) not null,--用户名
40 pId varchar(18) not null,--身份证号
41 telephone varchar(13) not null,--电话
42 address varchar(100) --住址
43
44 )
45 go
46
47 alter table userInfo --主键约束
48 add constraint PK_customerId primary key(customerId)
49
50 alter table userInfo --唯一约束
51 add constraint UQ_pId unique (pId)
52
53 alter table userInfo --身份证的约束(15位或者18位,18位的最后一位可能是X)
54 add constraint ck_pId check(pId 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][0-9][0-9][0-9]' or
55 pId 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][0-9][0-9][0-9][0-9][0-9][0-9,x,X]')
56
57 alter table userInfo --电话号码的check约束,区号有3位的,也有4位的,电话号码有7位的或者8位的,手机号有13开头的,也有15开头的
58 add constraint CK_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]' or
59 telephone like '[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]' or
60 telephone like '[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]' or
61 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
62 telephone like '13[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'or
63 telephone like '15[0,8,9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
64 )
65
66 --银行卡信息表
67 create table cardInfo
68 (
69 cardID varchar(19) not null,--卡号
70 curType varchar(10) not null,--货币类型
71 savingType varchar(8) not null,--存款类型
72 openDate datetime not null,--开户日期
73 openMoney money not null, --开户金额
74 balance money not null,--余额
75 pass char(8) not null,--密码
76 IsReportLoss char(2) not null,--是否挂失
77 customerId int not null--用户编号,外键
78 )
79
80 go
81
82 --添加约束
83 alter table cardInfo --银行卡号的约束
84 add constraint ck_cardID check(cardID like '1010 3576 [0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9]')
85 alter table cardInfo --主键约束
86 add constraint pk_cardID primary key(cardID)
87 alter table cardInfo --货币类型默认为RMB
88 add constraint df_curType default ('RMB') for curType
89 alter table cardInfo --存款类型必须为'活期','定活两便','定期'
90 add constraint ck_savingType check(savingType in('活期','定活两便','定期'))
91 alter table cardInfo --存款类型默认为'活期'
92 add constraint df_savingType default('活期') for savingType
93 alter table cardInfo --开户日期默认为当前日期
94 add constraint df_openDate default (getdate()) for openDate
95 alter table cardInfo --开关日期必须小于等于当前时间
96 add constraint ck_openDate check(openDate <=getdate())
97 alter table cardInfo --开户金额必须大于等于1元
98 add constraint ck_openMoney check(openMoney >=1)
99 alter table cardInfo --开户金额默认为1
100 add constraint df_openMoney default(1) for openMoney
101 alter table cardInfo --余额必须大于1元
102 add constraint ck_balance check(balance >= 1)
103 alter table cardInfo --密码为6位数字
104 add constraint ck_pass check(pass like '[0-9][0-9][0-9][0-9][0-9][0-9]')
105 alter table cardInfo --是否挂失默认为否
106 add constraint df_isReportLoss default ('否') for isReportLoss
107 alter table cardInfo --是否挂失只能为'是'或者'否'
108 add constraint ck_isreportLoss check(isReportLoss = '是' or isReportLoss = '否')
109 alter table cardInfo --与用户表建立主外键关系
110 add constraint fk_customerID foreign key (customerId) references userInfo(customerId)
111 go
112
113 --创建交易表
114 create table transInfo
115 (
116 transDate datetime not null,--交易日期
117 cardId varchar(19) not null,--卡号
118 transType varchar(4) not null,--交易类型(存入和支取)
119 transMoney money not null,--交易金额
120 remark varchar(100)--备注
121 )
122 go
123 --创建约束
124 alter table transInfo --交易日期默认为当前时间
125 add constraint df_transDate default(getdate()) for transDate
126 alter table transInfo --交易日期必须小于或者等于当前日期
127 add constraint ck_transDate check(transDate <=getdate())
128 alter table transInfo --与卡号表建立主外键关系
129 add constraint fk_cardId foreign key (cardId) references cardInfo(cardId)
130 alter table transInfo --交易类型必须为 '存入' 或者 '支取'
131 add constraint ck_transType check(transType like '存入' or transType like '支取')
132
133 alter table transInfo --交易金额必须大于 等于 0
134 add constraint ck_transMoney check (transMoney > =0)
135
136
137
138
139
140