Oracle知识点总结(一)

查看用户下的所有表

select * from tabs;

数据库 ---> 数据库实例 ---> 表空间(逻辑单位)(用户) ---> 数据文件(物理单位)

地球 ---> 一个国家 ---> 省份(逻辑单位)(公民) ---> 山川河流(物理单位)
通常情况下,ORacle数据库只会有一个实例ORCL,

新建一个项目:
MYSQL : 创建一个数据库,创建相应的表
Oracle: 创建一个表空间,创建用户,用户去创建表

Oracle和MYSQL的差别

Oracle是多用户的, MYSQL是多数据库的

  1. 遵循SQL标准

  2. 不同厂商,不同的数据库产品,但是有自己的方言

  3. 使用自己的方言,也能够完成相同的功能

  4. Oracle安全级别要高,MYSQL开源免费
    基本查询:
    SQL : 结构化查询语言

    请听题: 请说一下SQL的分类以及每类常见的操作符都有哪些
    四类:
    DDL : 数据定义语言 create alter drop truncate
    DML : 数据操纵语言 insert update delete
    DCL : 数据控制语言 安全 授权 grant revoke
    DQL : 数据查询语言 select from子句 where子句
    查询语句的结构:

    select [列名] [*] from 表名 [where 条件] [group by 分组条件] [having 过滤] [order by 排序]

     select * from emp;
    
     select 1+1;  --在Oracle等于报错 ,在MYSQL中输出结果是2
    

    dual : oracle中的虚表 ,伪表, 主要是用来补齐语法结构

     select 1+1 from dual;
     select * from dual;
     select 1 from emp;
    

--直接写一个常量比写 * 要高效

select count(1) from emp;
select count(*) from emp;

别名查询: 使用as 关键字, 可以省略
别名中不能有特殊字符或者关键字, 如果有就加双引号

select ename 姓名, sal 工资 from emp;
select ename "姓       名", sal 工资 from emp;

去除重复数据 distinct
多列去除重复: 每一列都一样才能够算作是重复
--单列去除重复

select distinct job from emp;
--多列去除重复的

select distinct job,deptno from emp;
--查询中四则运算
select 1+1 from dual;

--查询员工年薪 = 月薪* 12

select sal*12 from emp;

--查询员工年薪+奖金

select sal*12 + comm from emp;
--nvl 函数 : 如果参数1为null  就返回参数2
select sal*12 + nvl(comm,0) from emp;

注意: null值 , 代表不确定的 不可预知的内容 , 不可以做四则运算

字符串拼接:

java : + 号拼接
Oracle 特有的连接符: || 拼接
在Oracle 中 ,双引号主要是别名的时候使用, 单引号是使用的值, 是字符
concat(str1,str2) 函数, 在mysql和Oracle中都有

--查询员工姓名 :  姓名:SCOTT
select ename from emp;
--使用拼接符
select '姓名:' || ename from emp;
--使用函数拼接
select concat('姓名:',ename) from emp;

条件查询 : [where后面的写法]
关系运算符: > >= = < <= != <>
逻辑运算符: and or not
其它运算符:
like 模糊查询
in(set) 在某个集合内
between..and.. 在某个区间内
is null 判断为空
is not null 判断不为空

--查询每月能得到奖金的员工信息
select * from emp where comm is not null;
--查询工资在1500--3000之间的员工信息
select * from emp where sal between 1500 and 3000;

select * from emp where sal >= 1500 and sal <= 3000;

--查询名字在某个范围的员工信息 ('JONES','SCOTT','FORD') in
select * from emp where ename in ('JONES','SCOTT','FORD');

_ 匹配单个字符
如果有特殊字符, 需要使用escape转义
模糊查询: like
% 匹配多个字符

--查询员工姓名第三个字符是O的员工信息
select * from emp where ename like '__O%';
--查询员工姓名中,包含%的员工信息
select * from emp where ename like '%\%%' escape '\';
select * from emp where ename like '%#%%' escape '#';

排序 : order by
升序: asc ascend
降序: desc descend
排序注意null问题 : nulls first | last
同时排列多列, 用逗号隔开

--查询员工信息,按照奖金由高到低排序
select * from emp order by comm desc nulls last;

--查询部门编号和按照工资  按照部门升序排序, 工资降序排序
select deptno, sal from emp order by deptno asc, sal desc;

函数: 必须要有返回值
单行函数: 对某一行中的某个值进行处理
数值函数
字符函数
日期函数
转换函数
通用函数
多行函数: 对某一列的所有行进行处理
max() min count sum avg
1.直接忽略空值

多行函数

--统计员工工资总和
select sum(sal) from emp;

--统计员工奖金总和  2200
select sum(comm) from emp;

--统计员工人数 14
select count(1) from emp;

--统计员工的平均奖金  550  错误    2200/14 =
select avg(comm) from emp;


--统计员工的平均奖金 157.
select sum(comm)/count(1) from emp;
select ceil(sum(comm)/count(1)) from emp;

update emp set ename = 'TUR%NER' where ename = 'TURNER';

select * from emp;

单行函数

--数值函数
select ceil(45.926) from dual;  --46
select floor(45.926) from dual; --45
--四舍五入
select round(45.926,2) from dual; --45.93
select round(45.926,1) from dual; -- 45.9
select round(45.926,0) from dual; --46
select round(45.926,-1) from dual; --50
select round(45.926,-2) from dual; --0
select round(65.926,-2) from dual; --100

--截断
select trunc(45.926,2) from dual; --45.92
select trunc(45.926,1) from dual; -- 45.9
select trunc(45.926,0) from dual; --45
select trunc(45.926,-1) from dual; --40
select trunc(45.926,-2) from dual; --0
select trunc(65.926,-2) from dual; --0

--求余
select mod(9,3) from dual; --0
select mod(9,4) from dual; --1
--字符函数
-- substr(str1,起始索引,长度) 
--注意: 起始索引不管写 0 还是 1 都是从第一个字符开始截取
select substr('abcdefg',0,3) from dual; --abc
select substr('abcdefg',1,3) from dual; --abc

select substr('abcdefg',2,3) from dual; --bcd

--获取字符串长度 24 28
select length('abcdefg') from dual;

--去除字符左右两边的空格
select trim('  hello  ') from dual;

--替换字符串
Select replace('hello','l','a') from dual;

--日期函数
--查询今天的日期
select sysdate from dual;
--查询3个月后的今天的日期
select add_months(sysdate,3) from dual;
--查询3天后的日期
select sysdate + 3 from dual;
--查询员工入职的天数
select sysdate - hiredate from  emp;

select ceil(sysdate - hiredate) from  emp;

--查询员工入职的周数
select (sysdate - hiredate)/7 from emp;

--查询员工入职的月数
select months_between(sysdate,hiredate) from emp;

--查询员工入职的年份
select months_between(sysdate,hiredate)/12 from emp;

--转换函数  数值转字符 字符转数值  日期
--字符转数值 to_number(str) 鸡肋
select 100+'10' from dual;  --110  默认已经帮我们转换
select 100 + to_number('10') from dual; --110

--数值转字符
select to_char(sal,'$9,999.99') from emp;

select to_char(sal,'L9,999.99') from emp;

to_char(1210.73, '9999.9') 返回 '1210.7' 
to_char(1210.73, '9,999.99') 返回 '1,210.73' 
to_char(1210.73, '$9,999.00') 返回 '$1,210.73' 
to_char(21, '000099') 返回 '000021' 
to_char(852,'xxxx') 返回' 354'

--日期转字符 to_char()  
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
--只想要年
select to_char(sysdate,'yyyy') from dual;  --2017

--只想要日
select to_char(sysdate,'d') from dual; --2  代表一个星期中第几天
select to_char(sysdate,'dd') from dual;  --10  代表一个月中的第几天
select to_char(sysdate,'ddd') from dual; --100 代表一年中的第几天


select to_char(sysdate,'day') from dual;  --monday
select to_char(sysdate,'dy') from dual;   --mon  星期的简写

--字符转日期
select to_date('2017-04-10','yyyy-mm-dd') from dual;

--查询1981年 -- 1985年入职的员工信息
select * from emp where hiredate between to_date('1981','yyyy') and
to_date('1985','yyyy');

通用函数:
nvl(参数1,参数2) 如果参数1 = null 就返回参数2
nvl2(参数1,参数2,参数3) 如果参数1 = null ,就返回参数3, 否则返回参数2
nullif(参数1,参数2) 如果参数1 = 参数2 那么就返回 null , 否则返回参数1
coalesce: 返回第一个不为null的值

select nvl2(null,5,6) from dual; --6;

select nvl2(1,5,6) from dual; --5;

select nullif(5,6) from dual; --5
select nullif(6,6) from dual; --null

select coalesce(null,null,3,5,6) from dual;  --3

select ceil(-12.5) from dual; --12
select floor(12.5) from dual; --12

select '  hello  ' from dual;
select * from emp;

练习

--题目:查询可以得到奖金的雇员所有信息
select * from emp where nvl(comm,0)>0;
--题目:找出员工可以获取到奖金的工作
select distinct job from emp where comm is not null;
--题目:找出各月倒数第3天受雇的所有员工信息  
select * from emp where LAST_DAY(hiredate)-2 = hiredate;
--题目:以首字母大写的方式显示所有员工的姓名
select initcap(ename) from emp;
--题目:显示所有员工的姓名,加入公司的年份和月份,
按受雇日期所在月排序,若月份相同则将最早年份的员工排在前面(要求使用extract函数)
select ename,extract(month from hiredate) "月份",extract
(year from hiredate) "年份" from emp order by "月份" asc , "年份" asc;
--题目:请查询员工的名字和薪水,并将薪水列变成15个字符长度,左边填充“$”符号
select last_name,lpad(salary,15,'$') from employees;
--题目:创建报告,显示员工名和奖金系数,如果奖金系数为空,则显示"无奖金"
select last_name,decode(commission_pct,'','无奖金',commission_pct) from employees;
--题目:找出早于12年前受雇的员工信息
select * from emp where months_between(sysdate,hiredate)/12 >12;
 --题目:显示满10年服务年限的员工的姓名和受雇日期
select ename,hiredate from emp where months_between(sysdate,hiredate)/12 >10;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • MYSQL 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 MySQL基本操...
    Kingtester阅读 7,648评论 5 116
  • 观其大纲 page 01 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 M...
    周少言阅读 3,113评论 0 33
  • 永远有一个明天,生活给我们另一个机会将事情做好,可是如果我搞错了,今天就是我们所剩的全部。人们都想伫立在颠峰上,殊...
    安全保卫处阅读 166评论 0 0
  • 我吧从来就不喜欢与人斗嘴斗心眼,平生也一直认为每个人都有善良可爱的一面。今天遇到这个尖酸刻薄的女子啊,原来是他认为...
    七壹壹阅读 289评论 0 0
  • 林语堂的率真与张爱玲的“三恨” 文/鲁先圣 历代的文人墨客中,要说哪一位最有真性情,哪一位最有率真味,...
    鲁先圣阅读 786评论 0 2