sql第八章 实践练习
- 格式:doc
- 大小:987.50 KB
- 文档页数:10
第一章1、数据完整性是指( D )A.数据库存中的数据不存在重复B.数据库中所有的数据格式是一样的C.所有的数据全部保存在数据库中D.数据库中的数据能够正确反映情况2、SQL中pubs数据库属于(A )A.用户数据库B.系统数据库C.数据库模板D.数据库管理系统3、数据冗余指的是( D )A.数据与数据之间没有联系B.数据有丢失C.数据量太大D.存在重复的数据4、SQL Server数据库的主数据文件的扩展名为( B )A. .sqlB. .mdfC. .mdbD. .ldf5、下列关于关系数据库叙述错误的是(B )A.关系数据库的结构一般保持不变,但也可根据需要进行改变B.一个数据表组成一个关系数据库,多种不同数据则需要创建多个数据库C. 关系数据库表中的所有记录的关键字字段的值互不相同D. 关系数据库表中的外部关键字不能用于区别该表中的记录6、创建数据库时,需要指定( C )属性。
A.数据库初始大小B.数据库的存放位置C.数据库的物理名和逻辑名D.数据库的访问权限7、以下说法正确的是( A )A.通过SQL Server服务器对SQL Server的启动、停止和通过服务管理器对SQL Server的启动、停止是同等功效B.必须先启动服务管理器中的SQL Server服务之后才能通过SQL Server服务管理器启动SQL ServerC.必须先通过SQL Server服务管理器启动SQL Server之后才能启动服务管理器中的SQL Server服务D.只能通过服务管理器对SQL S erver进行启动和停止8、SQL Server提供的4个系统数据库,以下说法正确的是( D )A.tempdb数据库是一个空数据库,完全可以删除B.adventure works是用来做模板的一个数据库C.msdb数据库是用来做例子的数据库D.创建新的空白数据库时,将使用Model数据库所规定的默认值9、以下说法错误的是(C)A.数据完整性是指存储在数据库中数据的准确性B.SQL Server是一个DBMSC.ERP、CRM、MIS等都是DBMSD.设计数据库时允许必要的冗余第二章1、创建银行的贷款情况表时,“还款日期”默认为当天,且必须晚于“借款日期”,应采用(A)约束。
第8章习题参考答案2.求程序的运行结果(1)Public Sub 习题8_2_1()Dim i As IntegerDebug.Print Tab(10); "*"For i = 1 To 5Debug.Print Tab(10 - i); "*"; Spc(i - 1); "*"; Spc(i - 1); "*"Next iFor i = 4 To 1 Step -1Debug.Print Tab(10 - i); "*"; Spc(i - 1); "*"; Spc(i - 1); "*"Next iDebug.Print Tab(10); "*"End Subrun:***** * ** * ** * ** * ** * ** * ** * *****(2)Public Sub 习题4_2_2()Dim x, y, i As Doublex = 0: y = 0For i = 1 To 8If i Mod 2 <> 0 Thenx = x - iElsey = y + iEnd IfNextDebug.Print "i="; iDebug.Print "x="; xDebug.Print "y="; yEnd Subrun:x=-16y= 20(3)Public Sub习题4_2_3()Dim m, s, k As Doublem = 28s = 0k = 1Do While k <= Int(m / 2)If Int(m / k) = m / k ThenDebug.Print ks = s + kEnd Ifk = k + 1LoopDebug.Print "s="; sEnd Subrun:124714s= 28(4)Public Sub 习题8_2_4()Dim n, a1, a2, a3, i As Doublen = Val(InputBox("请输入n,要求n>=3"))If n <= 2 ThenExit SubEnd Ifa1 = 1a2 = 1Debug.Print a1; a2For i = 2 To n - 1a3 = a1 + a2a1 = a2a2 = a3Debug.Print a3NextEnd Sub1 12353.改错题(1)Public Sub 改错8_3_1()Dim i, n, s As Doublen = Val(InputBox("请输入n"))i = 2: s = 1Do While i <= ns = s + ii = i + 1LoopDebug.Print "S="; sEnd Sub(2)Public Sub 改错8_3_2_求分式多项和()Dim nm, n, k ,p As integerDim y As Doublenm = Val(InputBox("请输入计算公式1后面的项目数目个数")) n = 1: p = 1: y = 1Do While n <= nmk = 2 * n + 1p = p * (k - 1) * ky = y + ((-1) ^ n) / pn = n + 1LoopDebug.Print "y="; yEnd Sub4.编程题(1)用if……else语句编程Public Sub分段函数1()Dim x, y, z As Doublex = Val(InputBox("请输入x的值:"))y = Val(InputBox("请输入y的值:"))If x > y And y <> 0 Thenz = x / yElseIf x = y Thenz = x * y * Sgn(y)Elsez = x + yEnd IfDebug.Print "x="; xDebug.Print "y="; yDebug.Print "z="; zEnd Sub(1)用select case语句结构编程Public Sub分段函数2()Dim x, y, z, a As Doublex = Val(InputBox("请输入x"))y = Val(InputBox("请输入y"))a = y - xSelect Case aCase Is > 0z = x + yDebug.Print zCase 0z = x * y * Sgn(y)Debug.Print zCase ElseIf y <> 0 Thenz = x / yDebug.Print zElseDebug.Print "z没有值"End IfEnd SelectEnd Sub(2)Public Sub 求解一元二次方程()Dim a, b, c As IntegerDim d, x1, x2, x3, x4 As Doublea = Val(InputBox("请输入a的整型数:"))b = Val(InputBox("请输入b的整型数:"))c = Val(InputBox("请输入c的整型数:"))d = b * b - 4 * a * cIf d > 0 Thenx1 = (-b + Sqr(d)) / (2 * a)x2 = (-b - Sqr(d)) / (2 * a)Debug.Print "x1="; x1, "x2="; x2Else if d=0 thenx3 = -b / (2 * a)x4 = -b/ (2 * a)Debug.Print "x3=";x3Debug.Print "x4=";x4ElseDebug.Print "方程没有实数解"End IfEnd Sub(3--1)用无条件转向语句GOTO编程Public Sub 数字与星期的转换1()Dim num As Integer10 num = Val(InputBox("请输入整数值:")) If num = 0 ThenDebug.Print "这是星期日"ElseIf num = 1 ThenDebug.Print "这是星期一"ElseIf num = 2 ThenDebug.Print "这是星期二"ElseIf num = 3 ThenDebug.Print "这是星期三"ElseIf num = 4 ThenDebug.Print "这是星期四"ElseIf num = 5 ThenDebug.Print "这是星期五"ElseIf num = 6 ThenDebug.Print "这是星期六"ElseIf num = -1 ThenDebug.Print "程序运行结束"EndElseDebug.Print "输入数据错误!"GoTo 10End IfEnd Sub(3--2)Public Sub数字与星期的转换2 ()Dim x As IntegerDo While Truex = Val(InputBox("请输入数字"))If x = 0 ThenDebug.Print "这是星期日"Exit DoElseIf x >= 1 And x <= 6 ThenDebug.Print "这是星期" + Str(x)Exit DoElseIf x = -1 ThenExit DoElseMsgBox ("输入数据错误!")End IfLoopEnd Sub(4)Public Sub 行李重量计费()Dim an, cn, weight, s_w, distance, fee As Doublean = Val(InputBox("请输入成年人数量"))cn = Val(InputBox("请输入未成年人数量"))weight = Val(InputBox("请输入行李重量"))distance = Val(InputBox("请输入距离"))s_w = weight - 20 * an - 10 * cnIf s_w <= 0 Thenfee = 0ElseIf distance / 100 = Int(distance / 100) Thenfee = 0.2 * s_w * (distance / 100)Elsefee = 0.2 * s_w * (Int(distance / 100) + 1) End IfEnd IfDebug.Print feeEnd Sub(5)Public Sub 求自然数的多项式和()Dim n, s As Integers = 0For n = 1 To 10s = s + (s + n)NextDebug.Print "S=1+(1+2)+(1+2+3)+...+1+2+3+...+10)="; s End SubRun:S=1+(1+2)+(1+2+3)+...+1+2+3+...+10)= 2036Public Sub 求多项自然数阶乘的和()Dim s, t As SingleDim n As Integert = 1s = 0For n = 1 To 20t = t * ns = s + tNextDebug.Print "1!+2!+3!+...+20!="; sEnd SubRun:1!+2!+3!+...+20!= 2.561327E+18(6)Public Sub 既能被3整除又能被5整除正整数个数() Dim i, x As Integerx = 0For i =100 To 200If i / 3 = Int(i / 3) And i / 5 = Int(i / 5) ThenDebug.Print ix = x + 1End IfNextDebug.Print "x="; xEnd SubRun:120135150165180195x= 7(7)Public Sub 输出直角三角形图案1()Dim i, j As IntegerFor i = 1 To 9Debug.PrintNextFor i = 1 To 4Debug.Print Tab(20); "*";For j = 1 To (2 * i - 2)Debug.Print "*";NextDebug.PrintNextEnd SubRun:****************Public Sub 输出直角三角形图案2() Dim i, j As IntegerFor i = 1 To 9Debug.PrintNextDebug.Print Tab(20); "*";For i = 1 To 4Debug.Print Tab(19 - i); "*";For j = 1 To (i + 1)Debug.Print "*";NextDebug.PrintNextEnd SubRun:*******************Public Sub 输出平行四边形图案() Dim i, j As IntegerFor i = 1 To 9Debug.PrintNextFor i = 1 To 5Debug.Print Tab(21 - i);For j = 1 To 6Debug.Print "*";NextDebug.PrintNextEnd SubRun:******************************(8)Public Sub 求选手获得的平均分()Dim score(1 To 11), minno, maxno, sum, aver As Single Dim i As Integerminno = 1maxno = 1sum = 0For i = 1 To 10score(i) = Val(InputBox("请输入选手的成绩值:")) Debug.Print score(i)NextFor i = 2 To 10If score(i) < score(minno) Thenminno = iEnd IfIf score(i) > score(maxno) Thenmaxno = iEnd IfNext iFor i = 1 To 10sum = sum + score(i)Next isum = sum - score(minno) - score(maxno)aver = sum / 8Debug.Print "该选手的平均分是:"; averEnd Sub(9)关于素数的求解(9-1)求100之内的所有素数Public Sub 求所有素数之和()Dim s, w, n As Integers = 0For w = 2 To 99 Step 2For n = 2 To Sqr(w)If w Mod n = 0 ThenExit ForEnd IfNextIf n > Sqr(w) Thens = s + wEnd IfDebug.Print "S="; sNextEnd Sub(9-2)Public Sub 求200以内的所有素数()Dim w, n As IntegerDebug.Print "200 以内的所有素数是:" For w = 2 To 199For n = 2 To Sqr(w)If w Mod n = 0 ThenExit ForEnd IfNext nIf n > Sqr(w) ThenDebug.Print w;End IfNextDebug.PrintEnd Sub(10)Public Sub 求水仙花数1()Dim i, j, k, n As IntegerDebug.Print "水仙花数是:"For i = 1 To 9For j = 0 To 9For k = 0 To 9n = i * 100 + j * 10 + kIf n = i * i * i + j * j * j + k * k * k ThenDebug.Print n;End IfNext kNext jNext iDebug.PrintEnd SubPublic Sub 求水仙花数2()Dim i, j, k, n As IntegerDebug.Print "水仙花数是:"For n = 150 To 999i = Int(n / 100)j = Int(n / 10 - i * 10)k = n Mod 10If n = i * i * i + j * j * j + k * k * k ThenDebug.Print n;End IfNextDebug.PrintEnd Subrun:水仙花数是:153 370 371 407(11)Public Sub 求分数数列和()Dim i, t, n As IntegerDim a, b, s As Singlen = 20a = 2:b = 1: s = 0For i = 1 To ns = s + a / bt = aa = a + bb = tNextDebug.Print "sum="; s;End Sub(12)Public Sub N年达到的利息()Dim y As IntegerDim interest1,interest As DoubleP=10000y = 0interest = 0Do Until interest >= 1000Interest1 =2*p* 0.0225 *(1-0.2) ‘一期2年整存整取扣税后的利息p=p+interest1 ‘扣税后的利息加上本金成为新一期的本金Interest=p-10000 ‘存款以来实际所的利息y = y + 2Debug.Print interest, yLoopEnd SubRun:360 2732.959999999999 4 1119.34656 6。
SQL Server 2008数据库应用与开发教程(第二版)第一章习题参考答案1.简述SQL Server 2008系统中主要数据库对象的特点。
答:主要的数据库对象包括数据库关系图、表、视图、同义词、存储过程、函数、触发器、程序集、类型、规则和默认值等。
“表”节点中包含了数据库最基本、最重要的对象——表。
表实际用来存储系统数据和用户数据,是最核心的数据库对象。
“视图”节点包含了数据库中的视图对象。
视图是一种虚拟表,用来查看数据库中的一个或多个表,视图是建立在表基础之上的数据库对象,它主要以SELECT语句形式存在。
在“同义词”节点中包含了数据库中的同义词对象。
这是Microsoft SQL Server 2008系统新增的一种对象。
“可编程性”对象是一个逻辑组合,它包括存储过程、函数、触发器、程序集、类型、规则和默认值等对象。
数据库中的函数对象包含在“函数”节点中。
函数是接受参数、执行复杂操作并将结果以值的形式返回的例程。
2.SQL Server 2008数据库管理系统产品分为哪几个版本,各有什么特点?答:SQL Server 2008数据库管理系统产品的服务器版本包括了企业版和标准版,专业版本主要包括以下版本:工作组版(Workgroup)、开发人员版(Developer)、免费精简版(Express)、Web版,以及免费的集成数据库SQL Server Compact 3.5。
3.SQL Server 2008包含哪些组件,其功能各是什么?答:SQL Server 2008的体系结构是对SQL Server的组成部分和这些组成部分之间的描述。
Microsoft SQL Server 2008系统由4个组件组成,这4个组件被称为4个服务,分别是数据库引擎、Analysis Services、Reporting Services和Integration Services。
数据库引擎是Microsoft SQL Server 2008系统的核心服务,负责完成数据的存储、处理、查询和安全管理等操作。
学习资料第9 课为学生信息管理系统创建存储过程1. 什么是存储过程?使用存储过程有哪些特点?答:存储过程是一种数据库对象,通常是把实现某个特定任务的一组预编译的SQL 语句创建一个存储过程,以一个存储单元的形式存储在服务器上,供用户反复调用,提高程序的使用效率。
使用存储过程的优点:允许模块化程序设计;允许更快执行;减少网络流量;可作为安全机制使用。
2. 试说明存储过程分类的特点。
答:1)系统存储过程。
存储在master数据库中,并以sp_为前缀,许多管理和信息活动可以通过系统存储过程执行。
2)本地存储过程。
是用户自行创建的并存储在用户数据库中的存储过程。
这类存储过程能根据用户的实际需要完成某以特定的功能。
3)临时存储过程。
临时存储过程分为本地临时存储过程和全局临时存储过程。
在创建存储过程时。
如果过程名的第一个字符取“#”,那么创建的就是本地临时存储过程;如果过程名的第一•第二字符都取,那么创建的就是全局临时存储过程。
临时存储过程存储在tempbd 内,它们在连接到SQL Server 以前的版本时很有用。
4)远程存储过程。
指非本地服务器上的存储过程,只有在分布式查询中使用此存储过程。
5)扩展存储过程。
扩展存储过程是用户使用外部程序语言编写的存储过程。
使用时需要先加载到SQLServer 系统中,且只能存储在master 数据库中,其执行与一般的存储过程完全相同。
引入扩展存储过程主要是弥补SQLServer 的不足之处,可以按需要大幅扩展其功能。
3. 请分别写出用企业管理器和T_SQL语句命令创建存储过程的主要步骤。
答:使用企业管理器1. 运行企业管理器,展开数据库STUM,S 在“存储过程”图标上右击,在弹出的快捷菜单中选择“新建存储过程”命令。
2. 在该窗口中首先输入所有者和存储过程名。
3. 输入实现存储过程功能的语句,单击“检查语法”按钮,进行语法检查。
4. 如果没有任何错误,单击“确定”按钮,将存储过程保存到STUMS数据库中。
实践综合训练之三参考答案思路或参考答案(在MS SQL SERVER中的实现):思路一:使用一个SQL语句完成查询统计,然后在界面显示统计结果。
这样做的条件是能够使用一个SQL语句完成查询统计,否则,不能使用此方法。
该方法的好处是数据不保存节省空间,不会造成访问冲突;缺点是每次都要重新统计一次数据,对服务器性能产生一些影响,尤其是在服务器忙的时候。
该SQL语句可以直接写在应用程序中,也可以写成存储过程在应用程序中调用执行。
本题的一个SQL语句:select zc.kc,zc.yyjb,zcrs,case when bsrs is null then 0 else bsrs end as bsrs,bsrs*100.0/zcrs bsjgl, case when jsrs is null then 0 else jsrs end as jsrs,jsrs*100.0/zcrs jsjgl,case when qbrs is null then 0 else qbrs end as qbrs,qbrs*100.0/zcrs qbjglfrom (select kc,substring(zkzh,1,2) yyjb,count(*) zcrs from ta_kscj where kc='25' and yzkzh is nullgroup by kc,substring(zkzh,1,2)) as zcleft join (select substring(zkzh,1,2) yyjb,count(*) bsrs from ta_kscj where kc='25' and yzkzh is null and bscjdd>0 group by substring(zkzh,1,2)) as bs on(zc.yyjb=bs.yyjb)left join(select substring(zkzh,1,2) yyjb,count(*) jsrs from ta_kscj where kc='25' and yzkzh is null and sjcjdd>0 group by substring(zkzh,1,2)) as js on(zc.yyjb=js.yyjb)left join(select substring(zkzh,1,2) yyjb,count(*) qbrs from ta_kscj where kc='25' and yzkzh is null and zcjdd>0 group by substring(zkzh,1,2)) as qb on(zc.yyjb=qb.yyjb)order by zc.yyjb;可以再与TC_YYJBDM表关联,得出具体的语言级别名称。
Section 1Exercise 8.1.1a)CREATE VIEW RichExec ASSELECT * FROM MovieExec WHERE netWorth >= 10000000;b)CREATE VIEW StudioPres (name, address, cert#) ASSELECT , MovieExec.address, MovieExec.cert# FROM MovieExec, Studio WHERE MovieExec.cert# = Studio.presC#;c)CREATE VIEW ExecutiveStar (name, address, gender, birthdate, cert#, netWorth) AS SELECT , star.address, star.gender, star.birthdate, exec.cert#, WorthFROM MovieStar star, MovieExec exec WHERE = ANDstar.address = exec.address;Exercise 8.1.2a)SELECT name from ExecutiveStar WHERE gender = ‘f’;b)SELECT from RichExec, StudioPres where = ;c)SELECT from ExecutiveStar, StudioPresWHERE Worth >= 50000000 ANDStudioPres.cert# = RichExec.cert#;Section 2Exercise 8.2.1The views RichExec and StudioPres are updatable; however, the StudioPres view needs to be created with a subquery.CREATE VIEW StudioPres (name, address, cert#) ASSELECT , MovieExec.address, MovieExec.cert# FROM MovieExecWHERE MovieExec.cert# IN (SELECT presCt# from Studio);Exercise 8.2.2a) Yes, the view is updatable.b)CREATE TRIGGER DisneyComedyInsertINSTEAD OF INSERT ON DisneyComediesREFERENCING NEW ROW AS NewRowFOR EACH ROWINSERT INTO Movies(title, year, length, studioName, genre)VALUES(NewRow.title, NewRow.year, NewYear.length, ‘Disney’, ‘comedy’);c)CREATE TRIGGER DisneyComedyUpdateINSTEAD OF UPDATE ON DisneyComediesREFERENCING NEW ROW AS NewRowFOR EACH ROWUPDATE Movies SET length NewRow.lengthWHERE title = NewRow.title AND year = NEWROW.year ANDstudionName = ‘Disney’ AND genre = ‘comedy’;Exercise 8.2.3a) No, the view is not updatable since it is constructed from two different relations.b)CREATE TRIGGER NewPCInsertINSTEAD OF INSERT ON NewPCREFERENCING NEW ROW AS NewRowFOR EACH ROW(INSERT INTO Product VALUES(NewRow.maker, NewRow.model, ‘pc’))(INSERT INTO PC VALUES(NewRow.model, NewRow.speed, NewRow.ram, NewRow.hd, NewRow.price));c)CREATE TRIGGER NewPCUpdateINSTEAD OF UPDATE ON NewPCREFERENCING NEW ROW AS NewRowFOR EACH ROWUPDATE PC SET price = NewPC.price where model = NewPC.model;d)CREATE TRIGGER NewPCDeleteINSTEAD OF DELETE ON NeePCREFERENCING OLD ROW AS OldRowFOR EACH ROW(DELETE FROM Product WHERE model = OldRow.model)(DELETE FROM PC where model = OldRow.model);Section 3Exercise 8.3.1a)CREATE INDEX NameIndex on Studio(name);b)CREATE INDEX AddressIndex on MovieExec(address);c)CREATE INDEX GenreIndex on Movies(genre, length);Section 4Exercise 8.4.1Exercise 8.4.2Q1 = SELECT * FROM Ships WHERE name = n;Q2 = SELECT * FROM Ships WHERE class = c;Q3 = SELECT * FROM Ships WHERE launched = y;I = InsertsIndexesNone Name Class Launched Name & Name & Class & ThreeSection 5Exercise 8.5.1Updates to movies that involves title or yearUPDATE MovieProd SET title = ‘newTitle’ where title=’oldTitle’ AND year = oldYear; UPDATE MovieProd SET year = newYear where title=’oldYitle’ AND year = oldYear;Update to MovieExec involving cert#DELETE FROM MovieProdWHERE (title, year) IN (SELECT title, yearFROM Movies, MovieExecWHERE cert# = oldCert# AND cert# = producerC#);INSERT INTO MovieProdSELECT title, year, nameFROM Movies, MovieExecWHERE cert# = newCert# AND cert# = producerC#;Exercise 8.5.2Insertions, deletions, and updates to the base tables Product and PC would require a modification of the materialized view.Insertions into Product with type equal to ‘pc’:INSERT INTO NewPCSELECT maker, model, speed, ram, hd, price FROM Product, PC WHEREProduct.model = newModel and Product.model = PC.model;Insertions into PC:INSERT INTO NewPCSELECT maker, ‘newModel’, ‘newSpeed’, ‘newRam’, ‘newHd’, ‘newPrice’FROM Product WHERE model = ‘newModel’;Deletions from Product with type equal to ‘pc’:DELETE FROM NewPC WHERE maker = ‘deletedMaker’ AND model=’deletedModel’; Deletions from PC:DELETE FROM NewPC WHERE model = ‘deletedModel’;Updates to PC:Update NewPC SET speed=PC.speed, ram=PC.ram, hd=PC.hd, price=PC.price FROM PC where model=pc.model;Update to the attribute ‘model’ needs to be treated as a delete and an insert. Updates to Product:Any changes to a Product tuple whose type is ‘pc’ need to be treated as a delete or an insert, or both.Exercise 8.5.3Modifications to the base tables that would require a modification to the materialized view: inserts and deletes from Ships, deletes from class, updates to a Class’ displacement. Deletions from Ship:UPDATE ShipStats SETdisplacement=((displacement * count) –(SELECT displacementFROM ClasssesWHERE class = ‘DeletedShipClass’)) / (count – 1),count = count – 1WHEREcountry = (SELECT country FROM C lasses WHERE class=’DeletedShipClass’); Insertions into Ship:Update ShipStat SETdisplacement=((displacement*count) +(SELECT displacement FROM ClassesWHERE class=’InsertedShipClass’)) / (count + 1),count = count + 1WHEREcountry = (SELECT country FROM Classes WHERE classes=’InsertedShipClass); Deletes from Classes:NumRowsDeleted = SELECT count(*) FROM ships WHERE class = ‘DeletedClass’; UPDATE ShipStats SETdisplacement = (displacement * count) - (DeletedClassDisplacement *NumRowsDeleted)) / (count – NumRowsDeleted),count = count – NumRowsDeletedWHERE country = ‘DeletedClassCountry’;Update to a Class’ displacement:N = SELECT count(*) FROM Ships where class = ‘UpdatedClass’;UPDATE ShipsStat SETdisplacement = ((displacement * count) + ((oldDisplacement – newDisplacement) * N))/countWHEREc ountry = ‘UpdatedClassCountry’;Exercise 8.5.4Queries that can be rewritten with the materialized view:Names of stars of movies produced by a certain producerSELECT starNameFROM StarsIn, Movies, MovieExecWHERE movieTitle = title AND movieYear = year AND producerC# = cert# AND name = ‘Max Bialystock’;Movies produced by a certain producerSELECT title, yearFROM Movies, MovieExecWhere produce rC# = cert# AND name = ‘George Lucas’;Names of producers that a certain star has worked withSELECT nameFROM Movies, MovieExec, StarsInWhere producerC#=cert# AND title=movieTitle AND year=movieYear AND starName=’Carrie Fisher’;The number of movies produced by given producerSELECT count(*)FROM Movies, MovieExecWHER E producerC#=cert# AND name = ‘George Lucas‘;Names of producers who also starred in their own moviesSELECT nameFROM Movies, StarsIn, MovieExecWHERE producerC#=cert# AND movieTitle = title AND movieYear = year AND = starName;The number of stars that have starred in movies produced by a certain producer SELECT count(DISTINCT starName)FROM Movies, StarsIn, MovieExecWHERE producerC#=cert# AND movieTitle = title AND movieYear = year AND n ame ‘George Lucas’;The number of movies produced by each producerSELECT name, count(*)FROM Movies, MovieExecWHERE producerC#=cert# GROUP BY name。
第8章习题解答1.思考题(1)SQL Server 2008提供了哪些安全管理机制?安全性管理是建立在什么机制上的?答:SQL Server 2008提供了非常完善的安全管理机制,包括用户登录、管理和对用户使用数据库对象的管理。
SQL Server2008的安全性管理是建立在身份验证和访问许可机制上的。
(2)SQL Server 2008有几种身份验证方式?它们的区别是什么?哪种身份验证方式更安全?答:SQL Server 2008有两种身份验证方式,即Windows身份验证模式和混合模式。
Windows 身份验证模式会启用 Windows 身份验证并禁用 SQL Server 身份验证。
混合模式会同时启用 Windows 身份验证和 SQL Server 身份验证。
Windows 身份验证始终可用,并且无法禁用。
SQL Server 2008 的默认身份验证模式是Windows身份验证模式,混合模式更为安全。
(3)数据库的权限是指什么权限?权限管理的主要任务是什么?角色中的所有成员能否继承该角色所拥有的权限?答:SQL Server2008中的权限包括3种类型:对象权限、语句权限和隐含权限。
权限管理的主要任务是对象权限和语句权限的管理。
角色中的所有成员继承该角色所拥有的权限。
(4)SQL Server 2008中有几种角色类型?它们的主要区别是什么?答:SQL Server 2008中有3种角色类型:固定角色、用户定义的数据库角色和应用程序角色。
固定角色:是指其权限已被SQL Server 2008定义,且SQL Server 2008管理者不能对其权限进行修改的角色。
这些固定角色涉及服务器配置管理以及服务器和数据库的权限管理。
按照管理目标对象的不同,固定角色又分为固定服务器角色和固定数据库角色。
用户定义数据库角色:就是当一组用户需要设置的权限不同于固定数据库角色所具有的权限时,为了满足要求而定义的新的数据库角色。
sql实验习题答案SQL实验习题答案在学习SQL(Structured Query Language)时,习题是一种非常有效的学习方式。
通过实践操作,我们可以更好地理解SQL语言的各种概念和用法。
下面是一些常见的SQL实验习题及其答案,希望对大家的学习有所帮助。
1. 查询某个表的所有数据答案:SELECT * FROM 表名;2. 查询某个表的前n行数据答案:SELECT * FROM 表名 LIMIT n;3. 查询某个表中满足某个条件的数据答案:SELECT * FROM 表名 WHERE 条件;4. 查询某个表中某个字段的最大值答案:SELECT MAX(字段名) FROM 表名;5. 查询某个表中某个字段的最小值答案:SELECT MIN(字段名) FROM 表名;6. 查询某个表中某个字段的总和答案:SELECT SUM(字段名) FROM 表名;7. 查询某个表中某个字段的平均值答案:SELECT AVG(字段名) FROM 表名;8. 查询某个表中某个字段的记录数答案:SELECT COUNT(字段名) FROM 表名;9. 查询某个表中某个字段的记录数,并按照字段值进行分组答案:SELECT 字段名, COUNT(字段名) FROM 表名 GROUP BY 字段名;10. 查询某个表中满足多个条件的数据答案:SELECT * FROM 表名 WHERE 条件1 AND 条件2;11. 查询某个表中满足多个条件中的任意一个条件的数据答案:SELECT * FROM 表名 WHERE 条件1 OR 条件2;12. 查询某个表中满足某个条件,并按照某个字段进行排序的数据答案:SELECT * FROM 表名 WHERE 条件 ORDER BY 字段名;13. 查询某个表中满足某个条件,并限制结果的行数答案:SELECT * FROM 表名 WHERE 条件 LIMIT n;14. 查询某个表中满足某个条件,并跳过前n行的数据答案:SELECT * FROM 表名 WHERE 条件 OFFSET n;15. 查询某个表中满足某个条件,并按照某个字段进行分页显示答案:SELECT * FROM 表名 WHERE 条件 ORDER BY 字段名 LIMIT n OFFSET m;这些习题涵盖了SQL语言的基本操作和常用函数。
课后习题答案第8章1. 概述第8章是XXX课程的课后习题答案,本文档将为大家提供第8章的课后习题答案,以帮助同学们复习和加深对知识点的理解。
2. 习题答案2.1 问题1问题描述:请列举出几种数据库管理系统(DBMS)的类型,并且给出它们的特点。
答案:•关系型数据库管理系统(RDBMS):以关系模型为基础的数据库管理系统,使用表格的形式来组织数据,支持SQL语言进行数据操作。
具有数据一致性、可靠性高等特点。
•非关系型数据库管理系统(NoSQL):相对于关系型数据库,NoSQL数据库采用非关系型的数据组织方式,更加适用于大规模分布式存储和处理。
具有高可扩展性、灵活性等特点。
•面向对象数据库管理系统(OODBMS):将对象概念引入数据库系统,将对象作为数据库的主要组织单位,支持面向对象的数据库操作和查询。
具有数据隐蔽性、可重用性等特点。
•XML数据库管理系统(XML-DBMS):以XML标准为基础的数据库管理系统,可以存储和管理XML格式的数据。
具有对XML数据的高效操作和查询能力。
2.2 问题2问题描述:请解释什么是数据库索引,它的作用是什么?答案:数据库索引是对数据库表中一列或多列的值进行排序的一种数据结构,它可以快速地定位到具有特定值的数据记录。
索引可以加快数据库的查询速度,减少数据扫描的时间。
具体而言,索引的作用包括:•提高数据库查询的效率:索引可以根据索引键值快速定位到数据记录,加快查询速度。
•减少磁盘I/O的需求:通过使用索引,数据库可以减少需要扫描的数据块数量,从而减少磁盘I/O的次数。
•保证数据的唯一性:索引可以设置为唯一索引,确保某一列的值在表中是唯一的。
•支持表之间的关联:通过使用外键和关联索引,数据库可以实现表之间的关联和连接操作。
2.3 问题3问题描述:请解释什么是数据库事务,它的特点是什么?答案:数据库事务是一组数据库操作的逻辑单元,它被视为一个不可分割的工作单位,要么全部执行成功,要么全部回滚。
第八章SQL Server的安全性要点:SQL Server 2000两级权限管理机制登陆帐户和服务器角色数据库用户和数据库角色权限的管理第一节SQL Server 2000安全性概述作为一种数据库管理系统,SQL Server2000系统中存储了用户大量的业务数据,这些数据都是用户的商业机密。
这些商业数据必须得到安全保障。
安全性管理是数据库管理系统的一个重要组成部分。
安全性就是确保只有授权的用户才能使用数据库中的数据和执行相应的操作,安全性管理包括两个方面的内容,一是用户能否登录系统和如何登录的管理,二是用户能否使用数据库中的对象和执行相应操作的管理。
SQL Server2000提供了一套完整的安全机制。
一、认证进程和认证模式认证进程就是指当用户访问数据库系统时,系统对该用户帐号和口令的确认过程,认证的内容包括确认用户的帐号是否有效、是否能访问系统、能访问系统中的哪些数据等。
认证模式就是指系统选择何种认证进程确认用户的方式。
用户必须使用一个登录帐号,才能连接到SQL Server系统中。
SQL Server 系统通过两种认证进程来确认用户的身份,这两种认证进程是:(1)WINDOWS认证进程(2)SQL Server认证进程当SQL Server在WINDOWS环境中运行时,系统管理员必须指定系统的认证模式类型。
认证模式类型有两种:(1)WINDOWS认证模式(2)混合模式WINDOWS认证模式只允许使用WINDOWS认证进程。
这时,用户不能指SQL Server的登录帐号。
混合认证模式既允许使用WINDOWS认证进程,又允许使用SQL Server认证进程。
在混合认证模式中,当某个用户希望登录SQL Server系统时,系统是采用WINDOWS认证进程还是采用SQL Server认证进程取决于该用户连接到系统的网络协议类型。
注意:在SQL Server系统中,除了网络协议Named pipes和TCP/IP,其他的网络通信协议都是非信任连接协议。
第9课为学生信息管理系统创建存储过程1.什么是存储过程?使用存储过程有哪些特点?答:存储过程是一种数据库对象,通常是把实现某个特定任务的一组预编译的SQL语句创建一个存储过程,以一个存储单元的形式存储在服务器上,供用户反复调用,提高程序的使用效率。
使用存储过程的优点:允许模块化程序设计;允许更快执行;减少网络流量;可作为安全机制使用。
2.试说明存储过程分类的特点。
答:1)系统存储过程。
存储在master数据库中,并以sp_为前缀,许多管理和信息活动可以通过系统存储过程执行。
2)本地存储过程。
是用户自行创建的并存储在用户数据库中的存储过程。
这类存储过程能根据用户的实际需要完成某以特定的功能。
3)临时存储过程。
临时存储过程分为本地临时存储过程和全局临时存储过程。
在创建存储过程时。
如果过程名的第一个字符取“﹟”,那么创建的就是本地临时存储过程;如果过程名的第一.第二字符都取“﹟”,那么创建的就是全局临时存储过程。
临时存储过程存储在tempbd内,它们在连接到SQL Server 以前的版本时很有用。
4)远程存储过程。
指非本地服务器上的存储过程,只有在分布式查询中使用此存储过程。
5)扩展存储过程。
扩展存储过程是用户使用外部程序语言编写的存储过程。
使用时需要先加载到SQL Server系统中,且只能存储在master数据库中,其执行与一般的存储过程完全相同。
引入扩展存储过程主要是弥补SQL Server的不足之处,可以按需要大幅扩展其功能。
3.请分别写出用企业管理器和T_SQL语句命令创建存储过程的主要步骤。
答:使用企业管理器1.运行企业管理器,展开数据库STUMS,在“存储过程”图标上右击,在弹出的快捷菜单中选择“新建存储过程”命令。
2.在该窗口中首先输入所有者和存储过程名。
3.输入实现存储过程功能的语句,单击“检查语法”按钮,进行语法检查。
4.如果没有任何错误,单击“确定”按钮,将存储过程保存到STUMS数据库中。
Section 1Exercise 8.1.1a)CREATE VIEW RichExec ASSELECT * FROM MovieExec WHERE netWorth >= 10000000;b)CREATE VIEW StudioPres (name, address, cert#) ASSELECT , MovieExec.address, MovieExec.cert# FROM MovieExec, Studio WHERE MovieExec.cert# = Studio.presC#;c)CREATE VIEW ExecutiveStar (name, address, gender, birthdate, cert#, netWorth) AS SELECT , star.address, star.gender, star.birthdate, exec.cert#, WorthFROM MovieStar star, MovieExec exec WHERE = ANDstar.address = exec.address;Exercise 8.1.2a)SELECT name from ExecutiveStar WHERE gender = ‘f’;b)SELECT from RichExec, StudioPres where = ;c)SELECT from ExecutiveStar, StudioPresWHERE Worth >= 50000000 ANDStudioPres.cert# = RichExec.cert#;Section 2Exercise 8.2.1The views RichExec and StudioPres are updatable; however, the StudioPres view needs to be created with a subquery.CREATE VIEW StudioPres (name, address, cert#) ASSELECT , MovieExec.address, MovieExec.cert# FROM MovieExecWHERE MovieExec.cert# IN (SELECT presCt# from Studio);Exercise 8.2.2a) Yes, the view is updatable.b)CREATE TRIGGER DisneyComedyInsertINSTEAD OF INSERT ON DisneyComediesREFERENCING NEW ROW AS NewRowFOR EACH ROWINSERT INTO Movies(title, year, length, studioName, genre)VALUES(NewRow.title, NewRow.year, NewYear.length, ‘Disney’, ‘comedy’);c)CREATE TRIGGER DisneyComedyUpdateINSTEAD OF UPDATE ON DisneyComediesREFERENCING NEW ROW AS NewRowFOR EACH ROWUPDATE Movies SET length NewRow.lengthWHERE title = NewRow.title AND year = NEWROW.year ANDstudionName = ‘Disney’ AND genre = ‘comedy’;Exercise 8.2.3a) No, the view is not updatable since it is constructed from two different relations.b)CREATE TRIGGER NewPCInsertINSTEAD OF INSERT ON NewPCREFERENCING NEW ROW AS NewRowFOR EACH ROW(INSERT INTO Product VALUES(NewRow.maker, NewRow.model, ‘pc’))(INSERT INTO PC VALUES(NewRow.model, NewRow.speed, NewRow.ram, NewRow.hd, NewRow.price));c)CREATE TRIGGER NewPCUpdateINSTEAD OF UPDATE ON NewPCREFERENCING NEW ROW AS NewRowFOR EACH ROWUPDATE PC SET price = NewPC.price where model = NewPC.model;d)CREATE TRIGGER NewPCDeleteINSTEAD OF DELETE ON NeePCREFERENCING OLD ROW AS OldRowFOR EACH ROW(DELETE FROM Product WHERE model = OldRow.model)(DELETE FROM PC where model = OldRow.model);Section 3Exercise 8.3.1a)CREATE INDEX NameIndex on Studio(name);b)CREATE INDEX AddressIndex on MovieExec(address);c)CREATE INDEX GenreIndex on Movies(genre, length);Section 4Exercise 8.4.1Exercise 8.4.2Q1 = SELECT * FROM Ships WHERE name = n;Q2 = SELECT * FROM Ships WHERE class = c;Q3 = SELECT * FROM Ships WHERE launched = y;I = InsertsIndexesNone Name Class Launched Name & Name & Class & ThreeSection 5Exercise 8.5.1Updates to movies that involves title or yearUPDATE MovieProd SET title = ‘newTitle’ where title=’oldTitle’ AND year = oldYear; UPDATE MovieProd SET year = newYear where title=’oldYitle’ AND year = oldYear;Update to MovieExec involving cert#DELETE FROM MovieProdWHERE (title, year) IN (SELECT title, yearFROM Movies, MovieExecWHERE cert# = oldCert# AND cert# = producerC#);INSERT INTO MovieProdSELECT title, year, nameFROM Movies, MovieExecWHERE cert# = newCert# AND cert# = producerC#;Exercise 8.5.2Insertions, deletions, and updates to the base tables Product and PC would require a modification of the materialized view.Insertions into Product with type equal to ‘pc’:INSERT INTO NewPCSELECT maker, model, speed, ram, hd, price FROM Product, PC WHEREProduct.model = newModel and Product.model = PC.model;Insertions into PC:INSERT INTO NewPCSELECT maker, ‘newModel’, ‘newSpeed’, ‘newRam’, ‘newHd’, ‘newPrice’FROM Product WHERE model = ‘newModel’;Deletions from Product with type equal to ‘pc’:DELETE FROM NewPC WHERE maker = ‘deletedMaker’ AND model=’deletedModel’; Deletions from PC:DELETE FROM NewPC WHERE model = ‘deletedModel’;Updates to PC:Update NewPC SET speed=PC.speed, ram=PC.ram, hd=PC.hd, price=PC.price FROM PC where model=pc.model;Update to the attribute ‘model’ needs to be treated as a delete and an insert. Updates to Product:Any changes to a Product tuple whose type is ‘pc’ need to be treated as a delete or an insert, or both.Exercise 8.5.3Modifications to the base tables that would require a modification to the materialized view: inserts and deletes from Ships, deletes from class, updates to a Class’ displacement. Deletions from Ship:UPDATE ShipStats SETdisplacement=((displacement * count) –(SELECT displacementFROM ClasssesWHERE class = ‘DeletedShipClass’)) / (count – 1),count = count – 1WHEREcountry = (SELECT country FROM C lasses WHERE class=’DeletedShipClass’); Insertions into Ship:Update ShipStat SETdisplacement=((displacement*count) +(SELECT displacement FROM ClassesWHERE class=’InsertedShipClass’)) / (count + 1),count = count + 1WHEREcountry = (SELECT country FROM Classes WHERE classes=’InsertedShipClass); Deletes from Classes:NumRowsDeleted = SELECT count(*) FROM ships WHERE class = ‘DeletedClass’; UPDATE ShipStats SETdisplacement = (displacement * count) - (DeletedClassDisplacement *NumRowsDeleted)) / (count – NumRowsDeleted),count = count – NumRowsDeletedWHERE country = ‘DeletedClassCountry’;Update to a Class’ displacement:N = SELECT count(*) FROM Ships where class = ‘UpdatedClass’;UPDATE ShipsStat SETdisplacement = ((displacement * count) + ((oldDisplacement – newDisplacement) * N))/countWHEREc ountry = ‘UpdatedClassCountry’;Exercise 8.5.4Queries that can be rewritten with the materialized view:Names of stars of movies produced by a certain producerSELECT starNameFROM StarsIn, Movies, MovieExecWHERE movieTitle = title AND movieYear = year AND producerC# = cert# AND name = ‘Max Bialystock’;Movies produced by a certain producerSELECT title, yearFROM Movies, MovieExecWhere produce rC# = cert# AND name = ‘George Lucas’;Names of producers that a certain star has worked withSELECT nameFROM Movies, MovieExec, StarsInWhere producerC#=cert# AND title=movieTitle AND year=movieYear AND starName=’Carrie Fisher’;The number of movies produced by given producerSELECT count(*)FROM Movies, MovieExecWHER E producerC#=cert# AND name = ‘George Lucas‘;Names of producers who also starred in their own moviesSELECT nameFROM Movies, StarsIn, MovieExecWHERE producerC#=cert# AND movieTitle = title AND movieYear = year AND = starName;The number of stars that have starred in movies produced by a certain producer SELECT count(DISTINCT starName)FROM Movies, StarsIn, MovieExecWHERE producerC#=cert# AND movieTitle = title AND movieYear = year AND n ame ‘George Lucas’;The number of movies produced by each producerSELECT name, count(*)FROM Movies, MovieExecWHERE producerC#=cert# GROUP BY name。
sql实践题目SQL(Structured Query Language)是一种用于在关系型数据库中处理数据的标准化编程语言。
它可以用于从数据库中提取数据、插入、更新和删除数据、创建和管理数据库对象等。
在本文中,我们将探讨几个SQL实践题目,并给出相关参考内容。
1. 查询指定条件下的数据题目:查询一个名为"students"的表中,所有年龄大于等于18岁且成绩大于80分的学生的姓名和成绩。
解答:可以使用SELECT语句结合WHERE子句来实现该查询。
```sqlSELECT name, scoreFROM studentsWHERE age >= 18 AND score > 80;```这个查询语句将返回满足条件的学生的姓名和成绩。
2. 更新数据题目:将一个名为"products"的表中所有价格低于10美元的产品的价格提高10%。
解答:可以使用UPDATE语句结合WHERE子句来实现该操作。
```sqlUPDATE productsSET price = price * 1.1WHERE price < 10;```这个更新语句将会将所有价格低于10美元的产品的价格提高10%。
3. 插入数据题目:在一个名为"employees"的表中插入一条新数据,员工的姓名是"John Smith",部门是"Sales",工资是5000美元。
解答:可以使用INSERT INTO语句来实现该操作。
```sqlINSERT INTO employees (name, department, salary)VALUES ('John Smith', 'Sales', 5000);```这个插入语句将会在"employees"表中插入一条新数据。
4. 删除数据题目:删除一个名为"orders"的表中所有已经完成的订单。
Section 1Exercise 8.1.1a)CREATE VIEW RichExec ASSELECT * FROM MovieExec WHERE netWorth >= 10000000;b)CREATE VIEW StudioPres (name, address, cert#) ASSELECT , MovieExec.address, MovieExec.cert# FROM MovieExec, Studio WHERE MovieExec.cert# = Studio.presC#;c)CREATE VIEW ExecutiveStar (name, address, gender, birthdate, cert#, netWorth) AS SELECT , star.address, star.gender, star.birthdate, exec.cert#, WorthFROM MovieStar star, MovieExec exec WHERE = ANDstar.address = exec.address;Exercise 8.1.2a)SELECT name from ExecutiveStar WHERE gender = ‘f’;b)SELECT from RichExec, StudioPres where = ; c)SELECT from ExecutiveStar, StudioPresWHERE Worth >= 50000000 ANDStudioPres.cert# = RichExec.cert#;Section 2Exercise 8.2.1The views RichExec and StudioPres are updatable; however, the StudioPres view needs to be created with a subquery.CREATE VIEW StudioPres (name, address, cert#) ASSELECT , MovieExec.address, MovieExec.cert# FROM MovieExec WHERE MovieExec.cert# IN (SELECT presCt# from Studio);Exercise 8.2.2a) Yes, the view is updatable.b)CREATE TRIGGER DisneyComedyInsertINSTEAD OF INSERT ON DisneyComediesREFERENCING NEW ROW AS NewRowFOR EACH ROWINSERT INTO Movies(title, year, length, studioName, genre)VALUES(NewRow.title, NewRow.year, NewYear.length, ‘Disney’, ‘comedy’);c)CREATE TRIGGER DisneyComedyUpdateINSTEAD OF UPDATE ON DisneyComediesREFERENCING NEW ROW AS NewRowFOR EACH ROWUPDATE Movies SET length NewRow.lengthWHERE title = NewRow.title AND year = NEWROW.year ANDstudionName = ‘Disney’ AND genre = ‘comedy’;Exercise 8.2.3a) No, the view is not updatable since it is constructed from two different relations.b)CREATE TRIGGER NewPCInsertINSTEAD OF INSERT ON NewPCREFERENCING NEW ROW AS NewRowFOR EACH ROW(INSERT INTO Product VALUES(NewRow.maker, NewRow.model, ‘pc’))(INSERT INTO PC VALUES(NewRow.model, NewRow.speed, NewRow.ram, NewRow.hd, NewRow.price));c)CREATE TRIGGER NewPCUpdateINSTEAD OF UPDATE ON NewPCREFERENCING NEW ROW AS NewRowFOR EACH ROWUPDATE PC SET price = NewPC.price where model = NewPC.model;d)CREATE TRIGGER NewPCDeleteINSTEAD OF DELETE ON NeePCREFERENCING OLD ROW AS OldRowFOR EACH ROW(DELETE FROM Product WHERE model = OldRow.model)(DELETE FROM PC where model = OldRow.model);Section 3Exercise 8.3.1a)CREATE INDEX NameIndex on Studio(name);b)CREATE INDEX AddressIndex on MovieExec(address);c)CREATE INDEX GenreIndex on Movies(genre, length);Section 4Exercise 8.4.1Action No Index Star Index Movie Index Both Indexes Q110041004Q210010044I2446 Average 2 + 98p1 + 98p2 4 + 96 p2 4 + 96 p1 6 – 2 p1 – 2 p2 Exercise 8.4.2Q1 = SELECT * FROM Ships WHERE name = n;Q2 = SELECT * FROM Ships WHERE class = c;Q3 = SELECT * FROM Ships WHERE launched = y;I = InsertsIndexes Actions None Name Class Launched Name &ClassName &LaunchedClass &LaunchedThreeIndexesQ1502505022502 Q21121212 2 Q35050502650262626 I24446668Average 2 +48p1 -p2 +48p34 +46 p3- 2 p1- 3 p24 +46p1 -2p2 +46p34 + 46p1- 3p2 +22p36 - 4p1- 4p2 +44p36 - 4p1 -5p2 + 20p36 - 44p1 -4p2 + 20p38 - 6p1 -6p2 + 18p3The best choice of indexes (name and launched) has an average cost of 6 - 4p1 - 5p2 + 20p3 per operation.Section 5Exercise 8.5.1Updates to movies that involves title or yearUPDATE MovieProd SET title = ‘newTitle’ where title=’oldTitle’ AND year = oldYear; UPDATE MovieProd SET year = newYear where title=’oldYitle’ AND year = oldYear; Update to MovieExec involving cert#DELETE FROM MovieProdWHERE (title, year) IN (SELECT title, yearFROM Movies, MovieExecWHERE cert# = oldCert# AND cert# = producerC#);INSERT INTO MovieProdSELECT title, year, nameFROM Movies, MovieExecWHERE cert# = newCert# AND cert# = producerC#;Exercise 8.5.2Insertions, deletions, and updates to the base tables Product and PC would require a modification of the materialized view.Insertions into Product with type equal to ‘pc’:INSERT INTO NewPCSELECT maker, model, speed, ram, hd, price FROM Product, PC WHEREProduct.model = newModel and Product.model = PC.model;Insertions into PC:INSERT INTO NewPCSELECT maker, ‘newModel’, ‘newSpeed’, ‘newRam’, ‘newHd’, ‘newPrice’FROM Product WHERE model = ‘newModel’;Deletions from Product with type equal to ‘pc’:DELETE FROM NewPC WHERE maker = ‘deletedMaker’ ANDmodel=’deletedModel’;Deletions from PC:DELETE FROM NewPC WHERE model = ‘deletedModel’;Updates to PC:Update NewPC SET speed=PC.speed, ram=PC.ram, hd=PC.hd, price=PC.price FROM PC where model=pc.model;Update to the attribute ‘model’ needs to be treated as a delete and an insert. Updates to Product:Any changes to a Product tuple whose type is ‘pc’ need to be treated as a delete or an insert, or both.Exercise 8.5.3Modifications to the base tables that would require a modification to the materialized view: inserts and deletes from Ships, deletes from class, updates to a Class’ displacement. Deletions from Ship:UPDATE ShipStats SETdisplacement=((displacement * count) –(SELECT displacementFROM ClasssesWHERE class = ‘DeletedShipClass’)) / (count – 1),count = count – 1WHEREcountry = (SELECT country FROM Classes WHERE class=’DeletedShipClass’); Insertions into Ship:Update ShipStat SETdisplacement=((displacement*count) +(SELECT displacement FROM ClassesWHERE class=’InsertedShipClass’)) / (count + 1),count = count + 1WHEREcountry = (SELECT country FROM Classes WHERE classes=’InsertedShipClass); Deletes from Classes:NumRowsDeleted = SELECT count(*) FROM ships WHERE class = ‘DeletedClass’; UPDATE ShipStats SETdisplacement = (displacement * count) - (DeletedClassDisplacement *NumRowsDeleted)) / (count – NumRowsDeleted),count = count – NumRowsDeletedWHERE country = ‘DeletedClassCountry’;Update to a Class’ displacement:N = SELECT count(*) FROM Ships where class = ‘UpdatedClass’;UPDATE ShipsStat SETdisplacement = ((displacement * count) + ((oldDisplacement – newDisplacement) * N))/countWHEREcountry = ‘UpdatedClassCountry’;Exercise 8.5.4Queries that can be rewritten with the materialized view:Names of stars of movies produced by a certain producerSELECT starNameFROM StarsIn, Movies, MovieExecWHERE movieTitle = title AND movieYear = year AND producerC# = cert# AND name = ‘Max Bialystock’;Movies produced by a certain producerSELECT title, yearFROM Movies, MovieExecWhere producerC# = cert# AND name = ‘George Lucas’;Names of producers that a certain star has worked withSELECT nameFROM Movies, MovieExec, StarsInWhere producerC#=cert# AND title=movieTitle AND year=movieYear AND starName=’Carrie Fisher’;The number of movies produced by given producerSELECT count(*)FROM Movies, MovieExecWHERE producerC#=cert# AND name = ‘George Lucas‘;Names of producers who also starred in their own moviesSELECT nameFROM Movies, StarsIn, MovieExecWHERE producerC#=cert# AND movieTitle = title AND movieYear = year AND = starName;The number of stars that have starred in movies produced by a certain producer SELECT count(DISTINCT starName)FROM Movies, StarsIn, MovieExecWHERE producerC#=cert# AND movieTitle = title AND movieYear = year AND name ‘George Lucas’;The number of movies produced by each producerSELECT name, count(*)FROM Movies, MovieExecWHERE producerC#=cert# GROUP BY name。
sql课后习题答案SQL课后习题答案在学习SQL课程时,课后习题是非常重要的一部分。
通过完成这些习题,我们可以加深对SQL语言的理解,提高自己的实际操作能力。
下面是一些SQL课后习题的答案,希望对大家的学习有所帮助。
1. 查询所有员工的信息```sqlSELECT * FROM employees;```2. 查询员工编号为1001的员工信息```sqlSELECT * FROM employees WHERE employee_id = 1001;```3. 查询员工表中的员工数量```sqlSELECT COUNT(*) FROM employees;```4. 查询员工表中的员工姓名和薪水```sqlSELECT employee_name, salary FROM employees;```5. 查询员工表中薪水大于5000的员工信息```sqlSELECT * FROM employees WHERE salary > 5000;```通过以上的习题答案,我们可以看到SQL语言的基本操作,包括查询、条件筛选、统计等。
这些都是我们在日常工作中经常会用到的操作,对于掌握SQL语言非常重要。
除了以上的习题,我们还可以通过实际的项目案例来练习SQL语言的应用。
例如,可以通过查询销售数据、客户信息等来加深对SQL语言的理解和掌握。
总的来说,通过课后习题的答案,我们可以更好地掌握SQL语言的基本操作,提高自己的实际操作能力,为以后的工作做好准备。
希望大家在学习SQL语言的过程中能够多加练习,不断提升自己的技能水平。
sql实践题目在这个SQL实践题目中,我们将讨论如何根据一个特定的数据库表格进行数据查询和操作。
这个题目将涉及到基本的SQL语句和操作,帮助您加深对SQL语言的理解和应用能力。
首先,我们有一个名为"students"的数据库表格,包含以下字段:学生ID (student_id)、学生姓名(student_name)、学生年龄(student_age)、学生性别(student_gender)、所在班级(class_id)。
任务一:查询所有学生的信息要完成这个任务,您需要使用SELECT语句从"students"表格中选择所有学生的信息。
SQL语句如下:SELECT * FROM students;这条语句将返回所有学生的信息,包括学生ID、学生姓名、学生年龄、学生性别和所在班级。
任务二:查询年龄大于18岁的学生信息要完成这个任务,您需要使用SELECT语句从"students"表格中选择年龄大于18岁的学生信息。
SQL语句如下:SELECT * FROM students WHERE student_age > 18;这条语句将返回所有年龄大于18岁的学生信息。
任务三:查询女生的信息要完成这个任务,您需要使用SELECT语句从"students"表格中选择女生的信息。
SQL语句如下:SELECT * FROM students WHERE student_gender = '女';这条语句将返回所有女生的信息。
任务四:查询学生的姓名和所在班级要完成这个任务,您需要使用SELECT语句从"students"表格中选择学生的姓名和所在班级。
SQL语句如下:SELECT student_name, class_id FROM students;这条语句将返回学生的姓名和所在班级信息。