0
点赞
收藏
分享

微信扫一扫

Oracle常用基础知识

互联网码农 2023-08-17 阅读 35

整体介绍

SQL语言是一种数据库语言

 1、DDL:数据定义语言

create-创建  drop-删除 alter-修改 rename-重命名 truncate-截断

2、DML:数据操作语句

insert-插入 delete-删除 update-更新 select-查询

3、DCL:数据控制语句

grant-授权 revoke-回收权力 commit-提交事务 rollback-回滚事务

注:Oracle命令不区分大小写,但数据和内容区分 

一、select语法

1、基本结构和简单查询

基本结构:select 查询的信息 from 数据来源

select * from emp
select deptno,sal,ename from emp

*:表示查询该表的所有数据

查询的信息可以是*也可以是一个或多个字段名;多个字段之间用逗号分隔,字段的顺序即查询结果顺序

2、去重(distinct)

select distinct deptno from emp;

distinct去重是去除的整条记录的重复

3、别名

给列取别名有两种写法:

写法1: 字段名后跟空格和别名

select ename 姓名,sal 工资 form emp

写法2:使用as关键字

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

4、排序(order by)

降序:desc  升序:asc(默认升序排序)

select ename,sal from emp order by sal desc

多字段排序:用逗号分隔

select ename,sal from emp order by deptno asc,sal desc

解释:查询emp表中的ename,sal字段,查询结果集按照先deptno字段值升序排序,在deptno升序的基础上,按照sal降序排序;

5、伪列

生成表中没有的数据列称为伪列

select ename,sal,sal*2 年薪 from emp

其中年薪属于别名而sal*2是表中没有的数据,是通过计算得到,所以称为伪列

6、字符串拼接 ||

select ename,ename || ‘a’ 别名 from emp

注:两个变量拼接时,如a||b,当a为null则结果为b

7、虚表 dual

只有一行一列,是用来构成select的语法规则,可以执行插入、更新、删除操作,还可以执行drop操作

注:虽然可以执行drop操作,但是还是不建议对dual执行drop操作,否则会使系统不能用、起不了数据库。

select 11*22 from dual

8、select 执行顺序

 以上是之前知识的综合语句:

解析顺序:from====》select====》order by

先找表,再找需要查询的内容,最后对结果集排序

9、条件查询*

 解析过程:1、查询数据源 2、判断是否符合行记录条件 3、将满足条件的记录在select指定的信息中存放(结果集当中)

条件运算

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

基础结构:

select * form emp where deptno=10;

 between...and... 值在一定范围内、闭区间(包含两段界限)

select * from emp where sal between 2000 and 4000

in(a,b)满足a或b都可以

select * from emp where deptno in(10,20)

条件连接运算

and、or、not

and 同时满足

select * from emp where sal>1500 and deptno=20;

 or 满足一个即可

select * from emp where sal>1500 or deptno=20;

 not 取反

select * from emp where not deptno=20;

10、null处理

nvl(expl,res)

null在参加计算式,结果为null,使用nvl(expl,res)处理空值:expi-表达式、res-结果

当expi不为空时,结果为expi,为空时结果为res

select ename,sal,comm,sal+nvl(comm,0)月收入 from emp

null排序

nulls first、nulls last 将空值放置在列前或后

select * from emp order by comm desc nulls first

null查询

不为空

select * from emp where comm is not null;

 为空

select * from emp where comm is null;

 不为空

select * from emp where not comm is null;

11、模糊查询 like

select * from emp where like ‘%s%’

%表示任意个字符  _表示一个任意字符

当模糊查询的内容包含”%“、”_“时

select * from emp where like ‘%a%%’ escape(‘a’)

escape中的a属于标识符,用于证明a后的字符不是特殊字符,只对其身后的一位字符有效

如想查询‘a%’则,...like ‘%aaa%%’ escape(‘a’)

12、where子句

select * from emp where deptno = (select deptno from dept where dname = ‘SALES’)

二、函数

1、函数介绍

根据函数的返回结果,分为单行函数和多行函数。

单行函数:一条记录返回一个结果。

多行函数(组函数、聚合函数):可同时对多条记录进行操作,并返回一个结果。

2、常用的单行函数

字符函数

concat(x,y):连接字符串x和y

instr(x,str,start,n):在x中查找str,从start开始,也可以从第n次开始。初始位置从1开始。

select instr(‘HelloWorld’,‘l’,1,3)from dual

length(x):返回x的长度

lower(x):x转换为小写

upper(x):x转换为大写

itrim(x,trim_str):把x左边截去trim_str字符串,没有写trim_str就截去空格

replace(x,old,new):在x中,将old替换为new,替换的是能找到的所以old

substr(x,start,length):从start开始,截取length个字符,缺length默认到结尾

日期函数

sysdate、current_date:当前日期(精确到秒)

add_months(d1,n1):在d1基础上再加n1个月后的日期

last_day(d1):放回d1所在月份的最后一天

months_between(d1,d2):从d1到d2之间的月数

next_day(d1[ ,c1]):d1日期的下周星期几(c1)

转换函数

to_char(x,c):将日期或数据x按照c格式转换

to_date(x,c):将字符串x按照c的格式转换为日期

to_number(x):将字符串x转为数字型

3、组行数

avg()平均值、sum()求和、min()最小值、max()最大值、count()统计

注:null不参与运算

group by 分组

结构:select...from...group by....

select deptno,avg(sal)from emp group by deptno

having 过滤组信息

select deptno,avg(sal)from emp group by deptno having avg(sal)>2000

执行顺序:①找到数据源from emp ②对数据源进行分组group by deptno ③对分组内容进行过滤having avg(sal)>2000 ④从分组中查询数据select deptno,avg(sal)

过滤行记录和组信息

行记录的过滤是针对每条记录的筛选,组信息的过滤是针对组的筛选,是可以同时出现的先筛选行,再过滤组

结构:⑤select...①from...②where...③group by...④having...(序号代表执行顺序)

三、分页和去重

1、分页

假分页:一次查询出数据库中的所有记录,然后在每页中显示指定的记录

缺:服务器负载大 优:与数据库只对接一次

真分页:对数据库多次查询,每次只获得本页的数据并显示

缺:频繁操作数据库 优:服务器负载小

2、rownum

是对每一个结果集中的每一条记录的编号,从1开始

查询伪列rownum

select ename,sal,deptno,rownum from emp

将查询出的结果集作为临时的数据来源,此时结果集中的rownum rw为普通字段

select * from(select ename,sal,deptno,rownum rw from emp)where rw>5 and rw <=10

3、排序分页(每页只展示3条数据)

select ename,sal,deptno,r1,r2
  from(select ename,sal,deptno,r1,rownum r2
    from(select ename,sal,deptno,rownum r1
      from emp order by sal desc))
where r2>(3-1)*3 and r2<=3*3

4、rowid去重

rowid相当于伪列,由Oracle生成,是唯一的

delete from copy where rowid not in(select min(rowid)from copy group by deptno)

四、表连接

概念:多表查询-多表拼接

1、92语法

结构:select...from table1,table2...where

很多时候需要为表取别名(①简化表名②可能存在自连接的情况)

原理:from后面出现的顺序,前面的表作为内存的for循环,后出现的表作为外层的for循环

笛卡儿积

两个集合中的每一个成员,都与对方集合中的任意一个成员有关联;如果没有给筛选条件,即为笛卡儿积

等值连接

在笛卡儿积的基础上取条件列相同的值

select * from emp e,deptno d where e.deptno=d.deptno

非等值连接

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

select ename,sal,hiredate,grade s from emp e,salgrade s where e.sal between losl and hisal

自连接

特殊的等值连接(来自于同一张表)

外连接

内连接使用比较运算符根据每个表共有的列的值匹配两个表中的行

外连接可以是左向外连接、右向外连接或完整外部连接

注:没有+的是主表

select * 
  from detp d,(select count(*),deptno 
    from emp group by deptno)c
where d.deptno=c.deptno(+)

2、99语法

笛卡儿积 cross join

select * from dept cross join emp

自然连接 natural join

做等值连接,需要有(同名列,主外键关系)

select ename,empno,deptno,dname from emp natural join dept

using连接、等值连接 join using

select ename,empno,deptno,daname from emp join dept using(deptno)

join on

等值:

select ename,empno,e.deptno,dname from emp e join dept d on e.deptno=d.deptno

非等:

select ename,sal,deptno,grade from emp e join salgrade s on sal between losal and hisal

outer join

 全连接 full join on

 3、集合操作

union 并集 union All 全集 instersect 交集 Minus 差集

五、DDL语法

1、表操作

创建表

 数据类型

详细解释可以查看官方文档

修改表结构

修改表名:

修改列名:

修改字段类型:

alter table tb_student modify(studentId char(10))

添加列:

删除列:

2、删除表

 3、约束

主键约束(primary key):该字段在表中唯一且非空

唯一性约束(unique):只能出现一次

非空约束(not null):不能为空

外键约束(foreign key):外键(从表)中的数据必须在主表中已经存在才能使用

检查约束(check)

创建表的同时,将约束进行创建,约束不设定名称

create table tb_user(
  userid number(5) primary key,
  username varchar2(30) check(length(username) berween 4 and 20) not null,
  age number(3) default(18) check(age>=18),
  gender char(3) default('男') check(gender in('男','女')),
  regtime date default(sysdate)
)

创建表的同时,给约束设定名称

create table tb_user(
  userid number(5),
  username varchar2(30) not null,
  age number(3) default(18),
  gender char(3) default('男'),
  regtime date default(sysdate),
  constraint pk_user_id primary key(userid),
  constraint ck_user_name check(length(username) berween 4 and 20),
  constraint ck_user_age check(age>=18),
  constraint ck_user_gender check(gender in('男','女'))
)

创建完表后,追加约束

alter table tb_user add constraint pk_user_id primary key(userid)

 修改约束

alter table tb_user modify(username constrant nn_user_name no null)

 外键处理方式

强制不让删除

alter table tb_txt add constraint fk_txt_user_id foreign key(userid) references tb_user(userid)

删除后自动设置为null

alter table tb_txt add constraint fk_txt_user_id foreign key(userid) references tb_user(userid) on delete set null

级联删除

alter table tb_txt add constraint fk_txt_user_id foreign key(userid) references tb_user(userid) on delete cascade

禁用和启用约束

启用/禁用(enable/disable):对后来的数据进行校验

验证/非验证(validate/novalidate):对已存在的数据进行校验

删除约束

alter table tb_user drop cpnstraint ag_user_email cascade

六、DML

1、插入

 插入记录时要满足长度兼容、约束

默认添加:

 使用默认添加时,数据必须和表结构字段顺序和个数一致

添加时指定列和顺序

 如果是外键,被参考的主表字段一定要有数据

2、更新

 注:在更新时一定记得加上where条件

3、删除

 注:先删从表后删主表

举报

相关推荐

0 条评论