实验内容:
实验一:数据库的操作
使用Management Studio和sql语句分别完成以下操作:
1.创建一个名为“SM”的数据库,数据文件初始大小为3MB,最大为50MB,数据库自动增长,增长方式按10%;
日志文件初始大小为2MB,数据大小不受限制,按1MB增长。 create database sm on (
name='smdata',
filename='e:\\smdata.mdf', size=3,
maxsize=50, filegrowth=10%) log on
(name='smlog',
filename='e:\\smlog.ldf', size=2,
maxsize=unlimited, filegrowth=1)
2.修改数据库“SM”,将数据文件名改成“sm_data”,初始大小改成5MB alter database sm modify file
( name='smdata', newname='sm_data', size=5)
3.分别查看数据库“SM”,该数据库中的文件和文件组。 execsp_helpfilesm
execsp_helpfilegroupsm 4.删除数据库“SM”。 drop database sm
实验二:创建表
1. 在数据库SM中创建学生表student,课程表course,选课表sc student(sid,sno,clno,sname,ssex,sage,sbir) 说明:sidint identity(1,1) 序号 sno为主关系键,为字符类型学号 clno字符类型,班级号 sname字符类型,并不为空
ssex字符类型,check的值的范围为男女 sbir日期类型出生日期
sage int;
usesm
create table student ( sidint identity(1,1),
sno char(10) constraint pk_st primary key, clno char(10),
sname varchar(20) not null,
ssex char(2) constraint ck_ssex check(ssex in('男','女')), sbirdatetime, sageint )
course(cno,cname,ccredits,ctno,cpno,ctime)
说明:cno字符类型,主关系键 cname字符类型,唯一键
ccredits学分,精确数值型,精确长度为2,小数位为1 ctno,cpno字符类型 ctime整型
create table course
(cno char(4) constraint pk_c primary key, cname varchar(20) constaintuk_cname unique, ccredit decimal(2,1), ctno char(2), cpno char(4), ctimetinyint )
sc(sno,cno,score)
说明:sno+cno为主键,并且sno是student的外部键,cno是course的外部键。 score精确数值型,精确长度为4,小数位为1
create table sc
( sno char(10) constraint fk_sno foreign key references student(sno), cno char(4) constriantfk_cno foreign key references course(cno), score decimal(4,1),
constraintpk_sc primary key(sno,cno) )
2.使用Management Studio对数据库SM中的表插入数据
实验三:表的维护
1.用sql语句修改表course的列属性,将cname的长度改为40,且不允许空 alter table course
drop constraint uk_cname alter table course
alter column cname char(40) not null
2.用sql语句向表student中增加列email,且要求输入的电子邮件地址必须包括\alter table student
add email varchar(20) constraint ck_email check(email like '%@%') 3.用sql语句删除表student中的列sbir alter table student drop column sbir
4.删除sname列上的约束。 alter table student
alter column sname varchar(20) null
5.删除表student drop table student
实验四:简单数据查询
在实验二的基础上,再在sm数据库中新建表teacher,包括如下数据项 teacher(tno,tname,age,sal,dno)
tno为教职工编号,tname姓名,age年龄,sal为月薪,dno为部门号
在student,course,sc,teacher四张表中进行下列查询
1,查询所有0002部门职工的信息; select * from teacher where dno='0002'
2,查询1984年和1985年出生的女生的信息; select * from student where year(sbir) in(1984,1985) and ssex='女'
3,查询0001部门、0002部门或0003部门的职工信息; select * from teacher where dno in('0001','0002','0003') 4,查询学号为03004的同学正在学习的课程; selectcno from sc where sno='03004' 5,查询不姓王或李的同学的信息。
select * from student where sname like '[^王李]%' 6,查询有多少名学生的物理课成绩不及格; select count(sno) fromsc,course
where sc.cno=course.cno and cname='物理' and score<60 7,求女学生的学生总数; select count(sno) from student
where ssex='女'
8,求职工的最高工资、最低工资和平均工资; select max(sal),min(sal),avg(sal) from teacher
9,查询职工的年薪,并按年薪的升序排列; select tname,sal*12 年薪 from teacher order by salasc 10,求每个班的学生数
student(sid,sno,clno,sname,ssex,sage,sbir) select count(sno) from student group by clno
11,查询每个学生已获得的学分(成绩及格即得相应课程的学分)。 selectsno,sum(ccredit) fromsc,course
where score>60 and sc.cno=course.cno group by sno
实验五复杂查询(自连接和外连接不考) 1. 查询体育课成绩不及格的男生名单 selectsname
fromstudent,course,sc
wherestudent.sno=sc.sno and course.cno=sc.cno
and cname='体育' and ssex='男' and score<60 2. 将04001班全体学生的成绩置0 sc(sno,cno,score) student(sno,clno...) updatesc set score=0
wheresno in( select sno from student where clno='04001') 3. 删除04002班全体学生的选课记录
delete from sc where sno in( select sno from student where clno='04002') 4. 查询所有选修了001号课程的学生的姓名 selectsname fromstudent,sc
wherestudent.sno=sc.sno and cno='001'
5. 查询其他班中比04001班所有学生年龄都小的学生,并按年龄的降序输出 select * from student
whereclno<>'04001' and sage
6.查询没有选修数据库课程的学生的信息 select * from student wheresno not in( select sno
fromsc,course
where course.cno=sc.cno and cname='数据库')
实验六创建视图和数据操纵 1.建立04002班学生的视图 create view v_clno as
select * from student whereclno='04002'
2.建立04001班学生的视图,输出其学号、姓名、和年龄,并且更换列名 create view v1(学号,姓名,年龄) as
selectsno,sname,sage from student whereclno='04001'
3.将学生的学号和平均成绩建立一个视图 create view v2(学号,平均成绩) as
selectsno,avg(score) fromsc
group by sno
4. 建立04003班学生选修了0001号课程的学生的视图. create view v3(学号,姓名) as
selectsno,sname fromstudent,sc
whereclno='04003' and student.sno=sc.sno and cno='0001' 5. 向数据表S添加一个学生信息
201320180309 刘可女 1998-2-15 经管 insert into s (sno,sname,ssex,sbirthday,sdept)
values('201320180309','刘可','女','1998-2-15',null) 6. 将张三的C语言课程成绩改为100分 updatesc set score=100
where sno=(select sno from s where sname='张三') and cno=(select cno from c where cname='c语言') 7. 删除1001号课程的选修信息 delete from sc where cno='1001'
实验七数据安全(角色部分不考)
1.在服务器中创建test数据库的用户yy和登陆账号。 sp_addlogin 'login1','pass1' use test
sp_adduser 'login1','yy'
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库数据库实验内容--答案在线全文阅读。
相关推荐: