一、查询

1.1基础查询语句

和其他的数据库一样,select * from 表名。

select  * from hr.employees

1.2算术表达式

简单的加减乘除。

select salary *12 from hr.employees //计算年薪

1.3别名

在查询结果中,给一个查询结果列另取一个名字

select salary*12 as "年薪" from hr.employees

1.4字符串连接

select last_name,first_name from hr.employees

分别查询,不进行字符串连接

select last_name || first_name 姓名 from hr.employees

把姓和名连接在一起,并且显示在同一个字段中

select last_name || ' ' || first_name 姓名 from hr.employees

在上面的基础上,在两个字段中间加上空格

1.5重复行(去重)

select department_id from hr.employees

这是不进行去重查询部门id,有的员工在一个部门,可能会看到重复的部门id

select distinct department_id from hr.employees

这是进行去重,消除重复的部门id号

select department_id from hr.employees group by department_id having count(department_id ) > 1

也可以这样。

1.6条件限定

只查询工资高于5000的员工

select * from hr.employees where salary > 5000

常用的条件限定表达式:

闭区间between and

select * from hr.employees where salary between 3000 and 5000

查询工资在3000到5000之间的员工,闭区间,也就是大于等于3000,小于等于5000.

或者in

select * from hr.employees where salary in(3000,6000)

薪水要么等于3000,要么等于6000。

模糊查询like

select * from hr.employees where first_name like '%a_'

名字里倒数第二个字母是a的员工,%表示任意匹配,有或者没有都可以。
是否为空is null

select * from hr.employees where department_id is null

查询部门id为空的员工

1.7逻辑条件

常用的逻辑条件:

与and

select * from hr.employees where salary between 3000 and 5000
select * from hr.employees where salary>=3000 and salary<=5000

and和between效果是一样的,不过and灵活一些。

或or

select * from hr.employees where first_name like '%a_' or first_name like '_a%'

要么名字倒数第二个是a,要么名字正数第二个字母是a。
非not

select * from hr.employees where first_name not like '%a_'

排除名字倒数第二个字母是a的员工。

1.8排序查询

select * from hr.employees order by salary desc/asc

根据工资倒排序。

1.9关联查询(多表查询)

select e.first_name,e.department_id from hr.employees e

查询员工名称和部门id,还需要部门名称,该怎么做?

select d.department_id,d.department_name from hr.departments d

直接查询部门表?可这样无法对应员工名称的关系啊。
所以,我们需要将两张表进行关联

select e.first_name,d.partment_name from hr.employees e left join hr.departments d on e.department_id = d.department_id

这样就能关联查询了,值得一提的是,left join是进行表关联,on是指定关联字段。

查询有员工的部门的平均工资,要求显示部门编号,部门名,部门所在地(需要多表关联查询: employees, department, location)
select avg(e.salary), e.department_id,d.department_name,l.street_address  from hr.employees e
left join hr.departments d
on e.department_id = d.department_id
left join hr.locations l
on d.location_id = l.location_id

上面是三个表的多表查询

1.10统计函数

count:统计月薪高于5000的有多少人

select count(*) from hr.employees e where e.salary > 5000

max,min:统计id为100的部门最高薪水和最低薪水是多少

select max(salary),min(salary) from hr.employees e where e.department_id = 100

avg:统计平均工资

select avg(salary) from hr.employees e

1.11分组查询

分组查询常常和统计函数结合起来使用。
按照部门进行分组,查看每个部门的平均薪资是多少,如果没有分组查询,就需要进行多次查询,再手动汇总。

select avgf(salary) from hr.employees e where e.department_id = 90
select avgf(salary) from hr.employees e where e.department_id = 100
select avgf(salary) from hr.employees e where e.department_id = 110
……

如果使用分组查询:

select avg(salary),e.department_id from hr.employees e group by e.department_id

值得一提的是,分组的时候,查询字段只能是统计函数或者被分组的字段,例如department_id,因此id是所有部门都共有的,如果用department_name来进行分组,那就每个组就只有一个部门,没有意义。

1.12having

以部门进行分组,找出部门平均薪资高于5000的数据。

select avg(salary),e.department_id from hr.employees e group by e.department_id having avg(salary) > 5000

1.13子查询

子查询也就是可以简单地理解成同时执行多条sql语句,将一条sql语句的结果作为条件来执行另一条sql语句。

select e.salary from hr.employees e where e.first_name = 'Bruce'

查询出Bruce的月薪,得到数据是6000。

select count(*) from hr.employees e where e.salary > 6000

统计月薪高于6000的人数
这需要两条sql语句、两次操作,通过子查询的话一条sql就足够了:

select count(*) from hr.employees e where  e.salary >
(
select e.salary from hr.employees e where e.first_name = 'Bruce'
)

小括号后面的即为子查询

1.14分页查询

只查出5条数据

select * from hr.employees e where rownum <= 5

查出薪水最高的5-10条数据

select * from
(select rownum r,e1.* from(select e.* from hr.employees e order by e.salary desc)e1
)e2 where e2.r >=5 and e2.r <= 10

二、增删改

创建表:

create table hero(id number,name varchar2(30),hp number
)

插入数据:

insert into hero (id,name,hp) values(1,'炸弹人',450);
commit;

创建序列:

--序列从1开始,每次增加1,最大9999999
create sequence hero_seqincrement by 1 start with 1 maxvalue 9999999

使用序列:

--获取下一个值
select hero_seq.nextval from dual
--获取当前值
select hero_seq.currval from dual

作为id插入到表中(oracle没有id自增长,只能通过序列实现了):

insert into hero (id,name,hp,mp,damage,armor,speed) values(hero_seq.nextval,'炸弹人',450,);

修改字段:

--修改一个字段
update hero set damage = 46 where id = 22;
--修改多个字段
update hero set damage = 46,hp = 33 where id = 22;

删除数据:

--删除某条数据
delete from hero where id = 21
--删除一个表里所有的数据
delete from hero
--舍去一个表里数据的数据(无事务,作用于全表),不能恢复,不能回滚!
truncate table hero
--删除表,不能回滚
drop table hero

修改表结构:

--增加一列
alter table hero add (kills number)
--修改某列
alter table hero modify(name varchar2(300))
--删除某列
alter table hero drop column kills

三、索引

创建索引:

create index 索引名 on 表名(字段名);

一次创建多个索引:

create index 索引名 on 表名(字段名1,字段名2);

也可以基于函数创建索引:

create index 索引名 on 表名(字段-100)

查看当前用户下的索引:

select index_name,index_type,table_name,uniqueness from user_indexes;

查看当前用户下的索引(具体到列):

select index_name,table_name,column_name,column_position from user_ind_columns;

删除索引:

drop index 索引名;

四、实用语句

分页查询

select * from (select rownum r,e. * from 要分页的表 e where rownum<=m*n) t where r>m*n-m;--每页显示m条数据,查询第n页数据

全局查询库中某用户下是否有某个值:

declarev_Sql   varchar2(2000);v_count number;
beginfor xx in (select t.OWNER, t.TABLE_NAME, t.COLUMN_NAME from dba_tab_columns t where t.OWNER = 'EAM200723(用户名)') loopbeginv_Sql := 'select count(1) from ' || xx.owner || '.' || xx.table_name ||' where ' || xx.column_name || ' like ''%DELL3.jpg(值名称)%'' ';execute immediate v_Sqlinto v_count;if (v_count >= 1) thendbms_output.put_line(xx.table_name || ':' || xx.column_name);end if;exceptionwhen others thennull;end;end loop;
end;

查看表空间名称、路径、大小

select file_name,tablespace_name,bytes from dba_data_files

拓展表空间文件

ALTER TABLESPACE NNC_DATA01 ADD DATAFILE
'D:\APP\YANGC\ORADATA\ORCL\NNC_DATA01_7.DBF'
size 7167M autoextend on ;

查看用户占用空间大小

select *
from (select owner || '.' || tablespace_name name, sum(b) g
from (select owner,
t.segment_name,
t.partition_name,
round(bytes / 1024 / 1024 / 1024, 2) b,
tablespace_name
from dba_segments t)
where owner not in
('SYS', 'OUTLN', 'SYSTEM', 'TSMSYS', 'DBSNMP', 'WMSYS')
group by owner || '.' || tablespace_name)
order by name

查看表空间使用情况

select total.tablespace_name,  round(total.MB, 2) as Total_MB,  round(total.MB - free.MB, 2) as Used_MB,  round((1 - free.MB / total.MB) * 100, 2) as Used_Pct  from (select tablespace_name, sum(bytes) / 1024 / 1024 as MB  from dba_free_space  group by tablespace_name) free,  (select tablespace_name, sum(bytes) / 1024 / 1024 as MB  from dba_data_files  group by tablespace_name) total  where free.tablespace_name = total.tablespace_name;

impdp命令导入dmp文件

-- 创建用户并授权
create user nc200717 identified by nc200717 default tablespace nnc_data01 temporary tablespace temp;
grant connect,dba to nc200717 ;
create or replace directory DB020710 as 'D:\DB020710\';
grant read,write on directory DB020710 to nc200717 ;
--执行导入语句
impdp 用户名/密码 dumpfile=DMP文件名,多个使用空格隔开 directory=目录名 remap_schema=源用户:目标用户 logfile=日志文件名;

imp命令导入dmp文件

imp 用户名/密码@数据库实例名   -- 创建实例
file=C:\data\filename.dmp    -- 配置待导入的dmp文件
log=C:\data\logname.log     -- 配置要导入时生成的日志文件地址(可不配置)
[fromuser=source用户名    -- 配置dmp导出的数据库的用户名(仅在根据用户名导入数据时配置)
touser=target用户名     -- 配置dmp要导入的数据库的用户名(仅在根据用户名导入数据时配置)
|full=y]        -- 配置导入文件中的全部内容,有可能有多个用户的内容
ignore=y;    -- 配置导入的时候,如果没有表,创建表并导入数据,如果已经存在的表,忽略创建表,但不忽略导入。(注意:full=y 和 fromuser、touser 只能二选一)

创建表空间:

CREATE TABLESPACE TRAFFIC
LOGGING
DATAFILE '/home/oracle/tablespace/TRAFFIC.DBF'
SIZE 32M
AUTOEXTEND ON
NEXT 32M MAXSIZE UNLIMITED
EXTENT MANAGEMENT LOCAL;

删除表空间数据文件

ALTER TABLESPACE TEST DROP DATAFILE 3;

五、触发器

触发器语法:

create [or replace] trigger triggername
after|before of              --触发动作:之前/之后
[insert][[or] update [of 列列表]][[or] delete]
on table/view                         --表/视图
[referencing{:old [as] old/:new [as] new}]  --引用新表老表
[for each row]                              --行级模式
[when(condition)]                           --条件
pl/sql_block;                               --pl/sql语句(begin...end)

例子:
1.delete

create or replace trigger test_delete
after delete --执行删除后触发
on pam_turnaround --触发器所在的表
for each row --行级
begin--可执行部分if(:old.def6='Y') thenraise_application_error(-20001,'导入单据不允许删除');--自定义异常编码:-20000 到-20999end if;
end;

2.insert

create or replace trigger test_insert
after insert
on pam_turnaround
for each row
beginif(:new.creator='dwl') thenraise_application_error(-20002,'制单人为dwl的单据不允许新增');end if;
end;

3.update

create or replace trigger test_update
after update
of dr--只有修改具体的这个字段才会触发
on pam_turnaround
for each row --行级
begin--可执行部分if(:old.def6='Y') thenraise_application_error(-20001,'导入单据不能删除');end if;
end;

启用或禁用触发器

ALTER TRIGGER trigger_name DISABLE
ALTER TRIGGER trigger_name ENABLE

六、参考

oracle创建表空间、创建用户
如何使用imp导入dmp文件
HOW2J ORACLE系列教程

oracle从入门到跑路相关推荐

  1. Spring全家桶系列–[SpringBoot入门到跑路]

    //本文作者:cuifuan Spring全家桶----[SpringBoot入门到跑路] 对于之前的Spring框架的使用,各种配置文件XML.properties一旦出错之后错误难寻,这也是为什么 ...

  2. 【Java进阶营】Spring全家桶系列–[SpringBoot入门到跑路]

    Spring全家桶----[SpringBoot入门到跑路] 对于之前的Spring框架的使用,各种配置文件XML.properties一旦出错之后错误难寻,这也是为什么SpringBoot被推上主流 ...

  3. javascript从入门到跑路-----小文的js学习笔记(25)------运动框架----匀速运动、缓冲运动、多物体运动、链式运动

    ** javascript从入门到跑路-----小文的js学习笔记(1)---------script.alert.document.write() 和 console.log 标签 javascri ...

  4. unity 2020 怎么写shader使其接受光照?_Shader从入门到跑路:阶段性自我小测2(屏幕后处理、替换渲染、双Pass渲染)...

    以下是一些可供读者自我检测的问题,同上次一样,笔者不会提供答案,但如果实在想不明白依然可以私信笔者问思路.经某些读者建议,每题加上了分数,供各位自检. 练习1:使用第5章讲到的屏幕后处理效果,对屏幕颜 ...

  5. javascript从入门到跑路-----小文的js学习笔记目录

    ** javascript从入门到跑路-----小文的js学习笔记(1)---------script.alert.document.write() 和 console.log 标签 javascri ...

  6. 反手来个K8S入门到跑路

    layout: post title: 反手来个K8S入门到跑路 category: linux date: 2019-06-09 tags: linux k8s 反手来个K8S入门到跑路 前言 放假 ...

  7. mysql入门到跑路_MySQL 24小时入门笔记(3),插入和删除,删库到跑路

    MySQL 24小时入门笔记 插入 INSERT INSERT用法非常简单.现在我们有表students如下. 列名 类型 约束 id int primary key name char(16) NO ...

  8. Web 3D 从入门到跑路

    大厂技术  坚持周更  精选好文 前情概要 作为在该领域小白的我,在准备该主题分享之前,我信心满满的去知乎查了下,如何学习 WebGL? 如何入手webGl以及three.js呢? - 知乎[1] 前 ...

  9. 【视频教程免费领取】48G全套Java视频教程,从入门到跑路!

    领取方式 关注公众号,发送java0407获取下载链接. 扫码关注公众号,公众号回复 java0407 获取下载地址 目录结构 目录:/读书ReadBook [57.6G] ┣━━48G全套Java视 ...

最新文章

  1. LVS实现负载均衡及高可用
  2. 这些用来审计 Kubernetes RBAC 策略的方法你都见过吗?
  3. VTK:可视化算法之SpikeFran
  4. aix察看根目录各文件和子目录大小,去除文件系统统计
  5. python输入一个英文句子、翻转句子中单词的顺序_ODOA(1) 翻转句子中单词的顺序(C语言实现)...
  6. 软件工程:第五章作业
  7. [渝粤教育] 西南科技大学 基础工业工程 在线考试复习资料
  8. oracle报错00838,oracle11g的内存分配不当,导致的错误ORA-01034,ORA-00838,ORA-27101
  9. pytorch: 在训练中保存模型,加载模型
  10. [Angularjs]国际化
  11. python处理ppt的插件_几款PPT神器插件,千万不能错过!
  12. python批量删除数据库记录_GitHub - TracyMcgrady6/pymsql_Operation: Python3操作mysql数据库,实现增、批量增、删、改、查...
  13. ai怎么平均排列_一篇AI打麻将的论文,理科生眼中的麻将是这样的
  14. 齿轮计算机在线,齿轮参数计算器(萝卜花齿轮计算工具)9.5 中
  15. 犀牛系统连接服务器失败,犀牛5.0安装运行失败,Rhino安装失败解决方法
  16. 江山三侠—Flash短片轻松学(第2季)
  17. 《创业维艰》笔记 2
  18. 努比亚 Z17(Nubia NX563J) 解锁BootLoader 并刷入recovery
  19. 电科 | 电子器件微纳米加工技术
  20. CentOS 7 搭建企业内网 DNS 服务器

热门文章

  1. “展厅三维全景”技术,将产品和企业文化以vr展示出来
  2. 家具vr虚拟交互展示外包制作
  3. 林业调查规划资质全国林业单位办理认定标准和申请条件
  4. 视频禁止快进的解决方法
  5. Windows系统部分软件显示乱码
  6. Activity启动过程源码分析
  7. python-长宽不同多张图片生成一列长图
  8. 国标GB/T28181协议下EasyGBS互联网无插件直播服务器对接华为VCN平台接入EasyGBS视频自动停止传输
  9. 2022年国家高新技术企业认定最新规定及国家高新补助重点,补贴20-50万
  10. 0x800700c1添加语言,Win10更新失败出现”0x800700C1“错误的解决方法