Oracle·studynote

序、概要

-Oracle数据库是一种网络上的数据库,它在网络上支持多用户,支持服务器、客户端等部署(或配置)

-服务器与客户端是软件概念,他们与计算机硬件不存在一对一对应的关系,即:同一台计算机既可以充当服务器又可以充当客户机,或者一台计算机只充当服务器或只充当客户机。

-Oracle数据库由Oracle数据库和Oracle实例(instance)构成

导入数据库文件

@e:/del_data.sql

查询表

select * from employees;

users system

users 给其他盘赋予表空间

system在C盘操作(所以不推荐)

各种名词

对象:类 出来的实体(赋予其他表的具体的权限)

系统: 可创建表权限等操作权限

限额:分配表的空间

角色:对象的套装 相当于领导负责直辖的范围 ,其有相应的功能

安排

1-6 查询 7表的操作 8增删改、回流 9约束(某列值非空) 10视图 11其他

一、查询

DML 数据记录的增删改查

DDL 表的增删改查 + 索引增删

DCL 高阶: Grant Revoke Commit Rollback Savepoint Lock

显示所有列

desc employees;

显示指定列

select employee_id,last_name,email 
from employees;

SQL语言大小写不敏感,可以写在一行或者多行。

关键词不能缩写也不能分行。

各子句一般要分行写。

伪表可以进行计算

select 8*4 from dual

看表结构

desc employees;

日期也可以进行计算(但是只能做加减,不能乘除)

select last_name,salary,12*salary+1000
from employees;

空值

空值不同于0,凡是空值参与的运算,结果都为空(null)

空值是无效的、未指定的、未知的或不可预知的值

空值不是空格或者0

别名

列的别名:重命名一个列,便于计算

紧跟列名,也可以在列名和别名之间加入关键字’AS’,别名使用双引号,以便在别名中包含空格或特殊的字符并区分大小写。

select employee_id id,last_name name,12*salary annual_sal
from employees;
--或者(建议上者)
select employee_id as id,last_name as name,12*salary as annual_sal
from employees;.
--或者(可选择显示出来的是大写还是小写)
select employee_id as "Id",last_name as "Name",12*salary annual_sal
from employees;

连接符

把列与列,列与字符连接在一起

用“||”表示

可以用来‘合成’列

select last_name||'`s email is '||email as details
from employees;

去除重复行

select distinct department_id from employees;

二、过滤和排序数据

过滤数据

过滤数据

select employee_id,last_name,salary
from employees
where salary > 5000;

between and

select last_name,salary
from employees
where salary between 4000 and 7000;

in

select last_name,department_id,salary
from employees
where department_id in(70,80,90);

like 含有字符

select last_name,department_id,salary
from employees
where last_name like '%a%';
--名字中前后都有a的筛出来
where last_name like '_a%';
--名字中第二位是a的筛出来
where last_name like '__a%';
--名字中第三位是a的筛出来
where last_name like '%\_%' escape '\';
--名字中都有_的筛出来

is null 查空

select last_name,department_id,salary,commission_pct
from employees
where commission_pct is null;

and or not

排序

order by子句进行排序

asc :升序(ascend) 默认

desc :降序(descend)

单层排序

select last_name,department_id,salary
from employees
where department_id = 80
order by salary desc;

多层排序

select last_name,department_id,salary
from employees
order by salary desc,last_name asc;
--先按酬金降序排,再按名字升序

三、单行函数

大小写转换

select lower('hEllo'),upper('hello'),initcap('sdi joi')
from dual;
--lower 都改成小写
--upper 都改成大写
--initcap 首字母大写
--显示结果:
hello HELLO sdi Joi
select * from employees
where upper(last_name) = 'KING';
--在表中查询名为king的人,但是不确定他的名里是不是大写

字符控制函数

函数结果
concat(‘Hello’ , ‘World’)HelloWorld
substr(‘Helloworld’,2,4)ello
length(‘HelloWorld’)10
instr(‘HelloWorld’, ‘W’)6 (首次出现的位置)
LPAD(salary , 10 , ‘W’)*****24000 (左对齐,不够的用*补位)
RPAD(salary , 10 , ‘*’)24000*****
trim(‘H’ from ‘HelloWorld’)elloWorld (仅移除收尾位的)
replace(‘abcd’ , ‘b’ , ‘m’)amcd (替换所有的)
round四舍五入
select round(435.45,2),round(435.45),round(435.45,-2) from dual;
--显示结果:435.45   435   400

trunc直接截断
select trunc(435.45,1),trunc(435.45),trunc(435.45,-1) from dual;
--显示结果:435.4  435  430
求余
select mod(1100,100) from dual;

dual,是一个伪表,可以用来测试函数和表达式

日期函数

转换函数

日期转换

select employee_id,to_char(hire_date,'yyyy"年"mm"月"dd"日"')
from employees
where to_char(hire_date,'yyyy-mm-dd') = '1994-06-07';
where to_char(hire_date,'yyyy"年"mm"月"dd"日"') = '1994年06月07日';

char与number

select to_char(1234567.89,'999,999,999.99') from dual;
--显示结果: 1,234,567,89
select to_char(1234567.89,'000,000,999.99') from dual;
--显示结果: 001,234,567,89
select to_char(1234567.89,'$999,999,999.99') from dual;
--显示结果: $1,234,567,89
select to_char(1234567.89,'L999,999,999.99') from dual;
--显示结果: ¥001,234,567,89   (本地的货币)

在这里插入图片描述

NVL函数(为了减少空值产生的影响)

将空值转换成一个已知的值

NVL(expr1,expr2)

不为空走1,为空走2

select last_name,nvl(to_char(department_id,'999999'),'没部门')
from employees;

NVL2(expr1,expr2,expr3)

expr1!=NULL?expr2:expr3 !=null为T 走expr2; 为F 走expr3

NULLIF(expr1,expr2)

expr1与expr2相等返回NULL,不等返回expr1

条件表达式

1)case

case [expr] when [comparison_expr1] then [return_expr1]when [comparison_expr2] then [return_expr2]else[else_expr]
end
select employee_id,department_id,
case department_id  when 10 then salary*1.1
                    when 20 then salary*1.2
                    else salary*1.3
end "New_sal"
from employees
where department_id in (10,20,30);

注意:case中无标点!写完记得END!

2)decode

decode ([expr],'[comparison_expr1]',[return_expr1],
               '[comparison_expr2]',[return_expr2],
                [else_expr])

e.g.

select employee_id,last_name,department_id,
decode (department_id,10,salary*1.1,
                      20,salary*1.2,
                      salary*1.3)
"New_sal"
from employees
where department_id in (10,20,30);

Practice

打印出“2009年10月14日 9:25:40”格式的当前系统的日期和时间。

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

四、多表查询

4.1内连接

where

为了避免笛卡尔集,可在where加入有效的连接条件

select employees.employee_id,employees.department_id,departments.department_name
from employees,departments
where employees.department_id=departments.department_id;
--说的是这里employees.department_id=departments.department_id
--或者这样写
select e.employee_id,e.department_id,d.department_name
from employees e,departments d
where e.department_id=d.department_id;
--或者这样写          ↓这里e./d.都可以
select employee_id,e.department_id,department_name
from employees e,departments d
where e.department_id=d.department_id;

带条件的查询

select employee_id,last_name,salary,grade_level
from employees e,job_grades j
where e.salary between j.lowest_sal and j.highest_sal

4.2 外连接

左外连接

select e.last_name,e.department_id,d.department_name
from employees e,departments d
where e.department_id=d.department_id(+)
--意思是,左表中的都显示(包括右表中没有的也显示)
--有个人在左表中,但是在右表中没有部门,左外后,会将他显示出来

右外连接

select e.last_name,e.department_id,d.department_name
from employees e,departments d
where e.department_id(+)=d.department_id
--意思是,右表中的都显示(包括左表中没有的也显示)
--有个人在左表中,但是在右表中没有部门,右外后,不会将他显示出来

natural join 自动匹配连接

select employee_id,department_id,department_name
from employees natural join departments
--意思是,两个表中都有的字段都要匹配上
select employee_id,department_id,department_name
from employees join departments
using (department_id)
--意思是只匹配这一列,跟where的结果一样,但是两表中的字段名要一致,数据类型也要一致

join on 的多表联查

--两个表连接
select employee_id,d.department_id,department_name
from employees e join departments d
on e.department_id=d.department_id;
--三个表连接
select employee_id,d.department_id,department_name,city
from employees e join departments d
on e.department_id=d.department_id
join locations l
on d.location_id=l.location_id;

左外连接

select employee_id,d.department_id,department_name
from employees e left outer join departments d
on e.department_id=d.department_id;

右外连接

select employee_id,d.department_id,department_name
from employees e right outer join departments d
on e.department_id=d.department_id;

满外连接(都出来)

select employee_id,d.department_id,department_name
from employees e full outer join departments d
on e.department_id=d.department_id;

自连接

查询公司中员工‘Chen’的manager的信息

select emp.last_name,manager.last_name,manager.salary,manager.email
from employees emp,employees manager
where emp.manager_id=manager.employee_id 
and lower(emp.last_name)='chen';

五、分组函数

分组函数作用于一组数据,并对一组数据返回一个值。

5.1组函数

stddev 样本标准偏差

数越小越稳

每个数跟平均数的差数的平方和。

avg,max,min,sum

select avg(salary),max(salary),min(salary),sum(salary)
from employees;

max与min对括号里的数据类型不严格,num也可,varchar也行

avg,sum只能是num

count(计数)函数

select count(employee_id),count(last_name),count(hire_date) from employees;

avg=sum/count

计算的是非空的有多少个

所以算平均的时候记得考虑空值的数据(使用NVL函数使分组函数无法忽略空值)

distinct(非空且不重复的记录总数)

select count(distinct department_id) from employees;

5.2group by子句(对数据分组)

--求出employees表中各部门的平均工资
select department_id,avg(salary) from employees
grounp by department_id;
select department_id,avg(salary)
from employees
where department_id in(40,60,80)
grounp by department_id;
--除去了没有部门的人
select department_id,job_id,avg(salary)
from employees
grounp by department_id,job_id;
--除了按部门分组,还按工种分组

查询相应的列不是组函数,就都应该让他出现在group by中

select department_id,job_id,avg(salary)
from employees
grounp by department_id,job_id

第一行有的,第三行一定有。第三行有的,第一行不一定有。

注意:

在where的子句中不能使用组函数

但是可以在having子句中使用组函数

5.3having子句过滤分组的结果集

--求出各部门中平均工资大于6000的部门,以及其平均工资
--如果用where写(是错误的)
select department_id,avg(salary)
from employees
where avg(salary)>6000   --这里会报错,因为在where的子句中不能使用组函数
grounp by department_id;

--如果用having写(是正确的)
select department_id,avg(salary)
from employees
having avg(salary)>6000   --这里没有问题
grounp by department_id;

5.4嵌套组函数

--显示各部门平均工资的最大值
select max(avg(salary))
from employees
group by department_id;

六、子查询

单行子查询

查询结果只有一条结果

--谁的工资比Abel高?
select last_name,salary
from employees
where salary > (select salary
                from employees
                where last_name='Abel'
				)
;
--查询员工名为Chen的manager的信息
select last_name,salary
from employees
where employee_id = (select manager_id
                     from employees
                     where last_name='Chen'
					)
;

子查询要包含在括号内。

将子查询放在比较条件的右侧。

单行操作符对应单行子查询,多行操作符对应多行子查询。

--返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资
select last_name, salary, job_id
from employees
where job_id=(select job_id
              from employees
              where employee_id=141)
   and
      salary>(select salary
             from employees
             where employee_id=143
             ) 
;
--返回公司工资最少的员工的last_name,job_id和salary
select last_name,job_id,salary
from employees
where salary=(select min(salary)
             from employees
			 )
;
--查询最低工资大于50号部门最低工资的department_id和其最低工资
--自己没写出来的:
select department_id,min(salary)
from employees
having salary>min(salary)



(select min(salary)
              from employees
              where(select department
                    from employees
                    where department_id=50
              )
    
       )
       
       
--查询最低工资大于50号部门最低工资的department_id和其最低工资
select department_id,min(salary)
from employees
group by department_id
having min(salary)>(select min(salary)
                    from employees
                    where department_id=50
                    )

写组函数的时候,一定记得写group by

多行子查询

操作符含义
in等于列表中的任意一个
any和子查询返回的某一个值进行比较
all和子查询返回的所有值进行比较
--返回其他部门中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id以及salary
select employee_id, last_name, job_id, salary
from employees
where job_id <> 'IT_PROG' 
  and salary < any(select salary
                   from employees
                   where job_id = 'IT_PROG'
                   );

IT_PROG中的工资是2000,4000,7500,9000

如果是salary>any, salary只要大于2000的值就可。

如果是salary>all, salary必须大于9000(所有数)。

练习

--41. 查询工资最低的员工信息: last_name, salary  
select last_name, salary
from employees
where salary=(select min(salary)
              from employees
              );
--42. 查询平均工资最低的部门信息
--1).查询公司中各部门的平均工资是多少
select department_id,avg(salary)
from employees
grounp by department_id
--2).查询公司中各部门的平均工资中最少
select department_id,min(avg(salary))
from employees
grounp by department_id
--3).哪个部门的平均工资=2.
select department_id
from employees
where avg(salary)=(select min(avg(salary))
                   from employees
                   group by department_id
                   )
group by department_id
--4).查询这个部门的全部信息
select *
from departments
where department_id=(select department_id
                     from employees
                     where avg(salary)=(select min(avg(salary))
                                        from employees
                                        group by department_id
                                        )
                     );
--43*. 查询平均工资最低的部门信息和该部门的平均工资

select d.*, (select avg(salary) from employees where department_id=d.department_id)
from departments d
where d.department_id=(select department_id
                       from employees
                       group by department_id
                       having avg(salary)=(select min(avg(salary))
                                           from employees
                                           group by department_id
                                           )
);

如果不知道变量是什么,可以先写上一个常量,然后再用变量换。

例如第一行的where,开始不知道department_id应该等于什么,就先写个50,然后改成应该符合的变量。

--44. 查询平均工资最高的 job 信息
select *
from jobs
where job_id=(

select job_id
from employees
group by job_id
having avg(salary)=(select max(avg(salary))
                   from employees
                   group by job_id
                   )
);
--45. 查询平均工资高于公司平均工资的部门有哪些?
select department_id
from employees
group by department_id
having avg(salary)>(select avg(salary)
              from employees
              )

ORA-01427: 单行子查询返回多个行 -->没加group by

--46. 查询出公司中所有 manager 的详细信息.
select *
from employees
where employee_id in (select manager_id
                      from employees
)
--47. 各个部门中 最高工资中最低的那个部门的 最低工资是多少
select min(salary)
from employees
where department_id=(select department_id
                     from employees
                     group by department_id
                     having max(salary)=(select min(max(salary))
                                         from employees
                                         group by department_id
                                         )
                      )
--48. 查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
select last_name, department_id, email, salary
from employees
where employee_id=(select manager_id
                   from departments
                   where department_id=(select department_id
                                        from employees
                                        group by department_id
                                        having avg(salary)=(select max(avg(salary))
                                                            from employees
                                                            group by department_id
                                                            )
                                        )

                    )
--49. 查询 1999 年来公司的人所有员工的最高工资的那个员工的信息.
select *
from employees
where salary=(select max(salary)
              from employees
              where to_char(hire_date,'yyyy')='1999'
              )
   and to_char(hire_date,'yyyy')='1999'

七、DDL

1.数据库对象

对象描述
基本的数据存储集合,由行和列组成
视图从表中抽出的逻辑上相关的数据集合
序列提供由规律的数值
索引提高查询的效率
同义词给对象起别名

查询数据字典

--查看用户定义的表
select table_name
from user_tables;
--查看用户定义的各种数据库对象
select distinct object_type
from user_objects;
--查看用户定义的表,视图,同义词和序列
select *
from user_catalog;

2.创建表

命名规则:

表名和列名:

  • 必须以字母开头
  • 必须在1-30个字符之间
  • 必须只能包含A-Z,a-z,0-9,_,$,#
  • 必须不能和用户定义的其他对象重名
  • 必须不能是Oracle的保留字

DML对数据操作

DDL对数据库的表进行操作

创建表

--方法一(白手起家创建)
create table emp1(
    id number(10),
    name varchar2(20),
    salary number(10,2),--总共10位,其中有两位是小数位
    hire_date date
);

--方法二(仿照其他表进行创建,依托于现有的表)
CREATE TABLE emp2
AS
SELECT employee_id id,last_name name,hire_date,salary
from employees
where department_id=80(若此条件不满足,创建的就是一张空表)

查看表中具体结构

desc emp1;

3.数据类型

数据类型描述
varchar2(size)可变长字符数据
char(size)定长字符数据
number(p,s)可变长数值数据
date日期型数据
long可变长字符数据,最大可达到2G
clob字符数据,最大可达到4G
raw(long raw)原始的二进制数据
blob二进制数据,最大可达到4G
bfile存储玩不文件的二进制数据,最大可达到4G
rowid行地址

4.修改表的定义

使用 ALTER TABLE 语句可以:

  • 追加新的列
  • 修改现有的列
  • 为新追加的列定义默认值
  • 删除一个列
  • 重命名表的一个列名
--新加一列
alter table emp1
add(email varchar2(20));

--修改
alter table emp1
modify(id number(15));

--默认值
alter table emp1
modify(salary number(20,2) default 2000);

--更改数据类型(表中没数据时才可修改)
alter table emp1
modify (email number(20));

--删除一列
alter tabel emp1
drop column email;

--重命名表的一个列名
alter table emp1
rename column salary to sal;

5.删除表

drop table emp1;

6.清空表

truncate table emp1;

7.改变对象的名称

rename emp2 to employees2; 

八、SQL语句 – 数据处理

目标:

  • 使用DML语句
  • 向表中插入数据
  • 更新表中数据
  • 从表中删除数据
  • 控制事务
  • 事务是由完成若干项工作的DML语句组成的

1.插入数据

insert into emp1
values(1001,'AA',sysdate,10000);

insert into emp1
values(1002,'BB',to_date('1998-12-21','yyyy-mm-dd'),20000);

--一次只能插入一条数据,且要与表类型一一对应
insert into emp1
values(1003,'CC',to_date('1998-12-21','yyyy-mm-dd'),null);

insert into emp1(last_name,employee_id,hire_date)
values('DD',1004,to_date('1998-12-21','yyyy-mm-dd'));

将已有的表的数据复制到指定的表中:

--将已有的表的数据复制到指定的表中
insert into emp1(employee_id,hire_date,last_name,salary)
select employee_id,hire_date,last_name,salary
from employees
where department_id=80;
--创建脚本
insert into emp1(employee_id,last_name,salary,hire_date)
values(&id,'&name',&salary,'&hire_date');

2.更新数据

update emp1
set salary=12000
where employee_id=179;
commit;
rollback;   --不commit时可以commit

3.删除数据

delete from departments
where department_id=60;
--报错原因:因为表中存在数据,所以无法删除
--从emp1表中删除部门名称中含Public字符的部门id
delete from emp1
where department_id=(select department_id
                     from department_name like '%Public'
);

九、数据库事务

  • 事务:一组逻辑操作单元使数据从一种状态变换到另一种状态
  • 数据库事务由以下的部分组成:
    • 一个或多个DM语句
    • 一个DDL( Data Definition Language-数据定义语言)语句
    • 一个DCL( Data Control Language-数据控制语言)语句

一个事务的始终:

  • 以第一个DML语句的执行作为开始
  • 以下面的其中之一作为结束
    • commit或 ROLLBACK语句
    • DDL語句(自动提交)
    • 用户会话正常结東
    • 系统异常终止

回滚:

  • 使用 SAVEPOINT语句在当前事务中创建保存点。

  • 使用 ROLLBACK TO SAVEPOINN语句回滚到创建的保存点。

例子:
   savepoint a;
   DML语句。。。;
   savepoint b;
   DML语句。。。;
   rollback to savepoint b;

提交或回滚前的数据状态:

  • 改变前的数据状态是可以恢复的
  • 执行DML操作的用户可以通过 SELECT语句查询之前的修正
  • 其他用户不能看到当前用户所做的改变,直到当前用户结束事务。
  • DML语句所涉及到的行被锁定,其他用户不能操作。

提交后的数据状态:

  • 数据的改变已经被保存到数据库中;
  • 改变前的数据已经丢失
  • 所有用户可以看到
  • 结果锁被释放,其他用户可以操作涉及到的数据。
  • 所有保存点被释放

在这里插入图片描述

十、约束

增加约束

有以下五种约束:

  • not null 不为空
  • unique 唯一,就是整个表中该列的值不能重复
  • primary key 主键
  • foreign key 外键
  • check 检查条件
create table emp2(
    id number(10) constraint emp2_id_nn not null,  --constraint起别名
    name varchar2(20) not null,
    salary number(10,2)
)
create table emp3(
    id number(10) constraint emp3_id_uk unique,   --unique唯一  列级约束
    name varchar2(20) constraint emp3_name_nn not null,
    salary number(10,2),
    email varchar2(20),
    constraint emp3_email_uk unique(email)   --表级约束
)
create table emp4(
    id number(10) constraint emp4_id_pk primary key,   --主键不能为空
    name varchar2(20) constraint emp4_name_nn not null,
    salary number(10,2),
    email varchar2(20),
    constraint emp3_email_uk unique(email) 
)
create table emp6(
    id number(10) constraint emp4_id_pk primary key,   --主键不能为空
    name varchar2(20) constraint emp4_name_nn not null,
    salary number(10,2),
    email varchar2(20),
    constraint emp3_email_uk unique(email) 
)
create table emp6(
id number(10),
name varchar2(20) constraint emp6_name_nn not null,
email varchar2(20),
salary number(10,2),
department_id number(10),
constraint emp6_email_uk unique(email),
constraint emp6_id_pk primary key(id),
constraint emp6_dept_id_fk foreign key(department_id) references departments(department_id)    --外键
)
  • ON DELETE CASCADE(级联删除):当父表中的列被删除时,子表中相对应的列也被删除
  • ON DELETE SET NULL(级联置空):当父表中的列被删除时,子表中相应的列置空
create table emp7(
id number(10),
name varchar2(20) constraint emp7_name_nn not null,
email varchar2(20),
salary number(10,2),
department_id number(10),
constraint emp7_email_uk unique(email),
constraint emp7_id_pk primary key(id),
constraint emp7_dept_id_fk foreign key(department_id) references departments(department_id) on delete set null    --当departments表中的列被删除时,此表中这列置空
)

check:定义每一行必须满足的条件;

create table emp7(
id number(10),
name varchar2(20) constraint emp7_name_nn not null,
email varchar2(20),
salary number(10,2) check(salary>1500 and salary<30000), --check
department_id number(10),
constraint emp7_email_uk unique(email),
constraint emp7_id_pk primary key(id),
constraint emp7_dept_id_fk foreign key(department_id) references departments(department_id) on delete set null  
);

修改约束

添加或删除约束语法

alter table emp5
modify(salary number(10,2) not null); --修改约束

alter table emp5
drop constraint emp5_name_nn; --删除约束

alter table emp5
add constraint emp5_name_uk unique(name);  --给name增加约束

有效化或无效化约束

alter table emp3
disable constraint emp3_email_uk;  --使用 DISABLE 子句将约束无效化

alter table emp3
enable constraint emp3_email_uk;   --使用 ENABLE 子句将约束有效化

十一、视图

数据库对象有:

对象描述
表-table基本的数据存储集合,由行和列组成
视图-view从表中抛出的逻辑上相关的数据集合
序列提供有规律的数值
索引提高查询的效率
同义词给对象起别名

概念:

  • 视图是一种虚表。
  • 视图建立在已有表的基础上,视图赖以建立的这些表称为基表
  • 向视图提供数据内容的语句为 SELECT语句,可以将视图理解为存储起来的 SELECT语句
  • 视图向用户提供基表数据的另一种表现形式;

好处:

  • 控制数据访问
  • 简化查询 ---- 既是把查询多个表的结果集映射成视图
  • 避免重复访问相同的数据

1.创建、修改和删除

create view empview     --创建视图
as
select employee_id, last_name, salary
from employees
where department_id=80;

create view empview2                 --跨表创建视图
as
select employee_id id, last_name name, salary, department_name
from employees e, departments d
where e.department_id = d.department_id


select * from empview;   --查询视图

drop view viewName;   --删除视图语法
create or replace view empview2   --可用replace覆盖掉原来的  (可视为修改)
as
select employee_id id, last_name name, department_name
from employees e, departments d
where e.department_id = d.department_id

2.视图的增删改查

insert into empview         --增
[(columnName [,columnName...])] 
values(columnName [,columnName...]) 
[where ...];

update empview              --改
set salary = 20000
where employee_id = 179;

delete from empview         --删
where employee_id = 176;

select columnName from viewName [where ...];  --查

3.屏蔽DML操作

  • 可以使用 with read only 选项屏蔽对视图的DML操作
  • 任何DML操作都会返回一个 Oracle server错误

说明,执行此操作后,用户就只能进行查询操作,而不能进行增删改操作了;

create or replace view viewName as select .... with read only;
--或者
create view viewName as select .... with read only;

取消此屏蔽操作方法:

create or replace view viewName as select .... ;
-- 只要使用修改操作,并且不要 with read only 子句即可

4.视图类别

create or replace view empview3
as
select department_name dept_name, avg(salary) avg_sal
from employees e, departments d
where e.department_id = d.department_id
group by department_name

视图中使用DML操作规定

  • 可以在简单视图中执行DML操作
  • 当视图定义中包含以下元素之一时,不能使用 delete、update、insert:
    • 组函数
    • GROUP BY 子句
    • DISTINCT 关键字
    • rownum 伪列 — 查询数据库后的结果集自动的行数;说明: rownum伪列数据表本身并没有这样的列,是 oracle数据库为每个数据表“加上的”列可以标识行号。默认情况下 rownum按主索引来排序,若没有主素引则自然排序

5.top-n分析

用途:求top前几

select rownum, employee_id, last_name, salary
from(
       select employee_id, last_name, salary
       from employees
       order by salary desc
     )
where rownum <= 10
--理论上可以,但是报错

对rownum只能使用 <或 <= ,而用 = , > ,>= 都将不能返回任何数据。

解决办法:让rownum变成不是伪列

修改:

select rn, employee_id, last_name, salary
from(
        select rownum rn, employee_id, last_name, salary
        from(
               select employee_id, last_name, salary
               from employees
               order by salary desc
             )
     )
where rn <= 10

十二、其他数据库对象

对象描述
表-table基本的数据存储集合,由行和列组成
视图-view从表中抛出的逻辑上相关的数据集合
序列提供有规律的数值
索引提高查询的效率
同义词给对象起别名

1.序列

序列:可供多个用户用来产生唯一数值的数据库对象

  • 自动提供唯一的数值
  • 共享对象
  • 主要用于提供主键值
  • 将序列值装入内存可以提高访问效率

部分借鉴于:
https://gitee.com/zzwuweijun/learning_documents/blob/master/java%E5%BC%80%E5%8F%91/19-Oracle/Oracle01-%E5%85%A5%E9%97%A8.md#%E4%BA%94sql%E8%AF%AD%E5%8F%A5----%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值