Oracle学习笔记(一)

来源:互联网 发布:编程达人视频教程 编辑:程序博客网 时间:2024/06/05 18:43
一.介绍
1.Oracle数据库: DB / DBMS
2.数据库的使用者: DBA(Database Administrator) / 开发人员
3.数据库的种类:
oracle / sybase / db2(IBM)
sql server / mysql (开源,现已被oracle收购) /
access,postgreSQL
4.数据表:行和列组成.
行: row / record 记录
列: column / field 字段
5.SQL分类: Structured Query Language
DQL: select
DML: insert / update / delete
DDL: create / drop / alter / truncate
DCL: grant / revoke
TCL: commit / rollback / savepoint
6.了解练习常用到的数据表:
emp: 职员表
empno 员工号
ename 员工姓名
job 工作
mgr 上级编号
hiredate 受雇日期
sal 薪金
comm 佣金
deptno 部门编号
dept: 部门表
salgrade: 薪水等级表
数据类型:
数字类型:number(p,s)最长p位,小数点后s位
字符类型:char(n) / varchar2(n) 推荐
varchar
日期:date

number(7,2) 99999.99
number(5) 99999
number 随便
二.SQL
-- *号表示查询全部列
select * from dept;
select * from emp;
1.列的选取(或称为投影),列名(字段名,field)
select empno,ename,deptno from emp;
2.列别名
select empno 员工编号,ename 员工姓名 from emp;
select empno as 员工编号,ename as 员工姓名 from emp;
3.列的算术表达式(+ ,-, *, /)
例:显示年收入
select empno,ename,sal,sal*12 年收入 from emp;
4.处理空值的函数nvl() -- null value,要考虑字段类型
例:显示所有人月总收入
select * from emp;
select empno,ename,sal,comm,sal+nvl(comm,0) from emp;
select ename,sal,comm,sal+comm from emp
5.连接字符串||,例
select empno,ename,empno||ename from emp
6.技巧:
select t.ename,t.sal,t.empno||ename from emp t;--取别名
select * from emp for update -- oracle特有
select rowid,t.* from emp t -- oracle特有
7.对行进行选取
例:选取部门号为10的所有员工
select * from emp where deptno=10
例:查询所有岗位是manager的员工
select * from emp where job='MANAGER'
8.distinct关键字
例:查询所有员工的岗位名称列表
select distinct job from emp;
select * from emp;
例:显示部门号及岗位的名称列表对应关系
select distinct deptno,job from emp --将这一行的数据重复的去掉
9.between, in,like ,is NULL 关键字
例:查询所有sal在[1600,3000]
select * from emp where sal between 1600 and 3000
select * from emp where sal >=1600 and sal<=3000
sal between low and high;--闭区间
sal >= low and sal <= high
[low, high]
例:查询部门号为10或20的员工
select * from emp where deptno =10 or deptno=20
10.like 关键字
_ 任意一个字符
% 任何多个字符
例:查找名字中第2个字符为A的员工
select * from emp where ename like '_A%' --不常用
例:查找名字中带A的员工
select * from emp where ename like '%A%' --很重要
11.like短语中特殊字符的处理: escape
select * from emp for update
select ename from emp
where ename like '%\%%' escape '\'; --可能笔试有人出
在使用like匹配字符串的时候,如果要匹配 ‘A_B’的时候,需要用到转移字符’\'为’_'进行转义。
在进行匹配的时候使用
SELECT last_name FROM employees WHERE last_name LIKE ‘%A_B%’;
可能得到的结果如下:
last_name
———
AA_B
AA1B
此时使用\转移就可以了。
SELECT last_name FROM employees WHERE last_name LIKE ‘%A\_B%’ ESCAPE ‘\’;
就可以得到想要的
last_name
———
AA_B

例:查询所有comm为null的员工
select t.* ,comm from emp t where comm is null --重要
例:查询谁是公司最大的boss
select * from emp where mgr is null
12.逻辑运算:not,and,or
例:不在部门10,20的员工
select empno,ename, sal ,deptno from emp
where not ( deptno =10 or deptno= 20);
select empno,ename, sal ,deptno from emp
where deptno <> 10 and deptno<> 20;
select empno,ename, sal ,deptno from emp
where deptno not in (10,20)
where A and B or C;
where A and (B or C);
例:比较如下两句差异
select ename, sal, deptno
from emp
where sal > 1000
and deptno = 10
or deptno = 20;
select ename, sal, deptno
from emp
where sal > 1000
and (deptno = 10
or deptno = 20);
13.排序:
例:将员工按姓名升序排序,或降序排序
select * from emp for update
select ename, job from emp order by ename ASC
select ename, job from emp order by ename ASC,job DESC;
ASC --升序 acend ,默认
DESC --倒序 descend
select ename, sal from emp
例:部门号升序,sal降序排
select ename, sal, deptno
from emp order by deptno, sal desc;
14.单行函数
虚表dual(Oracle特有的)
select upper('Sql Course') from dual;
select lower(ename), t.* from emp t;
select * from emp where lower(ename) like '%a%'
15.字符函数
upper / lower / initcap / substr / length / lpad / rpad / replace / trim
select lpad((to_date('2017-01-01','yyyy-mm-dd')-to_date('2016-01-02','yyyy-mm-dd'))/365,1,' ' )from dual--左填充
select trim(' hello') from dual
子串:SUBSTR('String',1,3) => Str
select SUBSTR('String',1,3) from dual
长度:LENGTH('String') => 6
16.数字函数 (了解)
round / trunc / mod
select round(45.678,2) from dual; --四舍五入
select trunc(45.678, 1) from dual; --截取
select trunc(45.678, 0) from dual;
select trunc(45.678, -1) from dual;
select mod(16,5) from dual; --求余
17.日期
取系统时间: sysdate(Oracle独有的函数)
select sysdate from dual;
日期 +/- n = n天以后/前
--to_char(日期,格式化字符)是转换函数,将日期转换为字符类型
select sysdate, to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate, 'yyyy"年"mm"月"dd"日"') from dual;
select (to_date('2017-01-01','yyyy-mm-dd')-to_date('2016-01-02','yyyy-mm-dd'))from dual;--两个date类型的日期相差多少天
select months_between(to_date('2017-01-01','yyyy,mm,dd'),to_date('2016-01-01','yyyy,mm,dd')) from dual;--两个date类型之间相差几个月
yyyy:用数字表达的四位年(2009年)
mm:用数字表达的二位月(01月)
dd:用数字表达的二位日(01日)
hh24:用数字表达的24进制的小时(20点)
h12:用数字表达的12进制的小时(8点)
mi:用数字表达的分钟(30分)
ss:用数字表达的小时(30秒)
d:用数字表达的一周内的第几天(周日:1)
day:用全拼表达的星期几(sunday)
month:用全拼表达的月(march)
select sysdate from dual;
默认的日期格式是: DD-Mon-RR
假设现在是2011年:
RR YY(直接取当前日期的前两位)
05 2005 2005
99 1999 2099 <--
假设现在是1998年:
RR YY
05 2005 1905 <--
95 1995 1995
TO_NUMBER将字符串转换成数值
TO_CHAR将数值或日期转化成字符串
转换函数中包含格式说明
TO_NUMBER TO_DATE
数值<------------ 字符 ------------> 日期
-------------> <------------
TO_CHAR TO_CHAR

select to_char(sysdate, 'yyyy/mm/dd'),
to_char(123456789,'$999,999,999.99') from dual

格式说明
9 代表数字位
0 定义宽度大于实际值宽度时,0会被强制显示在前面,以补齐位数
$ 美元的货币符号
L 本地货币符号
. 小数点
, 每千位显示一个逗号
SELECT TO_NUMBER(to_char(sysdate, 'yyyy'))*10 FROM dual;
select * from emp where hiredate = TO_DATE('1981/5/1','yyyy-mm-dd')
select TO_DATE('1981/5/1','yyyy/mm/dd') from dual
18.组函数 (聚合函数)
AVG(DISTINCT|ALL|n) 平均数
SUM(DISTINCT|ALL|n) 求和
COUNT(DISTINCT|ALL|expr|*) 记数
MAX(DISTINCT|ALL|expr) 最大值
MIN(DISTINCT|ALL|expr) 最小值
select avg(comm),avg( nvl(comm,0) ) from emp; --null的记录不参与计算
select sum(comm),sum(nvl(comm,0) ) from emp
select count(*) from emp
select count(distinct ename) from emp;
select count(distinct job) from emp
例如:部门人数
select * from emp;
select count(empno) from emp where deptno=10
例如:收入最多的人
select * from emp;
select max(empno),max(sal) from emp; --这两列没关系,不可行
--注意,一旦使用组函数后,select 中的字段,必须放在组函数中,或 group by中
--正解 ?
select * from emp group by empno
19.group by 语句 --重点
例:求每一个部门的人数
select * from emp order by deptno
select deptno,count(empno) from emp group by deptno
例:求每一个部门的收入大户是谁?(收入是多少,不能知道人?)
select deptno,max(sal) from emp group by deptno




一.介绍
1.Oracle数据库: DB / DBMS
2.数据库的使用者: DBA(Database Administrator) / 开发人员
3.数据库的种类:
oracle / sybase / db2(IBM)
sql server / mysql (开源,现已被oracle收购) /
access,postgreSQL
4.数据表:行和列组成.
行: row / record 记录
列: column / field 字段
5.SQL分类: Structured Query Language
DQL: select
DML: insert / update / delete
DDL: create / drop / alter / truncate
DCL: grant / revoke
TCL: commit / rollback / savepoint
6.了解练习常用到的数据表:
emp: 职员表
empno 员工号
ename 员工姓名
job 工作
mgr 上级编号
hiredate 受雇日期
sal 薪金
comm 佣金
deptno 部门编号
dept: 部门表
salgrade: 薪水等级表
数据类型:
数字类型:number(p,s)最长p位,小数点后s位
字符类型:char(n) / varchar2(n) 推荐
varchar
日期:date

number(7,2) 99999.99
number(5) 99999
number 随便
二.SQL
-- *号表示查询全部列
select * from dept;
select * from emp;
1.列的选取(或称为投影),列名(字段名,field)
select empno,ename,deptno from emp;
2.列别名
select empno 员工编号,ename 员工姓名 from emp;
select empno as 员工编号,ename as 员工姓名 from emp;
3.列的算术表达式(+ ,-, *, /)
例:显示年收入
select empno,ename,sal,sal*12 年收入 from emp;
4.处理空值的函数nvl() -- null value,要考虑字段类型
例:显示所有人月总收入
select * from emp;
select empno,ename,sal,comm,sal+nvl(comm,0) from emp;
select ename,sal,comm,sal+comm from emp
5.连接字符串||,例
select empno,ename,empno||ename from emp
6.技巧:
select t.ename,t.sal,t.empno||ename from emp t;--取别名
select * from emp for update -- oracle特有
select rowid,t.* from emp t -- oracle特有
7.对行进行选取
例:选取部门号为10的所有员工
select * from emp where deptno=10
例:查询所有岗位是manager的员工
select * from emp where job='MANAGER'
8.distinct关键字
例:查询所有员工的岗位名称列表
select distinct job from emp;
select * from emp;
例:显示部门号及岗位的名称列表对应关系
select distinct deptno,job from emp --将这一行的数据重复的去掉
9.between, in,like ,is NULL 关键字
例:查询所有sal在[1600,3000]
select * from emp where sal between 1600 and 3000
select * from emp where sal >=1600 and sal<=3000
sal between low and high;--闭区间
sal >= low and sal <= high
[low, high]
例:查询部门号为10或20的员工
select * from emp where deptno =10 or deptno=20
10.like 关键字
_ 任意一个字符
% 任何多个字符
例:查找名字中第2个字符为A的员工
select * from emp where ename like '_A%' --不常用
例:查找名字中带A的员工
select * from emp where ename like '%A%' --很重要
11.like短语中特殊字符的处理: escape
select * from emp for update
select ename from emp
where ename like '%\%%' escape '\'; --可能笔试有人出
在使用like匹配字符串的时候,如果要匹配 ‘A_B’的时候,需要用到转移字符’\'为’_'进行转义。
在进行匹配的时候使用
SELECT last_name FROM employees WHERE last_name LIKE ‘%A_B%’;
可能得到的结果如下:
last_name
———
AA_B
AA1B
此时使用\转移就可以了。
SELECT last_name FROM employees WHERE last_name LIKE ‘%A\_B%’ ESCAPE ‘\’;
就可以得到想要的
last_name
———
AA_B

例:查询所有comm为null的员工
select t.* ,comm from emp t where comm is null --重要
例:查询谁是公司最大的boss
select * from emp where mgr is null
12.逻辑运算:not,and,or
例:不在部门10,20的员工
select empno,ename, sal ,deptno from emp
where not ( deptno =10 or deptno= 20);
select empno,ename, sal ,deptno from emp
where deptno <> 10 and deptno<> 20;
select empno,ename, sal ,deptno from emp
where deptno not in (10,20)
where A and B or C;
where A and (B or C);
例:比较如下两句差异
select ename, sal, deptno
from emp
where sal > 1000
and deptno = 10
or deptno = 20;
select ename, sal, deptno
from emp
where sal > 1000
and (deptno = 10
or deptno = 20);
13.排序:
例:将员工按姓名升序排序,或降序排序
select * from emp for update
select ename, job from emp order by ename ASC
select ename, job from emp order by ename ASC,job DESC;
ASC --升序 acend ,默认
DESC --倒序 descend
select ename, sal from emp
例:部门号升序,sal降序排
select ename, sal, deptno
from emp order by deptno, sal desc;
14.单行函数
虚表dual(Oracle特有的)
select upper('Sql Course') from dual;
select lower(ename), t.* from emp t;
select * from emp where lower(ename) like '%a%'
15.字符函数
upper / lower / initcap / substr / length / lpad / rpad / replace / trim
select lpad((to_date('2017-01-01','yyyy-mm-dd')-to_date('2016-01-02','yyyy-mm-dd'))/365,1,' ' )from dual--左填充
select trim(' hello') from dual
子串:SUBSTR('String',1,3) => Str
select SUBSTR('String',1,3) from dual
长度:LENGTH('String') => 6
16.数字函数 (了解)
round / trunc / mod
select round(45.678,2) from dual; --四舍五入
select trunc(45.678, 1) from dual; --截取
select trunc(45.678, 0) from dual;
select trunc(45.678, -1) from dual;
select mod(16,5) from dual; --求余
17.日期
取系统时间: sysdate(Oracle独有的函数)
select sysdate from dual;
日期 +/- n = n天以后/前
--to_char(日期,格式化字符)是转换函数,将日期转换为字符类型
select sysdate, to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate, 'yyyy"年"mm"月"dd"日"') from dual;
select (to_date('2017-01-01','yyyy-mm-dd')-to_date('2016-01-02','yyyy-mm-dd'))from dual;--两个date类型的日期相差多少天
select months_between(to_date('2017-01-01','yyyy,mm,dd'),to_date('2016-01-01','yyyy,mm,dd')) from dual;--两个date类型之间相差几个月
yyyy:用数字表达的四位年(2009年)
mm:用数字表达的二位月(01月)
dd:用数字表达的二位日(01日)
hh24:用数字表达的24进制的小时(20点)
h12:用数字表达的12进制的小时(8点)
mi:用数字表达的分钟(30分)
ss:用数字表达的小时(30秒)
d:用数字表达的一周内的第几天(周日:1)
day:用全拼表达的星期几(sunday)
month:用全拼表达的月(march)
select sysdate from dual;
默认的日期格式是: DD-Mon-RR
假设现在是2011年:
RR YY(直接取当前日期的前两位)
05 2005 2005
99 1999 2099 <--
假设现在是1998年:
RR YY
05 2005 1905 <--
95 1995 1995
TO_NUMBER将字符串转换成数值
TO_CHAR将数值或日期转化成字符串
转换函数中包含格式说明
TO_NUMBER TO_DATE
数值<------------ 字符 ------------> 日期
-------------> <------------
TO_CHAR TO_CHAR

select to_char(sysdate, 'yyyy/mm/dd'),
to_char(123456789,'$999,999,999.99') from dual

格式说明
9 代表数字位
0 定义宽度大于实际值宽度时,0会被强制显示在前面,以补齐位数
$ 美元的货币符号
L 本地货币符号
. 小数点
, 每千位显示一个逗号
SELECT TO_NUMBER(to_char(sysdate, 'yyyy'))*10 FROM dual;
select * from emp where hiredate = TO_DATE('1981/5/1','yyyy-mm-dd')
select TO_DATE('1981/5/1','yyyy/mm/dd') from dual
18.组函数 (聚合函数)
AVG(DISTINCT|ALL|n) 平均数
SUM(DISTINCT|ALL|n) 求和
COUNT(DISTINCT|ALL|expr|*) 记数
MAX(DISTINCT|ALL|expr) 最大值
MIN(DISTINCT|ALL|expr) 最小值
select avg(comm),avg( nvl(comm,0) ) from emp; --null的记录不参与计算
select sum(comm),sum(nvl(comm,0) ) from emp
select count(*) from emp
select count(distinct ename) from emp;
select count(distinct job) from emp
例如:部门人数
select * from emp;
select count(empno) from emp where deptno=10
例如:收入最多的人
select * from emp;
select max(empno),max(sal) from emp; --这两列没关系,不可行
--注意,一旦使用组函数后,select 中的字段,必须放在组函数中,或 group by中
--正解 ?
select * from emp group by empno
19.group by 语句 --重点
例:求每一个部门的人数
select * from emp order by deptno
select deptno,count(empno) from emp group by deptno
例:求每一个部门的收入大户是谁?(收入是多少,不能知道人?)
select deptno,max(sal) from emp group by deptno




一.介绍
1.Oracle数据库: DB / DBMS
2.数据库的使用者: DBA(Database Administrator) / 开发人员
3.数据库的种类:
oracle / sybase / db2(IBM)
sql server / mysql (开源,现已被oracle收购) /
access,postgreSQL
4.数据表:行和列组成.
行: row / record 记录
列: column / field 字段
5.SQL分类: Structured Query Language
DQL: select
DML: insert / update / delete
DDL: create / drop / alter / truncate
DCL: grant / revoke
TCL: commit / rollback / savepoint
6.了解练习常用到的数据表:
emp: 职员表
empno 员工号
ename 员工姓名
job 工作
mgr 上级编号
hiredate 受雇日期
sal 薪金
comm 佣金
deptno 部门编号
dept: 部门表
salgrade: 薪水等级表
数据类型:
数字类型:number(p,s)最长p位,小数点后s位
字符类型:char(n) / varchar2(n) 推荐
varchar
日期:date

number(7,2) 99999.99
number(5) 99999
number 随便
二.SQL
-- *号表示查询全部列
select * from dept;
select * from emp;
1.列的选取(或称为投影),列名(字段名,field)
select empno,ename,deptno from emp;
2.列别名
select empno 员工编号,ename 员工姓名 from emp;
select empno as 员工编号,ename as 员工姓名 from emp;
3.列的算术表达式(+ ,-, *, /)
例:显示年收入
select empno,ename,sal,sal*12 年收入 from emp;
4.处理空值的函数nvl() -- null value,要考虑字段类型
例:显示所有人月总收入
select * from emp;
select empno,ename,sal,comm,sal+nvl(comm,0) from emp;
select ename,sal,comm,sal+comm from emp
5.连接字符串||,例
select empno,ename,empno||ename from emp
6.技巧:
select t.ename,t.sal,t.empno||ename from emp t;--取别名
select * from emp for update -- oracle特有
select rowid,t.* from emp t -- oracle特有
7.对行进行选取
例:选取部门号为10的所有员工
select * from emp where deptno=10
例:查询所有岗位是manager的员工
select * from emp where job='MANAGER'
8.distinct关键字
例:查询所有员工的岗位名称列表
select distinct job from emp;
select * from emp;
例:显示部门号及岗位的名称列表对应关系
select distinct deptno,job from emp --将这一行的数据重复的去掉
9.between, in,like ,is NULL 关键字
例:查询所有sal在[1600,3000]
select * from emp where sal between 1600 and 3000
select * from emp where sal >=1600 and sal<=3000
sal between low and high;--闭区间
sal >= low and sal <= high
[low, high]
例:查询部门号为10或20的员工
select * from emp where deptno =10 or deptno=20
10.like 关键字
_ 任意一个字符
% 任何多个字符
例:查找名字中第2个字符为A的员工
select * from emp where ename like '_A%' --不常用
例:查找名字中带A的员工
select * from emp where ename like '%A%' --很重要
11.like短语中特殊字符的处理: escape
select * from emp for update
select ename from emp
where ename like '%\%%' escape '\'; --可能笔试有人出
在使用like匹配字符串的时候,如果要匹配 ‘A_B’的时候,需要用到转移字符’\'为’_'进行转义。
在进行匹配的时候使用
SELECT last_name FROM employees WHERE last_name LIKE ‘%A_B%’;
可能得到的结果如下:
last_name
———
AA_B
AA1B
此时使用\转移就可以了。
SELECT last_name FROM employees WHERE last_name LIKE ‘%A\_B%’ ESCAPE ‘\’;
就可以得到想要的
last_name
———
AA_B

例:查询所有comm为null的员工
select t.* ,comm from emp t where comm is null --重要
例:查询谁是公司最大的boss
select * from emp where mgr is null
12.逻辑运算:not,and,or
例:不在部门10,20的员工
select empno,ename, sal ,deptno from emp
where not ( deptno =10 or deptno= 20);
select empno,ename, sal ,deptno from emp
where deptno <> 10 and deptno<> 20;
select empno,ename, sal ,deptno from emp
where deptno not in (10,20)
where A and B or C;
where A and (B or C);
例:比较如下两句差异
select ename, sal, deptno
from emp
where sal > 1000
and deptno = 10
or deptno = 20;
select ename, sal, deptno
from emp
where sal > 1000
and (deptno = 10
or deptno = 20);
13.排序:
例:将员工按姓名升序排序,或降序排序
select * from emp for update
select ename, job from emp order by ename ASC
select ename, job from emp order by ename ASC,job DESC;
ASC --升序 acend ,默认
DESC --倒序 descend
select ename, sal from emp
例:部门号升序,sal降序排
select ename, sal, deptno
from emp order by deptno, sal desc;
14.单行函数
虚表dual(Oracle特有的)
select upper('Sql Course') from dual;
select lower(ename), t.* from emp t;
select * from emp where lower(ename) like '%a%'
15.字符函数
upper / lower / initcap / substr / length / lpad / rpad / replace / trim
select lpad((to_date('2017-01-01','yyyy-mm-dd')-to_date('2016-01-02','yyyy-mm-dd'))/365,1,' ' )from dual--左填充
select trim(' hello') from dual
子串:SUBSTR('String',1,3) => Str
select SUBSTR('String',1,3) from dual
长度:LENGTH('String') => 6
16.数字函数 (了解)
round / trunc / mod
select round(45.678,2) from dual; --四舍五入
select trunc(45.678, 1) from dual; --截取
select trunc(45.678, 0) from dual;
select trunc(45.678, -1) from dual;
select mod(16,5) from dual; --求余
17.日期
取系统时间: sysdate(Oracle独有的函数)
select sysdate from dual;
日期 +/- n = n天以后/前
--to_char(日期,格式化字符)是转换函数,将日期转换为字符类型
select sysdate, to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate, 'yyyy"年"mm"月"dd"日"') from dual;
select (to_date('2017-01-01','yyyy-mm-dd')-to_date('2016-01-02','yyyy-mm-dd'))from dual;--两个date类型的日期相差多少天
select months_between(to_date('2017-01-01','yyyy,mm,dd'),to_date('2016-01-01','yyyy,mm,dd')) from dual;--两个date类型之间相差几个月
yyyy:用数字表达的四位年(2009年)
mm:用数字表达的二位月(01月)
dd:用数字表达的二位日(01日)
hh24:用数字表达的24进制的小时(20点)
h12:用数字表达的12进制的小时(8点)
mi:用数字表达的分钟(30分)
ss:用数字表达的小时(30秒)
d:用数字表达的一周内的第几天(周日:1)
day:用全拼表达的星期几(sunday)
month:用全拼表达的月(march)
select sysdate from dual;
默认的日期格式是: DD-Mon-RR
假设现在是2011年:
RR YY(直接取当前日期的前两位)
05 2005 2005
99 1999 2099 <--
假设现在是1998年:
RR YY
05 2005 1905 <--
95 1995 1995
TO_NUMBER将字符串转换成数值
TO_CHAR将数值或日期转化成字符串
转换函数中包含格式说明
TO_NUMBER TO_DATE
数值<------------ 字符 ------------> 日期
-------------> <------------
TO_CHAR TO_CHAR

select to_char(sysdate, 'yyyy/mm/dd'),
to_char(123456789,'$999,999,999.99') from dual

格式说明
9 代表数字位
0 定义宽度大于实际值宽度时,0会被强制显示在前面,以补齐位数
$ 美元的货币符号
L 本地货币符号
. 小数点
, 每千位显示一个逗号
SELECT TO_NUMBER(to_char(sysdate, 'yyyy'))*10 FROM dual;
select * from emp where hiredate = TO_DATE('1981/5/1','yyyy-mm-dd')
select TO_DATE('1981/5/1','yyyy/mm/dd') from dual
18.组函数 (聚合函数)
AVG(DISTINCT|ALL|n) 平均数
SUM(DISTINCT|ALL|n) 求和
COUNT(DISTINCT|ALL|expr|*) 记数
MAX(DISTINCT|ALL|expr) 最大值
MIN(DISTINCT|ALL|expr) 最小值
select avg(comm),avg( nvl(comm,0) ) from emp; --null的记录不参与计算
select sum(comm),sum(nvl(comm,0) ) from emp
select count(*) from emp
select count(distinct ename) from emp;
select count(distinct job) from emp
例如:部门人数
select * from emp;
select count(empno) from emp where deptno=10
例如:收入最多的人
select * from emp;
select max(empno),max(sal) from emp; --这两列没关系,不可行
--注意,一旦使用组函数后,select 中的字段,必须放在组函数中,或 group by中
--正解 ?
select * from emp group by empno
19.group by 语句 --重点
例:求每一个部门的人数
select * from emp order by deptno
select deptno,count(empno) from emp group by deptno
例:求每一个部门的收入大户是谁?(收入是多少,不能知道人?)
select deptno,max(sal) from emp group by deptno





0 0