2016年数据库的卷二练习答案

上传人:无*** 文档编号:210028262 上传时间:2023-05-15 格式:PDF 页数:25 大小:1.86MB
收藏 版权申诉 举报 下载
2016年数据库的卷二练习答案_第1页
第1页 / 共25页
2016年数据库的卷二练习答案_第2页
第2页 / 共25页
2016年数据库的卷二练习答案_第3页
第3页 / 共25页
资源描述:

《2016年数据库的卷二练习答案》由会员分享,可在线阅读,更多相关《2016年数据库的卷二练习答案(25页珍藏版)》请在装配图网上搜索。

1、-1、创建数据库create database teston primary(name=test_data,filename=D:SQL 期末作业test_data.mdf,size=10MB,maxsize=10MB,filegrowth=llVIB)log on(name=,test_logfilename=D:SQ.L 期末作业test_log.ldf,size=lMB,maxsize=5MB,filegrowth=10%)go-2、删除数据库drop database test-3 创建 student 表use testgocreate table student(st_id nva

2、rchar(9)not null constraint pk primary key,st_nm nvarchar(8)not null,st_sex nvarchar(2)zst_birth datetime null,st_score int null,st_date datetime null,st_from nchar(20)7st_dpid nvarchar,st_mnt tinyint)-3、创建 course 表use testgocreate table course(csjd nvarchar(4)not null constraint pk_id primary key,c

3、s_nm nvarchar(20)not null,cs_tm int,cs_sc int)-4、创建 slt_courseuse testgocreate table slt_course(cs_id nvarchar(4)not null constraint fk_id references course(cs_id),st_id nvarchar(9)not null constraint fk_st_id references student(stjd),score int,sitdate datetime)-5、创建院系(dept)use testgocreate table de

4、pt(dp_id nvarchar(2)not null,dp_nm nvarchar(20)not null,dp_drt nvarchar(8),dt_tel nvarchar(12)-修改表结构-1)向表中添加列use testgoalter table deptadd dp_count nvarchar(3)go-2)修改列数据use testgoalter table deptalter column dp_count intgo-3 删除表指定列(column)use testgoalter table deptdrop column dp_countgo-4、删除dept表use

5、 testgodrop table deptgo-向表中输入数据记录use testgoinsert studentvalues(20151090101,邓建娥),女?1996,400,20157山东省潍坊,JO L学习部副部,)go-3、数据完整性-1)空值约束use testgoalter table studentalter column st_sex varchar(2)not null-默认值约束(default)use testgoalter table studentadd constraint df_from default陕西省 for st_fromgo-检查约束(chec

6、k)use testgoalter table slt_courseadd constraint ck_course check(score=0 and score 学分use testgoselect 课程号=cs_id,课程名=cs_nm,学分=cs_sc,学时=cs_sc*16from coursego-5.6查询列表中使用系统函数显示所有学生的学号、姓名、性别和入学年份use testgoselect st_id,st_nm,st_sex,st_datefrom studentgroup by st_idgo-5.7 消除查询结果中的重复项显示所有学生班级use testselect

7、distinct st_idfrom studentgo-6 数据查询()-条件查询-L 使用关系表达式查询条件-6.1查询dept表中系号为的院系信息use testgoselect*from deptwhere dp_id=ll,go-6.2查询student表中系的学生学号、姓名、性别和所在系编号use testgoselect st_id,st_nm,st_sex,st_dpidfrom studentwhere st_dpid=11go-6.3 查询student表中年及以后入学的学生信息use testselect*from studentwhere st_date 2008-12

8、-31go-6.4 在查询student表班学生的学号、姓名、性别和入学成绩use testgoselect st_id,st_nm,st_sex,st_scorefrom studentwhere st_bj=080808,go-使用逻辑表达式表示查询条件-6.5 查询选修了号课程且成绩在以下的学生学号use testgoselect st_idfrom slt_coursewhere cs_id=,1002and score 75go-9 数据查询()连接查询、子查询-9.1 查询学生学号、姓名、性别及其所选课程编号use testgoselect student.stjd,st_nm,s

9、t_sexzcs_idfrom student join slt_courseon student.st_id=slt_course.st_idgo-9.2 查询学生学号、姓名及其所选课程名称及成绩use testgoselect student.stjd;st_nm,cs_id;scorefrom student join slt_courseon student.st_id=slt_course.st_idgo-9.3 查询选修了“数据结构 课程的学生学号、姓名及课程成绩use testgoselect student.st_id,st_nm,scorefrom student join

10、slt_courseon student.st_id=slt_course.st_idwhere cs_id in(select cs_id from course where cs_nm=数据结构go-1 0 数据查询()子查询-10.1查询选修了课程成绩不及格的学生的学号、姓名和性别,并按姓名升序排序use testgoselect st_id,st_nm,st_sexfrom studentwhere st_id in(select st_id from slt_course where cs_id=1002 andscore2014-12-30go-11.2为 course表创建基于课

11、程编号列的聚集索引kcbhjndexuse te-77stgocreate clustered index kcbhjndex on course(cs_id)goexec sp_helpindex course-1 2 游标-12用游标实现:输出所有女学生的学号、姓名、性别和班级代码。use testgodeclare c_xsxx cursor keyset forselect st_id,st_n m,st_sex,st_bjfrom studentopen c_xsxxdeclare xh nvarchar,xm nvarchar(8),xb nvarchar(8),bjdmnvarc

12、har(lO)if ERROR=0-判断游标打开是否成功beginif CURSOR_ROWS 0beginprint 共有女学生,+rtrim(cast(cursor_rows as char)+名,分别是:printfetch next from c_xsxx into xh,xm,xb,bjdmwhile(FETCH_STATUS=O)begin print xh+,+xm+xb+fetch next from c_xhxx into xh,xm,xb,bjdmendendendelseprint,游标存在问题!,close c_xsxxdeallocate-1 3 存储过程和触发器-1

13、3.1创建一个带参数的存储过程cj:当任意输入一个学生的姓名时,将从三个表-(学生表、课程表、课程注册表)中返回该学生的学号、选修的课程名称和课程成绩-学生表课程课程注册use studentgocreate proc cj xm varchar(lO)asselect学生.学号,课程名称,成绩from 课程注册jo in 学 生 o n 课程注册.学号=学生.学号jo in 课 程 o n 课程注册.课程号=课程.课程号where学生.姓名=xm-执 行 cj存储过程,查询刘永辉的学号、选修课程和课程成绩。use studentgoexec cj xm=刘永辉,-13.3创建一个带返回值的存

14、储过程,根据 课程 表和 课程注册”表中的数据-返回某课程的成绩大于分的人数。use studentgocreate proc g rs-创建一个del_zy的 delete触发器use studentgocreate trigger del_zy on 专业for deleteasdeclare zydm char(9)select zydm=专业代码 from deletedif exists(select*from 班级 where 专业代码=2丫|01)beginprint(正在使用,不能被使用!,rollback transactionendgo-13.5创建一个触发器bjdm_update,当班级表中的班级代码被更新时,内部

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!