Oracle数据库应用与实训教程 实训习题答案 (肖璞 第2版)北邮_第1页
Oracle数据库应用与实训教程 实训习题答案 (肖璞 第2版)北邮_第2页
Oracle数据库应用与实训教程 实训习题答案 (肖璞 第2版)北邮_第3页
Oracle数据库应用与实训教程 实训习题答案 (肖璞 第2版)北邮_第4页
Oracle数据库应用与实训教程 实训习题答案 (肖璞 第2版)北邮_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实训练习答案

实训1

4、

⑴INFo

因为存在部分函数依赖:员工号一姓名,性别部门号一部门名

即存在非主属性对码的部分函数依赖,因此不是2NF0

存在删除异常。当删除E2员工信息时,同时也会删除C1部门的信息。

(3)

员工(员工号,姓名,年玲,性别,所在部门号),主码是员工号

部门(部门号,部门名),主码是部门号

解决了数据冗余、插入异常和删除异常的问题。

5、

(1)

仓库(仓库号,面积,电话号码),主码是仓库号

零件(零件号,零件名称,规格),主码是零件号

零件存放(零件号,仓库号,库存数量),主码是零件号+仓库号

(2)

员工(员工编号,员工姓名,部门编号),土码是员工号

部门(部门编号,部门名称,部门所在城市),主码是部门编号

项目(项目编号,项目名称,预算),主码是项目编号

员工参与项目(项目编号,员工编号,职务,参加项目日期),主码是项目编号+员工编号

实训2

E0

八//J

3、设如下关系,计算下列关系代数:

R3

(1)YT

R31=TTYT(R)=

dc

fe

eD

⑵/?32=6p>5r)T=e(R)PQTY

9aef

7gef

⑶R3=Rx]V条件为[4]=[2]

pQR.TR.YW.TW.YB

2bcdcdm

2bcdcdn

9aefdfn

2befdfn

7gefdfn

7gcdcdm

7gcdcdn

4)R4=n12MlM6](q3H5](Rx0)

43]=[5](RxD)=

PQR.TR.YD.TD.Y

2bcdcd

9aefef

2befef

7gefef

7gcdcd

R4=口[2则,血(q3H5](RX°))=

QPD.Y

b2d

a9f

b2f

g7f

g7d

PQ

⑸R35=R+D=11

IJ

4、设有四个关系:

学生信息:S(S#,SNAME,AGE,SEX)

教师信息:T(T#,TNAME,AGE,SEX)

课程信息:C(C#,CNAME,T#)

选修:SC(S#,C#,GRADE)

请用关系代数表达式表示下列查询语句:

(1)查询年龄大于20岁的男生的学号与姓名。

"s#,SNAME("AGE>20andsex=0⑸)

(2)查询刘明老师所谩课程的课程号、课程名。

"cC#CNAME("TJ#=C.T#andT.TNAME=,刘明,(TXC))

(3)查询学号为S3的学生所学课程的课程名与任课教师名。

"cNAME,TNAME("sC.C#=C.C#andC.T#=T.T#andS#='S3'(SCxCxT))

5、设有以下关系:

学生(学号,姓名,性别,专业,出生H期)

教师(教师编号,姓名,所在部门,职称)

授课(教师编号,学号,课程编号,课程名称,教材,学分,成绩)

请用关系代数表达式表示下列查询语句:

(1)查找教授“数据库原理”课程的任课教师编号和姓名。

TT课程授课.教他编号=敦卿.敦他粕号and课程名称='数据库原理,(授课X教师))

教师。教如编号,姓名

(2)查找学习"英语〃课程的〃计算机应用〃专业学生的学号、姓名和成绩。

"学生.学号,姓名,成绩(“授课•学号=学生•学号and课程名称=,数据库原理,and专业='计算机应用'

(授课X学生))

实训3

1.

createtabledept3

(

dnointprimarykey,

dnamevarchar(20)notnull

createtablestudent3

(

snointprimarykey,

snamevarchar2(10)notnullunique,

sexchar(l),

dnoint,

foreignkey(dno)referencesdept3(dno)

);

createtableteacher3

(

tnointprimarykey,

tnamevarchar2(20)notnull

);

createtablecourse3

(

enoint,

cnamevarchar2(20)notnull,

tnoint,

creditint,

primarykey(eno),

foreignkey(tno)referencesteacher3(tno)

createtablesc3

snoint,

enoint,

gradeint,

primarykey(sno,eno),

foreignkey(sno)referencesstudent3(sno),

foreignkey(cno)referencescourse3(cno)

);

createviewcs_student3

asselect*fromstudents

wherednoin(selectdnofromdept3wheredname='信息技术’);

2.

altertablestudentsadd(sageint);

altertablestudentsmodify(sagesmallint);

3.

insertintodept3values(10,'信息技术,);

insertintodept3values(20J软件工程,);

insertintodept3values(30J网络工程。;

insertintostudentsvalues(1001,,张天',0,10,20);

insertintostudentsvalues(1002,'李兰',1,10,21);

insertintostudentsvalues(1003,'文lj茜',1,20,21);

(马朝阳)

insertintostudent3values1004,'',0z30,22;

insertintoteacher3values(101,'张星');

insertintoteacher3values(102,'李珊');

insertintoteacher3values(103,'赵天');

insertintocourse3values(l,'数据结构101,4);

insertintocourse3values(2,'数据库原理102,3);

insertintocourse3valuesa'离散数学103,4);

insertintocourse3values(4,'C语言程序设计,,101,2);

insertintosc3values(1001,1,80);

insertintosc3values(1001,2,85);

insertintosc3values(1002,1,78);

insertintosc3values(1002,2,82);

insertintosc3values(1002,3,86);

insertintosc3values(1003,1,92);

insertintosc3values(1003,3,90);

insertintosc3values(1004,4,90);

4.

select*fromstudent3;

selectsname,sagefromstident3

wheresex=l;

selectsno,grade

fromsc3

wherecno=landgradebetween80and89

orderbygradedesc;

selectdno,count(*)

fromstudents

groupbydno;

select*fromcs_student3;

5.

selectsname,sage

fromstudentss,dept3d

wheres.dno=d.dnoanddname='软件工程,

andsage<=21andsex=l;

selectcourse.*,sc.sno,sc.g,ade

fromsc3,courses

whereo=oandcname=’数据结构';

selects.sno,sname

fromsc3,course3c,student3s

whereo=oands.sno=sc.snoandcname=,数据结构,;

selectcount(sno)

fromstudentss,dept3d

wheres.dno=d.dnoanddname='软件工程';

6.

updatestudent3setsage=sage+2wheresname='刘茜';

updatesc3setgrade=grade-10

whereeno=(selectenofromcourse3wherecname=,数据结构,)

andsno=(selectsnofromstudentswheresname='张天');

7.

deletefromsc3wheresnoin(selectsnofromstudentswheresname='马朝阳');

dropviewcs_student3;

实训5

2.

createuserulidentifiedbyul;

grantcreatesession,createtablespace,altertablespace,droptablespace,alterdatabasetoul;

3.

connectul/ul

createtablespacedatal

datafile'%oracle_base%\oradata\dbl\dsl.dbf'size50m

uniformsize128k;

4.

altertablespacedataladddatafile'%oracle_base%\oradata\dbl\ds2.dbf'size10m;

5.

alterdatabasedatafile'%oracle_base%\oradata\dbl\ds2.dbf'resize15m;

6.

createtemporarytablespacetempitempfile'%oracle_base%\oradata\dbl\templ.db「size10m;

7.

connsystem/systempwd

alteruseruldefaulttablespacedataltemporarytablespacetempi;

实训6

1.

connsystem/systempwd

createuseru2identifiedbyu2;

connu2/u2无法连接数据库,因为缺少权限

connsystem/systempwd

grantcreatesessiontou2;

connu2/u2

connsystem/systempwd

alteruseru2identifiedbyu22;

connu2/u22

2.

connsystem/systempwd

createuseru3identifiedbyu3

defaulttablespaceusersquota2Monusers;

grantcreatesession,createtabletou3;

connu3/u3

createtabletl(emplDvarchar2(10),empNamevarchar2(10));

connsystem/systempwd

revokecreatetablefromu3;

connu3/u3

createtablet2(emplDvarchar2(10),empNamevarchar2(10));

3.

connsystem/systempwd

createrolemanagers;

grantcreatesession,createtable,createviewtomanagers;

select*fromrole_sys_priv$whererole='MANAGERS';

createuseru4identifiedbyu4

defaulttablespaceusersquota2Monusers;

grantmanagerstou4;

connu4/u4

createtablet2(idint);

4.

connsystem/systempwd

createuseru5identifiedbyu5;

createuseru6identifiedbyu6;

grantconnect,resourcetou5withadminoption;

connu5/u5

grantconnecttou6;

connsystem/systempwd

revokeconnect,resourcefromu5;

connu5/u5不能连接数据库

connu6/u6可以连接数据库

5.

connsystem/systempwd

createuseru7identifiedbyu7;

createuseru8identifiedbyu8;

grantconnect,resourcetou7,u8;

createtablet3(idint);

insertintot3values(l);

grantselectont3tou7withgrantoption;

connu7/u7

select*fromsystem.t3;

grantselectonsystem.t3tou8;

connu8/u8

select*fromsystem.t3;

connsystem/systempwd

revokeselectont3fromu7;

connu7/u7

select*fromsystem.t3;无法访问

connu8/u8

select*fromsystem.t3;无法访问

实训7

1.

createtabletl

(

enonumber(4)notnullprimarykey,

enamevarchar2(20)notnull,

sexchar(l)notnull,

birthdaydate,

salarynumber。,2)default(O)

);

2.

insertintotlvalues。,'丁一',0,null,null);

insertintotlvalues(2,'T—1,null,null);

3.

createtablet2

as

select*fromtlwheresex=0;

4.

createtablet3

as

select*fromtlwhere1=2;

5.

altertablet2addconstraintpklprimarykey(eno);

6.

createtableauthor

(

idnumber(3),

namevarchar2(20),

salnumber(6,2)

);

altertableauthoradd(addressvarchar2(100));

altertableauthorrenamecolumnsaltosalary;

altertableauthoraddconstraintpk_lprimarykey(id);

altertableauthoraddconstraintck_lcheckfsalarybetween0and10000);

altertableauthordropcolumnaddress;

droptableauthor;

7.

insertintostul

select*fromstu2;

8.

select*fromstul;

select*fromstu2;

updatestulsetname=(selectnamefromstu2wheresno=7i

wheresno=7;

实训8

1.

select*

fromemp8

wheredno=30;

2.

selectename,eno,dno

fromemp8

wherejob='CLERK,;

3.

selectinitcap(ename)

fromemp8;

4.

select*

fromemp8

wherelength(ename)=5;

5.

select*

fromemp8

whereenamelike'%R%';

6.

selectsubstr(ename,1,3)

fromemp8;

7.

select*

fromemp8

wheremonths_between(sysdate,hiredate)>=60;

8.

select*

fromemp8

orderbyename;

9.

selectename,hiredate

fromemp8

orderbyhiredate;

10.

selectenamejob,sal

fromemp8

orderbyjobdesc,sal;

11.

selectto_char(hiredate,'yyyy-mm-dd1)

fromemp8;

12.

selectconcat(concat(lower(ename),"'ssalaryis'),sal)

fromemp8;

selectlower(ename)||"'ssalaryis'||sal

fromemp8;

13.

select*

fromemp8

wherejobin('ANALYST;'SALESMAN');

select*

fromemp8

wherejob='ANALYST'orjob='SALESMAN';

14.

select*

fromemp8

wherednoin(selectdnofromdept8wheredname='research');

15.

select*

fromemp8

wheresal>(selectavg(sal)fromemp8);

16.

select*

from(selectdno,count(*),trunc(avg(sal))

fromemp8

groupbydno)

orderbydno;

17.

selectjob,min(sal),max(sal),avg(sal)

fromemp8

groupbyjob;

18.

select*

from(select*fromemp8orderbysaldesc)

whererownum<4;

19.

selectename,hiredate

fromemp8

wherehiredate=(selectmin(hiredate)fromemp8)

union

selectename,hiredate

fromemp8

wherehiredate=(selectmax(hiredate)fromemp8);

20.

selectename,

trunc(months_between(sysdate/hiredate))asaaa

fromemp8

orderbyaaadesc;

21.

selectsysdate

fromdual;

selectdistinctsysdate

fromemp8;

22.

selectto_char(sysdate,'mm-dd-yyyy')

fromdual;

23.

selectlast_day(sysdate)

fromdual;

实训9

1.

违反了主键的唯一性约束

createuniqueindexind_naneonemployee(name);

selectindex_name,table_name,status

fromuserjndexes

wheretable_name='EMPLOYEE';

违反了name字段建立的唯一性约束

2.

createviewv_stul

as

select*

fromstu

wheresex=O

orderbysnodesc;

select*fromv_stul;

selectcount(*)fromv_stul

wheresno>100;

dropviewv_stul;

3.

connsystem/systempwd

createsequencesno_seq

incrementby1

startwith100

maxvalue999

nocache

nocycle;

createtablestu(

snovarchar(12)primarykey,

namevarchar2(20),

ageint,

sexint);

insertintostuvalues('23A'||sno_seq.nextval,'aaa'z21,1);

insertintostuvalues('23A'||sno_seq.nextval,'bbb',20,0);

select*fromstu;

selectsno_seq.currvalfromdual;

selectsequence_namefromuser_sequences;

4.

connsystem/systempwd

createuseru9identifiedbyu9;

defaulttablespaceusers

quotaunlimitedonusers;

grantcreatesession,createtable,createpublicsynonymtou9;

connu9/u9

createtabletablel(idint);

insertintotablelvalues(l);

insertintotablelvalues(2);

createpublicsynonymtlfortablel;

select*fromtl;

connsystem/systempwd

select*fromdba_synonyms

wheretable_owner='U9';

实训10

1.

setserveroutputon

declare

inumber:=0;

v_sumnumber:=0;

begin

whilei<=100loop

v_sum:=v_sum+i;

i:=i+l;

endloop;

dbms_output.put_line(v_sum);

end;

2.

declare

v_enoemp8.eno%type;

v_enameemp8.ename%type;

begin

v_eno:=7369;

selectenameintov_ename

fromemp8

whereeno=v_eno;

dbms_output.put_line(v_ename);

end;

3.

declare

v_enoemp8.eno%type;

typerecord_emp8isrecord(

enamevarchar2(20),

salarynumber,

dnamevarchar2(40));

v_erecord_emp8;

begin

v_eno:=7900;

selectename,sal,dnameintov_e

fromemp8,dept8

whereemp8.dno=dept8.dnoandeno=v_eno;

dbms_output.put_line(v_e.ename);

dbms_output.put_line(v_e.salary);

dbms_output.put_line(v_e.dname);

end;

declare

v_dnoemp8.dno%type;

v_countint;

begin

v_dno:=10;

selectcount(*)intov_count

fromdept8wheredno=v_dno;

ifv_count=0then

dbms_output.put_line('该部门不存在');

else

casev_dno

when10then

updateemp8setsal=sal*l.lwheredno=v_dno;

when20then

updateemp8setsal=sal*0.95wheredno=v_dno;

else

updateemp8setsal=sal+300wheredno=v_dno;

endcase;

endif;

end;

实训11

1.

setserveroutputon

declare

cursoremp8_cursorisselect*fromemp8;

v_emp8emp8%rowtype;

begin

openemp8_cursor;

loop

fetchemp8_cursorintov_emp;

exitwhenemp8_curscr%notfound;

dbms_output.put_line(v_emp8.ename||v_emp8.sal);

endloop;

closeemp8_cursor;

end;

2.

altertabledept8add(totalpersonint);

declare

cursordept8_cursorisselectdnofromdept8forupdate;

v_dnodept8.dno%type;

v_countint:=0;

begin

opendept8_cursor;

loop

fetchdept8_cursorintov_dno;

exitwhendept8_cursor%notfound;

selectcount(*)intov_countfromemp8wheredno=v_dno;

updatedept8settotaberson=v_countwherecurrentofdept8_cursor;

endloop;

closedept8_cursor;

end;

/

selectdno,totalpersonfromdept8;

3.

insertintoemp8values(7749,'HANS','SALES',7782,,09-3月-15',2300,NULL,10);

declare

v_enoemp8.eno%type;

v_salemp8.sal%type;

exexception;

begin

v_eno:=7749;

selectsalintov_salfromemp8whereeno=v_eno;

ifv_sal<3000then

raiseex;

endif;

exception

whenno_data_foundthen

dbms_output.put_line('nodatafound');

whentoo_many_rowsthen

dbms_output.put_line('toomanyrows');

whenexthen

dbms_output.put_line(v_eno11'salaryistoolow');

end;

实训12

1.

setserveroutputon

createorreplaceprocedureselect_dname

(v_enoinempl2.eno%type,

v_dnameoutdeptl2.dname%type

)

is

begin

selectdnameintov_dname

fromempl2,deptl2

whereempl2.dno=deptl2.dnoandeno=v_eno;

exception

whenno_data_foundthen

dbms_output.put_line('nodatafound');

whenothersthen

dbms_output.put_line('unkownerror');

end;

/

调用该存储过程:

varv_dnamevarchar2(20);

execselect_dname(7369,:v_dname);

printv_dname;

varv_dnamevarchar2(20);

execselect_dname(7777,:v_dname);

printv_dname;

2.

createorreplacefunctionvmax

(ainnumber,

binnumber,

cinnumber

)

returnnumber

is

v_resultnumber;

v_exexception;

begin

ifa<0orb<0orc<0then

raisev_ex;

endif;

ifa>bthen

ifa>cthen

v_result:=a;

else

v_result:=c;

endif;

else

v_result:=b;

ifc>bthen

v_result:=c;

endif;

endif;

returnv_result;

exception

whenv_exthen

温馨提示

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

最新文档

评论

0/150

提交评论