Oracle笔记7——Oracle相关题目

Oracle 试卷

1.创建用户abc,密码为123,并设置密码过期

create user abc identified by 123  password expire;

2.授予用户abc拥有r1角色和创建视图的权限

 create role r1;

 grant create view to r1;

 grant r1 to abc;

3.授予用户abc查询scott用户emp表的权限,再撤销此用户权限

 grant select on scott.emp to abc;

 revoke select on scott.emp from abc;

4.如何查询用户创建的所有的表?如何查询用户创建的所有的触发器、函数、存储过程?

select * from user_procedures;

select * from user_triggers;

select * from user_tables;

select * from user_objects where object_type='FUNCTION';

5.创建表tblStudent,sid主键约束,score数值类型(1位小数,3位整数),name唯一约束,字符型(最多可存5个汉字)。

 create table tblStuduent(

sid number(11) primary key,

score number(4,3),

name varchar(10) unique

);

6.新增一个列sgender(检查约束’M’或者‘P’),修改列名name为sname,并且最多可存10各汉字。

alter table tblStudent add  (sgender varchar(2) check(sgender='M' or sgender='P'));

7.将学号1~100的学生记录导出到tblStu_backup(此表不存在)

create table tblStu_backup

as select * from tblStudent where id between 1 and 100;

8.求round(3.5)round(3.456,2)round(3456,-2)trunc(3.456)trunc(3.456,2)trunc(3.456,-2)?

4,4.46,3400,3,3.45,0

9.问truncate table 和 delete from 有什么区别?

Tr uncate table 是删除全部释放表空间,效率高,但是只能删除全部表,delete from 删除表不释放表空间,效率低,可用删除指定数据,如果开启事务的话需要commit提交

10.in , =any, >any,<any,>all ,<all 分别是什么意思?

in(子查询):判断是否与子查询结果的任意一个返回值相同

 

<any(子查询): 比子查询内任意一个结果小即可==>  小于子查询结果的最大值

<all(子查询):比子查询内所有的结果都小==》小于子查询结果的最小值

>any:大于子查询结果的最小值

>all:大于子查询结果的最大值

=any: 相当于 in

=all:不存在

11.查询工资比SMITH高的雇员信息?

select * from empcp where sal>(select * from empcp where ename='SMITH');

12.哪些个部门的雇员最高工资和最低工资差额超过1000?

select deptno from empcp group by deptno having max(sal)-min(sal)>1000;

13.将当前时间转换为“2016年9月18日 21:27:21”格式输出

select to_char(sysdate, 'yyyy"年"mm"月"dd"日" hh24:mi:ss') from dual;

14.行列转换(姓名、课程、成绩-->姓名、java、Android、IOS)

select username,

sum(decode(cource,'java',score,null))as java,

sum(decode(cource,'Android',score,null))as Android,

sum(decode(cuorce,'IOS',score,null))as IOS

from t_stu

group by username;

15.用rowid删除重复记录

delete from t_student where rowid not in (

  select max(rowid) from t_student group by id, name, addr);

16.求11到20行(分页)

select * from(

select rownum rn,t.* from(

select * from t_stu;) t where rownum <= 20;) tt

where tt.rn >=11;

17.create table t_salary (id int,name varchar(10),salary int);

     create table t_sale(id int ,quantity int);

如果销售总额超过15000,就给每个员工涨10%工资(用exists实现)

update t_salary set salary=salary*1.1 where exists (select sum(quantity) from t_sale having sum(quantity)>15000);

18.显示今年圣诞节是星期几?

select to_char(to_date('2017-12-25','yyyy-mm-dd'),'day')from dual; 

19.union,union all,intersect,minus分别什么意思?

Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;

Union All,对两个结果集进行并集操作,包括重复行,不进行排序;

Intersect,对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;

Minus,对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。

可以在最后一个结果集中指定Order by子句改变排序方式。

20.用case...end case 显示雇员生日的农历属相(birth列为’1980-12-21‘格式)80猴81鸡82狗

select hiredate,

  case to_char(hiredate, 'yy')

    when '80' then '猴'

    when '81' then '鸡'

    when '82' then '狗'

    else '未知'

  end "生肖"

    from emp;

 

编程题

1.编写PL/SQL:求前100个素数

declare

  flag boolean;

begin

  for i in 2..100

  loop

    flag := true;

    for j in 2..i-1

    loop

      if mod(i, j)=0 then

        flag := false;

        exit;

      end if;

    end loop;

    if flag then

      dbms_output.put_line(i);

    end if;

  end loop;

end;

2.编写游标:修改emp表中的数据,将sal小于2000的工资上调20%,2000~4000的工资上调10%,4000以上的上调5%

declare

cursor c_emp is select sal,empno from emp;

v_sal emp.sal%type;

begin

for item in c_emp loop

case when item.sal < 2000 then

v_sal := item.sal * 1.2;

when item.sal > =2000 and item.sal <=4000 then

v_sal:= item.sal*1.1;

when item.sal>4000 then

v_sal := item.sal*1.05;

end case;

update emp set sal = v_sal where empno = item.empno

end loop;

end;

 

  1. 编写触发器:实现修改雇员薪水时,不能为负值且涨幅不能超过10%

create or replace trigger tr_sal

before update on empcp

for each row

declare

begin

if :old.sal < 0 or :new.sal > :old.sal*1.01

then raise_application_error('不能为负切不能超过10%');

end;

 

4.编写函数:getScore,参数为学生学号,返回成绩,如果不及格就返回60,如果学号不存在就返回0

create or replace function getScore(

sid number

)return number

as

v_score number(20);

v_count number(20);

begin

select count(0) into v_count from tblStudent where id=sid;

if v_counr =0 then v_score:=0

elsif v_score < 60 then v_score :=60;

end if;

exception

when no_data_found

then dbms_output.put_line('找不到数据');

return v_score;

end;

 

5.编写存储过程:getStudent,输入参为学生学号,输出参为姓名和成绩,因为所有人考试太差,将每人成绩开平方根后再乘以10显示,如果这样家还不及格,成绩就显示-1(要写PL调用)

create or replace procedure getStudent(

id number,

v_name out varchar2,

v_score out number

)

as

begin

select name,score into v_name,v_score from tblStudent where id = sid;

if sqrt(v_score)*10<60 then v_score :=-1;

else v_score:=sqrt(v_score)*10;

exception

when no_data_found

then dbms_output.put_line('数据未找到');

end if;

end;

 

declare

v_val number(10) :=10;

v_name tblStudent.name%type;

v_score tblStudent.score%type;

begin

 getStudent(v_val,v_name,v_score);

end;

/

猜你喜欢

转载自blog.csdn.net/u011189148/article/details/81416978
今日推荐