1.常用命令

1.使用password命令,为scott用户名修改新密码,以字母开头,但提倡使用tiger

password
旧口令:tiger
新口令:abc123
再次输入新口令:abc123

2.查看当前用户

show user;

3.查询scott用户下的所有对象,使用tab表,tab表每个用户都有

select * from tab;

4.查询EMP的表结构

desc EMP;

5.设置显示的列宽(字符型varchar2、日期型date),10个宽度位,a表示字符型,大小写均可

column ename format a12;
column hiredate format a10;设置显示的列宽(数值型number),9表示数字型,一个9表示一个数字位,四个9表示四个数字位,只能用9
column empno format 9999;
column mgr format 9999;
column sal format 9999;
column comm format 9999;
column deptno format 9999;

6.设置一页显示80个条记录的高度

set pagesize 80;

7.使用/杠,执行最近一次的SQL语句

/

8.清屏,属于SQL*PLUS工具中的命令

host cls;

9.查询emp表的结构

desc emp;

10.select 的使用

查询emp表的所有内容,*号表示通配符,表示该表中的所有字段,但*号不能和具体字段一起使用
select * from emp;
或
select empno,ename,sal,deptno from emp;查询emp表的员工编号,姓名,工资,部门号,列名,大小写不敏感,但提倡大写
select empno "编号",ename "姓名",sal "工资",deptNO "部门号" FROM Emp;查询emp表的不重复的工作
select distinct job from emp;查询员工的编号,姓名,月薪,年薪(月薪*12)
select empno,ename,sal,sal*12 "年薪" from emp;查询员工的编号,姓名,入职时间,月薪,年薪,年收入(年薪+奖金)
select empno "编号",ename"姓名",hiredate "入职时间",sal "月薪",sal*12 "年薪",sal*12+comm "年收入" from emp;
如果结果为null,在sqlplus客户端工具中,是不显示null这个值的解决null的问题,使用NVL()函数,NVL(a,b):如果a是NULL,用b替代;如果a是非NULL,就不用b替代,直接返回a的值\
select NVL(null,10) from emp;结果有14行记录
select NVL(null,10) from dual;结果有1行记录
select empno "编号",ename"姓名",hiredate "入职时间",sal "月薪",sal*12 "年薪",sal*12+NVL(comm,0) "年收入"
from emp;
注意:null与具体数字运算时,结果为null使用列别名,查询员工的编号,姓名,月薪,年薪,年收入(年薪+奖金),AS大小写都可且可以省略AS,别名用双引号
select empno AS "编号",ename as "姓名",sal "月薪"
from emp;
或
select empno AS 编号,ename as 姓名,sal 月薪
from emp;
区别
select empno AS "编号",ename as 姓名,sal "月    薪"
from emp;
不加双引号的别名不能有空格;加了双引号的别名可以有空格
要加只能加双引号,不能加单引号,因为在oracle中单引号表示字符串类型或者是日期类型
列名不能使用单引号,因为oracle认为单引号是字符串型或日期型使用dual哑表或者伪表,使用字符串连接符号||,输出"hello world",在oracle中from是必须写的
select 'hello' || ' world' "结果" from dual;使用字符串连接符号||,显示如下格式信息:****的薪水是****美元
select ename || '的薪水是' || sal || '美元'
from emp; 

11.oracle 日期

使用sysdate,显示系统当前时间,在默认情况下,oracle只显示日期,而不显示时间,格式:26-4月-15

select sysdate from dual;

Mysql 显示日期:

select now();

12.where

查询emp表中20号部门的员工信息
select * from emp where deptno = 20;查询姓名是SMITH的员工,字符串使用'',内容大小写敏感
select * from emp where ename = 'SMITH';
总结:你所学过的技术中,哪些是大小写敏感,哪些是大小写不敏感查询1980年12月17日入职的员工,注意oracle默认日期格式(DD-MON-RR表示2位的年份)
select * from emp where hiredate = '17-12月-80';查询工资大于1500的员工
select * from emp where sal > 1500;查询工资不等于1500的员工【!=或<>】
select * from emp where sal <> 1500;查询薪水在1300到1600之间的员工,包括1300和1600
select * from emp where (sal>=1300) and (sal<=1600);
或
select * from emp where sal between 1300 and 1600;查询薪水不在1300到1600之间的员工,不包括1300和1600
select * from emp where sal NOT between 1300 and 1600;查询入职时间在"1981-2月-20"到"1982-1月-23"之间的员工
select * from emp where hiredate between '20-2月-81' and '23-1月-82';
注意:
1)对于数值型,小数值在前,大数值在后
2)对于日期型,年长值在前,年小值在后查询20号或30号部门的员工,例如:根据ID号,选中的员工,批量删除
select * from emp where (deptno=20) or (deptno=30);
或
select * from emp where deptno in (30,20);查询不是20号或30号部门的员工
select * from emp where deptno NOT in (30,20);查询姓名以大写字母S开头的员工,使用%表示0个,1个或多个字符
select * from emp where ename like 'S';
等价
select * from emp where ename = 'S';
select * from emp where ename like 'S%';注意:
凡是精确查询用=符号
凡是不精确查询用like符号,我们通常叫模糊查询查询姓名以大写字母N结束的员工
select * from emp where ename like '%N';查询姓名第一个字母是T,最后一个字母是R的员工
select * from emp where ename like 'T%R';查询姓名是4个字符的员工,且第二个字符是I,使用_只能表示1个字符,不能表示0个或多个字符
select * from emp where ename like '_I__';插入一条姓名为'T_IM'的员工,薪水1200
insert into emp(empno,ename) values(1111,'T_IM');查询员工姓名中含有'_'的员工,使用\转义符,让其后的字符回归本来意思【like '%\_%' escape '\'】
select * from emp where ename like '%\_%' escape '\';

13.insert

插入一个姓名叫'的员工
insert into emp(empno,ename) values(2222,'''');插入一个姓名叫''的员工
insert into emp(empno,ename) values(2222,'''''');查询所有员工信息,使用%或%%
select * from emp;
select * from emp where ename like '%';
select * from emp where ename like '%_%';查询佣金为null的员工
select * from emp where comm is null;
注意:null不能参数=运算null能参数number/date/varchar2类型运算查询佣金为非null的员工
select * from emp where comm is not null;查询无佣金且工资大于1500的员工
select *
from emp
where (comm is null) and (sal>1500); 查询工资是1500或3000或5000的员工
select *
from emp
where sal in (4000,10000,1500,3,300,3000,5000);查询职位是"MANAGER"或职位不是"ANALYST"的员工(方式一,使用!=或<>)
select *
from emp
where (job='MANAGER') or (job<>'ANALYST');查询职位是"MANAGER"或职位不是"ANALYST"的员工(方式二,使用not)
select *
from emp
where (job='MANAGER') or (not(job='ANALYST'));

14.order by

查询员工信息(编号,姓名,月薪,年薪),按月薪升序排序,默认升序,如果月薪相同,按oracle内置的校验规则排序
select empno,ename,sal,sal*12
from emp
order by sal asc; 查询员工信息(编号,姓名,月薪,年薪),按月薪降序排序
select empno,ename,sal,sal*12
from emp
order by sal desc; 查询员工信息,按入职日期降序排序,使用列名
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by hiredate desc;order by后面可以跟列名、别名、表达式、列号(从1开始,在select子句中的列号)
列名:
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by hiredate desc;别名:
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by "年薪" desc;表达式:
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by sal*12 desc;列号,从1开始:
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by 5 desc;查询员工信息,按佣金升序或降序排列,null值看成最大值
select * from emp order by comm desc;查询员工信息,对有佣金的员工,按佣金降序排列,当order by 和 where 同时出现时,order by 在最后
select *
from emp
where comm is not null
order by comm desc;查询员工信息,按工资降序排列,相同工资的员工再按入职时间降序排列
select *
from emp
order by sal desc,hiredate desc;select *
from emp
order by sal desc,hiredate asc;
注意:只有当sal相同的情况下,hiredate排序才有作用查询20号部门,且工资大于1500,按入职时间降序排列
select *
from emp
where (deptno=20) and (sal>1500)
order by hiredate desc;select * from emp where deptno in (10,20,30,50,'a');

15.单行函数

单行函数:只有一个参数输入,只有一个结果输出
多行函数或分组函数:可有多个参数输入,只有一个结果输出     测试lower/upper/initcap函数,使用dual哑表
select lower('www.BAIdu.COM') from dual;
select upper('www.BAIdu.COM') from dual;
select initcap('www.BAIdu.COM') from dual;测试concat/substr函数,从1开始,表示字符,不论中英文
select concat('hello','你好') from dual;正确
select concat('hello','你好','世界') from dual;错误
select 'hello' || '你好' || '世界' from dual;正确
select concat('hello',concat('你好','世界')) from dual;正确
select substr('hello你好',5,3) from dual;
5表示从第几个字符开始算,第一个字符为1,中英文统一处理
3表示连续取几个字符测试length/lengthb函数,编码方式为UTF8/GBK(赵君),一个中文占3/2个字节长度,一个英文一个字节
select length('hello你好') from dual;
select lengthb('hello你好') from dual; 测试instr/lpad/rpad函数,从左向右找第一次出现的位置,从1开始
select instr('helloworld','o') from dual;
注意:找不到返回0大小写敏感
select LPAD('hello',10,'#') from dual;
select RPAD('hello',10,'#') from dual;测试trim/replace函数
select trim(' ' from '  he  ll                ') from dual;
select replace('hello','l','L') from dual;测试round/trunc/mod函数作用于数值型
select round(3.1415,3) from dual;
select trunc(3.1415,3) from dual;
select mod(10,3) from dual;当前日期:sysdate = 26-4月-15测试round作用于日期型(month)
select round(sysdate,'month') from dual;测试round作用于日期型(year)
select round(sysdate,'year') from dual;测试trunc作用于日期型(month)
select trunc(sysdate,'month') from dual;测试trunc作用于日期型(year)
select trunc(sysdate,'year') from dual;显示昨天,今天,明天的日期,日期类型 +- 数值 = 日期类型
select sysdate-1 "昨天",sysdate "今天",sysdate+1 "明天" from dual;以年和月形式显示员工近似工龄,日期-日期=数值,假设:一年以365天计算,一月以30天计算
select ename "姓名",round(sysdate-hiredate,0)/365 "天数" from emp;使用months_between函数,精确计算到年底还有多少个月
select months_between('31-12月-15',sysdate) from dual;使用months_between函数,以精确月形式显示员工工龄
select ename "姓名",months_between(sysdate,hiredate) "精确月工龄" from emp;测试add_months函数,下个月今天是多少号
select add_months(sysdate,1) from dual;测试add_months函数,上个月今天是多少号
select add_months(sysdate,-1) from dual;测试next_day函数,从今天开始算,下一个星期三是多少号【中文平台】
select next_day(sysdate,'星期三') from dual;测试next_day函数,从今天开始算,下下一个星期三是多少号【中文平台】
select next_day(next_day(sysdate,'星期三'),'星期三') from dual;测试next_day函数,从今天开始算,下一个星期三的下一个星期日是多少号【中文平台】
select next_day(next_day(sysdate,'星期三'),'星期日') from dual;测试last_day函数,本月最后一天是多少号
select last_day(sysdate) from dual;测试last_day函数,本月倒数第二天是多少号
select last_day(sysdate)-1 from dual;测试last_day函数,下一个月最后一天是多少号
select last_day(add_months(sysdate,1)) from dual;测试last_day函数,上一个月最后一天是多少号
select last_day(add_months(sysdate,-1)) from dual;

注意:
1)日期-日期=天数
2)日期+-天数=日期

16.三大类型转换

oracle中三大类型与隐式数据类型转换
(1)varchar2变长/char定长-->number,例如:'123'->123
(2)varchar2/char-->date,例如:'25-4月-15'->'25-4月-15'
(3)number---->varchar2/char,例如:123->'123'
(4)date------>varchar2/char,例如:'25-4月-15'->'25-4月-15'oracle如何隐式转换:
1)=号二边的类型是否相同
2)如果=号二边的类型不同,尝试的去做转换
3)在转换时,要确保合法合理,否则转换会失败,例如:12月不会有32天,一年中不会有13月查询1980年12月17日入职的员工(方式一:日期隐示式转换)
select * from emp where hiredate = '17-12月-80';使用to_char(日期,'格"常量"式')函数将日期转成字符串,显示如下格式:2015 年 04 月 25 日 星期六
select to_char(sysdate,'yyyy" 年 "mm" 月 "dd" 日 "day') from dual;使用to_char(日期,'格式')函数将日期转成字符串,显示如格式:2015-04-25今天是星期六 15:15:15
select to_char(sysdate,'yyyy-mm-dd"今天是"day hh24:mi:ss') from dual;
或
select to_char(sysdate,'yyyy-mm-dd"今天是"day HH12:MI:SS AM') from dual;使用to_char(数值,'格式')函数将数值转成字符串,显示如下格式:$1,234
select to_char(1234,'$9,999') from dual;使用to_char(数值,'格式')函数将数值转成字符串,显示如下格式:¥1,234select to_char(1234,'$9,999') from dual;
select to_char(1234,'L9,999') from dual;使用to_date('字符串','格式')函数,查询1980年12月17日入职的员工(方式二:日期显式转换)
select * from emp where hiredate = to_date('1980年12月17日','yyyy"年"mm"月"dd"日"');
或
select * from emp where hiredate = to_date('1980#12#17','yyyy"#"mm"#"dd');
或
select * from emp where hiredate = to_date('1980-12-17','yyyy-mm-dd');使用to_number('字符串')函数将字符串‘123’转成数字123
select to_number('123') from dual;注意:
select '123' + 123 from dual;246
select '123' || 123 from dual;123123-----------------------------------使用NVL(a,b)通用函数,统计员工年收入,NVL()作用于任何类型,即(number/varchar2/date)
通用函数:参数类型可以是number或varchar2或date类型使用NVL2(a,b,c)通用函数,如果a不为NULL,取b值,否则取c值,统计员工年收入 使用NULLIF(a,b)通用函数,在类型一致的情况下,如果a与b相同,返回NULL,否则返回a,比较10和10.0是否相同使用SQL99标准通用语法中的case表达式,将职位是分析员的,工资+1000;职位是经理的,工资+800;职位是其它的,工资+400
case 字段 when 条件 then 表达式1when 条件 then 表达式2else 表达式n
end 使用oracle专用语法中的decode()函数,职位是分析员的,工资+1000;职位是经理的,工资+800;职位是其它的,工资+400
decode(字段,条件1,表达式1,条件2,表达式2,...表达式n)单引号出现的地方如下:
1)字符串,例如:'hello'
2)日期型,例如:'17-12月-80'
3)to_char/to_date(日期,'YYYY-MM-DD HH24:MI:SS')双引号出现的地方如下:
1)列别名,例如:select ename "姓 名" from emp
2)to_char/to_date(日期,'YYYY"年"MM"月"DD"日" HH24:MI:SS')

17.多行函数

函数:oracle服务器先事写好的一段具有一定功能的程序片段,内置于oracle服务器,供用户调用
单行函数:输入一个参数,输出一个结果,例如:upper('baidu.com')->BAIDU.COM
多行函数:输入多个参数,或者是内部扫描多次,输出一个结果,例如:count(*)->14统计emp表中员工总人数
select count(*) from emp;
*号适用于表字段较少的情况下,如果字段较多,扫描多间多,效率低,项目中提倡使用某一个非null唯一的字段,通常是主键 统计公司有多少个不重复的部门
select count(distinct deptno) from emp;统计有佣金的员工人数
select count(comm) from emp;
注意:今天讲的这些多个行函数,不统计NULL值员工总工资,平均工资,四舍五入,保留小数点后0位
select sum(sal) "总工资",round(avg(sal),0) "平均工资"
from emp;查询员工表中最高工资,最低工资
select max(sal) "最高工资",min(sal) "最低工资"
from emp;入职最早,入职最晚员工
select max(hiredate) "最晚入职时间",min(hiredate) "最早入职时间"
from emp;多行函数:count/sum/avg/max/min按部门求出该部门平均工资,且平均工资取整数,采用截断
select deptno "部门编号",trunc(avg(sal),0) "部门平均工资"
from emp
group by deptno;(继续)查询部门平均工资大于2000元的部门
select deptno "部门编号",trunc(avg(sal),0) "部门平均工资"
from emp
group by deptno
having trunc(avg(sal),0) > 2000; (继续)按部门平均工资降序排列
select deptno "部门编号",trunc(avg(sal),0) "部门平均工资"
from emp
group by deptno
having trunc(avg(sal),0) > 2000
order by 2 desc;除10号部门外,查询部门平均工资大于2000元的部门,方式一【having deptno<>10】
select deptno,avg(sal)
from emp
group by deptno
having deptno<>10;除10号部门外,查询部门平均工资大于2000元的部门,方式二【where deptno<>10】
select deptno,avg(sal)
from emp
where deptno<>10
group by deptno;
提倡显示部门平均工资的最大值
select max(avg(sal)) "部门平均工资的最大值"
from emp
group by deptno;思考:显示部门平均工资的最大值和该部门编号?
select max(avg(sal)) "部门平均工资的最大值",deptno "部门编号"
from emp
group by deptno;
错误group by 子句的细节:
1)在select子句中出现的非多行函数的所有列,【必须】出现在group by子句中
2)在group by子句中出现的所有列,【可出现可不现】在select子句中where和having的区别:
where:
1)行过滤器
2)针对原始的记录
3)跟在from后面
4)where可省
5)先执行having:
1)组过滤器
2)针对分组后的记录
3)跟在group by后面
4)having可省
5)后执行oracle中综合语法:
1)select子句-----必须
2)from子句-------必须,不知写什么表了,就写dual
3)where子句------可选
4)group by子句---可选
5)having子句-----可选
6)order by 子句--可选,如果出现列名,别名,表达式,字段

18.多表查询

员工表emp和部门表dept的笛卡尔集(笛卡尔集表=列数之和,行数之积,笛卡尔集表内中有些数据是不符合要求的)
select emp.ename,dept.dname
from emp,dept;使用等值连接/内连接(只能使用=号),显示员工的编号,姓名,部门名,使用表别名简化
select emp.empno,emp.ename,dept.dname,dept.deptno
from emp,dept
where emp.deptno = dept.deptno;使用非等值连接(不能使用=号,其它符号可以,例如:>=,<=,<>,betwen and等),显示员工的编号,姓名,月薪,工资级别
select e.empno,e.ename,e.sal,s.grade
from emp e,salgrade s
where e.sal between s.losal and s.hisal;内连接查询:只能查询出符合条件的记录
外连接查询:既能查询出符合条件的记录,也能根据一方强行将另一个方查询出来使用外连接,按部门10,20,30,40号,统计各部门员工人数,要求显示部门号,部门名,人数
部门号 部门名        人数
10     ACCOUNTING    3
20     RESEARCH      5
30     SALES         6
40     OPERATIONS    0等值连接/非等值连接/内连接:只会查询出多张表中,根据某个字段匹配,符合条件的记录,不符合条件的记录是不会存在的左外连接[是oracle专用的,不是SQL99规则]:
select dept.deptno "部门号",dept.dname "部门名",count(emp.empno) "人数"
from dept,emp
where dept.deptno = emp.deptno(+)
group by dept.deptno,dept.dname;右外连接:
select dept.deptno "部门号",dept.dname "部门名",count(emp.empno) "人数"
from dept,emp
where emp.deptno(+) = dept.deptno
group by dept.deptno,dept.dname;使用左外连接,按部门10,20,30,40号,统计各部门员工人数,要求显示部门号,部门名,人数,且按人数降序排列
select dept.deptno "部门号",dept.dname "部门名",count(emp.empno) "人数"
from dept,emp
where dept.deptno = emp.deptno(+)
group by dept.deptno,dept.dname
order by 3 desc;使用自连接,显示"SMITH的上级是FORD"这种格式
select users.ename || '的上级是' ||boss.ename
from emp users,emp boss
where users.mgr = boss.empno;
只有13条记录,不含有KING基于上述问题,将KING的上级是“”显示出来
select users.ename || '的上级是' ||boss.ename
from emp users,emp boss
where users.mgr = boss.empno(+);
14条记录
注意:自连接也用到内连接和外连接

19.子查询

子查询的作用:查询条件未知的事物查询条件已知的问题:例如:查询工资为800的员工信息
查询条件未知的问题:例如:查询工资为20号部门平均工资的员工信息
一个条件未知的问题,可以分解为多个条件已知的问题查询工资比WARD高的员工信息
第一:查询WARD的工资?select sal from emp where ename = 'WARD';第二:查询工资比1250高的员工信息?select * from emp where sal > 1250;子查询:select * from emp where sal > (select sal from emp where ename = 'WARD');查询部门名为'SALES'的员工信息(方式一:子查询)第一:查询部门名为'SALES'的编号?select deptno from dept where dname = 'SALES';
第二:查询部门号为30的员工信息? select * from emp where deptno = 30;
子查询:select * from emp where deptno = (select deptno from dept where dname = 'SALES');子查询细节:
1)子查询与父查询可以针对同一张表
2)子查询与父查询可以针对不同张表
3) 子查询与父查询在传统参数时,数量要相同
4) 子查询与父查询在传统参数时,类型要相同
5) 子查询与父查询在传统参数时,含义要相同查询部门名为'SALES'的员工信息(方式二:多表查询)
select emp.*
from dept,emp
where (dept.deptno=emp.deptno) and (dept.dname='SALES'); 查询每个员工编号,姓名,部门名,工资等级(三表查询,这三张表并无外健关联)
select e.empno,e.ename,d.dname,s.grade
from emp e,dept d,salgrade s
where (e.deptno=d.deptno) and (e.sal between s.losal and s.hisal);查询工资最低的员工信息(单行子查询,使用=号)
第一:查询出工资最低是多少?select min(sal) from emp;
第二:查询工资为800的员工信息?select * from emp where sal = 800;
子查询:select * from emp where sal = (select min(sal) from emp);查询部门名为'ACCOUNTING'或'SALES'的员工信息(多行子查询,使用in关键字)
第一:查询部门名为'ACCOUNTING'或'SALES'的部门编号?select deptno from dept where dname in ('ACCOUNTING','SALES');
第二:查询部门号为10或30号的员工信息?select * from emp where deptno in (10,30);
子查询:select * from emp where deptno in (select deptno from dept where dname in ('ACCOUNTING','SALES'));查询工资比20号部门【任意any】一个员工工资【低<】的员工信息(多行子查询,使用any关键字)
第一:查询20号部门的所有工资?select sal from emp where deptno = 20;
第二:查询工资比(800,2975,3000,1100,3000)任意一个低的员工信息?select * from emp where sal < any (800,2975,3000,1100,3000);
在oracle看来,<any就等于<集合中最大的那个值
子查询:select * from emp where sal <any (select sal from emp where deptno = 20); 查询工资比30号部门【所有all】员工【低<】的员工信息(多行子查询,使用all关键字) 第一:查询出30部门所有员工的工资?    select sal from emp where deptno = 30;
第二:查询工资比(1600,1250,1250,2850,1500,950)中所有的工资都低的员工信息?select * from emp where sal <all (1600,1250,1250,2850,1500,950);
子查询:select * from emp where sal <all (select sal from emp where deptno = 30);注意:学员们,不容易理解的几个概念:单行函数:输入一个参数,输出一个结果
多行函数:扫描多个参数,输出一个结果单行子查询:子查询只会返回一个结果,例如:800,父查询用=/<>/>=/<=这些符号来比较
多行子查询:子查询会返回多于一个结果,例如:30,20,父查询用in/any/all这些符号来比较当多表查询,子查询同时能解决问题时,按如下优先方案选择:多表查询-->子查询
注意:上述结果不是说多表查询可以替代子查询,某些情况下,只能用子查询解决,例如:oracle分页

20.集合查询

使用并集运算,查询20号部门或30号部门的员工信息
select * from emp where deptno = 20
union
select * from emp where deptno = 30;
注意:
union:二个集合中,如果都有相同的,取其一
union all:二个集合中,如果都有相同的,都取使用set time/timing on,打开时间的开关
set time on;
set time off;使用set tim/timing off,关闭时间的开关
set timing on;
set timint off;使用交集运算[intersect],查询工资在1000-2000和1500-2500之间的员工信息(方式一)
select * from emp where sal between 1000 and 2000
intersect
select * from emp where sal between 1500 and 2500;用where行过滤,查询工资在1000-2000和1500-2500之间的员工信息(方式二)
select *
from emp
where (sal between 1000 and 2000) and (sal between 1500 and 2500);使用差集运算[minus],查询工资在1000-2000,但不在1500-2500之间的员工信息(方式一)
select * from emp where sal between 1000 and 2000
minus
select * from emp where sal between 1500 and 2500;使用where行过滤,查询工资在1000-2000,但不在1500-2500之间的员工信息(方式二)
select *
from emp
where (sal between 1000 and 2000) and (sal not between 1500 and 2500);集合查询的细节:
1)集合操作时,必须确保集合列数是相等
select empno,ename,sal,comm from emp where deptno = 20
union
select empno,ename,sal from emp where deptno = 30;错2)集合操作时,必须确保集合列类型对应相同
select empno,ename,sal,comm from emp where deptno = 20
union
select empno,ename,sal,hiredate from emp where deptno = 30;错3)A union B union C = C union B union A
select * from emp where deptno = 10
union
select * from emp where deptno = 20
union
select * from emp where deptno = 30;4)当多个集合操作时,结果的列名由第一个集合列名决定
select empno "编号",ename "姓名",sal "薪水" from emp where deptno = 20
union
select empno,ename,sal from emp where deptno = 10;当多表查询,子查询,集合查询都能完成同样任务时,按如下优化方案选择:
多表查询->子查询->集合查询

21.oracle分页

回顾mysql分页
用limit关键字 查询users表中前二条记录
select * from users limit 0,2
或
select * from users limit 2;
0表示第一条记录的索引号,索引号从0开始
2表示最多选取二个记录查询出users前三条记录
select * from users limit 0,3
或
select * from users limit 3查询出users第2条到第4条记录
select * from users limit 1,3; 回顾hibernate分页API
Query.setFirstResult(0);
Query.setMaxResult(3);          什么是rownum,有何特点
1)rownum是oracle专用的关健字
2)rownum与表在一起,表亡它亡,表在它在
3)rownum在默认情况下,从表中是查不出来的
4)只有在select子句中,明确写出rownum才能显示出来
5)rownum是number类型,且唯一连续
6)rownum最小值是1,最大值与你的记录条数相同
7)rownum也能参与关系运算* rownum = 1    有值* rownum < 5    有值   * rownum <=5    有值       * rownum > 2    无值       * rownum >=2    无值* rownum <>2    有值   与  rownum < 2 相同* rownum = 2    无值
8)基于rownum的特性,我们通常rownum只用于<或<=关系运算   显示emp表中3-8条记录(方式一:使用集合减运算)
select rownum "伪列",emp.* from emp where rownum<=8
minus
select rownum,emp.* from emp where rownum<=2;显示emp表中3-8条记录(方式二:使用子查询,在from子句中使用,重点)
select xx.*
from (select rownum ids,emp.* from emp where rownum<=8) xx
where ids>=2;
注意:在子查询中的别名,不可加""引号显示emp表中5-9条记录
select yy.*
from (select rownum ids,emp.* from emp where rownum<=9) yy
where ids>=5;
注意:在项目中,from后台可能有真实表名,也可能用子查询看作的表名,同时真实表和子查询看作的表要做连接查询

22.创建表和约束

回顾MySQL创建表语句users(id整型/name字符串/birthday日期型,默认今天)
drop table if exists users;
create table if not exists users(id int(5) auto_increment primary key,name varchar(4) not null,birthday date default '2015-4-27'
);使用oracleSQL,创建用户表users(id整型/name字符串/birthday日期/sal整型,默认今天)
create table users(id number(5) primary key,name varchar2(8) not null unique,sal number(6,2) not null,birthday date default sysdate
);进入回收站
drop table users;查询回收站中的对象
show recyclebin;闪回,即将回收站还原
flashback table 表名 to before drop;
flashback table 表名 to before drop rename to  新表名;彻底删除users表
drop table users purge;清空回收站
purge recyclebin;测试如下类型
(1)number(5):
insert into users(id,name,sal) values(1,'A',6666.66);
insert into users(id,name,sal) values(11,'AA',6666.66);
insert into users(id,name,sal) values(111,'AAA',6666.66);
insert into users(id,name,sal) values(1111,'AAAA',6666.66);
insert into users(id,name,sal) values(99999,'AAAAA',6666.66);
insert into users(id,name,sal) values(100000,'AAAAAA',6666.66); 错
5表示最多存99999    (2)number(6,2):
col sal for 9999.99
insert into users(id,name,sal) values(1,'A',6.66);
insert into users(id,name,sal) values(11,'AA',66.666);
insert into users(id,name,sal) values(111,'AAA',666.6666);
insert into users(id,name,sal) values(1111,'AAAA',6666.66666);
insert into users(id,name,sal) values(11111,'AAAAA',66666.666666);错
number(6,2)
其中2表示最多显示2位小数,采用四舍五入,不足位数补0,同时要设置col ... for ...
其中6表示小数+整数不多于6位
其中整数位数不得多于4位,可以等于4位(3)varchar2(8):
insert into users(id,name,sal) values(1,'A',7777.77);
insert into users(id,name,sal) values(2,'AA',7777.77);
insert into users(id,name,sal) values(3,'AAA',7777.77);
insert into users(id,name,sal) values(4,'AAAA',7777.77);
insert into users(id,name,sal) values(5,'AAAAA',7777.77);
insert into users(id,name,sal) values(6,'AAAAAA',7777.77);
insert into users(id,name,sal) values(7,'AAAAAAA',7777.77);
insert into users(id,name,sal) values(8,'AAAAAAAA',7777.77);
insert into users(id,name,sal) values(9,'AAAAAAAAA',7777.77);错insert into users(id,name,sal) values(1,'哈',7777.77);
insert into users(id,name,sal) values(2,'哈哈',7777.77);
insert into users(id,name,sal) values(3,'哈哈哈',7777.77);
insert into users(id,name,sal) values(4,'哈哈哈哈',7777.77);
insert into users(id,name,sal) values(5,'哈哈哈哈哈',7777.77);错  8表示字节
GBK 赵 2字节(4)date:默认格式为:'27-4月-15'
(5)CLOB【Character Large OBject】:大文本对象,即超过65565字节的数据对象,最多存储4G
(6)BLOB【Binary Large OBject】:大二进制对象,即图片,音频,视频,最多存储4G为emp表增加image列,alter table 表名 add 列名 类型(宽度)
alter table emp
add image blob;修改ename列的长度为20个字节,alter table 表名 modify 列名 类型(宽度)
alter table emp
modify ename varchar2(20);删除image列,alter table 表名 drop column 列名
alter table emp
drop column image;重名列名ename为username,alter table 表名 rename column 原列名 to 新列名
alter table emp
rename column ename to username;将emp表重命名emps,rename 原表名 to 新表名
rename emp to emps;注意:修改表时,不会影响表中原有的数据笔试题:有【1000亿】条会员记录,如何用最高效的方式将薪水字段清零,其它字段内容不变?第一:从emp表中删除sal字段alter table emp drop column sal;      第二:向emp表中添加sal字段,且内容默认0alter table empadd sal number(6) default 0;修改表不可回滚 创建表customers(单)和orders(多),使用primary key/not null/unique/default/foreign key约束
要体现【on delete cascade/on delete set null】
需求:删除客户,级联删除他所有的订单delete from customers where id = 1;
需求:删除客户,不级联删除他所有的订单,只是将外健设置为NULLdelete from customers where id = 1;   create table customers(id number(3) primary key,name varchar2(4) not null unique
);
insert into customers(id,name) values(1,'A');
insert into customers(id,name) values(2,'B');create table orders(id number(3) primary key,isbn varchar2(6) not null unique,price number(3) not null,cid number(3),--constraint cid_FK foreign key(cid) references customers(id) on delete cascade constraint cid_FK foreign key(cid) references customers(id) on delete set null
);
insert into orders(id,isbn,price,cid) values(1,'isbn10',10,1);
insert into orders(id,isbn,price,cid) values(2,'isbn20',20,1);
insert into orders(id,isbn,price,cid) values(3,'isbn30',30,2);
insert into orders(id,isbn,price,cid) values(4,'isbn40',40,2);创建表students,包括id,name,gender,salary字段,使用check约束【性别只能是男或女,薪水介于6000到8000之间】
create table students(id number(3) primary key,name varchar2(4) not null unique,gender varchar2(2) not null check ( gender in ('男','女') ),salary number(6) not null check ( salary between 6000 and 8000 )
);
insert into students(id,name,gender,salary) values(1,'哈哈','中',6000);错
insert into students(id,name,gender,salary) values(2,'呵呵','男',5000);错
insert into students(id,name,gender,salary) values(3,'嘻嘻','女',7000);对

oracle一步一步学习(二)相关推荐

  1. 一步一步搭建oracle 11gR2 rac+dg之环境准备(二)【转】

    一步一步在RHEL6.5+VMware Workstation 10上搭建 oracle 11gR2 rac + dg 之环境准备 (二) 一步一步搭建oracle 11gR2 rac+dg之环境准备 ...

  2. Java程序员从笨鸟到菜鸟之(一百零八)一步一步学习webservice(二)webservice基本原理

    本来这第二篇打算讲解"开发第一个基于XFire的webservice"的内容来着.但是想想.开发实例只是局限于了会用的层面上.如果想真正的理解webservice还是需要挖掘其原理 ...

  3. 【深度学习基础】一步一步讲解卷积神经网络

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送 本文转自:一步一步讲解卷积神经网络 卷积神经网络(Convoluti ...

  4. 一步一步学习iOS 5编程(第三版)-PDF中文版-正式发布!

    目前,这是第一本介绍iOS 5.x 和 Xcode 4.4 的中文版书籍,尤其适合于iOS 编程开发初学者.本教程由 EntLib.com 团队编写.如有任何技术问题,欢迎留言. 电子版 – PDF ...

  5. 字典排序 python3_一步一步学Python3(小学生也适用) 第十二篇: 元组tuple类型

    上一篇我们学习了列表(List),这一篇我们将学习元组(Tuple). 一.Python元组(Tuple) python的元组与列表类似,列表使用方括号[ ],元组使用小括号(). 元组的创建跟列表一 ...

  6. visual studio学习python_一步一步学Python3(小学生也适用) 第三篇: Visual Studio Code

    工欲善其事,必先利其器: 器欲尽其用,必先得其法. 前面两章我们分别安装了Python3.8和在三种模式下运行了Python的print()函数程序.在开始我们正式学习Python基础语法时,我们先把 ...

  7. 一步一步手绘Spring IOC运行时序图二(基于XML的IOC容器初始化)

    相关内容: 架构师系列内容:架构师学习笔记(持续更新) 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程) 一步一步手绘Spring IOC运行时序图二(基于XM ...

  8. 一步一步学Spring Boot(二)课程发布了~~~

    课程名称 <一步一步学Spring Boot(二)> 学习地址 CSDN学习地址: http://edu.csdn.net/lecturer/994 51CTO学习地址:http://ed ...

  9. 一步一步学习iOS 摘记

    本文参考<一步一步学习iOS6编程>书籍, 下载链接: http://download.csdn.net/detail/u012605629/8788505 第一部分:Hello Worl ...

  10. 韩松老师模型压缩--三步剪枝法学习笔记

    韩松老师模型压缩–三步剪枝法学习笔记 Learning both Weights and Connections for Efficient Neural Networks Abstract 神经网络 ...

最新文章

  1. KOA2路由koa-router实现类似express router的文件结构设计---KOA入门学习
  2. python 第一个单词大写其他小写_Python入门的新手需要遵守哪些命名规范?
  3. 人工智能:第六章 专家系统
  4. Shadow Mapping 的原理与实践 【转】
  5. Linux shell删除变量
  6. IE6 下图片少一块
  7. 基础研究到底要基础到什么程度?
  8. mysql数据库高可用_MySQL数据库高可用
  9. mysql linux 中文乱码怎么解决_如何解决mysql linux 中文乱码的问题
  10. 【Mac新资讯】搭载Apple M2 晶片的Mac要来啦!是否值得期待
  11. 【STM32 基础实验矩阵按键】
  12. Xshell颜色及PS1
  13. P3376 (最大流 dinic)
  14. 更新计算机策略命令,强制更新组策略指令是什么
  15. Linux /etc/shadow(影子文件)内容解析(超详细)
  16. 解决问题:PostgreSQL类型为 json, 但表达式的类型为 character varying
  17. 中国移动开放平台(dev.cmccopen.cn)请求头Header:Authorization验证失败的原因(我遇到的)
  18. m4a怎么转换成mp3?
  19. IntelliJ IDEA java项目导入jar包,打jar包
  20. grbl源码解析——速度前瞻(1)

热门文章

  1. 购物车加商品时候结算步骤
  2. Mac 下常用的编程软件
  3. C语言中的 Sort 排序
  4. 密码嗅探、中间人攻击
  5. carsim与simulink联合仿真 eps+sbw+lka 动力学车辆模型
  6. Android错误代码返回,【已解决】JPUSH的回调返回错误代码6012
  7. 解决某些软件检测不到java环境问题
  8. 元素周期表排列的规律_元素周期表规律
  9. AI 与经济生产力的这场革命,怕是革不动
  10. 谷歌卫星地图下载器系统设置参数说明