zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Oracle数据库:oracle函数,单行函数,多行函数,upper,lower,initcap,字符串函数

Oracle数据库 函数 字符串 多行 单行 Lower upper
2023-09-11 14:15:38 时间

Oracle数据库:oracle函数,单行函数,多行函数,upper,lower,initcap,字符串函数

2022找工作是学历、能力和运气的超强结合体,遇到寒冬,大厂不招人,可能很多算法学生都得去找开发,测开
测开的话,你就得学数据库,sql,oracle,尤其sql要学,当然,像很多金融企业、安全机构啥的,他们必须要用oracle数据库
这oracle比sql安全,强大多了,所以你需要学习,最重要的,你要是考网络警察公务员,这玩意你不会就别去报名了,耽误时间!

oracle系列文章:
【1】Oracle数据库:啥是oracle数据库?你为啥要学oracle?
【2】Oracle数据库:oracle 11g安装教程,已安装好的oracle各个文件夹的作用,oracle用户权限怎么样
【3】Oracle数据库:oracle启动,oracle客户端工具plsql安装教程和使用方法
【4】Oracle数据库:创建表空间,创建新用户,给用户分配对象、角色和系统权限,登录新用户建表
【5】Oracle数据库:链接配置,包括sqlnet.ora里面的transnames.ora配置数据库标识符SID,listener暂时简单了解
【6】Oracle数据库:net configureation assistant工具配置监听listener,配置本地网络访问服务器上的数据库
【7】Oracle数据库:oracle字符类型、数字类型、创建表表名的命名规则
【8】Oracle数据库:约束条件:主键约束、唯一约束、检查约束、非空约束、外键约束、默认值填写
【9】Oracle数据库:表的关系:一对多,一对一,多对多,一一继承和修改的关系,表不是重点,重点是数据和约束关系
【10】Oracle数据库:sql语言结构,数据查询语言DQL,select * from table;算术,别名,连接,去重等操作
【11】Oracle数据库:约束行限制where语句,判断条件,比较条件,字符串日期格式,in,like,escape,null语句
【12】Oracle数据库:逻辑运算and,or,not和各种运算的优先级控制
【13】Oracle数据库:排序order by语句,select from where order by的执行先后顺序,各种样例


oracle函数,单行函数,多行函数

在这里插入图片描述
知道java的都好说
在这里插入图片描述
在这里插入图片描述
这是重点
单行
多行

最大的区别,是处理查询结果上是不一样的
当行是对每一行做处理,每一行都要返回结果【每个人的年薪】
多行是对某一组数据做处理,返回一个结果【部门的平均薪资】

在这里插入图片描述
在这里插入图片描述

这些和jav,Python,c++的函数一样
任何编程语言都一样的逻辑,只是形式具体规定不同,仅此而已

很骚

单行函数:5大类

在这里插入图片描述
select做列选择
where是条件控制
order by是排序操作

里面都可以放列为x
输出一些别的条件y

这样过滤就很舒服了

单独搞出来一个函数处理,这样,让sql语句不显得那么冗杂
在这里插入图片描述
在这里插入图片描述

字符函数

在这里插入图片描述
在这里插入图片描述
这些底层的函数,我们在数据结构与算法里面写过无数次
在这里插入图片描述
我们现在oracle就是用那些函数

很强,所以这些本质的东西都是想通的
initcap,将单词首字母搞大写,其他小写
666
参数都是1个字符串str
反正我已经对java很熟悉,这些都一样

在这里插入图片描述
姓名转大写
工作id转化为小写
要链接

表长啥样

SQL> desc employees;
Name           Type         Nullable Default Comments                                                                                                                                                                                  
-------------- ------------ -------- ------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
EMPLOYEE_ID    NUMBER(6)                     Primary key of employees table.                                                                                                                                                           
FIRST_NAME     VARCHAR2(20) Y                First name of the employee. A not null column.                                                                                                                                            
LAST_NAME      VARCHAR2(25)                  Last name of the employee. A not null column.                                                                                                                                             
EMAIL          VARCHAR2(25)                  Email id of the employee                                                                                                                                                                  
PHONE_NUMBER   VARCHAR2(20) Y                Phone number of the employee; includes country code and area code                                                                                                                         
HIRE_DATE      DATE                          Date when the employee started on this job. A not null column.                                                                                                                            
JOB_ID         VARCHAR2(10)                  Current job of the employee; foreign key to job_id column of the
jobs table. A not null column.                                                                                            
SALARY         NUMBER(8,2)  Y                Monthly salary of the employee. Must be greater
than zero (enforced by constraint emp_salary_min)                                                                                          
COMMISSION_PCT NUMBER(2,2)  Y                Commission percentage of the employee; Only employees in sales
department elgible for commission percentage                                                                                
MANAGER_ID     NUMBER(6)    Y                Manager id of the employee; has same domain as manager_id in
departments table. Foreign key to employee_id column of employees table.
(useful for reflexive joins and CONNECT BY query) 
DEPARTMENT_ID  NUMBER(4)    Y                Department id where employee works; foreign key to department_id
column of the departments table

里面数据是这样的,名字可能是小写

SQL> select last_name,job_id from employees;

LAST_NAME                 JOB_ID
------------------------- ----------
Abel                      SA_REP
Ande                      SA_REP
Atkinson                  ST_CLERK
Austin                    IT_PROG

要你把名字转大写,工作id转小写
拼接一个字符串,好说||

SQL> select 'the job id for '||upper(last_name)||' is '||lower(job_id) from employees;

'THEJOBIDFOR'||UPPER(LAST_NAME)||'IS'||LOWER(JOB_ID)
------------------------------------------------------
the job id for ABEL is sa_rep
the job id for ANDE is sa_rep
the job id for ATKINSON is st_clerk
the job id for AUSTIN is it_prog

upper()
lower()
放入参数名字即可
返回以后就是那些变量了

起一个别名

SQL> select 'the job id for '||upper(last_name)||' is '||lower(job_id) as "EMPLOYEE DETAILS" from employees;

EMPLOYEE DETAILS
------------------------------------------------------
the job id for ABEL is sa_rep
the job id for ANDE is sa_rep
the job id for ATKINSON is st_clerk
the job id for AUSTIN is it_prog

在这里插入图片描述
由于这个小写不行
我们需要首字符大写,其他小写
所以你要转换才能查名字

SQL> select employee_id,last_name,department_id from employees where last_name=initcap('higgins');

EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID
----------- ------------------------- -------------
        205 Higgins                             110

函数参数常量字符串是’'单引号扩起来的

拼接那边的字符串是双引号哦

Higgins就通过initcap函数搞定

单行函数,可以出现在select,where和order by函数中
因为单行哇

多行不知道,应该是要读取出来之后,放order by中

字符处理函数,dual表,是from后面可能没有表时,可以用dual,忽略操作

所我们只是想结构化查询和操作
在这里插入图片描述
下面这些函数,我们不用查一个实际的表格
只是做一些骚操作
所以参数并不是来自于真实的表格,就用dual表格骚一把
在这里插入图片描述
substr,start和end
length和java一样
instr查w的位置,indexof,返回的是位置
lpad,rpad,字符串填充,和cnn类似
左边添加*,右边,总长度要是10
补齐

trim()在java中是去掉两侧的空格
这里一样,默认去掉首尾的H

在这里插入图片描述

SQL> select concat('hello','world') from dual;

CONCAT('HELLO','WORLD')
-----------------------
helloworld

等价于||

函数还可以嵌套呢

SQL> select concat('hello',concat('world','gogogogo')) from dual;

CONCAT('HELLO',CONCAT('WORLD','GOGOGOGO'))
------------------------------------------
helloworldgogogogo

在这里插入图片描述
start–k,-1表示最后一位,-2倒数第二位
截取几个??arg3
这里的位置不是0开始哦,和java不一样
闭区间负数-那就是倒数位置开始从左往右截取,这个不变的

SQL> select substr('helloworld', 1, 5) from dual;

SUBSTR('HELLOWORLD',1,5)
------------------------
hello

SQL> select substr('helloworld', -1, 5) from dual;

SUBSTR('HELLOWORLD',-1,5)
-------------------------
d

SQL> select substr('helloworld', -5, 5) from dual;

SUBSTR('HELLOWORLD',-5,5)
-------------------------
world

在这里插入图片描述

不给长度,就是全部截取

SQL> select substr('helloworld', -5) from dual;

SUBSTR('HELLOWORLD',-5)
-----------------------
world

在这里插入图片描述

SQL> select length('helloworld') from dual;

LENGTH('HELLOWORLD')
--------------------
                  10

在这里插入图片描述

SQL> select instr('helloworld','wor') from dual;

INSTR('HELLOWORLD','WOR')
-------------------------
                        6

这些操作默认位置都是1,不看0
和正常其他的程序语言不同,这就很6了
是为了公司业务员用直观表示吗

在这里插入图片描述

SQL> select instr('helloworld','l',1,2) from dual;

INSTR('HELLOWORLD','L',1,2)
---------------------------
                          4

l从1位置开始,第二次出现的位置
4位置

SQL> select instr('helloworld','l',1,3) from dual;

INSTR('HELLOWORLD','L',1,3)
---------------------------
                          9

oracle 字符串函数继续讲

paddding
pad

lpad
在这里插入图片描述


SQL> select lpad('hello',10,'*') from dual;

LPAD('HELLO',10,'*')
--------------------
*****hello

左侧填充

右侧一样
在这里插入图片描述

SQL> select rpad('hello',10,'*') from dual;

RPAD('HELLO',10,'*')
--------------------
hello*****

原串,长度,填啥

在这里插入图片描述
竟然还要from

SQL> select trim('h' from 'helloworld') from dual;

TRIM('H'FROM'HELLOWORLD')
-------------------------
elloworld

from原串去除前面那个字符串

SQL> select trim('hello' from 'helloworld') from dual;
select trim('hello' from 'helloworld') from dual

ORA-30001: 截取集仅能有一个字符

还不能去多个字符

SQL> select trim('h' from 'helloworldh') from dual;

TRIM('H'FROM'HELLOWORLDH')
--------------------------
elloworld

SQL> select trim(both 'h' from 'helloworldh') from dual;

TRIM(BOTH'H'FROM'HELLOWORLDH')
------------------------------
elloworld

both也行的

如果还是要只去掉头
只去掉尾呢

SQL> select trim(leading 'h' from 'helloworldh') from dual;

TRIM(LEADING'H'FROM'HELLOWORLDH')
---------------------------------
elloworldh

SQL> select trim(trailing 'h' from 'helloworldh') from dual;

TRIM(TRAILING'H'FROM'HELLOWORLDH')
----------------------------------
helloworld

一个leading领头
一个trailing尾部

在这里插入图片描述

用别的字符,替代原串中的某个子串

SQL> select replace('helloworld','hello','hi') from dual;

REPLACE('HELLOWORLD','HELLO','HI')
----------------------------------
hiworld

很简单
Python或者java中一般就是str.replace(a,b)
用别的字符b,替代原串str中的某个子串a

差不多的
在这里插入图片描述

SQL> select concat(first_name,last_name),length(last_name),instr(last_name,'a'),job_id from employees where substr(job_id,4,3)='REP';

CONCAT(FIRST_NAME,LAST_NAME)                  LENGTH(LAST_NAME) INSTR(LAST_NAME,'A') JOB_ID
--------------------------------------------- ----------------- -------------------- ----------
SusanMavris                                                   6                    2 HR_REP
PatFay                                                        3                    2 MK_REP
HermannBaer                                                   4                    2 PR_REP
PeterTucker                                                   6                    0 SA_REP
DavidBernstein                                                9                    0 SA_REP
PeterHall                                                     4                    2 SA_REP
ChristopherOlsen                                              5                    0 SA_REP
NanetteCambrault                                              9                    2 SA_REP
OliverTuvault                                                 7                    4 SA_REP
JanetteKing                                                   4                    0 SA_REP
PatrickSully                                                  5                    0 SA_REP
AllanMcEwen                                                   6                    0 SA_REP
LindseySmith                                                  5                    0 SA_REP
LouiseDoran                                                   5                    4 SA_REP
SarathSewall                                                  6                    4 SA_REP
ClaraVishney                                                  7                    0 SA_REP
DanielleGreene                                                6                    0 SA_REP
MatteaMarvins                                                 7                    2 SA_REP
DavidLee                                                      3                    0 SA_REP
SundarAnde                                                    4                    0 SA_REP

CONCAT(FIRST_NAME,LAST_NAME)                  LENGTH(LAST_NAME) INSTR(LAST_NAME,'A') JOB_ID
--------------------------------------------- ----------------- -------------------- ----------
AmitBanda                                                     5                    2 SA_REP
LisaOzer                                                      4                    0 SA_REP
HarrisonBloom                                                 5                    0 SA_REP
TaylerFox                                                     3                    0 SA_REP
WilliamSmith                                                  5                    0 SA_REP
ElizabethBates                                                5                    2 SA_REP
SunditaKumar                                                  5                    4 SA_REP
EllenAbel                                                     4                    0 SA_REP
AlyssaHutton                                                  6                    0 SA_REP
JonathonTaylor                                                6                    2 SA_REP
JackLivingston                                               10                    0 SA_REP
KimberelyGrant                                                5                    3 SA_REP
CharlesJohnson                                                7                    0 SA_REP

33 rows selected

通过各种骚函数,截取字符串,或者定位字符的位置,或者拼接啥的,或者求字符串长度啥的
就搞定了这一切

在这里插入图片描述

SQL> select concat(first_name,last_name),length(last_name),instr(last_name,'a') from employees where substr(last_name,-1,1)='n';

CONCAT(FIRST_NAME,LAST_NAME)                  LENGTH(LAST_NAME) INSTR(LAST_NAME,'A')
--------------------------------------------- ----------------- --------------------
MozheAtkinson                                                 8                    0
DavidAustin                                                   6                    0
DavidBernstein                                                9                    0
JohnChen                                                      4                    0
LexDe Haan                                                    7                    5
LouiseDoran                                                   5                    4
MichaelHartstein                                              9                    2
AlyssaHutton                                                  6                    0
CharlesJohnson                                                7                    0
JackLivingston                                               10                    0
JasonMallin                                                   6                    2
SamuelMcCain                                                  6                    4
AllanMcEwen                                                   6                    0
ChristopherOlsen                                              5                    0
TJOlson                                                       5                    0
MarthaSullivan                                                8                    7
Jose ManuelUrman                                              5                    4
ShantaVollman                                                 7                    6
JenniferWhalen                                                6                    3

19 rows selected

substr截取字符串的某几个字符
很有趣,用得多
在这里插入图片描述

这种美团外卖经常搞
保证用户隐私安全

SQL> desc employees;
Name           Type         Nullable Default Comments                                                                                                                                                                                  
-------------- ------------ -------- ------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
EMPLOYEE_ID    NUMBER(6)                     Primary key of employees table.                                                                                                                                                           
FIRST_NAME     VARCHAR2(20) Y                First name of the employee. A not null column.                                                                                                                                            
LAST_NAME      VARCHAR2(25)                  Last name of the employee. A not null column.                                                                                                                                             
EMAIL          VARCHAR2(25)                  Email id of the employee                                                                                                                                                                  
PHONE_NUMBER   VARCHAR2(20) Y                Phone number of the employee; includes country code and area code                                                                                                                         
HIRE_DATE      DATE                          Date when the employee started on this job. A not null column.                                                                                                                            
JOB_ID         VARCHAR2(10)                  Current job of the employee; foreign key to job_id column of the
jobs table. A not null column.                                                                                            
SALARY         NUMBER(8,2)  Y                Monthly salary of the employee. Must be greater
than zero (enforced by constraint emp_salary_min)                                                                                          
COMMISSION_PCT NUMBER(2,2)  Y                Commission percentage of the employee; Only employees in sales
department elgible for commission percentage                                                                                
MANAGER_ID     NUMBER(6)    Y                Manager id of the employee; has same domain as manager_id in
departments table. Foreign key to employee_id column of employees table.
(useful for reflexive joins and CONNECT BY query) 
DEPARTMENT_ID  NUMBER(4)    Y                Department id where employee works; foreign key to department_id
column of the departments table    

HR用户的案例表中
有员工的电话号码
来看看
我们替换一把

SQL> select replace(phone_number,substr(phone_number,4,4),'****') from employees;

REPLACE(PHONE_NUMBER,SUBSTR(PHONE_NUMBER,4,4),'****')
--------------------------------------------------------------------------------
515****.4567
515****.4568
515****.4569
590****.4567
590****.4568
590****.4569
590****.4560
590****.5567

原串就是电话号码

我们要替换号码中间四位
那先截取一下中间4位置开始的4个字符
然后把它替换为四个星就行了
这好说


总结

提示:重要经验:

1)
2)学好oracle,即使经济寒冬,整个测开offer绝对不是问题!同时也是你考公网络警察的必经之路。
3)笔试求AC,可以不考虑空间复杂度,但是面试既要考虑时间复杂度最优,也要考虑空间复杂度最优。