数据库对象:表(table) 视图(view) 序列(sequence) 索引(index) 同义词(synonym)
1.视图: 存储起来的 select 语句
create view emp_vw
as
select employee_id, last_name, salary
from employees
where department_id = 90;
select * from emp_vw;
可以对简单视图进行 DML 操作
update emp_vw
set last_name = 'HelloKitty'
where employee_id = 100;
select * from employees
where employee_id = 100;
1). 复杂视图
create view emp_vw2
as
select department_id, avg(salary) avg_sal
from employees
group by department_id;
select * from emp_vw2;
复杂视图不能进行 DML 操作
update emp_vw2
set avg_sal = 10000
where department_id = 100;
2.序列:用于生成一组有规律的数值。(通常用于为主键设置值)
create sequence emp_seq1
start with 1
increment by 1
maxvalue 10000
minvalue 1
cycle
nocache;
select emp_seq1.currval from dual;
select emp_seq1.nextval from dual;
问题:裂缝,原因:
- 当多个表共用同一个序列时。
- rollback
- 发生异常
create table emp1(
id number(10),
name varchar2(30)
);
insert into emp1
values(emp_seq1.nextval, '张三');
select * from emp1;
3.索引:提高查询效率
自动创建:Oracle 会为具有唯一约束(唯一约束,主键约束)的列,自动创建索引
create table emp2(
id number(10) primary key,
name varchar2(30)
)
手动创建
create index emp_idx
on emp2(name);
create index emp_idx2
on emp2(id, name);
4.同义词
create synonym d1 for departments;
select * from d1;
5.表
DDL :数据定义语言 create table .../ drop table ... / rename ... to..../ truncate table.../alter table ...
DML : 数据操纵语言
insert into ... values ...
update ... set ... where ...
delete from ... where ...
【重要】
select ... 组函数(MIN()/MAX()/SUM()/AVG()/COUNT())
from ...join ... on ... 左外连接:left join ... on ... 右外连接: right join ... on ...
where ...
group by ... (oracle,SQL server中出现在select 子句后的非分组函数,必须出现在 group by子句后)
having ... 用于过滤 组函数
order by ... asc 升序, desc 降序
limit (0,4) 限制N条数据 如: topN数据
- union 并集
- union all(有重复)
- intersect 交集
- minus 相减
DCL : 数据控制语言 commit : 提交 / rollback : 回滚 / 授权grant...to... /revoke
SQL 优化:能使用 EXISTS 就不要使用 IN
问题:查询公司管理者的employee_id,last_name,job_id,department_id信息
select employee_id, last_name, job_id, department_id
from employees
where employee_id in (
select manager_id
from employees
)
select employee_id, last_name, job_id, department_id
from employees e1
where exists (
select 'x'
from employees e2
where e1.employee_id = e2.manager_id
)
问题:查询departments表中,不存在于employees表中的部门的department_id和department_name
select department_id, department_name
from departments d1
where not exists (
select 'x'
from employees e1
where e1.department_id = d1.department_id
)
更改 108 员工的信息: 使其工资变为所在部门中的最高工资, job 变为公司中平均工资最低的 job
update employees e1
set salary = (
select max(salary)
from employees e2
where e1.department_id = e2.department_id
), job_id = (
select job_id
from employees
group by job_id
having avg(salary) = (
select min(avg(salary))
from employees
group by job_id
)
)
where employee_id = 108;
删除 108 号员工所在部门中工资最低的那个员工.
delete from employees e1标签:yyds,面试题,...,employees,department,emp,oracle,id,select From: https://blog.51cto.com/u_11365839/5728307
where salary = (
select min(salary)
from employees
where department_id = (
select department_id
from employees
where employee_id = 108
)
)
select * from employees where employee_id = 108;
select * from employees where department_id = 100
order by salary;
rollback;