oracle

mac2024-08-09  58

虽然挺乱的,但是能自己留着看看

启动数据库

sqlplus scott/tiger

把列拉长一点

set linesize 300

修改上一条语句

c ed

列的计算:

select empno,ename,sal*12 from emp;

SQL:

什么控制行 where 选择行 select empno,ename,job from emp where empno>=7000 and empno<=8000; 什么控制列 select 选择列 select empno, ename,job from emp;

字符串/字符、日期: 加单引号

大小写问题:

命令/关键字: 不敏感(不区分) 数据 敏感(区分)

运算符:

操作运算符:+ - * / % 关系运算符: > >= < <= 等号 = 不用 == 不等于!= 或<> 逻辑运算符: or and not not: select *from emp where not (mgr = 7788 and job = 'CLERK') where 的执行顺序,从右往左

判断是否为null,必须使用is,is not 任何数字和null进行运算,结果为null

需要对null进行处理的话: null–>0;

nvl: if nvl(comm,0) nvl2: if...else nvl(comm,comm,0)

对查询的结果进行去重: distinct

select distinct deptno from emp

连接符

java "hello"+"world" oracle comact ||

dual: oracle 提供的学习时使用的 临时表 :单行单列

select 'hello'||'world'from dual;

修改oracle的默认的日期格式

默认:DD-MON-RR 修改:alter session set NLS_DATE_FORMAT = 'yyyy-mm-dd' ;

范围查询: 数字 日期

between 小 and 大 >= 小 and <= 大 select *from emp where hiredate between '19-9月-81' and '19-9月-88';

模糊查询:

like 配合通配符使用: _ : 一个字符 %: 任意个字符 数字 日期 select *from emp where empno like '%4'; select *from emp where hiredate like '%9%';

示例

姓名中第二个字母是M的员工信息 select *from emp where ename like '_M%'; 姓名中包含M的员工信息 select *from emp where ename like '%M%'; 姓名长度>6的员工信息 >6 -> >=7 select *from emp where ename like '_______%'; 姓名中包含下划线的员工信息 select *from emp where ename like '%\_%' escape '\'; not in 中不能出现 null 如果出现了null 结果为空

排序:

order by 字段名 | 表达式 | 序号 select empno,ename,sal from emp order by sal desc; select empno,ename,sal from emp order by 3 desc; select empno,ename,sal from emp order by sal esc; 默认 null默认是最大值 想将null放到最后面 加nulls last select empno,ename,sal from emp order by sal esc nulls last;

多列排序:

sal hiredate 先根据工资降序排序,工资一样的按照雇佣日期升序 select *from emp order by sal desc,hiredate asc;

函数:

单行函数:

字符函数 数值函数 日期函数 转换函数 通用函数 都是一次操作一行数据的函数

字符函数:

lower upper initcap
select 'HeLlo,wOrLD' 一,lower('HeLlo,wOrLD') 二,upper('HeLlo,wOrLD') 三,initcap('HeLlo,wOrLD') 四 from dual; dual 单行or单列
substr(str,begin,len)
从1开始数,截取从bengin开始的len个字符 select substr('hello world',3,3) from dual;
length() 返回字符数 / lengthb()返回字节数
英文或者数字,字符数就是字节数 如果中文或者符号: utf-8 :汉字或者符号 占3个字节 gbk: 汉字或者符号 占2个字节
insrt(str,substr): 在str中找substr的位置
select instr('helloworld','11') from dual;
lpad / rpad : 填充
select lpad('hello',10,'*') 左,rpad('hello',10,'*') 右 from dual;
trim: 去掉两端的任意字符
select trim('XXXXXXXhello worldXXXXXXX')from dual; select trim('X' from 'XXXXXXXhello worldXXXXXXX')from dual; 不写'X' 默认去掉两端的空格
replace: 替换
select replace ('hello','l','*')from dual;

数值函数:

round(数字,n位数): 四舍五入 保留n位小数
select round(67.183,2) 一 ,round(67.183,1) 二,round(67.183,0) 三,round(67.183,-1) 四,round(67.183,-2) 五 from dual;
trunc(数字,n位数): 舍尾 保留n位小数
select trunc(67.183,2) 一 ,trunc(67.183,1) 二,trunc(67.183,0) 三,trunc(67.183,-1) 四,trunc(67.183,-2) 五 from dual;
mod(a,b) 相当于a%b 求余
select mod(9,6) from dual;
sysdate: 当前时间 这是关键字 不是函数 不要写成sysdate()
select sysdate from dual; 格式化: 日期--->字符串 select to_char(sysdate,'yyyy-mm-dd') from dual; 日期 + - 数字(默认是天) select sysdate+1 from dual; 日期 - 日期 得到天数 日期不允许+
minths_between(日期1,日期2) : 日期1-日期2
select months_between(sysdate,sysdate-100) from dual;
add_months(日期,月数) 日期+月数,
last_day(日期) 日期所在月分的最大天数
select last_day(sysdate) from dual;
next_day(日期,星期n) 下一个星期n是哪一天
select next_day(sysdate,'星期五') from dual;
round(日期,‘year’ / ‘month’ / ‘day’)
对年月日进行四舍五入 按总数的中间值 舍或者入
trunc(日期,‘year’/‘month’ / ‘day’) 对年月日舍弃
最新回复(0)