Oracle-数据库所有查询命令

1.简单查询

1. 消除重复的姓
select distinct e.last_name as 姓氏 from employees e

2. 计算员工的月收入(工资+佣金)
select salary,salary*(nvl(commission_pct,0)+1) as 工资佣金 from employees

–3. 查询在 1998 年入职的员工
select * from employees e
where to_char(e.hire_date,‘yyyy’)=‘1998’

–4. 查询姓以 B 开头的员工
select * from employees e
where e.last_name like ‘B%’

–5. 查询部门号为 10 或者 20 或者 30 的员工
select *from employees e
where e.department_id in (10,20,30)

-6. -查询在 1998-02-02 和 1998-05-01 之间入职的员工的姓氏、职务标识和起始日期
select t.last_name,t.job_id,t.hire_date from employees t
where t.hire_date between
to_date(‘19980220’,‘yyyyMMdd’) and to_date(‘19980501’,‘yyyyMMdd’);

–7. 显示员工名字中的第三个字母为“a”的所有员工的姓氏
select t.last_name from employees t
where t.last_name like ‘__a%’;

–8. 显示赚取佣金的所有员工的姓氏、薪金和佣金,按薪金和佣金的降序对数据进行排序
select t.last_name , t.salary,t.commission_pct from employees t
where t.commission_pct is not null
order by t.salary desc,t.commission_pct desc;

2.时间函数

1. 显示当前日期
select sysdate from dual;

–2. 显示当前日期,格式为 yyyy 年 mm 月 dd 日,别名为 hday
select to_char(sysdate,‘yyyy"年"mm"月"dd"日"’) hday from dual;

–3. 编写一个查询,显示姓名以 J、A 或 M 开始的所有员工的姓氏(第一个字母大写,其余字母小写)和姓氏的长度,给每列一个合适的标签
select initcap(t.last_name) lname,length(t.last_name) len from employees t
where substr(upper(last_name),0,1) in(‘J’,‘A’,‘M’);

–4 . 计算每位员工截止到当前时间入职的星期数,别名为 weeks_worked。按聘用的星期数
对结果进行排序。该星期数舍入到最接近的整数。同时显示员工的名字
select t.first_name,round((sysdate-t.hire_date)/7) as weeks_worked from employees t;

–5. 创建一个查询。显示所有员工的薪金。将薪金格式规定为 15 个字符长,左边填充$ /
select lpad(t.salary,15,’$’) sal from employees t;

–6. 显示每位员工的姓氏、聘用日期和薪金复核日期,薪金复核日期是服务六个月之后的第一个星期一。将该列标记为 review,格式类似于:nday,the Thirty-First of July,2000
select t.last_name,t.hire_date,

/next_day(add_months(t.hire_date,6),2) from employees t/
to_char(next_day(add_months(t.hire_date,6),2),
‘fmday,"the “ddspth” of "month,yyyy’,‘nls_date_language=english’) review
from employees t;
注:to_char 的第三个参数用于设置本次查询使用的国家和地区,ddspth 中 spth 为后缀,表示 Spelled, ordinal number

–7. 显示员工的姓氏、聘用日期和该员工在星期几开始工作的
select t.last_name,t.hire_date,to_char(t.hire_date,‘DAY’) as start from employees t;

–8. 创建一个查询,使其显示员工的姓氏,并用星号指明他们的年薪。每个星号代表一千美元。按薪资降序排列数据
select t.last_name||lpad(’ ‘,trunc(t.salary/1000)+1,’*’),t.salary from employees t
order by t.salary desc;

–9. 创建一个查询。使其显示员工的姓氏和佣金额。如果某位员工不赚取佣金则显示“No
Commission”,将该列标记为 COMM
select t.last_name,nvl(to_char(t.salary*t.commission_pct,‘999,999.99’),‘No Commission’) COMM
from employees t;

–10. 使用 decode 函数编写一个查询,使其按照以下数据根据 JOB_ID 列的值显示所有员工
的级别,同时显示员工的姓氏

job_id grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
都不 0
select t.last_name,t.job_id job,
decode(t.job_id,‘AD_PRES’,‘A’
,‘ST_MAN’,‘B’
,‘IT_PROG’,‘C’
,‘SA_REP’,‘D’
,‘ST_CLERK’,‘E’
,‘0’) as grade
from employees t;

–11. 使用 case 语法重写一遍上题
select t.last_name,t.job_id job,
case t.job_id
when 'AD_PRES’then ‘A’
when ‘ST_MAN’ then ‘B’
when ‘IT_PROG’ then ‘C’
when ‘SA_REP’ then ‘D’
when ‘ST_CLERK’ then ‘E’
else ‘0’
end as grade
from employees t;

–12. 显示当前日期,本月最后一天的日期,以及本月还剩多少天
select sysdate,last_day(sysdate) “last”,last_day(sysdate)-sysdate “days left” from dual;

–13. 显示今年的第一天
select trunc(sysdate,‘year’) from dual;

–14. 显示本月的第一天
select trunc(sysdate,‘month’) from dual;

–15. 最近一个星期四是哪天(不含今日)
select next_day(sysdate,5) from dual;

3.分组查询

1. 求所有员工的平均工资、最高工资、最低工资和工资总和,给予适当的别名
select avg(t.salary),max(t.salary),min(t.salary),sum(t.salary) from employees t;

–2. 查询出各部门的部门编号以及各部门的总工资和平均工资,按部门编号升序排列。
select t.department_id,sum(t.salary),avg(t.salary) from employees t
group by t.department_id
order by t.department_id;

–3. 显示每种工作的人数
select t.job_id,count(*) from employees t
group by t.job_id;

–4. 显示员工最高工资超过 10000 的部门的 id 及其员工最高工资
select t.department_id,max(t.salary) m from employees t
group by t.department_id
having max(t.salary)>10000;

–5. 显示平均工资最高的部门 id 及其平均工资
select * from(
select t.department_id,avg(t.salary)
from employees t
group by t.department_id
order by avg(t.salary) desc
)
where rownum<2;

4.多表查询

多表查询,基本知识:
什么是多表查询?
什么是笛卡尔积?
怎样避免笛卡尔积?
要将 n 个表联结在一起,至少需要多少个联结条件?
什么是等值联结?
如何在联结条件外附加限制条件?
如何限定模糊的列名?
如何进行非等值联结?
什么是外联结?
什么是自联结?
如何用 sql1999 标准表达:笛卡尔积、自然联结、等值联结、内联结、左外联结、右外
联结和全外联结?
联结的分类:
Joins that are compliant with the SQL:1999 standard include the following:
Natural joins:
NATURAL JOIN clause
USING clause
ON clause
Outer joins:
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN
Cross joins
sql1999 联结语法:
SELECT table1.column, table2.column
FROM table1
[NATURAL JOIN table2]
|[JOIN table2 USING (column_name)]
|[JOIN table2 ON (table1.column_name = table2.column_name)]
|[LEFT|RIGHT|FULL OUTER JOIN table2 ON (table1.column_name = table2.column_name)]
|[CROSS JOIN table2];
–1. Write a query for the HR department to produce the addresses of all the departments. Use
the LOCATIONS and COUNTRIES tables. Show the location ID, street address, city, state or
province, and country in the output. Use a NATURAL JOIN to produce the results.
select lct.location_id,lct.street_address,lct.state_province,cty.country_name
from locations lct
natural join countries cty;
–2. The HR department needs a report of employees in Toronto. Display the last name, job,
department number, and the department name for all employees who work in Toronto.
select emp.last_name,emp.job_id,dpt.department_id,dpt.department_name
from employees emp
join departments dpt on emp.department_id=dpt.department_id
join locations lct on dpt.location_id=lct.location_id
where lct.city=‘Toronto’;
–3. Create a report for the HR department that displays employee last names, department
numbers, and all the employees who work in the same department as a given employee. Give
each column an appropriate label. Save the script to a file named lab_06_06.sql.
select emp.last_name Employee,emp.department_id,colleague.last_name colleague
from employees emp
join employees colleague on emp.department_id=colleague.department_id
where emp.employee_id<>colleague.employee_id
order by Employee;
–4. The HR department needs a report on job grades and salaries. To familiarize yourself with the
JOB_GRADES table, first show the structure of the JOB_GRADES table. Then create a query that
displays the name, job, department name, salary, and grade for all employees.*/
–建表
create table JOB_GRADES(
LOWEST_SAL NUMBER(6),
HIGHEST_SAL NUMBER(6),
GRADE_LEVEL CHAR(1)
)
–插入数据
insert into JOB_GRADES (LOWEST_SAL, HIGHEST_SAL, GRADE_LEVEL)
values (30000, 40000, ‘F’);
insert into JOB_GRADES (LOWEST_SAL, HIGHEST_SAL, GRADE_LEVEL)
values (20000, 30000, ‘E’);
insert into JOB_GRADES (LOWEST_SAL, HIGHEST_SAL, GRADE_LEVEL)
values (15000, 20000, ‘D’);
insert into JOB_GRADES (LOWEST_SAL, HIGHEST_SAL, GRADE_LEVEL)
values (8500, 15000, ‘C’);
insert into JOB_GRADES (LOWEST_SAL, HIGHEST_SAL, GRADE_LEVEL)
values (5500, 8500, ‘B’);
insert into JOB_GRADES (LOWEST_SAL, HIGHEST_SAL, GRADE_LEVEL)
values (2000, 5000, ‘A’);
commit;
select emp.last_name Employee,emp.salary,g.grade_level
from employees emp
join job_grades g on emp.salary between g.lowest_sal and g.highest_sal;
–5. The HR department wants to determine the names of all the employees who were hired after
Davies. Create a query to display the name and hire date of any employee hired after employee
Davies.
select emp.last_name Employee,emp.hire_date
from employees emp
join employees clg on emp.hire_date > clg.hire_date
where clg.last_name=‘Davies’
order by emp.hire_date;
–6. The HR department needs to find the names and hire dates of all the employees who were
hired before their managers, along with their managers’ names and hire dates. Save the script to
a file named lab_06_09.sql.
select emp.last_nam,emp.hire_date,mgr.last_name,mgr.hire_date
from employees emp
join employees mgr on emp.manager_id=mgr.employee_id and emp.hire_date < mgr.hire_date;

5.多表连接

using 子句
select e.employee_id, e.last_name, d.location_id
from employees e join departments d
using (department_id);
–on 子句
select e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id
from employees e
join departments d on (e.department_id=d.department_id);
–使用 on 子句创建多表连接
select employee_id, city, department_name
from employees e
join departments d on d.department_id=e.department_id
join locations l on d.location_id=l.location_id;
–左外连接
select e.last_name, e.department_id, d.department_name
from employees e
left outer join departments d on (e.department_id=d.department_id);
–右外连接
select e.last_name, e.department_id, d.department_name
from employees e
right outer join departments d on (e.department_id=d.department_id) ;
–满外连接
select e.last_name, e.department_id, d.department_name
from employees e
full outer join departments d on (e.department_id=d.department_id) ;
–增加条件连接
select e.employee_id, e.last_name, e.department_id,d.department_id,d.location_id
from employees e
join departments d on (e.department_id=d.department_id)
and e.manager_id=149

6.子查询

基础知识:
什么是子查询?
子查询能解决什么类型的问题?
子查询可以用在什么位置?
子查询有哪些类型?
核心知识:
–1. 子查询概念:
子查询就是按顺序执行系列查询并将前一个查询的结果作为下一个查询使用的值
–2. 子查询语法
select select_list from table
where expr operator(
select select_list from table
);
注:operator 包含比较表达式,如: >, =, IN ,等
2.1 子查询(内查询)先于主查询(外查询)执行
2.2 子查询的结果用于外查询
–3. 查询工资比 Abel 高的员工的姓氏和工资
select last_name, salary from employees
where salary > (
select salary from employees
where last_name=‘abel’
);
–4. 子查询中的空值:
in(…)的含义等价于= any(…),所以子查询中是否有空值,对结果没有影响
但是,not in(…)的含义等价于<> all(…),如果子查询中出现空值,整个表达式为空
自然:任意比较 all(…),如果子查询中出现空值,整个表达式为空
–1. 查询部门名称为 Marketing 和 IT 的员工信息
select * from employees e
where e.department_id in (
select d.department_id from departments d
where d.department_name in(‘Marketing’,‘IT’)
);
–2. 查询不是经理的员工的信息
select *
from employees e
where e.employee_id not in
(select distinct e1.manager_id
from employees e1
where e1.manager_id is not null);

–3. 查询工资比 10 号部门中其中一个员工低的员工信息
select *from employees e
where e.salary<any (
select e1.salary from employees e1
where e1.department_id=10
);
–4. 显示和 Austin 同部门,工资低于 Baer 的雇员有哪些
select e.last_name from employees e
where e.department_id=(select department_id from employees where last_name=‘Austin’)
and e.salary<(select salary from employees where last_name=‘Baer’);

7.创建和管理表

知识点:
1.有哪些数据库对象?
表:用于存储数据
视图:一个或者多个表中的数据的子集
序列:数字值生成器
索引:提高某些查询的性能
同义词:给出对象的替代名称
2.建表是要指定哪些内容?
3.如何建表时为列指定默认值?
4.如何使用子查询语法创建表?
5.如何为已有表新增列,删除列,修改列,为新增列定义默认值?
6.如何标记列为"unused"
7.如何批量删除"unused"列
8.如何删除表
9.如何更改表名?
10.如何舍去表中的内容?
11.如何为表,列添加注释?
12.oracle 有哪些常用的数据类型?
–1. 显示当前用户拥有的表
select table_name from user_tables;
–2. 显示当前用户拥有的表,视图,同义词和序列
select * from user_catalog;
–3. 使用 DEPARTMENTS 表中的数据填充 dept 表,只包含所需列
insert into dept(id,name)
select department_id,department_name
from departments;
–4. 创建 emp 表,结构如下:
列名 id last_name first_name dept_id
数据类型 number varchar2 varchar2 number
长度 7 25 25 7
create table emp(
id number(7),
last_name varchar2(25),
first_name varchar2(25),
det_id number(7)
);
–5. 修改 emp 表,从而允许更长的员工姓氏(50),并通过查询数据字典来确认
alter table emp
modify (last_name varchar2(50));
–6. 确认 dept 和 emp 表都在数据字典中
select table_name from user_tables
where table_name=‘DEPT’ or table_name=‘EMP’;
–7. 删除 emp 表
drop table emp;
–8. 将 employees2 重命名为 emp
rename employees2 to emp;
–9. 向 emp 表添加一列名为 birthday,日期类型,并通过查询数据字典来确认
alter table emp
add(birthday date);
–10. 向 EMP 表的列 birthday 添加备注(生日),并通过查询数据字典来确认
comment on column emp.birthday is’生日’;
select * from user_col_comments
where COLUMN_NAME=‘BIRTHDAY’;
–11. 修改表 emp,将 salary 的默认值设置为 0
alter table emp
modify(salary default(0));
–12. 向 DEPT 和 EMP 表定义中添加描述表的备注,并通过查询数据字典来确认
comment on table dept is ‘部门表’;
select * from user_tab_comments
where TABLE_NAME=‘DEPT’ ;
comment on table emp is’员工表’;
select * from user_tab_comments
where TABLE_NAME=‘EMP’ ;
–13. 删除 emp 表的 first_name 列
alter table emp
drop (first_name);
–14.将表 emp 的 dept_id 列标记为不适用(unused)
alter table emp
set unused(dept_id);
–15.删除 emp 表中所有不使用(unused)的列
alter table emp
drop unused column;
select table_name,column_name,data_type,t.data_default
from user_tab_cols t
where table_name=upper(‘employees’);

8.约束

知识点:
1.什么是约束?
2.有哪些约束?
什么是主键, 什么是外键?
3.他们的含义分别是什么?
4.可以在什么时候创建约束?
5.如何在建表是创建约束?
6.如何在建表后为表添加约束?
7.如何删除约束?
8.如何禁用/启用约束?
9.如何在删除列的时候删除其关联的约束?
10.如何查看用户的约束?
11.如何查看列上的约束?
–1. 为 emp 表的 id 列添加主键约束,命名为 my_emp_id_pk
alter table emp
add constraint my_emp_id_pk primary key(id);
–2. 为 dept 表的 id 列添加主键约束,命名为 my_dept_id_pk
alter table dept
add constraint my_dept_id_pk primary key(id);
–3. 向表 emp 中添加一个 dept_id 列,它引用部门表,该约束命名为 my_emp_dept_id_pk
alter table emp
add(dept_id number(8) ,
constraint my_emp_dept_id_pk foreign key(dept_id)references dept(id));
–4. 通过 user_constraints 视图,确认已经添加了这些约束
select * from user_constraints
where constraint_name=‘MY_EMP_ID_PK’
or constraint_name=‘MY_DEPT_ID_PK’
or constraint_name=‘MY_EMP_DEPT_ID_PK’;
–5. 修改 emp 表,添加一个 commission 列,其数据类型为 number,精度为 2,小数位数为
2,为该佣金列添加一个确保佣金值大于 0 的约束
alter table emp
add(commission number(2,2) constraint check_cms check(commission>0));
–6. 为 emp 表添加约束,保证 last_name 的唯一性
alter table emp
add constraint unique_n unique(last_name);
–7. 为 emp 表添加约束,保证 last_name 和 first_name 均不能为空
alter table emp
add constraint name_is_not_null check(last_name is not null and first_name is not null);
–8. 创建学生表 Student(id,name,birthday,sex) 含义分别为:学生编号,学生姓名,出生年月
日,学生性别,在建表的同时指定 id 为主键,name 不能为空,sex 必须为男或者女
create table Student(
id number(8) not null,
name varchar2(25) not null,
birthday date,
sex varchar2(10) check(sex='男’or sex=‘女’),
constraint stu_id_pk primary key(id)
);
–或者,让系统为逐渐约束命名
create table Student(
id number(8) primary key,
name varchar2(25) not null,
birthday date,
sex varchar2(10) check(sex='男’or sex=‘女’)
);

9.视图,序列,索引

  1. The staff in the HR department wants to hide some of the data in the EMPLOYEES table. They
    want a view called EMPLOYEES_VU based on the employee numbers, employee names, and
    department numbers from the EMPLOYEES table. They want the heading for the employee name
    to be EMPLOYEE(列名).
    create view EMPLOYEES_VU as
    select employee_id,last_name as EMPLOYEE,department_id
    from employees ;
    –2. Confirm that the view works. Display the contents of the EMPLOYEES_VU view.
    select * from user_views
    where view_name=‘EMPLOYEES_VU’;
    –3. Using your EMPLOYEES_VU view, write a query for the HR department to display all employee
    names and department numbers.
    select EMPLOYEE,department_id from EMPLOYEES_VU;
    –4. Department 50 needs access to its employee data. Create a view named DEPT50 that
    contains the employee numbers, employee last names, and department numbers for all
    employees in department 50. You have been asked to label the view columns EMPNO, EMPLOYEE,
    and DEPTNO. For security purposes, do not allow an employee to be reassigned to another
    department through the view.
    create view DEPT50(EMPNO,EMPLOYEE,DEPTNO) as
    select employee_id,last_name,department_id
    from employees
    where department_id=50
    with read only;
    –5. Display the structure and contents of the DEPT50 view.
    desc DEPT50;
    select text from user_views where view_name=‘DEPT50’;
    –6. Test your view. Attempt to reassign Matos to department 80.
    update DEPT50 set DEPTNO=80
    where EMPLOYEE=‘Matos’;
    –7. You need a sequence that can be used with the PRIMARY KEY column of the DEPT table. The
    sequence should start at 200 and have a maximum value of 1,000. Have your sequence
    increment by 10. Name the sequence DEPT_ID_SEQ.
    create sequence DEPT_ID_SEQ
    increment by 10
    start with 200
    maxvalue 1000;
    –8. To test your sequence, write a script to insert two rows in the DEPT table. Name your script
    lab_11_08.sql. Be sure to use the sequence that you created for the ID column. Add two
    departments: Education and Administration. Confirm your additions. Run the commands in your
    script.
    insert into DEPT (id,name) values(DEPT_ID_SEQ.nextval,‘Education’);
    insert into DEPT (id,name) values(DEPT_ID_SEQ.nextval,‘Administration’);
    –9. Create a nonunique index on the NAME column in the DEPT table.
    create index on DEPT(name);
    –查看索引
    select * from user_indexes where index_name=upper(‘index_dept_name’);
    –10. Create a synonym for your EMPLOYEES table. Call it EMP_synonym.
    create synonym EMP_synonym for EMPLOYEES;

10.pl sql 基础

  1. 在声明部分声明各种标量变量,并尝试在程序体中使用 select 语句为某个标量变量赋值
    declare
    –Local variables here
    hire_date DATE;
    first_name VARCHAR2(25):=‘zhangsan’;
    salary NUMBER(8,2);
    isLeader BOOLEAN;
    age pls_integer;
    blood_type CHAR DEFAULT ‘O’;
    acct_id INTEGER(4) NOT NULL := 9999;
    begin
    –直接赋值
    isLeader:=true;
    –常用方式,利用 select 对变量赋值
    select sysdate into hire_date from dual;
    select first_name into first_name from employees where employee_id=100;
    dbms_output.put_line(first_name);
    dbms_output.put_line(to_char(hire_date));
    end;
    –2. 声明行级记录变量,并尝试在程序体中使用 select 语句为整个记录赋值,尝试为单个分
    量赋值
    declare
    emp_rec employees%ROWTYPE;–emp_rec 的结构与 emp 表的结构一致
    cursor c1 IS
    SELECT dep.department_id,dep.department_name ,dep.location_id
    FROM departments dep;
    dept_rec c1%ROWTYPE;–也可以用在游标上
    Begin
    select dep.department_id,dep.department_name ,dep.location_id
    into dept_rec
    FROM departments dep
    where dep.department_id=20;–这里不能返回多行
    dbms_output.put_line(dept_rec.department_name);
    emp_rec.first_name:=‘zhang’;
    emp_rec.salary:=3000;
    end;
    –3. 在声明部分定义游标,并尝试或获取游标中的值
    declare
    cursor c_emp is
    select a.employee_id,a.first_name||’ ‘||a.last_name as ename ,a.job_id ,a.salary
    from employees a
    where a.department_id=20;
    total_salary employees.salary%type :=0;
    begin
    –依次提取游标中的行,赋值给 c_emp_r,c_emp_r 不用事先声明
    for c_emp_r in c_emp loop
    total_salary := total_salary+c_emp_r.salary;
    dbms_output.put_line(c_emp_r.ename);
    end loop;
    dbms_output.put_line(‘工资 sum:’||to_char(total_salary));
    end;
    –4. 根据指定 id 获得员工的工资,工资在 1000-3000 内的输出 A,3000-5000 之间的输出 B,
    5000-8000 之间的输出 C,8000 以上的输出 D 使用 if-else 和 case 两种方式来完成
    declare
    –Local variables here
    id integer;
    v_salary employees.salary%type;
    v_grade char(1);
    begin
    id:=:id;
    select salary into v_salary from employees t where t.employee_id=id;
    if v_salary>=1000 and v_salary<3000
    then v_grade:=‘A’;
    elsif v_salary>=3000 and v_salary<5000
    then v_grade:=‘B’;
    elsif v_salary>=5000 and v_salary<8000
    then v_grade:=‘C’;
    elsif v_salary>=8000
    then v_grade:=‘D’;
    else
    null;
    end if;
    dbms_output.put_line(‘工资级别:’||v_grade);
    end;
    –5. 使用 FOR 循环求 1-100 之间的素数
    declare
    – Local variables here
    i integer;
    v_flag boolean;
    begin
    – Test statements here
    for i in 2…100 loop
    v_flag:=true;
    for j in 2…i/2 loop
    if mod(i,j)=0
    then begin
    v_flag:=false;
    exit;
    end;
    end if;
    end loop;
    if v_flag then
    dbms_output.put(rpad(to_char(i),8,’ ‘));
    end if;
    end loop;
    dbms_output.new_line;
    end;
    –6. 打印 99 乘法表
    declare
    – Local variables here
    i integer;
    j integer;
    begin
    for i in 1…9 loop
    for j in 1…i loop
    dbms_output.put(i||’’||j||’=’||(ij)||’ ‘);
    end loop;
    dbms_output.put_line(’’);
    end loop;
    end;
    –7. 根据工资查询员工姓名。如果此员工不存在(发出 NO_DATA_FOUND 异常),打印相应
    的提示信息
    declare
    cursor c_name(p_salary employees.salary%type) is
    select last_name from employees t where t.salary=p_salary;
    begin
    for names in c_name(’&v_salary’) loop
    dbms_output.put_line(names.last_name);
    end loop;
    exception
    when no_data_found then dbms_output.put_line(‘没有改工资对应的员工’);
    end;
    –8. 显示 EMP 中的第四条记录。游标%rowcount=4
    declare
    cursor c_emp is
    select * from employees t ;
    begin
    for rec in c_emp loop
    if(c_emp%rowcount=4) then
    dbms_output.put_line(rec.last_name);
    end if;
    end loop;
    exception
    when no_data_found then dbms_output.put_line(‘没有改工资对应的员工’);
    end;
    –9. 根据部门名称(由用户输入),按以下格式打印各部门人员姓名:
    部门名称:RESEARCH
    部门人员:SMITH,JONES,FORD
    declare
    v_ename employees.last_name%type;
    v_ename_str varchar2(1000);
    v_dname departments.department_name%type; --部门名称
    cursor c_emp(dname varchar2) is
    SELECT e.last_name FROM employees e,departments d where
    e.department_id=d.department_id and department_name=dname;
    begin
    v_dname := ‘&请输入部门名称:’;
    –v_dname := ‘Marketing’;
    dbms_output.put_line(‘部门名称:’||v_dname);
    open c_emp(v_dname);
    loop
    fetch c_emp into v_ename;
    exit when c_emp%notfound;
    –dbms_output.put(v_ename||’,’);–put 会放缓冲区
    v_ename_str := v_ename_str||v_ename||’,’;
    end loop;
    v_ename_str := substr(v_ename_str,1,length(v_ename_str)-1);
    dbms_output.put_line(‘部门人员:’||v_ename_str);
    –dbms_output.new_line;–刷新缓冲区
    end;
    –10. 针对所有部门,按以下格式打印各部门人员姓名:
    部门名称:RESEARCH
    部门人员:SMITH,JONES,FORD
    部门名称:ACCOUNTING
    部门人员:CLARK,KING,MILLER,
    如果该部门没有人员,则输出:
    部门名称:Treasury
    部门人员:该部门没有员工

实现提示:
1)循环每个部门,用其部门号作条件去查员工表
2)用显示 cursor 完成
3)要求用 FOR,会用到嵌套循环
DECLARE
cursor c_department
is select * from departments;
cursor c_emp(p_deptno employees.department_id%type)
is select * from employees where department_id=p_deptno;
v_empname varchar2(1000);
i integer:=0;
begin
for dept in c_department loop
dbms_output.put_line(‘部门名称:’||dept.department_name);
dbms_output.put(‘部门人员:’);
v_empname:=’’;
i:=0;
for emp in c_emp(dept.department_id) loop
v_empname:=v_empname||emp.last_name||’,’;
i:=i+1;
end loop;
if(i=0) then
dbms_output.put_line(‘该部门没有员工’);
else
dbms_output.put_line(substr(v_empname,1,length(v_empname)-1));
end if;
end loop;
end;
–11. 对所有员工,如果该员工职位是 MANAGER,并且在 DALLAS 工作那么就给他薪金加 15%;
如果该员工职位是 CLERK,并且在 NEW YORK 工作那么就给他薪金扣除 5%;其他情况不作处

declare
cursor c_emp is
select t.employee_id,t.job_id,l.city
from employees t ,departments d ,locations l
where t.department_id=d.department_id and d.location_id=l.location_id;
begin
for rec in c_emp loop
if(REGEXP_LIKE (rec.job_id, '._MGR’) and rec.city=‘DALLAS’) then
update employees t set t.salary=t.salary
(1.15) where
t.employee_id=rec.employee_id;
end if;
if (REGEXP_LIKE (rec.job_id, ‘S[HT]_CLERK’) and rec.city=‘NEW YORK’) then
update employees t set t.salary=t.salary0.95 where
t.employee_id=rec.employee_id;
end if;
end loop;
end;
–12. 对直接上级是’BLAKE’的所有员工,按照参加工作的时间加薪:
81 年 6 月以前的加薪 10%
81 年 6 月以后的加薪 5%
declare
cursor c_emp is
select t.

from employees t
where t.manager_id in (select employee_id from employees where last_name =
‘BLAKE’);
begin
for rec in c_emp loop
if rec.hire_date<to_date(‘19810601’,‘yyyymmdd’) then
update employees t set t.salary=t.salary1.1 where t.employee_id=rec.employee_id;
end if;
if rec.hire_date>=to_date(‘19810601’,‘yyyymmdd’) then
update employees t set t.salary=t.salary
1.05 where
t.employee_id=rec.employee_id;
end if;
end loop;
end;

Guess you like

Origin blog.csdn.net/W_s_j/article/details/114930160