数据库原理实验报告.docx
- 文档编号:5020402
- 上传时间:2022-12-12
- 格式:DOCX
- 页数:44
- 大小:167.26KB
数据库原理实验报告.docx
《数据库原理实验报告.docx》由会员分享,可在线阅读,更多相关《数据库原理实验报告.docx(44页珍藏版)》请在冰豆网上搜索。
数据库原理实验报告
题目:
数据库原理实验报告
课本内容练习
1、创建表
createtableStudent(
Snochar(9)primarykey,
Snamechar(20)unique,
Ssexchar
(2),
Sagesmallint,
Sdeptchar(20)
)
createtableCourse(
Cnochar(4)primarykey,
Cnamechar(40),
Cpnochar(4),
Ccreditsmallint,
foreignkey(Cpno)referencesCourse(Cno)
);
createtableSC(
Snochar(9),
Cnochar(4),
Gradesmallint,
primarykey(Sno,Cno),
foreignkey(Sno)referencesStudent(Sno),
foreignkey(Cno)referencesCourse(Cno)
)
createtableDept_age(
Sdeptchar(15),
Avg_agesmallint,
)
2、插入
insert
intoStudent(Sno,Sname,Ssex,Sage,Sdept)
values('200215121','李勇','男',20,'CS')
insert
intoStudent(Sno,Sname,Ssex,Sage,Sdept)
values('200215122','刘晨','女',19,'CS')
insert
intoStudent(Sno,Sname,Ssex,Sage,Sdept)
values('200215123','王敏','女',18,'MA')
insert
intoStudent(Sno,Sname,Ssex,Sage,Sdept)
values('200215125','张立','男',19,'IS')
insert
intoCourse(Cno,Cname,Cpno,Ccredit)
values('2','数学',NULL,2)
insert
intoCourse(Cno,Cname,Cpno,Ccredit)
values('6','数据处理',NULL,2)
insert
intoCourse(Cno,Cname,Cpno,Ccredit)
values('4','操作系统','6',3)
insert
intoCourse(Cno,Cname,Cpno,Ccredit)
values('7','PASCAL语言','6',4)
insert
intoCourse(Cno,Cname,Cpno,Ccredit)
values('5','数据结构','7',4)
insert
intoCourse(Cno,Cname,Cpno,Ccredit)
values('1','数据库','5',4)
insert
intoCourse(Cno,Cname,Cpno,Ccredit)
values('3','信息系统','1',4)
insert
intoCourse(Cno,Cname,Cpno,Ccredit)
values('8','DB_Design','7',4)
insert
intoSC(Sno,Cno,Grade)
values('200215121','1',92)
insert
intoSC(Sno,Cno,Grade)
values('200215121','2',85)
insert
intoSC(Sno,Cno,Grade)
values('200215121','3',88)
insert
intoSC(Sno,Cno,Grade)
values('200215122','2',90)
insert
intoSC(Sno,Cno,Grade)
values('200215122','3',80)
3、查询
selectSno,Sname
fromStudent;
/*查询经过计算的值*/
selectSname,2014-Sage
fromStudent
/*指定字符通过小写输出*/
selectSname,'yearofbirth',2014-Sage,lower(Sdept)
fromStudent
/*为查询结果指定列标题*/
selectSnamename,'yearofbirth'birth,2014-Sagebirthday,lower(Sdept)department
fromStudent
/*消除取值重复的行*/
selectdistinctSno
fromSC
/*比较大小查询*/
selectSname,Sage
fromStudent
whereSage>19
selectSname
fromStudent
whereSdept='CS'
/*确定范围*/
selectSname,Sdept,Sage
fromStudent
whereSagebetween20and30
selectSname,Sdept,Sage
fromStudent
whereSagenotbetween20and30
/*确定集合*/
selectSname,Ssex
fromStudent
whereSdeptin('CS','MA')
/*字符匹配*/
select*
fromstudent
wheresnolike'200215121'
select*
fromstudent
wheresno<>'200215121'
select*
fromstudent
wheresno!
='200215121'/*不含有通配符的时候可以使用“=;<>;!
=”代替like,notlike*/
selectSname,Sno,Ssex
fromStudent
whereSnamelike'刘%'
selectSname
fromStudent
whereSnamelike'欧阳__'/*注意一个汉字两个字符*/
selectSname,Sno
fromStudent
whereSnamelike'__阳%'
selectSname,Sno
fromStudent
whereSnamenotlike'刘%'
/*字符匹配*/
selectCno,Ccredit
fromCourse
whereCnamelike'DB\_Desigen'escape'\'/*转码字符由自己定义*/
/*涉及空值的查询*/
selectSno,Cno
fromSC
whereGradeisnull/*注意is不能用=替代*/
selectSno,Cno
fromSC
whereGradeisnotnull
/*多重条件查询*/
selectSname
fromStudent
whereSdept='CS'andSage<20/*and优先级比or高*/
selectSname,Ssex
fromStudent
whereSdept='CS'orSdept='IS'orSdept='MA'/*有时in谓词可看做多个or的缩写*/
/*orderby子句*/
selectSno,Grade
fromSC
whereCno='3'
orderbyGradedesc/*asc:
升序;desc:
降序,缺省升序*/
select*
fromStudent
orderbySdept,Sagedesc
/*聚集函数*/
selectcount(*)/*貌似distinct和*是不能同时使用的*/
fromSC
selectcount(distinctSno)
fromStudent
selectavg(Grade)
fromSC
whereCno=1/*计算一号课程的平均成绩*/
selectmax(Grade)
fromSC
whereCno=2/*计算号课程的最高成绩*/
selectsum(Ccredit)
fromSC,Course
whereSno='200215121'andSC.Cno=Course.Cno/*查询学号为的学生选修课程的总学分*/
/*groupby子句*/
selectCno,COUNT(Sno)
fromSC
groupbyCno
/*having短语*/
selectSno
fromSC
groupbySno
havingCOUNT(*)>2
/*连接查询*/
selectStudent.*,SC.*
fromStudent,SC
whereStudent.Sno=SC.Sno
/*自然连接,两个表中不重复的属性名可以不加前缀*/
selectStudent.Sno,Sname,Ssex,Sdept,Cno,Grade
fromStudent,SC
whereStudent.Sno=SC.Sno
/*自身连接,要为表取别名,此时必须都要有前缀*/
selectfirst.Cno,second.Cpno
fromCoursefirst,Coursesecond
wherefirst.Cpno=second.Cno
/*外连接*/
selectStudent.Sno,Sname,Ssex,Sdept,Cno,Grade
fromStudentrightouterjoinSCon(Student.Sno=Sc.Sno)
/*复合条件连接*/
selectStudent.Sno,Sname
fromStudent,SC
whereStudent.Sno=SC.SnoandSc.Cno='2'andSC.Grade>80
/*复合条件连接*/
selectStudent.Sno,Sname,Cname,Grade
fromStudent,Course,SC
whereStudent.Sno=SC.SnoandCourse.Cno=SC.Cno
/*嵌套查询*/
/*in谓词*/
selectSno,Sname,Sdept
fromStudent
whereSdeptin(
selectSdept
fromStudent
whereSname='刘晨'
)/*此查询可以利用自身连接代替selectS1.Sno,S1.Sname,S1.Sdept
fromStudentS1,StudentS2
whereS1.Sdept=S2.SdeptandS2.name='刘晨'*/
selectSno,Sname
fromStudent
whereSnoin(
selectSno
fromSC
whereCnoin(
selectCno
fromCourse
whereCname='信息系统'
)
)
/*以上代码的连接方式*/
selectStudent.Sno,Sname
fromStudent,Course,SC
whereCourse.Cname='信息系统'andSC.Cno=Course.CnoandStudent.Sno=Sc.Sno
/*带有比较运算符的子查询*/
/*相关子查询*/
selectSno,Cno
fromSCx
whereGrade>=(
selectavg(Grade)
fromSCy
wherey.Sno=x.Sno
)
/*带有any(some)或all谓词的子查询*/
selectSname,Sage
fromStudent
whereSage selectSage fromStudent whereSdept='CS' )andSdept<>'CS' selectSname,Sage fromStudent whereSage selectSage fromStudent whereSdept='Cs' )andSdept<>'CS' /*带有exists谓词的子查询*/ selectSname fromStudent whereexists( select* fromSC whereSno=Student.SnoandCno='1' ) selectSname fromStudentStu1 whereexists( select* fromStudentStu2 whereStu2.Sname='刘晨'andStu1.Sdept=Stu2.Sdept ) 4、更新 /*插入数据*/ /*插入元组*/ insert intoStudent(Sno,Sname,Ssex,Sdept,Sage) values('200215128','陈东','男','IS',18) /*插入子查询结果*/ insert intoDept_age(Sdept,Avg_age) selectSdept,avg(Sage) fromStudent groupbySdept /*修改数据*/ updateStudent setSage=22 whereSno='200215121' updateStudent setSage=Sage+1 /*带子查询的修改语句*/ updateSC setGrade=0 where'CS'=( selectSdept fromStudent whereStudent.Sno=SC.Sno) /*删除数据*/ delete fromSC where'CS'=( selectSdept fromStudent whereStudent.Sno=SC.Sno ) 5、视图 /*建立视图*/ createviewIS_Student AS selectSno,Sname,Sage fromStudent whereSdept='IS' withcheckoption/*增删改操作时要保证修改的行满足视图定义中的谓词条件*/ createviewIS_S1(Sno,Sname,Grade) as selectStudent.Sno,Sname,Grade fromStudent,SC whereSdept='IS'andStudent.Sno=SC.SnoandSC.Cno='1' 实验一SQLSERVER2000的基本使用 实验目的和要求: 通过本实验,使学生熟悉SQLSERVER2000的工作环境,尤其是SQLSERVER2000的企业管理器与查询分析器,熟悉对SQLSERVER2000的各种操作。 具体地,掌握表(关系)和索引的建立方法;掌握表结构(关系模式)的修改方法。 实验内容: 1.在studentdb数据库中利用查询分析器创建以下3个表,同时完成数据完整性的定义(实体完整性、参照完整性和用户定义的域完整性): ①student(学生信息表): 主码 列名 数据类型 宽度 小数位 空否 取值范围 备注 Pk sno char 5 N 学号 sname char 10 N 姓名 ssex char 2 Y 性别 sage smallint Y 不小于12 年龄 sdept char 15 Y 系名 createtablestudent( snochar(5)primarykeynotnull, snamechar(10)uniquenotnull, ssexchar (2), sagesmallintcheck(sage>=12), sdeptchar(15), ) ②course(课程表): 主码 列名 数据类型 宽度 小数位 空否 备注 Pk cno Char 2 N 课程号 cname Char 20 Y 课程名称 cpno Char 2 Y 先行课号 ccredit smallint Y 学分 createtablecourse( cnochar (2)primarykey, cnamechar(20), cpnochar (2), ccreditsmallint, foreignkey(cpno)referencesCourse(cno) ) ③sc(学生选课表): 主码 列名 数据类型 宽度 小数 空否 外码 参照关系 取值范围 备注 Pk sno Char 5 N Fk student 学号 cno Char 2 N Fk course 课程号 grade Decimal 5 1 Y 0≤x≤100 成绩 createtablesc( snochar(5), cnochar (2), gradedecimal(5)constraintg1check(grade>=0andgrade<=100), primarykey(sno,cno), foreignkey(sno)referencesstudent(sno), foreignkey(cno)referencescourse(cno) ) 2.在spjdb数据库中利用查询分析器创建以下4个表,同时完成数据完整性的定义(实体完整性、参照完整性和用户定义的域完整性): ①S(供应商信息表): 主码 列名 数据类型 宽度 小数位 空否 取值范围 备注 Pk sno char 2 N 供应商号 sname char 10 N 供应商名称 status smallint Y 大于0 供应商状态 city char 10 Y 所在城市 createtableS( snochar (2)primarykey, snamechar(10)notnull, statussmallintcheck(status>0), citychar(10), ) ②P(零件信息表): 主码 列名 数据类型 宽度 小数位 空否 取值范围 备注 Pk pno char 2 N 零件号 pname char 10 N 零件名称 color char 2 Y 颜色 weight smallint Y 大于0 重量 createtableP( pnochar (2)primarykey, pnamechar(10)notnull, colorchar (2), weightsmallintcheck(weight>0), ) ③J(工程项目表): 主码 列名 数据类型 宽度 小数位 空否 取值范围 备注 Pk jno char 2 N 工程项目号 jname char 10 N 工程项目名称 city char 10 Y 所在城市 createtableJ( jnochar (2)primarykey, jnamechar(10)notnull, citychar (2), weightsmallint, ) ④SPJ(供应情况表): 主码 列名 数据类型 宽度 小数 空否 外码 参照关系 取值范围 备注 Pk sno Char 2 N Fk S 供应商号 pno Char 2 N Fk P 零件号 jno Char 2 N Fk J 工程项目号 qty smallint Y x>0 成绩 createtableSPJ( snochar (2)notnull, pnochar (2)notnull, jnochar (2)notnull, qtysmallintcheck(qty>0), primarykey(sno,pno,jno), foreignkey(sno)referencesS(sno), foreignkey(pno)referencesP(pno), foreignkey(jno)referencesJ(jno), ) 3.修改表结构,具体要求如下: (1)将表course的cname列的数据类型改为varchar(40). altertablecoursealtercolumncnamevarchar(40) (2)为表student增加一个新列: birthday(出生日期),类型为datetime,默认为空值 altertablestudentaddbirthdaydatetimenull (3)将表sc中的grade列的取值范围改为小于等于150的正数. altertablescdropconstraintg1 altertablescaddconstraintg1check(grade<=150) (4)为Student表的“Sex”字段创建一个缺省约束,缺省值为’男’ altertablestudentaddconstraintssexcheck(ssexin('男')) (5)为“Sdept”字段创建一个检查约束,使得
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 原理 实验 报告