Oracle 数据库入门

数据库作用:数据持久化

使用Oracle数据库必须开启的服务:
在这里插入图片描述
第一次使用CMD打开:
设置 scott 密码为 root 并使用 scott 登录。
在这里插入图片描述
测试SQL:
在这里插入图片描述

常用命令:

a) sqlplus
1.sqlplus /nolog
2.conn scott/root@orcl
3.show user
4.set linesize 150
5.set pagesize 20
6.passw
7.conn sys/abc123456@orcl as sysdba
8.select * from emp where ename=‘&ename’

b) alter user scott account unlock

SQL语句:

所用表:

▪emp表 雇员表(employee)
–Empno: 雇员工号 Ename: 雇员名字
–Job:工作。(秘书、销售、经理、分析员、保管)
–Mgr(manager):经理的工号 Hiredate:雇用日期
–Sal: 工资 Comm: 津贴 Deptno: 所属部门号

▪dept表 部门表(department)
–Deptno:部门号 Dname:部门名字 Loc: 地址

▪salgrade表 一个公司是有等级制度,用此表表示一个工资的等级

▪Grade:等级 losal:最低工资 hisal:最高工资

▪bonus表 奖金表:表示一个雇员的工资及奖金。
–Ename:雇员名字, job:工作,
–sal: 工资 comm:津贴

desc 表名; //查看表结构

添加注释:
comment on table emp is ‘雇员表’;
comment on column emp.ename is ‘雇员姓名’;

简单查询:
select empno,ename,job from emp where deptno = 10;

distinct去重:
select distinct deptno from emp;

查询给列、表添加别名:
select e.empno as “雇员编号“,e.ename as “雇员名称” from emp e where e.deptno = 10;

条件比较:
–= 等于

-!=,<> 不等于

-<,>,<=,>=

-any
select sal from emp where sal > any(1000,1500,3000)
取其中任意一个

-some
select sal from emp where sal > some(1000,1500,3000)
some跟any是同一个效果,只要大于其中某一个值都会成立

-all
select sal from emp where sal > all(1000,1500,3000)
满足全部条件

is null,is not null
用于判断是否为空(在SQL中不能使用!=或=,必须使用is)

between x and y
select * from emp where sal between 1500 and 3000;
左闭右闭,结果包含1500和3000

in(list),not in(list)
需要进行某些值的等值判断的时候可以使用in和not in
select * from emp where deptno in(10,20);

exists(sub-query)
子查询 ,类似双层for循环
查询部门号为10或20的雇员
select * from emp e
where exists(select deptno from dept d where (d.deptno = 10 or d.deptno = 20) and e.deptno = d.deptno );
or的优先级低于and,要使用括号提高优先级

like
模糊查询
使用like要慎重,因为效率比较低

_ 代表某个字符或数字只出现1次
%代表任意字符出现任意次数
escape表示转义字符,可以自己规定转义字符,常用 escape(’\’)
查询名字以S开头的雇员
select * from emp where ename like (‘S%’);
查询名字以S开头且倒数第二个字符为T的用户
select * from emp where ename liek (‘S%T_’);
查询名字带%的雇员
select * from emp where ename like (’%\%%’) escape (’\’);

-order by
如果是数值,按照从大到小
如果是字符串,按照A-Z字典序排序
order by是全排序,会比较消耗系统资源,因此选择在业务不太繁忙的时候进行
排序操作(默认升序)
order by col
升序
order by col(asc)
降序
order by col desc
多个列排序(优先级)
order by col1 desc(asc), col2 desc(asc)

练习:
使用in查询部门名称为 SALES 和 RESEARCH 的雇员姓名、工资、部门编号
select e.ename,e.sal,e.deptno from emp e,dept d
where e.deptno in
(select d.deptno from dept d where d.dname = ‘SALES’ or d.dname = ‘RESEARCH’);

使用exists查询部门名称为SALES和RESEARCH 的雇员姓名、工资、部门编号。
select e.ename,e.sal,e.deptno from emp e
where exists
(select d.deptno from dept d where (d.dname = ‘SALES’ or d.dname = ‘RESEARCH’) and d.deptno = e.deptno );

计算字段:

为什么需要计算字段?
–我们经常需要直接从数据库中检索出转换、计算戒格式化过的数据;而丌是检索出数据,然后再在客户机应用程序中重新格式化。

select ‘my name is’ || ename name from emp;

输出所有员工的年薪
- 引入函数nvl, nvl(arg1,arg2), 如果arg1是空,则返回arg2
select ename,(e.sal+nvl(e.comm,0))*12 from emp e;

并集,全集,交集,差集
–并集,将两个集合中的所有数据都进行显示,但是不包含重复的数据
select * from emp where deptno =30 union
select * from emp where sal >1000;

–全集,将两个集合的数据全部显示,不会完成去重的操作
select * from emp where deptno =30 union all
select * from emp where sal >1000;

–交集,两个集合中交叉的数据集,只显示一次
select * from emp where deptno =30 intersect
select * from emp where sal >1000;

–差集,包含在A集合而不包含在B集合中的数据,跟A和B的集合顺序相关
select * from emp where deptno =30 minus
select * from emp where sal >1000;

发布了40 篇原创文章 · 获赞 1 · 访问量 1094

猜你喜欢

转载自blog.csdn.net/weixin_44495162/article/details/103595316
今日推荐