实验四SQL语句
实验4:简单查询语句及操纵语句(1)

实验四数据简单查询及操纵语句【实验目的与要求】1、熟练掌握SELECT 语句的基本语法格式;2、熟练掌握使用SQL语句进行单表查询,掌握GROUP BY子句、HA VING 子句和集函数;3、熟练掌握使用SQL标准语句和T-SQL扩展语句进行连接查询。
【实验内容】4.0实验准备本实验将用到实验2中的CPXS数据库、实验3中数据库EDUC数据库以及相应的表及数据,若上述数据库及表尚未创建,或数据尚未录入,则请先完成之。
4.1在实验2中的产品销售数据库CPXS数据库完成以下简单查询⏹查询各种产品的产品编号、产品名称和价格。
写出对应SQL语句并给出查询结果:select 产品编号,产品名称,价格from CP⏹查询地区在“南京”的客户编号和客户名称,结果中各列的标题分别指定为:Customer_id和Customer_name。
写出对应SQL语句并给出查询结果:select 客户编号 Customer_id,客户名称 Customer_nameFROM XSSWHERE 地区='南京'⏹求各产品编号、名称和产品总值。
写出对应SQL语句并给出查询结果:select 产品编号,产品名称,价格*库存量产品总值from CP⏹查询至少购买了至少一种产品的客户编号(不重复)。
写出对应SQL语句并给出查询结果:select distinct 客户编号from CPXSBwhere 数量>=1⏹查询价格在1000-2000的产品信息,并依价格由高到低的顺序排序。
(请使用Between…and和算术比较运算符分别实现)写出对应SQL语句并给出查询结果:(1)select 产品名称from CPwhere 价格 between 1000 and 2000order by 价格 desc(2)select 产品名称from CPwhere 价格>= 1000 and 价格<= 2000order by 价格 desc⏹查询产品名称含有”空调”二字的产品情况。
实验四 SQL查询语句(二)

实验四 SQL查询语句(二)一、实验目的:1.掌握连接查询的使用方法2.掌握嵌套查询的使用方法3.掌握UNION子句、INTO子句的使用方法二、实验内容:使用教学据库,包括三张表:学生表,课程表,成绩表,另增加book表,定义语句如下:CREATE TABLE book (Book_id char(9) not null,Book_name varchar(30) not null,Price money not null)根据上面的表,完成下面的操作:1.检索学生的学号,姓名,课程号,课程名及成绩2.使用UNION子句查询查询‘软件学院’和‘电子学院’的学生的信息,要求显示:学号,姓名,班级。
3.查询选修了”C804”课程且成绩在90分以上的学生清单4.查询软件学院所有男生的学号,课程号和成绩(可选择带有IN 的查询或复合条件查询)5.查询大于学号“608013302”学生所有课程成绩的学生。
要求:列出学生的学号,姓名,性别,课名和成绩6.查询“软件学院”学生的学号,课程号及相应的成绩(可选择带有EXISTS的子查询或复合条件查询)7.在WHERE子句中使用集合函数按升序显示所有年龄超过平均值的学生名单8.INTO 子句1)创建一张女学生表,内容包括软件学院和电子学院的所有女学生的名单2)创建一个临时表,名为“newstudent“,包括学生表中所有男学生的信息。
9.创建“新学生表“,包括学号,姓名,性别,所在院系;将学生表中电子系或者男同学的记录全部插入到“新学生表”中;10.1)将软件学院全体学生的成绩加5分。
2)将图书的价格增加10%11.删除软件学院所有学生的选修课记录二、程序代码1. select student.sno,sname,o,ame,score.grade from student,course,score2. select sno,sname,sdept from student where sdept='软件学院'unionselect sno,sname,sdept from student where sdept='电子学院'3. select student.sno,sname,class,grade from student,score4.select student.sno,class,score.grade from student,score wherestudent.sno=score.sno and sex='男'and sdept='软件学院'5. select student.sno,sname,sex,class,grade from student,score,course where student.sno>6080133026. select student.sno,o,grade from student,course,score where sdept='软件学院'7.select * from studentwhere age>(select A VG(age) from student ) order by age ASC8.9.10.11.四、实验小结。
实验四 SQL语言-参考答案

实验四SQL语言【实验目的】1.掌握SQL语言的使用方法2.利用SQL语句实现相关的操作3.能够独立写出一些较复杂的SQL语句【实验内容】1. 建立一个Student表,由Sno(学号)、Sname(姓名)、Ssex(性别)、Sage(年龄)、Sdept (所在系)五个属性组成。
其中学号不能为空,值是唯一的,并且姓名取值也唯一。
查询名为“创建Student”。
Create table student(sno char(6) not null unique,sname char(4) unique,ssex char(1),sage smallint,sdept char(20))2. 建立一个SC (学生选课)表,它由学号Sno、课程号Cno,修课成绩Grade组成,其中(Sno, Cno)为主码。
),查询名为“创建SC”。
Create table sc(sno char(6),cno char(3),grade single,primary key (sno,cno))3. 查询所有姓刘的学生的学号与姓名及其出生年份,查询名为“查找姓刘的学生”。
Selelct 学号,姓名,year(出生日期) as 出生年份from 学生表where 姓名like “刘*”4. 查询选修了计算机基础的所有学生的学号、姓名、成绩,查询结果按分数降序排列,查询名为“选修了计算机基础的学生”。
Select 学生表.学号,学生.姓名,选课成绩表.成绩from 学生表,课程表,选课成绩表where 学生表.学号=选课成绩表.学号and 课程表.课程编号=选课成绩表.课程编号and 课程表.课程名称=”计算机基础” order by 选课成绩表.成绩6. 查询所有年龄在20岁及20岁以下的学生姓名、年龄,查询名为“20岁及以下的学生”。
Select 姓名,年龄from 学生表where year(date())-year(出生日期)<=207. 查询考试成绩有不及格的学生的学号和姓名。
实验4:数据库的高级查询操作

实验4:数据库的高级查询操作实验四:数据库的各类数据查询操作一、实验目的掌握SQL程序设计基本规范,熟练运用SQL语言实现数据的各种查询和设计,包括连接查询、嵌套查询、集合查询等。
二、实验内容和要求针对KingbaseES数据库设计单个表针对自身的连接查询,设计多个表的连接查询;设计各种嵌套查询和集合查询。
了解和掌握SQL查询语句各个子句的特点和作用,按照SQL程序设计规范写出具体的SQL查询语句,并调试通过。
三、实验步骤连接查询1. 查询每个学生及其选修课程的情况:select student.*, sc.* from student, sc where student.sno=sc.sno比较: 笛卡尔集: select student.*, sc.* from student, sc自然连接: select student.sno, sname, ssex, sdept, cno, grade from student, sc where student.sno=sc.sno2. 查询每一门课程的间接先行课(只求两层即先行课的先行课):select /doc/3d4429586.html,o, Second.pcno 间接先行课from course First, course Second where First.pcno=/doc/3d4429586.html,o比较:select /doc/3d4429586.html,o, Second.pcno 间接先行课from course First, course Second where First.pcno=/doc/3d4429586.html,o and Second.pcno is not null3. 列出所有学生的基本情况和选课情况, 若没有选课,则只列出基本情况信息:SQL Server 中: select s.sno, sname, ssex,sdept, cno, grade from student s, sc sc where s.sno*=sc.sno4. 查询每个学生的学号, 姓名, 选修的课程名和成绩:select S.sno, sname, cname, grade from student S, course C, sc SC where S.sno=SC.sno and /doc/3d4429586.html,o=/doc/ 3d4429586.html,o5. 查询平均成绩在80分以上的学生姓名Select sname from student,sc where student.sno=sc.sno GROUP BY sc.snoHAVING AVG(sc.grade)>80;高级查询使用带IN谓词的子查询1.查询与’刘晨’在同一个系学习的学生的信息:select * from student where sdept in(select sdept from student where sname='刘晨')比较: select * from student where sdept =(select sdept from student where sname='刘晨') 的异同比较: select * from student where sdept =(select sdept from student where sname='刘晨') and sname<>'刘晨' 比较: select S1.* from student S1, student S2 where S1.sdept=S2.sdept and S2.sname='刘晨'2.查询选修了课程名为’信息系统’的学生的学号和姓名:SQL Server中: select sno, sname from student where sno in (select sno from sc where cno in(select cno from course where cname='信息系统'))3.查询选修了课程’1’和课程’2’的学生的学号:select sno from student where sno in (select sno from sc where cno='1')and sno in (select sno from sc where cno='2') 比较: 查询选修了课程’1’或课程’2’的学生的sno:select sno from sc where cno='1' or cno='2'比较连接查询:select A.sno from sc A, sc B where A.sno=B.sno and /doc/3d4429586.html,o='1' and/doc/3d4429586.html,o='2'使用带比较运算的子查询1.查询比’刘晨’年龄小的所有学生的信息:select * from student where sage<(select sage from student where sname='刘晨')使用带Any, All谓词的子查询2.查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄;select sname, sage from student where sage <any< p=""> (select sage from student where sdept='IS')and sdept<>'IS'3.查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄:select sname, sage from student where sage <all< p="">(select sage from student where sdept='IS')and sdept<>'IS'4.查询与计算机系(CS)系所有学生的年龄均不同的学生学号, 姓名和年龄:select sno,sname,sage from student where sage<>all(select sage from student where sdept='CS')使用带Exists谓词的子查询和相关子查询5.查询与其他所有学生年龄均不同的学生学号, 姓名和年龄:select sno,sname,sage from student A where not exists(select * from student B where A.sage=B.sage andA.sno<>B.sno)6.查询所有选修了1号课程的学生姓名:select sname from student where exists(select * from sc where sno=student.sno and cno='1')7.查询没有选修了1号课程的学生姓名:select sname from student where not exists(select * from sc where sno=student.sno and cno='1')8.查询选修了全部课程的学生姓名:select sname from student where not exists(select * from course where not exists( select * from sc where sno=student.sno and cno=/doc/3d4429586.html,o))12. 查询至少选修了学生95002选修的全部课程的学生的学号:select distinct sno from sc A where not exists(select * from sc B where sno='95002'and not exists(select * from sc C where sno=A.sno and cno=/doc/3d4429586.html,o))13. 求没有人选修的课程号cno和cnamecname:select cno,cname from course C where not exists(select * from sc where /doc/3d4429586.html,o=/doc/ 3d4429586.html,o )14. 查询满足条件的(sno,cno)对, 其中该学号的学生没有选修该课程号cno 的课程select sno,cno from student,course where not exists(select * from sc where cno=/doc/3d4429586.html,o and sno=student.sno)15. 查询每个学生的课程成绩最高的成绩信息(sno,cno,grade):select * from sc A where grade=(select max(grade) from sc where sno=A.sno )集合查询1. 查询数学系和信息系的学生的信息;select * from student where sdept='MA' union select * from student where sdept='IS'2. 查询选修了1号课程或2号课程的学生的学号:select sno from sc where cno='1'Unionselect sno from sc where cno='2'思考:1. 连接查询速度是影响关系数据库性能的关键因素。
SQL实验报告(优秀范文5篇)

SQL实验报告(优秀范文5篇)第一篇:SQL实验报告实验四触发器实验(一)after触发器(1 1)在l l i neitem 表上定义一个 after 触发器, , 当修改列项目e e xtendedprice d i scountx tax 时, , 要把 s orders 表得to o tal pri ce e 一起修改, , 以保证数据一致性C RE ATE T RIGGERtrig _line ite m_ pr ice_ update on line it em fo rupda teaasbegin i f(UPDATE(ex tend edprice)o r UPDATE(tax)or UPD AT E(di scou nt))begin-—声明游标变量指向 inserted 表d eclarecursor_inserted c urs orrea d_onlyofor select order key,linenu mber,exte nd edpr ice, dis coun t, taxfromin ser ted—-息信找查取获量变明声ﻩ声明变量获取查找信息de clare order key in t, @linenumb erint,exte nd edpricereal,disscount real,tax real—-打开游标epoﻩen cursor_i ns ert ed—-标游取读ﻩ读取游标fe tchnextfrom cur sor _i ns erte dint o@o rderkey, @lin enumber,eext ende dprice, @di scount,t axwwhi le FETC H_S TATUS =0 nigebﻩnﻩ—-声明一个变量保存重新计算得新价格cedﻩﻩecl are @n ew_tota lpri cer ealﻩ select @n ew_t otal price= @ext en dedpr ice *(1 -@di scou nt)*(1 +@tax)—-用新得总价格变量更新 orders 表得 t ot alprice puﻩﻩupdate orde rsse t tot alpri ce= new_totalprice where or derkey=orde rkeyen hctefﻩext f ro m cur sor_i nser ted int o@order ke y, @li nenum ber, @ex tende dp rice,discoun t, @taxdneﻩllaedﻩlocate c ur sor_i nser te deend end ﻩ(2)在在 linei tem表上定义一个 aftr er 触发器, , 当增加一项订单明细时, ,自动修改 s orders 表得 total p rice, 以保证数据一致性CREATE TR IGGER tri g_ lineit em_price_ in sert on l ine item ffor inse rta sbegin ——向指量变标游明声ﻩ声明游标变量指向ins erted 表de clarec ursor_inse rtedcursorread_ onl yﻩ for s ele ct orderkey,linenumbe r,ex te ndedp ric e,di scou nt,t axfrom insserte d--声明变量获取查找信息edﻩeclare @orde rk eyint, @lin enumber int,e xten dedp ricereal, @discountreal, @ta xreal -—ﻩ-打开游标open cursor_i ns erte d--ﻩ-读取游标fe tc hnex tf romcu rsor_ins ertedinto@o rd erkey,li nen umbeerr, ex ten ded pric e,dis count,tax ihwﻩile @@FE TCH_ STATU S=0 ebﻩegin-—格价新得算计新重存保量变个一明声ﻩﻩ声明一个变量保存重新计算得新价格cedﻩclare @n ew_tot alprice realcelesﻩct new _to ta lprice= @extend edp rice *(1-d is count)*(1+tax)—-新更量变格价总得新用ﻩﻩ用新得总价格变量更新orders 表得totalpri ceﻩ u pda teor ders s ett ota lpric e=total pric e+ @new_ tota lpr ice wwhhe re o rderke y=orderkeytefﻩetch next from cursor_ ins erte dinto o rder key, @l inen umber,e xtendeddp ric e, @disc ou nt, @t axeend aedﻩdeall oca te cu rsor_in serted en d(3)在l in e ite m表上定义一个a ft er触发器, , 当删除一项订单明细记录时, ,自动修改 orders 表得 tot a lprice, 以保证数据一致性CREATE TRI GG ER trig _line item_price_de let eon line item fo r de let eAAS begin--声明游标变量指向delet ed 表de clar e curso r_d eleted cursorre ad _on lyesrofﻩﻩele ctord erk ey,line numbe r,extende dp rice,discoun t, taxfromdel eted-ﻩ--声明变量获取查找信息declare ord erkey int,linenum be rint,extendedp ri ce r eal,discou nt real,ta xreal -—ﻩ-打开游标epoﻩen c urso r_ deleted——标游取读ﻩ读取游标efﻩetch next fr om cur sor_delete d in toorder ke y,l inenumb er,ext enddedprice, @di scoun t,taxwh ile@FETCH_STATUS=0begi n-—声明一个变量保存重新计算得新价格ﻩ declare @ne w_ to talpric erealﻩsselec t@new_t otalpr ice= @exte nde dpri ce*(1-@disco unt)*(1+ tax)-ﻩ-—用新得总价格变量更新orders 表得tot alp rice uﻩﻩupd ateorders set t otalpri ce=to talpr ice-@new _totalp rice wh er e order key= @ord erkeyfetchn ext f rom cursor_inse rt ed in to orderkey, @line numbe r,extendeeddprice, @d iscou nt,t ax dneﻩndddeal lo cat e cur sor_inse rted eend((4 4))验证 up d at e触发器—-查瞧号订单得 to ta a l pr i ceselec t*fro o mo o r ders whereorde r key=1 8 30;—-查瞧明细表得相关信息se l ect *f ro mlin ei i te e mw here or de e r key=183 0and l ine num m be e r =1;——验证 e update 触发器updat elineitem set t ax=tax+0、05whe re orderkey=1830;(二)i i n steadof触发器((1))在在 lineit em 表上定义一个ins tead o fupda te触发器, 当修改明细表中得数量量quan ti ty 时, 应先检查供应表par tsupp 得av ai lqty 就是否足够, 不足够则拒绝执行, 否则执行并修改相应数值以保证数据一致性于由于 in steadof 触发器更新某个表会使得该表上其她不满足更新列不能更新,因因用此逆向思维使用 a fter 触发器实现相同效果即先更新 qu antity, 再比较av ailqqtty, 如果满足更新数量, 就修改partsupp 得表得 a vailqty, 如果不满足, 则把lineitem得quantity 更新之后得数据重新修改回来ccreate trigge r trig_lin eit em_quanti ty_ upda teonli neit em f or upda teas begin if UPDATE(qu ant ity)bbegin ——向指别分量变标游明声ﻩ声明游标变量分别指向 i nserted 表与 d el eted 表edﻩdeclare c urso r_inser tedcur sorr ead_on lyfortcelesﻩﻩtorde rkey, partk ey,s uppkey,lin enum ber, quantit yfr om i nserteddecl arecursor_de leted c urs orrea d_ onl yf or select quantityfr om deleted-—息信找查取获量变明声ﻩ声明变量获取查找信息decl are@qu an tity _dif f_lineitem i nt,q uanti ty_pa rtsup p i nt cedﻩclare suppkey int, @par tke y in t,o rd erkey i nt,unenilﻩﻩum ber int ,qty _inserted in t , @qty_deleted int--打开游标ruc nepoﻩrsor_in sert ed poﻩopen cur sor_d eleted-—量变给赋值数标游取读ﻩ读取游标数值赋给变量fﻩfet chnext fromcu rsor_ insert edﻩiinto or de rk ey,pa rtkey, @suppk ey, @line nu mber, @qty _ins erte df et chnext fromcursor _d eletedint o q ty _de lete dwhi le fe tch_st atus=0 gebﻩegin--计算订单明细修改时, 订购数量得变化值inserte d表项-d elet ed表项ssel ect quantity_d if f_ li neit em= @q ty _in se rte d—@@q ty_deleteedﻩ--从parts upp 表获取ava ilq ty值, 注意partsupp 表得主键为(partk ey,suppkeey)tcelesﻩﻩt@quanti ty_p ar tsupp =av ailq ty fro m pa rtsu ppwﻩﻩwh er e suppkey= suppke yand part key= @par tk ey-—断判始开ﻩﻩ开始判断gebﻩbegi nfiﻩf quant ity _d iff_ lin eite m=0ﻩﻩ p rin t“ 更新得数量与原表中得值相同, 不需要更新”e ls eif @quantit y_d iff_lin eitem 〈=q uantity_partsuppﻩbe ginﻩpuﻩﻩpd ate partsupps et avail qty= availqty-@qua ntit y_d iff_li ne item ﻩpus erehwﻩppkey=suppkeyandp artkey= @par tke yﻩﻩ p rint “ 两个表都更新成功’ ﻩﻩﻩneﻩndels eigebﻩﻩinﻩuﻩﻩupdate li nei temsetquantit y=quantity+ @quanti ty_diff_linei temwhe re o rd erke y=@orde rke y and li nenu mber= @liine numberﻩp ri nt '更新失败”ﻩﻩendﻩ e nd efﻩﻩetch ne xtfr om c urso r_i nsertedi nto @ord erke y, @partkey,s upp key, @linenumb er, @qt y_ ins ert edf etch ne xt from curso r_de leted into @qt y_d elet ed dneﻩndd eallocat e cur sor _i nserte ddealloc at e cursor_de le ted eend eend(2)在在 l ineite m表上定义一个 instea d of in sert 触发器, 当插入明细表中一条记录时时, 应先检查供应表par tsupp 得得 ava il qt y就是否足够qu anti ty 得数量c rea te t rig ger tri g_lineitem_q ua nti ty_ insertonline item iinstead of inser t as bbegin-—声明游标变量指向 inserte d表d eclar e cur sor_inserte dcur sorrea d_ only f orsﻩﻩselect or derk ey,pa rtkey,sup pk ey, lin en umber,q uantityf rom ins er ted-—声明变量获取查找信息dec lare quantity int, @av ailq ty i nt, @suppkeyin t, @partkeyinntt, @o rderkey int, @linenu mber int-—标游开打ﻩ打开游标c nepoﻩcurs or_ins erted -ﻩ-—读取游标f etc hnext fro mcursor_insert edint o@orde rkey,partkey,@@ssuppkey, @linenumber,qu antity wh ile @@FETCH_S TATUS= 0igebﻩin--为变量赋值a tcelesﻩavailqtyy==av ai lqt y fr ompartsuppwﻩwhe re suppkey =@su ppk ey and part ke y= partke yﻩ if @quant ity 〈= @avail qt y-—如果可以更新bﻩbegin /ﻩﻩﻩ/*将将 insert ed 表中得记录插入到明细表*/ ﻩsniﻩserti nto l ineite m select *from i nsertedro=yekredroerehwﻩﻩrderk ey andlinenumb er = @linenumber */ﻩﻩ新更时同ﻩ*同时更新 part supp 表得数量*/ﻩﻩuupd ate pa rtsup pset a vailqty=availqty-@quanti tyﻩerehwﻩe sup pke y= @sup pkey and partkey=part key ppr int ’paarts upp 表有足够得货物可以满足 lin eitem 得quan tityy, 插入成功’endelsebeginﻩﻩ p rintt''pa rt sup p表没有足够得货物可以满足 l ineitem 得得q uantity,插入失败’dneﻩfﻩﻩfetc h next from curso r_ins ert ed in to@ord erkey, partkey, suppkey, @liinenumbe r, qu antity eﻩen ddeall ocat ecursor_ inserted end(3)在在 line ite m表上定义一个 inste ad of del ete 触发器,当当删除明细表中记录时时, 同时改变表供应表 partsupp 得ava il qtyy数值 c re ate trigge r tri g_ lin eit em_quanti ty_ del ete o n line item inste ad of del ete as be gin—-声明游标变量指向deleted 表de cla re curs or_ del eted c ursorread _only forlesﻩﻩelect or derke y, par tkey,sup pkey, linenumbe r,quan tity fr om deleted -ﻩ--声明变量decl are s upp key i nt, par tke yint, @orde rkey int,linen umb erint, @quaant it y int—-标游开打ﻩ打开游标opencursor_deleted-—标游取读ﻩ读取游标ffetch next fr omcurs or _del et edin to @ord erkey , @partkey,s uppkeey, @lin en umber,q ua ntitywhi leFFET CH_ STATUS=0 igebﻩin*/*除删ﻩ*删除*/ﻩ de let e fromlineite m where linenu mber= line number and o rde rkey =ordder key*/新更时同ﻩ同时更新 pa rt supp 表得数量*/u pdateparts uppse tavailqt y=a vai lq ty+quant ityﻩ where s uppke y= @s uppkey a nd par tk ey= @p ar tkeyﻩ p rin t’ 删除成功, 并且已经把货物数量归还到 p ar tsup p里“ fﻩﻩfetc h ne xt fr omcurso r_del etedinto @ord er ke y,p ar tkey,@@suppkey, @lin enu mber,quanti ty neﻩnd edﻩea lloc ate cursor_ delete dend(4)验证 update 触发器--查瞧li neit em 得quan tit y select*fr omlin eit em whe re or derk ey =1830and li nenum ber=1;——查询partsupp 表得ava ilqt y se lect * from part supp w here suppk ey =(selectsupp key fr omlin eitem w here ord erkey=18 30)and partk ey=(s elec t part key from lin eite mwh er eorder key =18 30 a nd linenu mber=1)---更新数量过大——更新得值与原值相同---更新到+ + 2 00 数量, , 成功updateli neitem setquant ity=q uanti ty+ 200where order key=1830and lin en umber =1;--更新 +2 00成功后l ineite m得 quanti ty y 变化——更新+200 成功后par ts upp 表得a va ilqty 变化实验到此。
实验答案(四-五-六)参考答案

实验四简单查询和连接查询1. 简单查询实验用Transact-SQL语句表示下列操作,在“学生选课“数据库中实现其数据查询操作:(1) 查询数学系学生的学号和姓名。
select sno,snamefrom studentwhere dept='数学系';(2) 查询选修了课程的学生学号。
select distinct(sno)from sc;(3) 查询选修课程号为0101的学生学号和成绩,并要求对查询结果按成绩降序排列,如果成绩相同则按学号升序排列。
select distinct(sno),gradefrom scwhere cno='0101'order by grade desc,sno asc;(4) 查询选修课程号为0101的成绩在80-90 分之间的学生学号和成绩,并将成绩乘以系数0.8 输出。
select distinct(sno),grade*0.8 as'sore'from scwhere cno='0101'and grade between 80 and 90;(5) 查询数学系或计算机系姓张的学生的信息。
select*from studentwhere dept in('数学系','计算机系')and sname like'张%';(6) 查询缺少了成绩的学生的学号和课程号。
select sno,cnofrom scwhere grade is null;2. 连接查询实验用Transact-SQL语句表示,并在“学生选课”数据库中实现下列数据连接查询操作:(1) 查询每个学生的情况以及他(她)所选修的课程。
select student.*,amefrom student,sc,coursewhere student.sno=sc.sno and o=o;(2) 查询学生的学号、姓名、选修的课程名及成绩。
《数据库与信息系统》实验4指导解析_1-3
⚫ 格式 2:LIMIT 记录数 表示从第一条记录开始显示“记录数”的记录。如果“记录数”小于或者等于查询结果的记录
总数,那么将从第一条记录开始,显示指定条数的记录。如果“记录数”大于查询结果的记录总数, 数据库会直接显示查询出来的所有记录。
2
图 4.4 2009 年以后出版的少儿类图书(部分结果)
解析:Where 子句可以使用 year()函数从出版日期(PublishDate)字段中取得年份数据,其值为整型。 如果直接使用日期型的常量做比较,日期型常量要按照'年-月-日'完整结构来写,并且使用单引号括 起来,例如'2009-01-01 '。 参考语句:
要注意数据表名 Order 与关键字 Order 相同,在查询语句中可以使用完整的数据表名:
3
bookstore.`order`或者将数据库 bookstore 设置为当前数据库,然后在查询语句中使用单引号将 order 括起来。 参考语句: Select OrderCode, OrderTime, OrderStatus From bookstore.`orders` LIMIT 3; 或: USE bookstore; Select OrderCode, OrderTime, OrderStatus From `orders` LIMIT 3; (7)查询 TotalPrice 在 100~200 元之间的订单信息,部分结果如图 4.7 所示。
From Book;
(2)进行图书的价格汇总分析,分别显示图书的最高价、最低价、平均价、最高价与最低价的 差值,结果如图 4.11 所示。
图 4.11 图书的价格汇总分析
实验四__SQL数据操作
实验四 SQL数据操作目的:完成数据进行插入、删除、修改等操作。
体会数据完整性约束的作用,加深对数据完整性及其约束的理解。
实验内容及要求:1.准备工作:(在企业管理器中完成)(1) 将“教学数据库”附加的系统中,将教师提供的示例数据库(先将压缩文件解压)-----教学数据库中的.MDF文件导入到当前系统中。
(2)新建一个数据库,数据库命名为:C学号(如C0700001)(3)使用数据导入导出工具(DTC)将教学数据库中的数据导入到“C学号”数据库中。
(4)为“C学号”数据库中的各表设置实体完整性和参照完整性(采用方法任意)。
2. 使用T-SQL语句在查询分析器中完成数据的插入、更新和删除操作:(1)使用INSERT语句添加记录,分别为学生表中添加至少3条09级统计系(系号为16)的学生记录,其中一条记录是学生本人信息;为课程表中添加至少3条记录是学生本人的所学课程信息(应是原课程表中的没有的课程记录)。
(2)使用带有子查询的INSERT语句,为09级学生添加选课记录到选课表,要求选修的课程是课程表中的所有“必修”课程。
(3)使用UPDATE语句将C程序设计课程的学时改为比数据结构课程的学时少6学时。
(4)建一个重修课程表,将需要重修的信息添加的该表中。
(表结构为:课程号、学号,分数)使用命令任意。
(5)删除选修了课程而没有考试的选课记录。
(6)将为统计系的学生开设“VB程序设计”课程的选课信息填入选课表。
(7)删除学生李鹏飞的记录,注意与之相关信息的删除。
(注意级联关系的作用)(8)将信息系、物理系和统计系合并,改系科名称为“理学院”(系科号为28),其他与之相关的信息也要修改。
(本题所要修改的表为系科表、学生表)体会执行操作时检查实体完整性规则、参照完整性规则的效果。
在实验报告中写出以上题目2的命令和操作结果(可以使用select语句显示执行了数据操作语句以后的表中数据的变化,将结果放在每一题的后面,也可以使用抓图软件。
实验4 T-SQL语言
实验4 T-SQL语言实验目的:1、掌握Transact-SQL语言中的变量、运算符、函数;2、掌握Transact-SQL语句编写批处理和流程控制程序。
3、掌握游标的创建,打开,数据读取,关闭,销毁的基本方法实验内容和步骤:Transact-SQL语言是在SQL Server 2005 中使用的程序设计语言。
不但包含了标准SQL 语言部分,而且为了满足实际应用中的编程需要,在Transact-SQL语言中还另外增加了一些语言要素,它们包括注释、变量、运算符、函数和流程控制语句等。
所有的语句都在查询分析器中执行。
1. Transact-SQL语句实验内容1:返回在程序执行过程中上一条SQL语句影响的记录数(全局变量@@ROWCOUNT),注意观察结果窗口。
use teach--打开teach 数据库GO--查询学生表中的记录SELECT * FROM Student--查询上一条SELECT语句影响的记录数SELECT @@ROWCOUNT AS 第一次查询返回的记录数GO实验内容2:查询学生表的人数,将返回的记录数赋给局部变量@RowsUSE teachGODECLARE @Rows int--声明局部变量--给局部变量赋值SET @Rows=(SELECT COUNT(*) FROM Student)--显示局部变量的值SELECT @Rows AS SELECT返回的记录数GO实验内容3:设置一个局部变量为表,进行插入和查询操作-- 定义变量为临时表DECLARE @Table_Example TABLE(Col_num int PRIMARY KEY,Col_text char (50))-- 向该变量中添加两行内容INSERT INTO @Table_Example VALUES (1, 'this is a')INSERT INTO @Table_Example VALUES (2, 'table data type example')-- 从临时表中查询SELECT * FROM @Table_ExampleGO实验内容4:(练习,自己试着写一下)设置一个局部变量其值为选修了课程的人数,并显示提示:DECLARE@Rows int--声明局部变量--给局部变量赋值SET@Rows=(SELECT COUNT(distinct sno)FROM SC)--显示局部变量的值SELECT@Rows AS选课人数GO实验内容5:检查sysdatabases中的每一个数据库,使用数据库标识号来确定数据库名称。
实验4要求完成SQL语句
实验4要求完成SQL语句实验4要求完成SQL语句实验4 要求完成SQL语句实验目的(1)了解SQL Server数据库的逻辑结构和物理结构;(2)了解表的结构特点;(3)了解SQL Server的基本数据类型;(4)了解空值概念;(5)学会在企业管理器中创建数据库和表;(6)学会使用T-SQL语句创建数据库和表。
实验准备首先要明确,能够创建数据库的用户必须是系统管理员,或是被授权使用CREATE DA TABASE语句的用户。
其次创建数据库必须要确定数据库名、数据库大小(最初的大小、最大的大小、是否允许增长及增长方式)和存储数据库的文件。
然后,确定数据库包含哪些表,以及所包含的各表的结构,还要了解SQL Server的常用数据类型,以创建数据库的表。
此外还要了解两种常用的创建数据库、表的方法,即在企业管理器中创建和使用T-SQL 的CREATE DATABASE语句。
实验内容S SPJSNO SNAME ADDRESS TEL SNO PNO JNO QTYSl SN1 上海南京路68564345 S1 Pl J1 200P JPNO PNAME SPEC CITY COLOR JNO JNAME LEADER BGP1 PNl 8X8 无锡红J1 JN1 王总10S(SNO,SNAME,ADDRESS,TEL)其中,SNO:供应商代码SNAME:姓名ADDRESS:地址TEL:电话;J(JNO,JNAME,LEADER,BG)其中,JNO:工程代码JNAME:工程名LEADER:负责人BG:预算;P(PNO,PNAME,SPEC,CITY,COLOR)其中,PNO:零件代码PNAME:零件名SPEC:规格CITY:产地COLOR:颜色;SPJ(SNO,JNO,PNO,QTY)其中,SNO:供应商代码JNO:工程代码PNO:零件代码QTY:数量;(1) 为每个关系建立相应的表结构,添加若干记录。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
(2)修改数据
将张星老师数据结构课的学生成绩全部加2分。
use实验
update SC
set Grade = Grade + 2
where Cno in
(select Cno
from Course
where Cname ='数据结构'
);
(3)删除数据
删除马朝阳同学的所有选课记录。
use实验
delete
from SC
where Sno in
(select Sno
from Student
where Sname ='马朝阳'
);
2. 查询操作
(1)单表查询
查询所有学生的信息;
use实验
select*
from Student
查询所有女生的姓名;
use实验
select Sname
from Student
where Ssex ='女'
查询成绩在80 到89 分之间的所有学生选课记录,查询结果按成绩的降序排列;use实验
select*
from SC
where Grade between 80 and 89
order by Grade desc;
查询各个系的学生人数;
use实验
select Sdept,count(Sdept)count
from Student
group by Sdept;
(2)连接查询
查询信息系年龄在21 岁以下(含21 岁)的女生姓名及其年龄。
use实验
select Sname, Sage
from Student, Dept
where Dname ='信息'and Sage <= 21 and Dept.Deptno = Student.Sdept;
(3)嵌套查询
查询选课总学分在10 分以下的学生姓名;
use实验
select Sname
from Student
where Sno in
(select Sno
from Course, SC
where o = o
group by Sno
having sum(Credit)< 10
);
查询各门课程取得最高成绩的学生姓名及其成绩;
use实验
select Cno, Sname, Grade
from Student, SC
where o in
(select Cno
from SC
group by Cno
)and
Grade in
(select max(Grade)
from SC
group by Cno
)and
Student.Sno = SC.Sno
order by Cno;
查询选修了101 学生选修的全部课程的学生学号;
use实验
select Cno
from Course
where Ctno = 101;
use实验
select Sno
from SC
where Cno = 1
and Sno in
(select Sno
from SC
where Cno = 4);
查询选修了张星老师开设的全部课程的学生姓名。
use实验
select Cno
from Teacher, Course
where Tname ='张星'and Tno = Ctno;
use实验
select Sname
from SC, Student
where SC.Sno = Student.Sno
and Cno = 1
and SC.Sno in
(select Sno
from SC
where Cno = 4)。