Oracle数据库简单查询

Oracle数据库

一、导入scott.sql文件

导入scott.sql文件,使用用户表(emp表,dept表,salgrade表,bonus表)

步骤:

1.使用管理员进入dos(SQL)
conn sys as sysdba;
2.查找scott.sql 文件位置(默认下载路径)
C:\oraclexe\app\oracle\product\11.2.0\server\rdbms\admin\scott.sql
3.执行脚本文件
@C:\oraclexe\app\oracle\product\11.2.0\server\rdbms\admin\scott.sql
;
4.使用sql(dos) || plsql 登录
dos 窗口:
conn scott as sysdba; 密码为TIGER
select * from scott.emp;
plsql
登录界面 scott/TIGER
数据库 XE
身份normal
select table_name from tabs;
desc emp;
select * from emp;

二、数据库命令

SQL 结构化查询语言

分类命令
DDL(数据定义语言)create:创建;drop:删除;alter:修改;rename:重命名;truncate:截断
DML(数据管理语言)(DQL(数据查询语言)、DML(数据管理语言))insert:插入;delete:删除;update:更新;select:查询
DCL(数据库控制语言)grant:授权;revoke:回收权利;commit:提交事务;rollback:回滚事务

回滚事务:基于日志文件回滚

三、select查询

1.查询指定表的数据

-- 查询所有数据
select * from emp;
select * from dept;
-- 查询指定数据(员工姓名,员工薪资)
select e.name,e.sal from emp as e;
-- 查询员工表里的部门编号
select distinct deptno from emp order by deptno;

*:通配符,可以表示要查询表的所有字段名

e:别名,可通过别名.使用表的指定字段,通过as声明,as可省略

distinct :去除重复行

2.条件判断

where – 条件过滤

1)一般过滤

= 、 >、 <、 >=、 <=、 !=、 <>、 between and

-- = 判断值的范围  >、 <、 >=、 <=同理
select * from emp where ename='CLARK'
--  !=、 <> 不等于 效果相同
select * from emp where deptno != 10;
select * from emp where deptno <> 10;
--  between and 在...和...之间 可以取到边界值
select * from emp where sal between 1250 and 5000;
2)逻辑查询

and (且),or(或)

select * from emp where sal>=2000 and deptno=10;
3)null查询

null :is null、 is not null、 --not is null

-- 查询薪资补贴comm
select * from emp e where e.comm is null;  -- 没有补贴的员工信息
select * from emp e where e.comm is not null;  -- 有补贴的员工信息
select * from emp e where e.comm not is null;  -- 有补贴的员工信息(取反)

4)子查询

-- 查询年薪在[15000,30000)
select * from 
( select  empno,ename,(sal+nvl(comm,0))*12 as income 
from emp ) temp where temp.income>=15000 and temp.income<30000;

5)in查询

-- in 查询类似于or 
select * from emp where job in ('CLERK','SALESMAN');
-- 加not 
select * from emp where job not in ('CLERK','SALESMAN');

6)like 模糊查询

模糊查询,使用通配符:

​ %:零个及以上(任意个数的)的字符

​ _:一个字符

​ 遇到内容中包含 % _ 使用escape(‘单个字符’)指定转义符

-- 查询员工姓名包含'L'的员工信息
select * from emp where ename like '%L%'
-- 查询员工姓名中第二个字母为'L'的员工信息
select * from emp where ename like '_L%'
-- escape('单个字符')转义,当查询的字符被SQL占用时 如%
-- 添加含有%的SQL语句
insert into emp(empno,ename,sal) values(1000,'t_%test',8989); 
insert into emp(empno,ename,sal) values(1500,'t_tes%t',8000);
-- 转义 查找
select ename,job,sal,deptno from emp where ename like '%b%%' escape('b');

7)集合操作查询

对多个个结果集的操作,求并集,交集,差集

①Union 求并集(去重)

​ 对两个及以上结果集进行并集操作,不包括重复行同时进行默认规则的排序;

Union All,全集(不去重)

​ 对两个结果集进行并集操作,包括重复行,不进行排序 ;

select empno,ename,deptno from emp where sal>1500 
union  
select empno,ename,deptno from emp where comm is not null
union 
select empno,ename,deptno from emp where job='CLERK'

②Minus,差集(减去重复)

​ 对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序

--差集 查询显示不存在雇员的所有部门号
select deptno from dept
minus
select distinct deptno from emp
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值