Oracle

本文详细介绍了Oracle数据库的SQL查询技巧,包括基本查询、多表查询、子查询、集合运算,以及DDL和DML操作,如创建表、插入数据、更新与删除,还涵盖了视图、索引、PL/SQL编程等高级概念。
摘要由CSDN通过智能技术生成

Oracle

以下用到的表(除了DML模块创建了一些表外)都是安装完Oracle后,自带的一些表

SQL 的分类以及每类分类的常见的操作

DDL : 数据定义语言 create alter drop truncate
DML : 数据操纵语言 insert update delete
DCL : 数据控制语言 安全 授权 grant revoke
DQL : 数据查询语言 select

SQL执行顺序

from .. where ..group by..having .. select..rownum..order by

基本查询

select {列名/*} from 表名 {where 条件} {group by 分组条件} {having 过滤} {order by 排序}

select * from emp;
别名查询

使用as关键字,可以省略

select ename "姓 名",sal 工资 from emp;		--别名中不能有特殊字符或关键字,如果有就需要加双引号
去除重复数据 distinct;
select distinct job from emp;				--单列去除重复

select distinct job,deptno from emp;		--多列去除重复 每一列都一样才算重复
查询中的四则运算
select 1+1;   								--orcal报错,缺少from表名  mysql中是正确的

select 1+1 from dual;						--dual是虚表/伪表 主要用来补齐语法结构
  • 查询员工年薪 = 月薪 * 12

    select sal*12 from emp;
    
  • 查询员工年薪 + 奖金

    select sal*12 + commn from emp;			--有错误 null值,代表不确定的 不可预知的内容不可以做四则运算
    
    select sal*12 + nav(comm,0) from emp;	--nav函数:如果参数1为null 就返回参数2
    
字符串拼接
  • 查询员工姓名

    select '姓名:' || ename from emp;        --||表示拼接
    
    select concat('姓名:',ename) from emp; 	--concat函数:字符串拼接,mysql和orcal中都有
    
条件查询
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 >= 1500 and sal <= 3000;
    
    select * from emp where sal between 1500 and 3000;
    
  • 查询名字在某个范围内的员工信息

    select * from emp where ename in ('JONES','SCOTT','FORD');
    
  • 查询员工姓名第三个字符是o的员工信息

    select * from emp where ename like '___O%';
    
  • 查询员工姓名中包含&的员工信息

    select * from emp where ename like '%\%%' escape '\';	 --如果有特殊字符,需要用escape转义
    
排序
  • 查询员工信息,按照奖金降序排序

    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 直接忽略空值

  • 统计员工工资总和

    select sum(sal) from emp;
    
  • 统计员工个数

    select count(*) from emp;
    
  • 统计员工的平均奖金

    select avg(comm) from emp;				--错误,comm有空值
    
    select sum(comm)/count(1) from emp;
    
数值函数
  • 四舍五入

    select ceil(45.926) from dual;	--46
    
    select floor(45.926) from dual;	--45
    
    select round(45.926,2)			--45.93
    
    select round(45.926,1)			--45.9
    
    select round(45.926,0)			--46
    
    select round(45.926,-1)			--50
    
    select round(45.926,-2)			--0
    
    select round(65.926,-2)			--100
    
  • 截断

    select trunc(45.926,2)			--45.92
    
    select trunc(45.926,1)			--45.9
    
    select trunc(45.926,0)			--45
    
    select trunc(45.926,-1)			--40
    
    select trunc(45.926,-2)			--0
    
    select trunc(65.926,-2)			--0
    
  • 求余

    select mod(9,3) from dual;		--0
    
    select mod(9,4) from dual; 		--1
    
字符函数
  • 获取字符串子串 ( 不管是0还是1都是从第一个字符开始 )

    select substr('abcdefg',0,3) from dual;		--abc
    
    select substr('abcdefg',1,3) from dual;		--abc
    
  • 获取字符串长度

    select length('abcdefg') from dual;			--7
    
  • 去除字符串左右两边空格( 中间的空格不会去除 )

    select trim(' hel lo ') from dual;			--hel lo
    
日期函数
  • 查询今天的日期

    select sysdate from dual;
    
  • 查询三个月后的日期

    select add_months(sysdate,3) from dual;
    
  • 查询员工入职的天数

    select ceil(sysdate - hiredate) from emp; 
    
转换函数
  • 数值转字符

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

    select 100 + '10' from dual; 				--110,默认转换
    
    select 100 to_number('10') from dual;	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值