上海大学数据库选修上机练习5

合集下载

上海大学数据库选修上机练习5

上海大学数据库选修上机练习5

《数据库系统与应用》上机习题*************************************************************************************************第五部分、SQL高级应用要求掌握:熟练掌握T-SQL语言,了解异常处理的相关语句,学会用游标方式对数据库进行操作。

一、做书上第十章的例题二、写出书上198页练习题10中第7、8、9、11题的结果,并上机验证。

完成第12、13、14题7、数据库中没有stud表8、9、重复插入ID的值11、12、编写一个程序,采用游标的方式输出所有课程的平均分use schoolgoset nocount ondeclare @s_cj int,@s_name char(8)declare c_cursor cursor forselect score、课程号,AVG(score、分数)from scoregroup by score、课程号order by score、课程号open c_cursorfetch next from c_cursor into @s_cj,@s_namewhile@@FETCH_STATUS=0beginprint CAST(@s_cj as char(8))+@s_namefetch next from c_cursor into @s_cj,@s_nameendclose c_cursordeallocate c_cursorgo13、编写一个程序,使用游标的方式输出所有学号,课程号,成绩等级use schooldeclare @s_xh int,@c_name char(8),@s_cj float,@dj char(1) declare c_cursor cursor forselect student、学号,score、课程号,score、分数from score,studentwhere score、学号=student、学号group by student、学号,score、课程号,score、分数order by student、学号beginset @dj=CASEwhen @s_cj>=90 then'A'when @s_cj>=80 then'B'when @s_cj>=70 then'C'when @s_cj>=60 then'D'else'E'endopen cfetch next from c_cursor into @s_xh,@c_name,@s_cjprint'学号课程号等级'print'---------------------------'while@@FETCH_STATUS=0beginprint @s_xh+' '+@c_name+' '+@s_cjfetch next from c_cursor into @s_xh,@c_name,@s_cjendclose c_cursordeallocate c_cursor14、编写一个程序,输出各班各课程的平均分use schoolgoset nocount ondeclare @s_cj int,@s_name char(8),@s_bj char(8)declare c_cursor cursor forselect student、班级,score、课程号,AVG(score、分数)from score,studentgroup by score、课程号,student、班级order by score、课程号,student、班级open c_cursorfetch next from c_cursor into @s_cj,@s_name,@s_bjprint'学号班级成绩'print'-------------------'while@@FETCH_STATUS=0beginprint CAST(@s_cj as char(8))+@s_name+@s_bjfetch next from c_cursor into @s_cj,@s_name,@s_bjendclose c_cursordeallocate c_cursorgo三、完成书上394页上机实验题3(1)对各出版社的图书比例情况进行分析,即图书比例高于50%为“很高”,图书比例高于30%为“'较高”,图书比例高于10%为“一般”。

数据库上机实验题目和答案

数据库上机实验题目和答案

试用SQL的查询语句表达下列查询:1.检索王丽同学所学课程的课程号和课程名。

select Cno ,Cname from c where Cno in(select cno from sc where sno in (select sno from s where sname='王丽' ))2.检索年龄大于23岁的男学生的学号和姓名。

select sno,sname from swhere sex='男' and age>233.检索‘c01’课程中一门课程的女学生姓名select sname from swhere sex='女' and sno in(select sno from sc where cno='c01')4.检索s01同学不学的课程的课程号。

select cno from cwhere cno not in (select cno from sc where sno ='s01')5.检索至少选修两门课程的学生学号。

select sc.sno from s,scwhere s.sno=sc.snogroup by sc.snohaving count(o)>=26.每个学生选修的课程门数。

解法一:select so.sno sno,ount,s.snamefrom(select sc.sno sno,count(sc.sno) ccountfrom sc,swhere s.sno=sc.snogroup by sc.sno ) so,swhere s.sno=so.sno解法二:select sc.sno sno,s.sname,count(sc.sno) ccountfrom sc,swhere s.sno=sc.snogroup by sc.sno,sname7.求选修C4课程的学生的平均分。

数据库系统基础教程第五章答案

数据库系统基础教程第五章答案

Exercise 5.1.1 As a set:Average = 2。

37 As a bag:Average = 2.48 Exercise 5.1.2Average = 218 As a bag:Average = 215 Exercise 5.1.3a As a set:18As a bag:bore1516141615151418Exercise 5。

1。

3bπbore(Ships Classes)Exercise 5.1.4aFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the union of bags R and S will have tuple t appear n + m times。

The further union of bag T with the tuple t appearing o times will have tuple t appear n + m + o times in the final result.On the right—hand side:Given bags S and T where a tuple t appears m and o times respectively, the union ofbags R and S will have tuple t appear m + o times. The further union of bag R with thetuple t appearing n times will have tuple t appear m + o + n times in the final result.For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set。

《数据库技术与应用》上机大作业

《数据库技术与应用》上机大作业

《数据库技术与应⽤》上机⼤作业数据库⼤作业1、查询XS表中的所有列。

use XSCJselect *from XS;2、查询XS表中计算机专业同学的学号、姓名和总学分。

use XSCJselect 学号,姓名,总学分from XSwhere 专业名= '计算机';3、查询XS表中计算机专业同学的学号、姓名和总学分,结果中各列的标题分别指定为number、name和mark。

use XSCJselect 学号as number , 姓名as name , 总学分as markfrom XSwhere 专业名= '计算机';4、查询XS表中通信⼯程专业总学分⼤于等于42的学⽣情况。

use XSCJselect *from XSwhere 专业名= '通信⼯程' and 总学分>= 42;5、查询XS表中姓“王”且单名的学⽣情况。

use XSCJselect *from XSwhere 姓名like '王_';6、查询XS表中不在1979年出⽣的学⽣情况。

use XSCJselect *from XSwhere 出⽣时间not between '1979-01-01' and '1979-12-31';7、查询选修了课程号为101的学⽣情况。

use XSCJselect *from XSwhere 学号in ( select 学号from XS_KCwhere 课程号= '101');8、查询未选修离散数学的学⽣情况。

use XSCJselect *from XSwhere 学号not in ( select 学号from XS_KC where 课程号in ( select 课程号from KC where 课程名= '离散数学'));9、查询⽐所有计算机系的学⽣年龄都⼤的学⽣情况。

上海大学数据库上机作业上机练习5作业

上海大学数据库上机作业上机练习5作业

上大学数据库上机作业《数据库系统与应用》上机习题*************************************************************************************************第五部分、SQL高级应用一、做书上第十章的例题二、利用上次上机的学生_课程数据库1. 求选修了高等数学的学生学号和姓名。

USE学生课程SELECT学生.学号,姓名,选课.课程号FROM学生,选课,课程WHERE学生.学号=选课.学号AND课程.课程号=选课.课程号AND课程名='高等数学'2.求C1课程的成绩高于张三的学生学号和成绩。

USE学生课程SELECT x.学号,x.成绩FROM选课x,选课yWHERE x.课程号='C1'AND x.成绩>y.成绩AND y.学号='S4'AND y.课程号='C1'ORDER BY x.学号DESC第二种:USE学生课程SELECT学号,成绩FROM选课WHERE课程号='C1'AND成绩>(SELECT成绩FROM选课,学生WHERE课程号='C1'AND姓名='张三'AND选课.学号=学生.学号)3.求其他系中比自动化学院某一学生年龄小的学生。

USE学生课程SELECT学号,姓名,年龄,单位FROM学生WHERE年龄<(SELECT MAX(年龄)FROM学生WHERE单位='自动化学院')AND单位!='自动化学院'ORDER BY学号DESC4.求其他系中比自动化学院学生年龄都小的学生。

USE学生课程SELECT学号,姓名,年龄,单位FROM学生WHERE年龄<(SELECT MIN(年龄)FROM学生WHERE单位='自动化学院')AND单位!='自动化学院'ORDER BY学号DESC5.求选修了C2课程的学生的姓名。

上海大学数据库上机作业上机练习4作业(最新整理)

上海大学数据库上机作业上机练习4作业(最新整理)

上大学数据库上机作业《数据库系统与应用》上机习题*************************************************************************************************第四部分、SQL查询━━嵌套和组合统计查询要求掌握:利用SQL查询语言表达嵌套查询语句以及数据查询中的统计计算和组合操作。

1、做书上第九章余下的例题,并完成书上练习题9中第11、12、13、14题11.if exists(SELECT*FROM sys.objects WHERE name=student)12.二、利用图书_读者数据库1. 求机械工业出版社出版的各类图书的平均价。

USE图书读者SELECT类别,AVG(定价)AS平均价FROM图书WHERE出版社='机械工业出版社'GROUP BY类别2. 求各类图书的最高价、最低价、图书的数量。

USE图书读者SELECT类别,MAX(定价)AS最高价,MIN(定价)AS最低价,COUNT(*)AS数量FROM图书GROUP BY类别3. 查找图书类别,要求类别中最高的图书定价不低于全部按类别分组的图书平均定价的1.5倍。

USE图书读者SELECT类别FROM图书WHERE定价=ALL(SELECT MAX(定价)FROM图书WHERE定价<=ALL(SELECT AVG(定价)*1.5FROM图书))4.计算机类和机械工业出版社出版的图书。

USE图书读者SELECT*FROM图书WHERE出版社='机械工业出版社'AND类别='计算机'5.查询所有读者借阅过的书,要求按读者姓名、书名来排序。

USE图书读者SELECT读者.编号,借阅.读者编号,姓名,书名FROM图书,读者,借阅WHERE读者.编号=借阅.读者编号AND借阅.书号=图书.书号ORDER BY姓名6. 查询所有在2008.11.15日以后被借阅过的图书名及借阅者。

(完整版)数据库系统基础教程第五章答案

(完整版)数据库系统基础教程第五章答案

Exercise 5.1.1 As a set:speed2.662.101.422.803.202.202.001.863.06 Average = 2.37 As a bag:speed2.662.101.422.803.203.202.202.202.002.801.862.803.06 Average = 2.48 Exercise 5.1.2 As a set:hd25080320200300160 Average = 218 As a bag:hd2502508025025032020025025030016016080 Average = 215 Exercise 5.1.3a As a set:bore15161418As a bag:bore1516141615151418Exercise 5.1.3bπbore(Ships Classes)Exercise 5.1.4aFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the union of bags R and S will have tuple t appear n + m times. The further union of bag T with the tuple t appearing o times will have tuple t appear n + m + o times in the final result.On the right-hand side:Given bags S and T where a tuple t appears m and o times respectively, the union of bags R and S will have tuple t appear m + o times. The further union of bag R with the tuple t appearing n times will have tuple t appear m + o + n times in the final result.For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set. The tuple t only appears in the result if all the sets have the tuple t. Otherwise, the tuple t will not appear in the result. Since we cannot have duplicates, the result only has at most one copy of the tuple t.Exercise 5.1.4bFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the intersectionof bags R and S will have tuple t appear min( n, m ) times. The further intersection of bag T with the tuple t appearing o times will produce tuple t min( o, min( n, m ) ) times in the final result.On the right-hand side:Given bags S and T where a tuple t appears m and o times respectively, the intersection of bags R and S will have tuple t appear min( m, o ) times. The further intersection of bag R with the tuple t appearing n times will produce tuple t min( n, min( m, o ) ) times in thefinal result.The intersection of bags R,S and T will yield a result where tuple t appears min( n,m,o ) times. For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set. The tuple t only appears in the result if all the sets have the tuple t. Otherwise, the tuple t will not appear in the result.Exercise 5.1.4cFor bags:On the left-hand side:Given that tuple r in R, which appears m times, can successfully join with tuple s in S,which appears n times, we expect the result to contain mn copies. Also given that tuple tin T, which appears o times, can successfully join with the joined tuples of r and s, weexpect the final result to have mno copies.On the right-hand side:Given that tuple s in S, which appears n times, can successfully join with tuple t in T,which appears o times, we expect the result to contain no copies. Also given that tuple rin R, which appears m times, can successfully join with the joined tuples of s and t, weexpect the final result to have nom copies.The order in which we perform the natural join does not matter for bags.For sets:This is a similar case when dealing with bags except the joined tuples can only appear at most once in each result. If there are tuples r,s,t in relations R,S,T that can successfully join, then the result will contain a tuple with the schema of their joined attributes.Exercise 5.1.4dFor bags:Suppose a tuple t occurs n and m times in bags R and S respectively. In the union of these two bags R ⋃ S, tuple t would appear n + m times. Likewise, in the union of these two bags S ⋃ R, tuple t would appear m + n times. Both sides of the relation yield the same result.For sets:A tuple t can only appear at most one time. Tuple t might appear each in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t appears in both sets R and S will the union R ⋃ S have the tuple t. The same reasoning holds when we take the union S ⋃ R.Therefore the commutative law for union holds.Exercise 5.1.4eFor bags:Suppose a tuple t occurs n and m times in bags R and S respectively. In the intersection of these two bags R ∩ S, tuple t would appear min( n,m ) times. Likewise in the intersection of these two bags S ∩ R, tuple t would appear min( m,n ) times. Both sides of the relation yield the same result.For sets:A tuple t can only appear at most one time. Tuple t might appear each in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t appears in at least one of the sets R and S will the intersection R ∩ S have the tuple t. The same reasoning holds when we take the intersection S ∩R.Therefore the commutative law for intersection holds.Exercise 5.1.4fFor bags:Suppose a tuple t occurs n times in bag R and tuple u occurs m times in bag S. Suppose also that the two tuples t,u can successfully join. Then in the natural join of these two bags R S, the joined tuple would appear nm times. Likewise in the natural join of these two bags S R, the joined tuple would appear mn times. Both sides of the relation yield the same result.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuples u,v might appear respectively in sets R and S one or zero times. The combinations of number of occurrences for tuples u,v in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple u exists in Rand tuple v exists in S will the natural join R S have the joined tuple. The same reasoning holds when we take the natural join S R.Therefore the commutative law for natural join holds.Exercise 5.1.4gFor bags:Suppose tuple t appears m times in R and n times in S. If we take the union of R and S first, we will get a relation where tuple t appears m + n times. Taking the projection of a list of attributes L will yield a resulting relation where the projected attributes from tuple t appear m + n times. If we take the projection of the attributes in list L first, then the projected attributes from tuple t would appear m times from R and n times from S. The union of these resulting relations would have the projected attributes of tuple t appear m + n times.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuple t might appear in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t exists in R or S (or both R and S) will the projected attributes of tuple t appear in the result.Therefore the law holds.Exercise 5.1.4hFor bags:Suppose tuple t appears u times in R, v times in S and w times in T. On the left hand side, the intersection of S and T would produce a result where tuple t would appear min(v , w) times. With the addition of the union of R, the overall result would have u + min(v , w) copies of tuple t. On the right hand side, we would get a result of min(u + v, u + w) copies of tuple t. The expressions on both the left and right sides are equivalent.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuple t might appear in sets R,S and T one or zero times. The combinations of number of occurrences for tuple t in R, S and T respectively are (0,0,0), (0,0,1), (0,1,0), (0,1,1), (1,0,0), (1,0,1), (1,1,0) and (1,1,1). Only when tuple t appears in R or in both S and T will the result have tuple t.Therefore the distributive law of union over intersection holds.Exercise 5.1.4iSuppose that in relation R, u tuples satisfy condition C and v tuples satisfy condition D. Suppose also that w tuples satisfy both conditions C and D where w≤ min(v , w). Then the left hand side will return those w tuples. On the right hand side, σC(R) produces u tuples and σD(R) produces v tuples. However, we know the intersection will produce the same w tuples in the result.When considering bags and sets, the only difference is bags allow duplicate tuples while sets only allow one copy of the tuple. The example above applies to both cases.Therefore the law holds.Exercise 5.1.5aFor sets, an arbitrary tuple t appears on the left hand side if it appears in both R,S and not in T. The same is true for the right hand side.As an example for bags, suppose that tuple t appears one time each in both R,T and two times in S. The result of the left hand side would have zero copies of tuple t while the right hand side would have one copy of tuple t.Therefore the law holds for sets but not for bags.Exercise 5.1.5bFor sets, an arbitrary tuple t appears on the left hand side if it appears in R and either S or T. This is equivalent to saying tuple t only appears when it is in at least R and S or in R and T. The equivalence is exactly the right side’s expression.As an example for bags, suppose that tuple t appears one time in R and two times each in S and T. Then the left hand side would have one copy of tuple t in the result while the right hand side would have two copies of tuple t.Therefore the law holds for sets but not for bags.Exercise 5.1.5cFor sets, an arbitrary tuple t appears on the left hand side if it satisfies condition C, condition D or both condition C and D. On the right hand side, σC(R) selects those tuples that satisfy condition C while σD(R) selects those tuples that satisfy condition D. However, the union operator will eliminate duplicate tuples, namely those tuples that satisfy both condition C and D. Thus we are ensured that both sides are equivalent.As an example for bags, we only need to look at the union operator. If there are indeed tuples that satisfy both conditions C and D, then the right hand side will contain duplicate copies of those tuples. The left hand side, however, will only have one copy for each tuple of the original set of tuples.A+B A2B210154910164167916 Exercise 5.2.1bB+1C-1103334431143 Exercise 5.2.1cA B0101232434 Exercise 5.2.1dB C010224253434A B01232434 Exercise 5.2.1fB C0124253402 Exercise 5.2.1gA SUM(B)022734 Exercise 5.2.1hB AVG(C)0 1.52 4.534 Exercise 5.2.1iA23Exercise 5.2.1jA MAX(C)24 Exercise 5.2.1kA B C23423401┴01┴24┴34┴Exercise 5.2.1lA B C234234┴01┴24┴25┴02 Exercise 5.2.1mA B C23423401┴01┴24┴34┴┴01┴24┴25┴02Exercise 5.2.1nA R.B S.B C0124012501340134012401250134013423┴┴24┴┴34┴┴┴┴01┴┴02Exercise 5.2.2aApplying the δ operator on a relation with no duplicates will yield the same relation. Thus δ is idempotent.Exercise 5.2.2bThe result of πL is a relation over the list of attributes L. Performing the projection again will return the same relation because the relation only contains the list of attributes L. Thus πL is idempotent.Exercise 5.2.2cThe result of σC is a relation where condition C is satisfied by every tuple. Performing the selection again will return the same relation because the relation only contains tuples that satisfy the condition C. Thus σC is idempotent.Exercise 5.2.2dThe result of γL is a relation whose schema consists of the grouping attributes and the aggregated attributes. If we perform the same grouping operation, there is no guarantee that the expression would make sense. The grouping attributes will still appear in the new result. However, the aggregated attributes may or may not appear correctly. If the aggregated attribute is given a different name than the original attribute, then performing γL would not make sense because it contains an aggregation for an attribute name that does not exist. In this case, the resultingrelation would, according to the definition, only contain the grouping attributes. Thus, γL is not idempotent.Exercise 5.2.2eThe result of τ is a sorted list of tuples based on some attributes L. If L is not the entire schema of relation R, then there are attributes that are not sorted on. If in relation R there are two tuples that agree in all attributes L and disagree in some of the remaining attributes not in L, then it is arbitrary as to which order these two tuples appear in the result. Thus, performing the operation τmultiple times can yield a different relation where these two tuples are swapped. Thus, τ is not idempotent.Exercise 5.2.3If we only consider sets, then it is possible. We can take πA(R) and do a product with itself. From this product, we take the tuples where the two columns are equal to each other.If we consider bags as well, then it is not possible. Take the case where we have the two tuples (1,0) and (1,0). We wish to produce a relation that contains tuples (1,1) and (1,1). If we use the classical operations of relational algebra, we can either get a result where there are no tuples or four copies of the tuple (1,1). It is not possible to get the desired relation because no operation can distinguish between the original tuples and the duplicated tuples. Thus it is not possible to get the relation with the two tuples (1,1) and (1,1).Exercise 5.3.1a)Answer(model) ← PC(model,speed,_,_,_) AND speed ≥ 3.00b)Answer(maker) ← Laptop(model,_,_,hd,_,_) AND Product(maker,model,_) AND hd ≥100c)Answer(model,price) ← PC(model,_,_,_,price) AND Product(maker,model,_) ANDmaker=’B’Answer(model,price) ← Laptop(model,_,_,_,_,price) AND Product(maker,model,_)AND maker=’B’Answer(model,price) ← Printer(model,_,_,price) AND Product(maker,model,_) ANDmaker=’B’d)Answer(model) ← Printer(model,color,type,_) AND color=’true’ AND type=’laser’e)PCMaker(maker) ← Product(maker,_,type) AND type=’pc’LaptopMaker(maker) ← Product(maker,_,type) AND type=’laptop’Answer(maker) ← LaptopMaker(maker) AND NOT PCMaker(maker)f)Answer(hd) ← PC(model1,_,_,hd,_) AND PC(model2,_,_,hd,_) AND model1 <>model2g)Answer(model1,model2) ← PC(model1,speed, ram,_,_) ANDPC(model2,_speed,ram,_,_) AND model1 < model2h)FastComputer(model) ← PC(model,speed,_,_,_) AND speed ≥ 2.80FastComputer(model) ← Laptop(model,speed,_,_,_,_) AND speed ≥ 2.80Answer(maker) ← Product(maker,model1,_) AND Product(maker,model2,_) ANDFastComputer(model1) AND FastComputer(model2) AND model1 <> model2i)Computers(model,speed) ← PC(model,speed,_,_,_)Computers(model,speed) ← Laptop(model,speed,_,_,_,_)SlowComputers(model) ← Computers(model,speed) AND Computers(model1,speed1) AND speed < speed1FastestComputers(model) ← Computers(model,_) AND NOT SlowComputers(model)Answer(maker) ← FastestComputers(model) AND Product(maker,model,_) j)PCs(maker,speed) ← PC(model,speed,_,_,_) AND Product(maker,model,_) Answer(maker) ← PCs(maker,speed) AND PCs(maker,speed1) ANDPCs(maker,speed2) AND speed <> speed1 AND speed <> speed2 AND speed1 <>speed2k)PCs(maker,model) ← Product(maker,model,type) AND type=’pc’Answer(maker) ← PCs(maker,model) AND PCs(maker,model1) ANDPCs(maker,model2) AND PCs(maker,model3) AND model <> model1 AND model <> model2 AND model1 <> model2 AND (model3 = model OR model3 = model1 ORmodel3 = model2)Exercise 5.3.2a)Answer(class,country) ← Classes(class,_,country,_,bore,_) AND bore ≥ 16b)Answer(name) ← Ships(name,_,launched) AND launched < 1921c)Answer(ship) ← Outcomes(ship,battle,result) AND battle=’Denmark Strait’ AND result= ‘sunk’d)Answer(name) ← Classes(class,_,_,_,_,displacement) AND Ships(name,class,launched)AND displacement > 35000 AND launched > 1921e)Answer(name,displacement,numGuns) ← Classes(class,_,_,numGuns,_,displacement)AND Ships(name,class,_) AND Outcomes (ship,battle,_) AND battle=’Guadalcanal’AND ship=namef)Answer(name) ← Ships(name,_,_)Answer(name) ← Outcomes(name,_,_) AND NOT Answer(name)g)MoreThanOne(class) ← Ships(name,class,_) AND Ships(name1,class,_) AND name <>name1Answer(class) ← Classes(class,_,_,_,_,_) AND NOT MoreThanOne(class)h)Battleship(country) ← Classes(_,type,country,_,_,_) AND type=’bb’Battlecruiser(country) ← Classes(_,type,country,_,_,_) AND type=’bc’Answer(country) ← Battleship(country) AND Battlecruiser(country)i)Results(ship,result,date) ← Battles(name,date) AND Outcomes(ship,battle,result) ANDbattle=nameAnswer(ship) ← Results(ship,result,date) AND Results(ship,_,date1) ANDresult=’damaged’ AND date < date1Exercise 5.3.3Answer(x,y) ← R(x,y) AND z = zExercise 5.4.1aAnswer(a,b,c) ← R(a,b,c)Answer(a,b,c) ← S(a,b,c)Exercise 5.4.1bAnswer(a,b,c) ← R(a,b,c) AND S(a,b,c)Exercise 5.4.1cAnswer(a,b,c) ← R(a,b,c) AND NOT S(a,b,c)Exercise 5.4.1dUnion(a,b,c) ← R(a,b,c)Union(a,b,c) ← S(a,b,c)Answer(a,b,c) ← Union(a,b,c) AND NOT T(a,b,c)Exercise 5.4.1eJ(a,b,c) ← R(a,b,c) AND NOT S(a,b,c)K(a,b,c) ← R(,a,b,c) AND NOT T(a,b,c)Answer(a,b,c) ← J(a,b,c) AND K(a,b,c)Exercise 5.4.1fAnswer(a,b) ← R(a,b,_)Exercise 5.4.1gJ(a,b) ← R(a,b,_)K(a,b) ← S(_,a,b)Answer(a,b) ← J(a,b) AND K(a,b)Exercise 5.4.2aAnswer(x,y,z) ← R(x,y,z) AND x = yExercise 5.4.2bAnswer(x,y,z) ← R(x,y,z) AND x < y AND y < z Exercise 5.4.2cAnswer(x,y,z) ← R(x,y,z) AND x < yAnswer(x,y,z) ← R(x,y,z) AND y < zExercise 5.4.2dChange:NOT(x < y OR x > y)To:x ≥ y AND x ≤ yThe above simplifies to x = yAnswer(x,y,z) ← R(x,y,z) AND x = yExercise 5.4.2eChange:NOT((x < y OR x > y) AND y < z)NOT(x < y OR x > y) OR y ≥ z(x ≥ y AND x ≤ y) OR y ≥ zTo:x = y OR y ≥ zAnswer(x,y,z) ← R(x,y,z) AND x = yAnswer(x,y,z) ← R(x,y,z) AND y ≥ zExercise 5.4.2fChange:NOT((x < y OR x < z) AND y < z)NOT(x < y OR x < z) OR y ≥ z To:(x ≥ y AND x ≥ z) OR y ≥ zAnswer(x,y,z) ← R(x,y,z) AND x ≥ y AND x ≥ zAnswer(x,y,z) ← R(x,y,z) AND y ≥zExercise 5.4.3aAnswer(a,b,c,d) ← R(a,b,c) AND S(b,c,d)Exercise 5.4.3bAnswer(b,c,d,e) ← S(b,c,d) AND T(d,e)Exercise 5.4.3cAnswer(a,b,c,d,e) ← R(a,b,c) AND S(b,c,d) AND T(d,e)Exercise 5.4.4a)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = syb)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx < sy AND ry < szc)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx < syAnswer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry < szd)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = sye)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = syAnswer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry ≥ szf)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx ≥ sy AND rx ≥ szAnswer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry ≥ szExercise 5.4.5aR1 := πx,y(Q R)Exercise 5.4.5bR1 := ρR1(x,z)(Q)R2 := ρR2(z,y)(Q)R3 := πx,y(R1 (R1.z = R2.z) R2)Exercise 5.4.5cR1 := πx,y(Q R)R2 := σx < y(R1)。

数据库上机习题及答案

数据库上机习题及答案

数据库上机习题及答案 Revised by Liu Jing on January 12, 2021数据库及应用复习题一、设计题有一个[学生课程]数据库,数据库中包括三个表:学生表Student由学号(Sno)、姓名(Sname)、性别(Ssex)、年龄(Sage)、所在系(Sdept)五个属性组成,记为:Student(Sno,Sname,Ssex,Sage,Sdept) ,Sno 为关键字。

课程表Course由课程号(Cno)、课程名(Cname)、先修课号(Cpno)、学分(Ccredit)四个属性组成,记为:Course(Cno,Cname,Cpno,Ccredit) Cno为关键字。

成绩表SG由学号(Sno)、课程号(Cno)、成绩(Grade)三个属性组成,记为: SG(Sno,Cno,Grade) (SNO, CNO)为关键字。

用SQL语言实现下列功能:1.建立学生表Student,其中学号属性不能为空,并且其值是唯一的。

2.向Student表增加“入学时间(Scome)”列,其数据类型为日期型。

3.查询选修了3号课程的学生的学号及其成绩,查询结果按分数的降序排列。

4.查询学习1号课程的学生最高分数、平均成绩。

5.查询与“李洋”在同一个系学习的学生。

6.将计算机系全体学生的成绩置零。

7.删除学号为05019的学生记录。

8.删除计算机系所有学生的成绩记录。

1.CREATETABLE Student(Sno CHAR(5) NOT NULL UNIQUE,Sname CHAR(20),Ssex CHAR(2),Sage INT,Sdept CHAR(15))2.ALTER TABLE Student ADD Scome DATETIME 3.SELECT Sno, GradeFROM SGWHERE Cno='3'ORDER BY Grade DESC4.SELECT MAX(Grade), AVG(Grade)FROM SCWHERE Cno='1'5.SELECT Sno, Sname, SdeptFROM StudentWHERE Sdept IN(SELECT Sdept FROM StudentWHERE Sname='李洋')6. UPDATE SGSET Grade=0WHERE Sno in( SELECT Sno FROM StudentWHERE Sdept = '计算机系')7.DELETE FROM StudentWHERE Sno='05019'8. DELETE FROM SGWHERE Sno in( SELECT Sno FROM StudentWHERE Sdept = '计算机系')二、设计题现有关系数据库如下:数据库名:教师数据库教师表(编号 char(6),姓名,性别,民族,职称,身份证号)课程表(课号 char(6),名称)任课表(ID,教师编号,课号,课时数)用SQL语言实现下列功能的sql语句代码:1. 创建上述三表的建库、建表代码(14分);要求使用:主键(教师表.编号,课程表.课号)、外键(任课表.教师编号,任课表.课号)、默认(民族)、非空(民族,姓名)、唯一(身份证号)、检查(性别、课时数),自动编号(ID)2. 将下列课程信息添加到课程表的代码(6分)课号课程名称100001 SQL Server数据库100002 数据结构100003 VB程序设计修改课号为100003的课程名称:Visual Basic程序设计删除课号为100003的课程信息3. 写出创建[任课表视图](教师编号,姓名,课号,课程名称,课时数)的代码;(4分)4. 写出创建[某门课任课教师]内嵌表值函数以及检索的代码;(6分)检索:所有代'SQL Server数据库'这门课程的老师姓名;5. 写出创建[统计课时数]:输出最大课时数、最低课时数、平均课时的存储过程以及执行代码;(6分)6.写出创建:计算某教师代课总课时,并将值返回的存储过程以及执行代码。

数据库课后题参考答案

数据库课后题参考答案

学校有若干个系,每个系有各自的系号、系名和系主任;每个系有若干名教师和学生,教师有教师号、教师名和职称属性,每个教师可以担任若干门课程,一门课程只能由一位教师讲授,课程有课程号、课程名和学分,并参加多项项目,一个项目有多人合作,且责任轻重有个排名,项目有项目号、名称和负责人;学生有学号、姓名、年龄、性别,每个学生可以同时选修多门课程,选修有分数。

(1)请设计此学校的教学管理的E-R 模型。

(2)将E-R 模型转换为关系模型。

2)系 (系号,系名,系主任)教师 (教师号,教师名,职称,系号) 学生(学号,姓名,年龄,性别,系号) 项目(项目号,名称,负责人)课程(课号,课程名,学分,教师号) 选修(课号,学号,分数) 负责(教师号,项目号,排名)3、设有下图所示的医院组织。

试画出其E-R 图及关系模式并用关系代数方法写出下面之查询公式:病房医生 病人图3-1 某医院人员组织关系框图 编号 名称所在位置主任姓名编号姓名患何种病 病房号编号 姓名年龄职称管辖病房号① 找出外科病房所有医生姓名;② 找出管辖13号病房的主任姓名; ③ 找出管辖病员李维德的医生姓名。

关系模式:病房(编号、名称、所在位置、主任姓名) 医生(编号、姓名、年龄、职称、管辖病房号) 病人(病号、姓名、患何种病、病房号)答案:①、''(外科位置医生姓名=∏σ) ②、'13'(=∏病房号主任姓名σ(病房))③、''(李维德病人姓名医生姓名=∏σ)第三章 习题一、单项选择题1、如果要在Windows 平台上运行DB2应用程序访问运行在UNIX 上的数据库服务器,需要在Windows 上安装那种产品?( )A DB2 企业服务器版B DB2 个人版C DB2 连接器D DB2 运行时客户端 答案:D2、下面那种产品不允许远程的客户端应用程序连接到该服务器?( ) A DB2 快速版 B DB2 个人版C DB2 企业服务器版D DB2 工作组服务器版 答案:B 3、一个软件公司要开发一个需要访问DB2 for Linux 和DB2 for z/OS 的应用程序。

(完整版)上海大学数据库上机作业上机练习2作业(1)

(完整版)上海大学数据库上机作业上机练习2作业(1)

上大学数据库上机作业《数据库系统与应用》上机习题*************************************************************************************************第二部分、SQL查询━━单表查询二、使用购进凭证数据库。

(由老师提供复制,内含“商品信息表”和“购进凭证表”)⒈只显示购进凭证表中凭证号、单价、数量;并输出一个计算字段“金额”,计算金额的公式是:单价×数量。

(注:不是增加字段)USE 购进凭证SELECT 凭证号,单价,数量,(单价*数量)AS '金额'FROM 购进凭证表⒉加入筛选条件:只输出“饼干”大类商品。

USE 购进凭证SELECT 商品编号,品名,大类编号,大类名FROM 商品信息表WHERE 大类名='饼干'⒊除了“饼干”,再同时输出“酒类”商品;再同时输出“饮料”、“糖果”商品。

USE 购进凭证SELECT *FROM 商品信息表WHERE 大类名in('饼干','酒类','饮料','糖果')ORDER BY 大类编号ASC⒋按大类名排序、同一大类的单价从大到小排序。

USE 购进凭证SELECT 凭证号,商品编号,单价,数量,大类名,部门名称FROM 购进凭证表ORDER BY 大类名,单价DESC⒌清除原筛选条件,重设条件:只输出单价不在10至30元之间的酒类商品。

USE 购进凭证SELECT 凭证号,商品编号,单价,,量,大类名,部门名称FROM 购进凭证表WHERE 大类名='酒类', 单价not between 10 and 30⒍统计所有商品的总数量、总金额。

USE 购进凭证SELECT SUM(数量)AS'总数量',SUM(单价*数量)AS 总金额FROM 购进凭证表⒎统计饼干大类的总数量、总金额。

数据库习题及答案(五套)

数据库习题及答案(五套)

《数据库》课程模拟试题(一)一、填空题(30分)1.数据的独立性包括数据的物理独立性和数据的逻辑独立性。

2.数据的物理独立性是指当数据的存储结构(或内模式)改变时,通过系统内部的自动映象功能或转换功能,保持了数据的全局逻辑结构(或模式)不变。

3.数据模型由三部分组成:数据结构数据操作完整性约束4.一个学生可以同时借阅多本图书,一本图书只能由一个学生借阅,学生和图书之间为1:n(一对多)的联系。

5.一个关系模型由若干个关系模式组成。

6.在选择运算所得到的结果关系中,所含的元组数不多于原关系中的元组数。

7.SQL语言具有对数据的定义查询操纵控制等四个方面的功能。

8.设X→Y是关系模式R的一个函数依赖,并且Y是X的子集,则称X→Y是平凡函数依赖。

9.在一个关系模式中,若一个属性或属性组K完全函数决定整个元组,则称K为该关系的一个候选关键字。

10.如果一个关系R中的所有非主属性都不传递依赖于任何候选关键字,则称关系R属于第三范式,记作R∈3NF。

11.一个关系模式为Y(X1,X2,X3,X4),假定该关系存在如下函数依赖:X1←→X2,X1→X3,X1→X4,则该关系属于BCNF。

12.假定一个E-R图包含有A实体和B实体,并且从A到B存在着1∶n的联系,则转换成关系模型后,包含有3个关系模式。

13.实现系统案例,防止非法破坏数据,所采用的数据保护措施有:用户标识和鉴定、存取控制定义视图审计数据加密14.恢复的实现技术有:数据转储和登录日志文件。

13、15.____分布式数据库___________是一个逻辑上统一、地域上分布的数据集合。

16.关系中能唯一标识元组,且又不含多余属性称为___候选键_____________。

17.在概念结构设计中先,定义全局概念结构的框架,然后逐步细化。

这种方法称为_____________自顶向下方法___________。

18.分布式数据库系统中透明性层次越高,应用程序的编写越简单。

数据库第五章课后习题答案

数据库第五章课后习题答案

数据库第五章课后习题答案关系规范化理论题⽬4.20 设关系模式R(ABC),F是R上成⽴的FD集,F={B→A,C→A },ρ={AB,BC }是R上的⼀个分解,那么分解ρ是否保持FD集F?并说明理由。

答:已知F={ B→A,C→A },⽽πAB(F)={ B→A },πBC(F)=φ,显然,分解ρ丢失了FD C→A。

4.21 设关系模式R(ABC),F是R上成⽴的FD集,F={B→C,C→A },那么分解ρ={AB,AC }相对于F,是否⽆损分解和保持FD?并说明理由。

答:①已知F={ B→C,C→A },⽽πAB(F)=φ,πAC(F)={ C→A }显然,这个分解丢失了FD B→C②⽤测试过程可以知道,ρ相对于F是损失分解。

4.22 设关系模式R(ABCD),F是R上成⽴的FD集,F={A→B,B→C,A→D,D→C },ρ={AB,AC,BD }是R的⼀个分解。

①相对于F,ρ是⽆损分解吗?为什么?②试求F在ρ的每个模式上的投影。

③ρ保持F吗?为什么?答:①⽤测试过程可以知道,ρ相对于F是损失分解。

②πAB(F)={ A→B },πAC(F)={ A→C },πBD(F)=φ。

③显然,分解ρ不保持FD集F,丢失了B→C、A→D和D→C等三个FD。

4.23设关系模式R(ABCD),R上的FD集F={A→C,D→C,BD→A},试说明ρ={AB,ACD,BCD }相对于F是损失分解的理由。

答:据已知的F集,不可能把初始表格修改为有⼀个全a⾏的表格,因此ρ相对于F是损失分解。

4.24 设关系模式R(ABCD)上FD集为F,并且F={A→B,B→C,D→B}。

① R分解成ρ={ACD,BD},试求F在ACD和BD上的投影。

② ACD和BD是BCNF吗?如不是,望分解成BCNF。

解:① F在模式ACD上的投影为{A→C,D→C},F在模式BD上的投影为{D→B}。

②由于模式ACD的关键码是AD,因此显然模式ACD不是BCNF。

数据库实验5答案

数据库实验5答案

实验五:数据库综合查询一、实验目的1.掌握SELECT语句的基本语法和查询条件表示方法;2.掌握查询条件种类和表示方法;3.掌握连接查询的表示及使用;4.掌握嵌套查询的表示及使用;5.了解集合查询的表示及使用。

二、实验环境已安装SQL Server企业版的计算机(120台);具有局域网环境,有固定IP;三、实验学时2学时四、实验要求1.了解SELECT语句的基本语法格式和执行方法;2.了解连接查询的表示及使用;3.了解嵌套查询的表示及使用;4.了解集合查询的表示及使用;5.完成实验报告;五、实验内容及步骤1.利用Transact-SQL嵌套语句实现下列数据查询操作。

1) 查询选修了计算机体系结构的学生的基本信息。

select*from studentwhere Sno in(select Sno from coursewhere Cno in(select Cno from sc where Cname='计算机体系结构'))2) 查询年龄比李勇小的学生的学号和成绩。

select a.sno,grade from student a,coursewhere a.sno=course.sno and sage<(select sage from student where sname='李勇')3) 查询其他系中比系编号为‘D1’的学生中年龄最小者要大的学生的信息。

select*from student where dnum<>'D1'AND SAGE>(select min(sage)from student where dnum='D1')4) 查询其他系中比系编号为‘D3’的学生年龄都大的学生的姓名。

select*from student where dnum<>'D3'AND SAGE>all(selectsage from student where dnum='D3')5) 查询‘C1’课程的成绩高于70的学生姓名。

数据库第5版书本作业全部答案

数据库第5版书本作业全部答案

第一章:1、数据库的概念:P4数据库系统的概念: P59、数据模型的三个要素:数据结构,数据操作,完整性约束。

13、码:唯一标识实体的属性集。

16、模式:P29外模式:P29内模式:P2917、物理独立性:当数据库的存储结构改变时,对模式/内模式映象作相应改变,可以使模式保持不变,从而应用程序也不必改变,保证了数据与程序的物理独立性。

逻辑独立性:当数据库的模式改变时,对外模式/模式的映象作相应改变,可以使外模式保持不变,从而应用程序也不必改变,保证了数据与程序的逻辑独立性。

18、数据库系统的构成:数据库系统通常由数据库,数据库管理系统(及开发工具)、应用系统和数据库管理员构成。

第二章:1、关系模型的三个组成部分:关系数据结构、关系操作集合、关系完整性约束。

2、关系数据语言的分类:关系代数语言,关系演算语言,具有关系代数和关系演算双重特点的语言3、候选码:关系中能唯一标识一个元组的属性组。

主码:若候选码有多个,则选其中一个作为主码。

外码:关系模式R中属性或属性组X并非R的码,但X是另一个关系模式的码,则称X是R的外码。

(或者参照课本P50,定义2.5)5、答:实体完整性是指在基本表中,主属性不能取空值且取值唯一。

参照完整性是指在基本表中,外码可以是空值或者另一个关系主码的有效值。

6、(1)πSno(σJno=’J1’(SPJ))(2) πSno(σJno=’J1’∧ Pno=’P1’(SPJ))(3) πSno(σJno=’J1’∧ Color=’红’(SPJ∞P))(4)πJno(J)—πJno (σCity=’天津’∧ Color=’红’(S∞SPJ∞P)) (5)πJno,Pno(SPJ)÷πPno(σSno=’S1’(SPJ))第三章:4、建立S表Create table S(SNO CHAR(10) PRIMARY KEY,SNAME CHAR(10),STATUS CHAR(2),CITY CHAR(10));5、(1)select sname,cityFrom S;(2)select pname,color,weightFrom p;(3) select JnoFrom SPJWhere SNO=’S1’;(4)select p.pname,spj.qtyFrom p,spjWhere p.pno=spj.pno and spj.jno=’j2’;(5) select distinct pnoFrom spj,sWhere spj.sno=s.sno and city=’上海’;(6) select jnameFrom j,spj,sWhere j.jno=spj.jno and spj.sno=s.sno and s.city=’上海’;(7) select jnoFrom jWhere jno not in(select spj.jnoFrom spj,sWhere spj.sno=s.sno and s.city=’天津’);或者:select jnoFrom jWhere not exists(select spj.jnoFrom spj,sWhere spj.jno=j.jno and spj.sno=s.sno and s.city=’天津’);(8) update pSet color=’蓝’Where color=’红’;(9) update spjSet sno=’s3’Where sno=’s5’ and jno=’j4’ and pno=’p6’;(10) deleteFrom spjWhere sno=’s2’;deleteFrom sWhere sno=’s2’;(11)insert into spjValues(‘s2’,’j6’,’p4’,200)8、不是所有的视图都可以更新。

数据库上机实验部分题目及答案

数据库上机实验部分题目及答案

上机实验部分题目及答案一、实验目的1、基本表的定义、修改和删除2、视图的建立、删除和查询3、约束的命名、删除和重定义二、实验结果存放创建表homework8(result ),将每题的题目序号和SQL语句写到此表。

三、实验内容现有图书管理数据库的一个关系模式:book(总编号,分类号,书名,作者,出版单位,单价)1.利用SQL语句创建book的表结构,其中总编号为主码,书名的类型为char(50),并给主码的约束命名(主码约束命名参照第五章内容)。

2.利用SQL在book这个表中分别插入以下所给元组:34、为“数据库导论”设置“出版日期”的值为2009年6月5日,为“计算机基础”设置“出版日期”的值为2008年3月4日。

5、删除总编号为445503的元组。

6、删除列“出版日期”。

7、将列“书名”的类型改为char(100),其中修改列类型的语句为:alter table <表名>[modify 列名数据类型]8、删除book的主码约束(参照第五章)9、将表book中的总编码设置为主码(参照87页内容)。

11.建立表book1,其表结构与内容参照第2题。

12. 为表book1建立科学出版社所出图书的视图science13、删除表book1,其中删除表的格式为:drop table <表名>[restrict|cascade constraint] 其中restrict与cascade参数的含义参照87页14、为表book建立高等教育出版社所出图书的视图education15、查询视图education中的所有内容16、删除视图educationdrop table homework8drop table bookcreate table homework8(ti smallint,res char(400))create table book(总编码char(8) ,分类号char(7),书名char(50),作者char(30),出版单位char(200),单价char(8),constraint total primary key(总编码) )insertinto homework8(ti,res)values (1,'create table book(总编码char(8) ,分类号char(7),书名char(50),作者char(30),出版单位char(200),单价char(8),constraint total primary key(总编码) )')22222222insertinto book(总编码,分类号,书名,作者,出版单位,单价)values ('445501','TP3/12','数据库导论','王强','科学出版社','17.90')insertinto book(总编码,分类号,书名,作者,出版单位,单价)values ('445502','TP3/12','数据库导论','王强','科学出版社','17.90')insertinto book(总编码,分类号,书名,作者,出版单位,单价)values ('445503','TP3/12','数据库导论','王强','科学出版社','17.90')insertinto book(总编码,分类号,书名,作者,出版单位,单价)values ('332211','TP5/10','计算机基础','李伟','高等教育出版社','18.00')3333333alter table bookadd 出版日期char(20)alter table bookadd 页数char(5)444444444444444444444444444444444444444update bookset 出版日期='2009年6月5日'where 书名='数据库导论'update bookset 出版日期='2008年3月4日'where 书名='计算机基础'55555555555555555555555555555555555555 deletefrom bookwhere 总编号='445503'66666666666666666666666666666666666666 alter table bookdrop column 出版日期777777777777777777777777777777777777777 alter table bookmodify 书名char(100)8888888888888888888888888888888888888 alter table bookdrop constraint total9999999999999999999999999999999999999 alter table bookadd primary key (总编码)11 11 11 11 11 11 11 11 11 11 create table book1(总编码char(8) ,分类号char(7),书名char(50),作者char(30),出版单位char(200),单价char(8),constraint total primary key(总编码))12 12 12 12 12 12 12 12 CREATE VIEW scienceASSELECT 总编码,分类号,书名,作者,出版单位,单价FROM book1WHERE 出版单位='科学出版社'13DROP TABLE book114CREATE VIEW educationASSELECT 总编码,分类号,书名,作者,出版单位,单价FROM bookWHERE 出版单位='高等教育出版社'15SELECT *FROM education16DROP VIEW education。

数据库上机实验报告答案

数据库上机实验报告答案

数据库上机实验报告答案1.建立学生数据库模式学生表:student (sno 学号,sname 姓名,ssex 性别,sage 年龄,sdept 所在系)其中:sno 长度为4的字符串,为主码;sname 长度为8的字符串;ssex 长度为2的字符串,其值只取男、女;sage 整数,其值在0-150之间;sdept 长度为10的字符串。

2.建立课程数据库模式课程表:course ( cno课程号,cname课程名,ccredit学分)其中:cno 长度为4的字符串,为主码cname 长度为10的字符串,不能有重复课程名;ccredit 整数。

3.建立选课数据库模式。

选课表: sc (sno学号, cno课程号, grade成绩)其中:sno 长度为4的字符串,和student表sno外键关联,且级联删除cno 长度为4的字符串,course表cno外键关联,grade 整数,值或空或为0—100之间,(sno, cno) 联合作主码。

(1)创建上述三个表。

(2)将年龄的数据修改为15-30之间。

(3)为Student中sname添加列级完整性约束,不能为空。

(4)为SC建立按学号升序和课程号降序建立唯一索引.(5)在表student的sname字段建立一个升序索引。

(6)删除在表student的sname字段建立的索引。

(7)给student表增加一个地址(address)属性。

(8) 删除student表地址(address)属性。

(9)建立视图view1,要求有sno,sname,cname,grade四个字段。

1) create table studen(sno char(4) primary key,sname char(8),ssex char(2) check( ssex in('男','女')),sage int check(sage between 0 and 150),sdept char(10));create table course(cno char(4) primary key,cname char(10) unique,ccredit intcreate table sc(sno char(4),cno char(4),grade int check(grade between 0 and 100),primary key (sno,cno),foreign key(sno) references student(sno) on delete cascade, foreign key(cno) references course(cno));2) alter table studentadd constraint sage_con check(sage between 15 and 30);3) alter table studentmodify sname not null;4) create unique index index1on sc (sno asc,cno desc);5) create index index2on student(sname asc);6) drop index index2;7) alter table studentadd address char(30);8) alter table studentdrop column address;9) create view view1asselect A.sno,sname,cname,gradefrom student A,course B,sc Cwhere A.sno=C.sno and /doc/287635464.html,o=http://www.doczj .com/doc/287635464.html,o;(1)在上述三个表中输入若干记录。

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《数据库系统与应用》上机习题*************************************************************************************************第五部分、SQL高级应用要求掌握:熟练掌握T-SQL语言,了解异常处理的相关语句,学会用游标方式对数据库进行操作。

一、做书上第十章的例题二、写出书上198页练习题10中第7、8、9、11题的结果,并上机验证。

完成第12、13、14题7、数据库中没有stud表8、9、重复插入ID的值11、12、编写一个程序,采用游标的方式输出所有课程的平均分use schoolgoset nocount ondeclare @s_cj int,@s_name char(8)declare c_cursor cursor forselect score、课程号,AVG(score、分数)from scoregroup by score、课程号order by score、课程号open c_cursorfetch next from c_cursor into @s_cj,@s_namewhile@@FETCH_STATUS=0beginprint CAST(@s_cj as char(8))+@s_namefetch next from c_cursor into @s_cj,@s_nameendclose c_cursordeallocate c_cursorgo13、编写一个程序,使用游标的方式输出所有学号,课程号,成绩等级use schooldeclare @s_xh int,@c_name char(8),@s_cj float,@dj char(1) declare c_cursor cursor forselect student、学号,score、课程号,score、分数from score,studentwhere score、学号=student、学号group by student、学号,score、课程号,score、分数order by student、学号beginset @dj=CASEwhen @s_cj>=90 then'A'when @s_cj>=80 then'B'when @s_cj>=70 then'C'when @s_cj>=60 then'D'else'E'endopen cfetch next from c_cursor into @s_xh,@c_name,@s_cjprint'学号课程号等级'print'---------------------------'while@@FETCH_STATUS=0beginprint @s_xh+' '+@c_name+' '+@s_cjfetch next from c_cursor into @s_xh,@c_name,@s_cjendclose c_cursordeallocate c_cursor14、编写一个程序,输出各班各课程的平均分use schoolgoset nocount ondeclare @s_cj int,@s_name char(8),@s_bj char(8)declare c_cursor cursor forselect student、班级,score、课程号,AVG(score、分数)from score,studentgroup by score、课程号,student、班级order by score、课程号,student、班级open c_cursorfetch next from c_cursor into @s_cj,@s_name,@s_bjprint'学号班级成绩'print'-------------------'while@@FETCH_STATUS=0beginprint CAST(@s_cj as char(8))+@s_name+@s_bjfetch next from c_cursor into @s_cj,@s_name,@s_bjendclose c_cursordeallocate c_cursorgo三、完成书上394页上机实验题3(1)对各出版社的图书比例情况进行分析,即图书比例高于50%为“很高”,图书比例高于30%为“'较高”,图书比例高于10%为“一般”。

并按图书比例递增排列。

USE LibraryDECLARE @num intSELECT @num=(SELECT COUNT(*)FROM book)--图书总数SELECT a、出版社AS'出版社',CASEWHEN a、rate>50、0 THEN'很高'WHEN a、rate>30、0 THEN'较高'WHEN a、rate>10、0 THEN'一般'ELSE'较低'END AS'图书比例情况'FROM (SELECT出版社AS'出版社',CAST(ROUND(COUNT(*)*100、0/@num,1)AS decimal(5,1))AS'rate'FROM bookGROUP BY出版社) aORDER BY a、rate(2)对各系学生借书比例情况进行分析,即借书比例高于50%为“很高”,借书比例高于30%为“较高”,借书比例高于10%为“一般”。

并按借书比例递减排列。

USE LibraryDECLARE @num intSELECT @num=(SELECT COUNT(*)FROM borrow)--借书总数SELECT a、系名AS'系名',CASEWHEN a、rate>50、0 THEN'很高'WHEN a、rate>30、0 THEN'较高'WHEN a、rate>10、0 THEN'一般'ELSE'较低'END AS'借书情况'FROM (SELECT depart、系名,CAST(ROUND(COUNT(*)*100、0/@num,1)AS decimal(5,1))AS'rate' FROM borrow,student,departWHERE borrow、学号=student、学号AND student、班号=depart、班号GROUP BY depart、系名) aORDER BY a、rate DESC(3)采用游标方式对图书价格进行评价。

USE LibraryGODECLARE b_cur CURSORFOR SELECT DISTINCT(图书名),定价FROM bookDECLARE @bn char(20),@dj decimal(4,1),@pr char(10)OPEN b_curFETCH NEXT FROM b_cur INTO @bn,@djWHILE@@FETCH_STATUS= 0BEGINSET @pr=CASEWHEN @dj>=50 THEN'价格太高'WHEN @dj>=30 THEN'价格偏高'WHEN @dj>=20 THEN'价格适中'WHEN @dj>=10 THEN'价格偏低'ELSE'价格太低了'ENDPRINT @bn+CAST(@dj AS char(5))+@prFETCH NEXT FROM b_cur INTO @bn,@djENDCLOSE b_curDEALLOCATE b_cur(4)GO采用游标方式统计每个出版社图书的借出率。

USE LibraryGODECLARE A CURSORFORSELECT a、出版社,CAST(csum*100、0/bsum AS decimal(4,1))As'rate'FROM (SELECT出版社,COUNT(*)AS'bsum'FROM bookGROUP BY出版社) a,(SELECT b、出版社,COUNT(bor、图书编号)AS'csum'FROM book b,borrow borWHERE b、图书编号=bor、图书编号GROUP BY b、出版社) bWHERE a、出版社=b、出版社DECLARE @name char(16),@num decimal(4,1)OPEN AFETCH NEXT FROM A INTO @name,@numWHILE@@FETCH_STATUS= 0BEGINPRINT'"'+RTRIM(@name)+'"图书借出率为'+RTRIM(CAST(@num AS char(5)))+'%'FETCH NEXT FROM A INTO @name,@numENDCLOSE ADEALLOCATE A GO。

相关文档
最新文档