目录
1.打开并使用该数据库
use 活期存款
2.创建表
create table 储户
(账号 int not null,
姓名 char(10) null,
电话 int null,
地址 char(10) null,
存款额 money null)
create table 储蓄所
(储蓄所编号 int not null,
名称 char(10) null,
电话 int null,
地址 char(10) null)
create table 存取款单
(账号 int not null,
储蓄所编号 int not null,
存取日期 datetime not null,
存取标志 int not null,
存取金额 money null)
3.设置主键
alter table 储户
add constraint pk_账号 primary key(账号)
alter table 储蓄所
add constraint pk_储蓄所编号 primary key(储蓄所编号)
alter table 存取款单
add constraint pk_存取款单 primary key(账号,储蓄所编号,存取日期)
4.设置外键
alter table 存取款单
add constraint FK_储户_账号
foreign key(账号) references 储户(账号)
alter table 存取款单
add constraint FK_储蓄所_储蓄所编号
foreign key(储蓄所编号) references 储蓄所(储蓄所编号)
5.设置默认值
alter table 存取款单
add constraint DK_存取标志 default '1 or 0' for 存取标志
6.查看表的属性
sp_help 储户
exec sp_help 储蓄所
exec sp_help 存取款单
7.增加列
alter table 存取款单
add 款单号 int null
8.修改列
alter table 存取款单
alter column int not null
9.删除列
alter table 存取款单
drop column 款单号
10.修改表名
exec sp_rename 储户,储户_新名
11.修改表中列的名
exec sp_rename '表名,列名','改后名','column'
12.插入表记录
insert 储户
values
('10010','刘春玲','3692106','柳南','5000'),
('10011','李红萍','3926578','鱼峰','2000'),
('10012','黄风叶','7543176','城中','8000'),
('10013','杨海林','3693918','官塘','0'),
('10014','陈兴凯','3690162','阳和','1000')
insert 储蓄所
values
('730101','先锋路一所','7551018','先锋路号'),
('730102','先锋路二所','7552134','先锋路号'),
('730103','先锋路三所','7551263','先锋路号')
insert 存取款单
values
('10010','730103',getdate(),'1','5000'),
('10011','730101',getdate(),'1','2000'),
('10012','730101',getdate(),'1','8000'),
('10013','730102',getdate(),'1','1000'),
('10013','730101',getdate(),'0','1000'),
('10014','730103',getdate(),'1','6000'),
('10014','730102',getdate(),'0','5000')
13.查看表记录
select * from 储户
14.修改表记录
update 储户
set 存款额=存款额+2000
where 账号=10011
15.删除表记录
delete 储户
where 账号=10011
16.删除表
drop table 存取款单