oracle实例练习.docx
《oracle实例练习.docx》由会员分享,可在线阅读,更多相关《oracle实例练习.docx(16页珍藏版)》请在冰豆网上搜索。
![oracle实例练习.docx](https://file1.bdocx.com/fileroot1/2022-10/28/908eaffb-4d5a-4300-9974-233652ebe373/908eaffb-4d5a-4300-9974-233652ebe3731.gif)
oracle实例练习
1.用sys账户登录,解锁scott账户
代码:
Connectsys/orcl@orcl_clientASSYSDBA
ALTERUSER"SCOTT"ACCOUNTUNLOCK
2.以scott身份登录数据库
connscott/tiger@orcl
3.创建学生表student(sno,sname,sgender,sbirthday,sadd)score(sno,math,english)
代码:
createtablestudent(snochar(3),snamevarchar2(10),sgenderchar2(20),sbirthdaydate,saddvarchar2(50))
createtablescore(snochar(3),mathnumber(4,1),englishnumber(4,1))
4.插入记录
student插入记录:
001,小张,女,1980-8-20,济南
002,小王,男,1983-4-1,莱芜
003,小李,女,1980-5-20,济南
004,小赵,女,1980-5-20,莱芜
005,小孔,女,1982-6-18威海
score插入记录:
(005没参加考试,800是个进修生,不是学校的正式生)
001,90,92
002,85,79
003,80,94
004,78,77
80079,88
代码:
altersessionsetnls_date_format='YYYY-MM-DDHH24:
MI:
SS';
insertintostudentvalues('001','小张','女',to_date('1980-08-20','yyyy-mm-dd'),'济南');
insertintostudentvalues('002','小王','男',to_date('1983-04-01','yyyy-mm-dd'),'莱芜');
insertintostudentvalues('003','小李','女',to_date('1980-05-20','yyyy-mm-dd'),'济南');
insertintostudentvalues('004','小赵','女',to_date('1980-05-20','yyyy-mm-dd'),'莱芜');
insertintostudentvalues('005','小孔','女',to_date('1982-06-18','yyyy-mm-dd'),'威海');
insertintoscorevalues('001','90','92');
insertintoscorevalues('002','85','79');
insertintoscorevalues('003','80','94');
insertintoscorevalues('004','78','77');
insertintoscorevalues('800','79','88');
5.a统计各个地区的学生数
b计算各个学生的总成绩(数学+英语),并且按照成绩由高到低做出学生的成绩单报告(没考试的学生名字不要出现在报告单上,进修生的成绩也不在报告单上)
报告单标题显示:
学号姓名数学英语总成绩
c计算各个学生的总成绩(数学+英语),并且按照成绩由高到低做出学生的成绩单报告(没考试的学生名字也要出现在报告单上,进修生的成绩不在报告单上)
报告单标题显示:
学号姓名数学英语总成绩
代码:
selectsadd地区,count(*)as人数fromstudentgroupbysadd
selectstudent.sno学号,sname姓名,math数学,english英语,(math+english)总成绩fromstudentinnerjoinscoreonscorewherestudent.sno=score.snoorderby总成绩desc
selectstudent.sno学号,sname姓名,math数学,english英语,math+english总成绩fromstudentleftouterjoinscoreonstudent.sno=score.snoorderby总成绩desc
或selectstudent.sno学号,sname姓名,math数学,english英语,(math+english)总成绩fromstudent,scorewherestudent.sno=score.sno(+)orderby总成绩desc;
6.根据student表,创建一个新表student_copy(结构相同,数据只有济南的两个学生)
从student表中查出莱芜得同学信息,插入到student_copy表中
commit//提交刚才的插入
代码:
createtablestudent_copyasselect*fromstudentWheresadd='济南';
insertintostudent_copy(select*fromstudentwheresadd='莱芜');
commit;
7.插入一条新的学生纪录:
006小林男1979-7-9泰安
savepointa//设置保存点a
删除掉学号为003的学生纪录(误删)
rollbacktosavepointa
察看结果
commit(提交插入纪录的操作)/rollback(回滚到插入006记录前的数据状态)
代码:
insertintostudentvalues('006','小林','男',to_date('1979-07-09','yyyy-mm-dd'),'泰安');
select*fromstudent;
savepointa;
deletefromstudentwheresno='003';
rollbacktosavepointa;
select*fromstudent;
commit;/rollback;
8.修改student_copy表名为student2
删除表student2的数据(注意delete/truncate的区别)
删除表student,score,student2
代码:
renamestudent_copytostudent2;
deletefromstudent2;
rollback;
select*fromstudent2;
truncatetablestudent2;
rollback;
select*fromstudent2;
droptablestudent;
droptablescore;
droptablestudent2;
9.创建100个表,table_0到table_99,分别插入数据,第1条数据插入到第1个表。
。
。
第99条数据插入到第99个表
代码:
createorreplaceprocedurecreate_table(startnumberinnumber,endnumberinnumber)as
begin
forv_counterinstartnumber..endnumberloop
executeimmediate'createtabletable_'||v_counter||'(col1number)';
executeimmediate'insertintotable_'||v_counter||'values('||v_counter||')';
Endloop;
end;
execcreate_table('0','99');
10.创建一个emp1表,其结构和数据与emp表完全一致.用游标完成操作:
再sql*plus中显示工资低于1500的职员信息,并显示如果给他们涨30%工资后的工资.
代码:
createtableemp1asselect*fromemp;
setserveroutputon
declare
cursorcurisselect*fromempwheresal<1500;
begin
forv_counterincurloop
dbms_output.put_line(v_counter.empno||''||v_counter.ename||''||v_counter.mgr||''||v_counter.hiredate||''||v_m||''||v_counter.sal||''||v_counter.sal*1.3);
endloop;
end;
11.编写一个pl/sql块,输出所有员工的员工名、员工号、工资和部门号。
代码:
declare
cursorc_empisselect*fromemp;
begin
forv_empinc_emploop
dbms_output.put_line(v_emp.ename||''||v_emp.empno||''||v_emp.deptno||''||v_emp.sal);
endloop;
end;
12.查询名为“smith”的员工信息,并输出其员工号、工资、部门号。
如果该员工不存在,则插入一条新记录,员工号为2007,员工名为“smith”,工资为1500,部门号为10。
如果存在多个名为“smith”的员工,则输出所有名为“smith”的员工号、工资和部门号。
代码:
Declarev_empemp%rowtype;
begin
select*intov_empfromempwhereename='smith';
dbms_output.put_line(v_emp.empno||''||v_emp.sal||''||v_emp.deptno);
exception
whenno_data_foundthen
insertintoemp(empno,ename,sal,deptno)values(2007,'smith',1500,10);
whentoo_many_rowsthen
forvin(select*fromempwhereename='smith')loop
dbms_output.put_line(v.empno||''||v.sal||''||v.deptno);
endloop;
end;
13.创建一个存储过程,以员工号为参数,输出该员工的工资。
createorreplaceprocedureshowsal(p_empnoemp.empno%type)
asv_salemp.sal%type;
begin
selectsalintov_salfromempwhereempno=p_empno;
dbms_output.put_line(v_sal);
end;
begin
showsal(7844);
end;
14.创建一个函数,以