tongsiying

阅读|运动|自律

0%

oracle-all

数据库基础

1
<a href="https://lanlan2017.github.io/download/PhraseEdit.txt" download="PhraseEdit.txt">点击下载搜狗输入法自定义短语配置文件:PhraseEdit.txt</a>

点击下载搜狗输入法自定义短语配置文件:PhraseEdit.txt

学习目标

  1. 数据库的基本概念
  2. 掌握0racl1,e客户端和EM的安装;
  3. 掌握标准sQL语句, 特别是查询语句的使用;
  4. 掌握网络环境的配置;
  5. 掌握SQL*Plus的使用;
  6. 熟悉常用客户端工具比如PL/SQL的使用

大、中、小型数据库各有哪些?一个系统如何选择使用什么的数据库?

1
2
3
1.小型数据库:access、foxbase对安全性要求不太高、用用户量百人以内、基本无成本。
2.中型数据库:Mysql,SQL Server安全级别为中间级别,用户量在5000-15000以内,成本万元左右
3.大型数据库:Sybase、Oracle、Db2海量数据、海量用户、成本高

ORACLE数据库特点

1
2
3
4
5
1.支持多用户、 大事务量的事务处理
2.数据安全性和完整性控制
3.支持分布式数据处理
4.可移植性
5.Oracle数据库基于客户端/服务器技术

Oracle 体系结构

1
2
3
4
5
6
·数据库结构
物理结构: Oracle数据库所使用的操作系统物理文件主要分为:数据文件、控制文件、日志文件
逻辑结构: 数据库的逻辑结构是从逻辑的角度分析数据库的组成,Oracle的逻辑组件包括:
数据库一>表空间一>段一>区一>模式

·实例结构: 【Oracle实例是内存结构和后台进程的集合】

sys用户和system用户有什么区别?

1
2
3
1.sys用户是超级用户,具有高权限,具有sysdba角色,有create database的权限,该用户默认密码是change_on_install
2.system用户是管理操作员,权限也很大。具有syaoper角色,没有create database的权限,默认的密码是manager
3.一般来讲,对数据库维护,使用system用户登录就可以了

ORACLE几个名词

1
2
3
4
5
6
7
8
·数据库名
数据库名是用于区分不同数据库的内部标识,就像人的身份证号一样

·数据库实例名 '
数据库实例名用于0racle与OS之间的联系和用于Oracle Server与外部连接时使用,可以和数据库名一样,也可以不一样,该参数为instancename, 若OS要与数据库取得交互,必须使用数据库实例名

·数据库服务名
参数名是SERVICE NAME,数据库服务名与数据库名相同fuw

监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Oracle网络配置
·在服务器端需要配置1istener
1istener.ora
[0RACLE_HOME]/networMadmin
ora- 12541 :TNS:没有监听程序

·在客户端需要配置网络服务名
tnsnames.ora
ora-12154:TNS:无法解析指定的连接标识符

·两个配置工具
Oracle Net Configuration Assistant
Oracle Net Manager

·listener.ora监听配置
LISTENER=
(DESCRIPTION_LIST=
(DESCRIPTION=
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC)
)
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=TCP)(HOST=IP)(PORT=1521 ))
)
)
)

·tnsnames.ora配置
»ORA10G= (DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST= secdB)(PORT
=1521)) (CONNECT_DATA=
(SERVER= DEDICATED)
(SERVICE_NAME= ora10g)
)

·sql*plus常用命令
conn联接 2.disc断开联接 3.passwd改密码
show user 显示当前用户 5.exit退出

用户管理常用命令
·将用户解锁的命令
alter user scott account unlock;
·更改用户密码
alter user scott identified by m123;

请写出配置pl/sql develop客户端,使用pl/sql develop连接数据库的步骤

1
2
3
4
5
1.装oracle    
2.配置两个文件
3.安装client客户端
4.用sys给普通用户解锁
5.pl/sqdev(连scott)

启动、停止数据库及监听

1
lsnrctl stop  // 关闭                                          lsnrctl start  // 启动

老师给:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
数据库database (DB)
数据库的安装
安装的路径
服务名service name
密码

服务端:
配置监听文件:
路径:...../network/admin/listener.ora
配置HOST(ip),PORT(端口)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.197.129)(PORT = 1521))
)
)

客户端的配置:tnsnames.ora
连接名,host,port,service name(服务名)
zcw =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.197.129)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)

服务端的启动
windows:我的电脑-管理-服务管理和应用-服务 (services.msc进到服务中)
1.oracleservice服务名
2.oraclelisntener监听服务
3.sqlplus
linux:
1.启动和关闭监听 lsnrctl start|status|stop

数据库启动和关闭:
sqlplus sys/密码@orcl as sysdba
sql>startup ;开启
sql>shutdown immediate;关闭数据库实例

连接:
cmd:(linux) sqlplus 用户名/密码@连接名
sqlplus / as sysdba (仅限于服务端登录,为sys用户)
sqlplus sys/密码@实例名 as sysdba
sql> conn 用户名/密码@实例名

注:登录sys 用户必须as sysdba

连接过程中的报错:
1.the account is lockede (用户锁定)
解锁:用sys或者system用户登录
alter user 用户名 account unlock;

修改用户密码
password 用户名
alter user 用户名 identified by 密码;

SQL结构化查询语言

学习目标

  1. 掌握SELECT查询语句
  2. SELECT语句中的算术表达式
  3. 连接运算符、文字字特串、distinct关键字的用法
  4. describe命令
  5. where子句=、<=、between 、in、like和null
  6. 比较条件使用and、or和not运算符的逻辑条件
  7. 表达式中运算符的忧先级规则
  8. 使用order by子句对行进行排序
  9. 替代变量&

select语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
语法:
select * {[DISTINCT] column|expression[alias],...} from table;
select 关键字接要査询的内容 from 接某张表
例如:
select * from emp;
select distinct deptno,ename from emp;

select语句编写规范】
1.SQL语句不区分大小写
2.SQL语句可输入在一行或多行中
3.关键字不能缩写, 也不能跨行分开写
4.子句通常放在单独的行中
5.应使用缩进来提高可读性
6.在SQL Devebper中, 可以根据需要使用分号(;)终止SQL语句, 如果执行多条SQL语句, 则需要使用分号来分隔这些语句。 7.在SQL*Plus中, 必须使用分号(;)结東每条SQL语句

SELECT语句算术表达式】
算术表达式:
十 _ * /
例如:
select ename, sa1十100, deptno from emp;
select ename, sa1*12 from emp;
算术符优先级
select ename, sa1*12+1000 from emp; 优先计算乘法
select ename, (sa1+100)*12 from emp; 优先计算括号

select语句中定义列别名】
列别名具有以下特征和用途:
1.可重命名列标题
2.有助于计算
3.紧跟在列名后 (列名和别名之间也可以加上可选关键字AS)
4.如果别名包含空格或特殊字符,或者区分大小写,则需要使用双引号
定义列别名举例
举例:
select ename, sa1*12 as"年薪" from emp;
select ename, sa1*12+nv1(comm,0)*12 "总收入" from emp;
select ename"姓名" ,sa1"薪资" from emp;

select中连接运算符】
连接运算符具有以下特征和用途:
1.将列或字符串连接到其它列
2.由两条竖线(||)表示
3.创建一个由字符表达式生成的列
例如:
select ename ll job as"雇员信息" from emp;

select语句文字字符串 】
文字是指select语句中包含的字符、数字或日期
1.日期和字符文字值必须放在单引号内。
2.每个字符串在每个返回行中输出一次
例如:
select ename ||‘is a’|| job as"雇员职称" from emp;
mysql:
select concat (content_id,' is a ',create_date) as info from ott_content;

select语句distinct
distinct:默认情況下会显示査询返回的所有行, 包括重复行
例如:
select deptno from emp;会返回重复的行
去重,加关键字:distinct
select distinct deptno from emp;

【desc显示表结构的命令】
desc emp;(到command window下执行)

where子句査询语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
【语法:】
select * {[DISTINCT] column|expression[alias],...}
from table
where condition(s)];
1.使用where子句可以限制返回的行
2.where子句在from子句之后
例如:
select ename,job,sal from emp where deptno=10;

where査询字符串和日期】
1.字符串和日期值应放在单引号内
2.字符值区分大小写, 日期值区分格式
3.默认的日期显示格式为DD-MON-YY
例如:
select * from emp where ename='SCOTT'
其中scott为字符串类型, 不加' '会提示标识符无效
举例:
select * from emp where hiredate='28-9月-1981';
alter session set nls_date_format='yyyy-mm-dd'; 【修改日期默认格式】

where中比较运算符】
1.between中...and...两值之间(包含这两个值)
2.in与任一列表值相匹配
3.like 与字符模式相匹配
4.is null为空值
5.is not null不为空值
6.=等于 >大于 <小于
7.>=大于等于
8.<=小于等于
9.<>不等于

比较运算符举例:
【between······and······两值之间 (包含这两个值)】
select ename,job,sa1,deptno from emp where sa1 between 1000 and 1500;

in与任一列表值相匹配】
select ename,job,sa1,deptno from emp where deptno in(20,30);

【like与字符模式相匹配】
select ename,job,sal from emp where ename like '%s';

【like运算符执行模式匹配】
1.使用like运算符可执行通配符捜索, 査找有效捜索字符串值
2.捜索条件可包含文字字符或数字
3.%表示零个或多个字符, _表示一个字符
例如:
select ename from emp where ename like 'S%';
select ename,sa1 from emp where ename like '_LA%' ;

null条件】
1.使用is null运算符可测试空
select ename,sa1,comm from emp where comm is null;
2.使用is not null运算符可则试不为空
select ename,sal,comm from emp where comm is not nu11;

【其它运算符的举例】
1.>大于=等于<小于
select ename,sa1,comm from emp where sa1>1000;
2.>=大于等于<=小于等于
selectename,sa1,comm fromemp where sa1<=1500;
3.<>不等于或者!
select ename,sa1, comm from emp where sa1<>1500;
select ename,sa1, comm from emp where sa1 !1500;

【逻辑运算符定义条件】
1.and如果两个条件都为真,则返回true
select ename,job,sa1,conm from emp where sa1>1500 and comm is not nu11;
2.or如果其中一个条件为真,则返回true
select ename,sa1, conm from emp where sal between 2000 and 2800 or ename like '%S';
3.not如果条件为假,则返回true
selectename,sa1,comm,deptno fromemp where deptno not in(20,30) or comm is not null;

运算符的优先级

运算符 含义
1 算术运算符
2 连接运算符
3 比较条件
4 IS [NOT] NULL、LIKE、[NOT] IN
5 [NOT] BETWEEN
6 不等于
7 NOT逻辑条件
8 AND逻辑条件
9 OR逻辑条件

可以使用括号来改变优先级

order by子句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
【使用order by子句可对检索行进行排序】
1.asc:升序,默认顺序
2.desc: 降序
3.order by子句位于select语句的最后
例如:
select ename,sa1,hiredate from emp where comm is null order by hiredate;
select ename,sa1*12 AS"年薪",job,hiredate from emp where sa1>1500 order by "年薪" desc;

【使用&替代变量】
使用&为前缀的变量可提示用户输入一个值:
select empno, ename, sal,job from emp where empno=&aa;
select empno,ename,sal,job,hiredate from emp where deptno =&bb order by hiredate desc;

【字符串类型和日期类型需要单引号''
select empno,ename,sal,job,hiredate from emp where ename='&cc';

SQL常用函数及分组函数

学习目标

  1. 描述SQL提供的各类函数
  2. 在SELECT语句中使用字符、 数字和日期函数
  3. 描述SQL提供的各类转换函数
  4. 使用to_char、to_number和to_date转换函数
  5. 在SELECT语句中应·用条件表达式
  6. 描述组函数的使用方法
  7. 使用 group by子句对数据进行分组
  8. 通过使用 having子句包括或排除分组的行

字符函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
lower(char)         将字符串转换成小写格式
upper(char) 将字符串转换成大写格式
length(char) 返回字符串长度
substr(char,m,n) 取字符串子串
replace (字段,'源字符串', '目标字符串')
例如:
select replace(ename,'S','你好'),sal from emp;

字符函数举例:
1.lower(char) 将与字符串转化为小写格式
select lower(ename),sal from emp;
2.upper(char) 将字符串转换成大写格式
select upper(ename),job from emp;
3.length(char) 返回字符串长度
select ename,job,sal from emp where length(ename)=6;
4.substr(char,m,n) 取字符串子串
select substr(ename,1,3),sa1 from emp;

数字函数

1
2
3
4
5
6
7
8
9
10
11
12
13
round :   将值舍入到指定的小数位
trunc : 将值截断到指定的小数位
mod : 返回除法运算的余数

数字函数举例:
1.round:将值舍入到指定的小数位
select round(sal,1),round(comm,1)from emp where ename='BLAKE’;

2.trunc:将值截断到指定的小数位(mysql没有对应的)
select trunc(sal,2),trunc(comm,1) from emp where ename='FORD';

3.mod:返回除法运算的余数串
select mod(10,2) from dual;

日期函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1.Oracle DB以内部数字格式存储日期:世纪、年、月日、小时、分钟和秒

2.默认的日期显示格式为DD-MON-YY。
例如:
select * from emp where hiredate>'01-1月-1982'
MySQL:
select * from emp where hiredate>'2017-03-05 14:28:56'

3.sysdate该函数返回系统时间(mysql:select
now();
例如: select sysdate from dual;

5.add_months(d,n)可以得某段时间之前或之后n个月的时间

6.months_between(d,d),返回两个日期之间的月份数

7.last_day(d):返回指定日期所在月份的最后一天
例如:
select add_months(sysdate,6) from dual;
select * from emp where sysdate>add_months(hiredate);
select months_between(to_date('20100228','yyyymmdd'to_date('20100128','yyyymmdd')) as months from dual;

round和trunc处理日期

1
2
3
4
5
6
7
例如:【使用round函数舍到月份】
select round(sysdate,'MONTH') from dual;

例如:【使用 round函数舍到年】
select round(to_date(2013-07-22',yyyy-mm-dd'),'year) from dual;

例如: 【trunc截取到年(本年的第一天)】 select trunc(sysdate,'YEAR') from dual;

隐式转换

1
2
3
4
5
【隐式转换未使用到转换函数, 默认进行转换的】
1.insert和update操作, oracle会把插入值或者更新值隐式转换为字段的数据类型
例如: 【empno数据类型为number,从字符串到数值的转换】
update emp set empno='1234' where ename='SCOTT';
update emp set empno=to_char('1234') where ename='SCOTT';

显示转换

1
通过转换函数将不同类型进行装换

to_char

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
【将时间或者日期装换成字符】
1.必须放在单引号内
2.区分大小写
3.可以包含任何有效的日期格式元素
4.具有一个日期元素, 用于删除填充的空格或隐藏前导零
5.与日期值之间用逗号分隔

举例:
【修改默认的显示时间的格式】
select ename,job,to_char (hiredate,'yyy-mm-dd') from emp;
【显示时分秒】
select ename,job,to_char(hiredate,’yyyy-mm-dd hh24:mi:ss’) from emp;
Attachment
举例:将数字转化成字符
【将sal的格式转化成$格式显示】
select ename,to_char(sal,’$99,999.99’) from emp;
元素 结果
YYYY 用数字表示的完整年份
YEAR 拼写出的年份(用英文表示)
MM 月份的两位数值
MONTH 月份的完整名称
MON 月份的三个字母缩写
DY 一周中某日的三个字母缩写
DAY 一周中某日的完整名称
DD 用数字表示的月份中某日

to_number

1
2
3
【将字符串转换为数字格式】
语法:to_number(char[,'format_model'])
例如:select to_number($123.23','$999.99') from dual;
元素 结果
9 代表一个数字
0 强制显示零
$ 放置一个浮动的美元符号
L 使用浮动的本地货币符号
. 显示小数点
, 显示作为千位指示符的逗号

to_ date

1
2
3
4
5
【将字符串转换为日期格式】
语法:
to_date(char[, 'format_model'])
例如:
select to_date(‘2013-10-17 11:15:37',yyyy-mm-dd hh24:mi:ss') from dua1

nvl函数

1
2
3
4
5
6
7
8
【将空值转换为实际值】
1.可以使用的数据类型为日期、字符和数字
2.数据类型必须匹配
语法:
nv1 (value1 ,value2)
例如:
【如果comm的值为null,那么函数返回0 ,如果comm不为空就返回comm的值】
select ename, sa1*12+nv1(comm,0)*12 as"年收入" from emp;

MySQL:

nvl2

1
2
3
4
nvl2(value1 ,value2,value3)
如果value1不是空值则返回value2,否则返回value3 反正不会返回value1
例如:
select ename, sa1,comm ,nv12(comm ,'sa1+comm’,'sa1') incom from emp;

组函数(!!!!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
组函数会对行集进行计算,为每个组提供一个结果。
【分组函数类型:】
1.avg
2.count
3.max
4.min
5.sum

【分组函数语法】
select column,group_function(column),.... from table
where condition
order by column;

例如:select avg(sal),max(sal),min(sal) from emp;

注意:
1.只要有一个列是分组函数其他列都必须是分组函数
2.分组函数不能放在where子句中
3.分组函数只可以出现在列、having、order by的子句中

【分组函数举例:】
1.査询入职时间最早和入职时间最晚的员工入职日期
select min(hiredate),max(hiredate) from emp;
2.査询部门编号为10号的部门有多少员工
select count(*) from emp where deptno=10;
3 査询emp表共有多少条数据
select count(1) from emp;
4.査询emp表中不同部门值的数量
select count(distinct deptno) f rom emp;

group by

1
2
3
4
5
6
7
8
9
10
11
12
语法:
select column,group_function(column),.... from table
where condition
group by column
order by column;

select列表中未出现在组函数中的所有列都必须包含在group by子句中
select deptno,job,sum(sal) from emp where deptno>10 group by deptno,job order by deptno;

GROUP BY子句的找错
select department_id, count(1ast_name) from emp1oyees;
select department_id,,job_id, count(1ast_name)from emp1oyees group by department_id;

having 子句限定结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
【语法:】
select column,group_function(column),.... from table
where condition
group by column
having condition
order by column;

1.对行进行分组
2.应用组函数
3.显示符合having子句的组

分组查询
group by 可多列分组,用“,”隔开
语法:
select 分组列,分组函数 from table
where 条件
group by 分组列
having 分组条件
order by

分组査询的举例 :
【找出1981年雇佣的各部门员工的平均工资大于2000的部门,并按照平均薪资降序排序】
select avg(sa1),deptno from emp
where to_char(hiredate,yyyy')=1981
group by deptno
having avg(sa1)>2000
order by avg(sa1) desc;

嵌套组函数

1
2
【显示平均薪资最高的部门】
select max(avg(sal)) from emp group by deptno;

SQL语句多表联接

学习目标
1.编写select语句,以使用等值联接和非等值联接访问多个表中的数据
2.使用自联接将表联接到自身
3.使用0uTer联接査看通常不满足联接条件的数据
4.生成两个或多个表中所有行的笛卡尔积

联接

1
2
3
4
5
6
7
8
9
10
11
自然联接:
natural join子句
using子句
on子句

outer联接:
left outer join
right outer join
full outer join

交叉联接(笛卡尔积)

联接的语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
使用联接可查询多个表中的数据;
语法:
SELECT table1.column,table2.column
FROM table1
[NATURAL JOIN table2] |
[JOIN table2 USING (column_name)] |
[JOIN table2 ON (table1.column_name = table2.column name)] |
[LEFT|RIGHT|FULL OUTER JOIN table2 oN (table1.column_name = table2.column_name)] |
[CROSS JOIN table2];

限定不确定的列名
1.使用表前缀可以限定多个表中的列名
2.使用表前缀可以提高性能
3.可以使用表别名来代替完整表名前缀

表别名是表的短名称:
1.使SQL代码变得更短, 因而占用更少的内存
2.使用列别名可区分具有相同名称但位于不同表中的列

natural join创建联接

1
2
3
4
5
6
7
1.natural join的子句以两个表中具有相同名称的所有列为基础
2.它从两个表中选择在所有相匹配列中具有相同值的那些行
3.如果名称相同的列具有不同的数据类型, 则返回一个错误
例如:
select department_id, department_name,location_id,city
from departments
natural join locations;

join using创建联接

1
2
3
4
5
6
7
1.如果多个列具有相同的名称,但数据类型不匹配 ,请使用using子句指定等值联接的列。
2.当有多个列相匹配时, 使用using子句可仅与一列相匹配
3.natural joinjoin using关键字是互相排斥的 ,不能同时出现
例如:
select emp1oyee_id,1ast_name,1ocation_id,department_id
from employees join department
using (depanment_id);

主键、外键

在using子句中使用表别名

1
2
3
4
5
1.不要对using子句中使用列加以限定
2.如果在SQL语句中的另一个位置使用了同一列,则不要对其设置别名
例如:【其中d.location_id和a.location_id是错误的】
select a.city,d.department_name from loacations a join departments d using(location_id) where d.location=1400;
select a.city,d.department_name,a.location_id from locations a join departments d using(location_id) where location_id=1400;

join on 创建联接

1
2
3
4
5
6
7
1.自然联接的基本联接条件是对具有相同名称的所有列进行等值联接。
2.使用on子句可指定任意条件或指定要联接的列。
3.联接条件独立于其它搜索条件
4.使用on子句可使代码易于理解
例如:
select e.employee_id,e.last_name,e.department_id,d.department_id,
d.location_id from employees e join departments d on(e.department_id=d.department_id);

使用on子句创建三向联接

1
2
3
4
5
select employee_id,city,department_name from employees e
join departments d
on d.department_id=e.department_id
join locations a
on d.location_id=a.location_id;

对联接应用附加条件

1
2
3
4
5
6
7
8
使用and子句或where子句可应用附加条件
select e.employees_id,e.last_name,e.department_id,
d.department_id,d.location_id from employees e
join departments d
on(e.department_id=d.department_id)
and e.manager_id=149;

select last_name,department_name from employees e join departments d on e.department_id=d.department_id where e.salary>5000;

使用on子句进行自联接

1
2
将表联接到自身
select a.last_name,b.last_name from employees a join employees b on(a.employee_id=b.manager_id); 【查询每个雇员(last_name)的上级经理的名字(last_name)】

非等值联接

1
2
3
4
5
从多张表的联接值是否相等的联接方式,分为等值连接和非等值联接
select e.ename,g.grade from emp e
join salgrade g
on sal between losal and hisal;
【查询每位雇员工资等级表(emp表 salgrade表)】

inner联接与outer联接

1
2
3
1.如果两个表的联接只返回相匹配的行, 则称该联接为inner 联接
2.如果两个表之间的联接不仅返回inner联接的结果,还返回左(或右)表中不匹配的行,则称该联接为左(或右) outer联接
3.如果两个表之间的联接不仅返回inner联接的结果,还返回左和右联接的结果,则称该联接为完全outer联接.

left outer join(outer可以省略)

1
2
3
4
5
6
7
8
左连接就是以left join前面的表为主表,即使有些记录关联不上,主表的信息能够查询出来
select e.last_name,e.department_id,d.department_name
from employees e
left join departments d
on (e.department_id=d.derpartment_id);
举例:
select e.ename,b.dname from emp e left join dept b on( e.deptno=b.deptno);
【显示雇员所在的部门,并且没有部门的雇员也显示出来】

right outer join

1
2
3
4
5
6
7
8
右连接就是以right  join前面的表为主表,即使有些记录关联不上,主表的信息能够查询出来
select e.last_name,d.department_id,d.department_name
from employees e
right join departments d
on(e.department_id=d.department_id);
举例:
select e.ename,b.dname from emp e right join dept b on(e.deptno=b.deptno);
【显示雇员所在的部门,并且将没有雇员的部门也显示出来】

full outer join

1
2
3
4
5
全连接的查询结果是左外联接和右外联接查询结果的并集,即使有些记录关联不上,也能把部分信息查询出来
select e.last_name,d.department_id,d.department_name
from employees e
full join departments d
on(e.department_id=d.department_id);

笛卡尔积

1
2
3
4
5
出现以下情況时将形成笛卡尔积:
1.联接条件被忽略
2.联接条件无效
3.第一表中的所有行被联接到第二个表中的所有行
4.如果要避免生成笛卡尔积, 请始终包括有效的联接条件

生成笛卡尔积(交叉联接)

创建交叉联接

1
2
3
4
cross join 子句可生成两个表的叉积。,这也称为两个表间的笛卡尔积。
select last_name,department-name
from employees
cross join departments;

思考题:

1
2
3
4
5
6
7
8
9
oracle联接语法支持下列哪些联接类型?
1.等值联接
2.非等值联接
3.left outer联接(左联接)
4.right outer 联接(右联接)
5.full outer联接
6.自联接
7.自然连接
8.笛卡尔积

总结1

1
2
3
4
5
6
7
8
在本课中, 您应该已经学会如何通过以下方式使用联接来显示多个表中的数据:
·等值联接
·非等值联接
·outer联接
·自联接
·交叉联接
·自然联接
·full(或双边)outer联接

总结2:

1
2
3
4
5
6
连接归类:
1.从多张表的连接值是否相等的连接方式, 分为等值连接和非等值接
2.从不同的表还是同一张表引用多次来完成连接査询, 分为普通连接査询和自连接査询
3.从结果集合对于査询的行筛选条件的满足情况:
·完全满足连接条件的称为内连接 (交集)
·不但显示满足连接条件的结果集合,还显示不符合条件的结果集合称为外连接

面试题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
如何实现数据库导入与导出的更新?
如何只显示重复数据,或不显示重复数据?
如何用储过程写分页?
写分页有哪些方法,你一般用什么方法?用SQL语句写一个分页?
ORACLE中左连接与右连接
1.列举几种表连接方式
不借助第三方工具,怎样查看sql的执行计划?
Cost:Oracle计算出来的执行该操作的代价;
Cardinality:表示预期从一个行源返回的记录数;
Bytes:Oracle估算房钱操作的影响的数据量(单位byte);
执行计划里的各项指标都是越小越好。
Ⅰ)使用 EXPLAIN PLAN查询PLAN_TABLE;
EXPLAIN PLAN
SET STATEMENT_ID=’QUERY1'
FOR
SELECT*
FROM a
WHERE aa=1;
SELECT operation, options,object_name,object_type,ID,parent_id FROM plan_table
WHERE STATEMENT_ID='QUERY1' ORDER BY ID;

II)SQLPLUS中的SET TRACE即可看到Execution Plan Statistics SET AUTOTRACE ON;

2.0racle虫字符串用什么符号链接?
3.Oracle是怎样分页的?

补充:

1
2
3
4
5
6
7
8
重点:分组,多表,子查询;round,trunc,to_char,to_date
什么是自联接?(面试要说清楚)
natural join两张表名称相同,数据类型相同。
natural using 两张以上表,名称相同,数据类型不同。
natural joinnatural using互斥,不能同时出现
join on(用的比较多,重点)相同名称,等值联接
from 后面接左表,join后面接右表
tomcat查看日志catalina.out

课上例题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1.从部门表和雇员表中找出相同的部门,并显示所在的部门编号和部门名称
select a.department_id,d.department_name from employees a join departments d on (a.department_id=d.department_id);
select a.department_id,d.department_name from employees a,departments d where a.department_id=d.department_id;

2.找出员工对应其部门名称
select a.last_name,b.department_name from employees a join departments b on(a.department_id=b.department_id);

3.查询每个雇员(last_name)的上级经理的名字(last_name)
select a.last_name,b.last_name from employees a join employees b on(a.employee_id=b.manager_id);

4.查询出每一位雇员的姓名、职位、以及领导的姓名
select a.last_name,a.job_id,b.last_name from employees a join employees b on(a.employee_id=b.manager_id);

5.查询每位雇员工资等级(非等值联接)
select e.ename,g.grade from emp e join salgrade g on sal between g.losal and g.hisal;

6.显示雇员所在的部门,并且没有部门的雇员也显示出来
select e.ename,b.dname from emp e left join dept b on( e.deptno=b.deptno);

7.显示雇员所在的部门,并且将没有雇员的部门也显示出来
select e.ename,b.dname from emp e right join dept b on(e.deptno=b.deptno);

8.显示雇员所在的部门,并且没有部门的雇员也显示出来,没有雇员的部门也显示出来(全联接)
select e.ename,b.dname from emp e full join dept b on( e.deptno=b.deptno);

9.笛卡尔积
select * from emp,dept;

SQL语句子査询和集合运算符

学习目标
1.定义子査询
2.描述子査询可以解决的问题类型
3.列出子査询自类型 编写单行和多行子查询
4.描述集合运算符
5.使用集合运算符将多个査询组成一个査询控制返回行的顺序

子句查询

1
2
3
4
5
6
7
8
9
10
11
12
1.先执行子查询(内部查询),在执行主查询(外部查询)
2.主查询会使用子查询的结果

例如:
select salary from employees where last_name='Abel'
select * from employees where salary>( select salary from employees where last_name='Abel');
【谁的薪金高于Abel的薪金?】

使用子查询的准则
1.将子查询放在括号中
2.查询放在比较条件的右侧可增加可读性(但是,子查询可出现在比较运算符的任意一侧)
3.对单行子查询使用单行运算符,对对行子查询使用多行运算符

单行子查询

1.仅返回一行
2.使用单行比较运算符

运算符 含义
= 等于
> 大于
>= 大于或等于
< 小于
<= 小于或等于
<> 不等于

子查询类型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
单行子查询举例:
【找出和雇员Pat(first_name)属于同部门的员工】
select department_id from employees where first_name='Pat';
select first_name from employees
where department_id=(select department_id from employees where first_name='Pat') and first_name<>'Pat';

在子查询中使用组函数
【查询雇员工资最低的人的姓名(last_name),工作id,薪金】
1.子查询:最低工资
select min(salary) from employees;
2.主查询:雇员工资最低的人的姓名(last_name),工作id,薪金
select last_name,job_id,salary from employees
where salary=(select min(salary) from employees);

带有子查询的having子句
1.oracle service会先执行子查询
2.oracle servic会将结果返回到主查询的having子句中
【求出各个部门的最低薪水高于50这个部门的最低薪水的部门号,并显示最低薪水】
1.子查询:50这个部门的最低薪水的部门号
select min(salary) from employees where department_id=50;
2.主查询:各个部门的最低薪水高于50这个部门的最低薪水的部门号,并显示最低薪水
select department_id,min(salary) from employees
group by department_id having min(salary)>(select min(salary) from employees where department_id=50);

内部查询没有返回任何行
【查询工作id和‘Hass’相同的员工的姓名(last_name),工作id】
select last_name,job_id from employees where job_id=(select job_id from employees where last_name=’Hass’);

多行子查询

1.返回多个行
2.使用多行比较运算符

运算符 含义
IN 等于列表中的任意一个成员
ANY 前面必须是=、!=、>、<、<=、>=。将某个值与列表的每个值或查询返回的每个值进行比较。如果查询没有返回任何行,则求值结果为FALSE。
ALL 前面必须是=、!=、>、<、<=、>=。将某个值与列表中的每个值或查询返回的每个值进行比较。如果查询没有返回任何行,则求值结构为TRUE。

在多行子查询中使用any运算符

1
2
3
4
5
6
【比任意一个职位为(it_prog)的人工资低的员工的编号,姓名,职位,和薪水(排除职位为it_prog)】
1.子查询:职位为(it_prog)的人工资
select salary from employees where job_id='IT_PROG';
2.主查询:比任意一个职位为(it_prog)的人工资低的员工的编号,姓名,职位,和薪水(排除职位为it_prog)
select employee_id,last_name,job_id,salary from employees
where salary<any (select salary from employees where job_id='IT_PROG') and job_id<>'IT_PROG';

在多行子查询中使用all运算符

1
2
3
4
5
6
【比职位为(it_prog)的人工资都低的员工的编号,姓名,职位,和薪水(排除职位为it_prog)】
1.子查询:职位为(it_prog)的人工资
select salary from employees where job_id='IT_PROG';
2.主查询:比任意一个职位为(it_prog)的人工资低的员工的编号,姓名,职位,和薪水(排除职位为it_prog)
select employee_id,last_name,job_id,salary from employees
where salary<all (select salary from employees where job_id='IT_PROG') and job_id<>'IT_PROG';

使用exists运算符

exists里的子查询结果集非空,exists()子句的值就是true。
exists里的子查询结果集为空,exists()子句的值就是false。

1
2
3
4
5
6
7
8
9
10
11
【如果部门下面没有员工,则查询所有部门信息】
1.子查询:(部门下面有员工)
select distinct d.department_id from employees e
join departments d on (e.department_id=d.department_id);

2.主查询:如果部门下面没有员工,则查询所有部门信息
ppt:select * from departments where not exists (select * from employees where e.department_id=d.department_id);

我写的:
select * from departments where exists (select distinct d.department_id from employees e
join departments d on (e.department_id=d.department_id));

子查询中的空值

1
2
select * from departments
where department_id not in(select distinct department_id from employees);

rownum分页查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
select * from emp;

select e.*,rownum rn from(select * from emp)e;

select e.*,rownum rn from(select * from emp)e where rownum<=10;

select * from (select e.*,rownum rn from(select * from emp)e where rownum<=10) where rn>=6;

select * from(select e.*,rownum rn from(select * from emp)e where rownum<=10) where rn>=10;

--查询EMP表中前三条
select * from emp;
select a.*,rownum rn from(select * from emp) a where rownum<=3;

--查询工资从高到底的前5个
select * from emp order by sal desc;
select b.*,rownum rn from(select * from emp order by sal desc)b where rownum<=5;

集合运算符

集合运算符准则

1
2
3
4
1.select列表中的表达式在数量上必须匹配。
2.第二个査询中每一列的数据类型必须与第一个査询中对应列的数据类型相匹配 。
3.可以使用括号更改执行顺序 。
4.order by子句只能出现在语句的末尾。

oracle server和集合运算符

1
2
3
4
5
6
7
1.除非使用UNION ALL运算符, 否则会自动删除重复行
2.第一个査询中的列名将显示在结果中
3.除非使用UNION ALL运算符, 否则默认情况下输出按升序进行排序
备注:
下面使用的表包括:
1.EMPLOYEES: 提供有关所有当前雇员的详细信息
2.job_history:记录雇员更换职务时先前职务的开始日期和结束日期、职务标识及部门的详细信息

使用union运算符

1
2
3
4
5
6
7
8
9
【显示所有雇员的当前职务和先前职务的详细信息,每位雇员只显示一次(去重)】
select employee_id,job_id from employees
union
select employee_id,job_id from job_history;

【查询部门2030的所有职务】
select job_id from employees where department_id=30
union
select job_id from employees where department_id=20;

使用union all 运算符

1
2
3
4
【显示所有雇员的当前职务和先前职务的详细信息(不去重)】
select employee_id,job_id from employees
union all
select employee_id,job_id from job_history;

intersect运算符

1
2
3
4
5
6
7
8
9
【显示符合条件的雇员的id和职务id:这些当前职务与以前职务相同】
select employee_id,job_id from employees
intersect
select employee_id,job_id from job_history;

【查询部门30和20中是否有相同的职务和工资】
select job_id,salary from employees where department_id=30
intersect
select job_id,salary from employees where department_id=20;

minus运算符

1
2
3
4
5
6
7
8
9
【显示从未更换过职务的雇员的雇员id】
select employee_id from employees
minus
select employee_id from job_history;

【查询只在部门表中出现,但没有在雇员表中出现的部门编号】
select department_id from departments
minus
select department_id from employees;

匹配select语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1.使用UNION运算符可显示位置ID、 部门名称和所在的省/市/ 自治区。
2.如果某些列不存在于一个表或另一个表中, 则必须使用T0_CHAR 函数或任何其它转换函数匹配数据类型。
select location id,department name"Department",
to_char(null) “Warehouse locationfrom departments
union
select loaction_id,to_char(null) “Department” state_province from locations;

--查询只在部门表中出现,但没有在雇员表中出现的部门编号
--查询没有雇员的部门编号
select department_id from departments
minus
select department_id from employees;

select location_id,department_name "Department",
TO_CHAR(NULL) "Warehouse location" FROM departments
UNION
SELECT location_id,TO_CHAR(NULL),
state_province FROM locations;

在集合运算中使用order by子句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1.order by子句只能在复合査询的末尾出现一次 
2.这些査询中不能包含多个order by子句
3.order by子句仅识别第一个select查询中的列
4.默认情況下, 使用第一个select査询中的第一列按升序对输出进行排序
语法:
集合查询1
{集合运算}
集合查询2
[order by....]

order by 子句举例:
【显示雇员当前部门和先前部门的雇员id、职务id和薪金,并按职位id排序】
select employee_id,job_id,salary from employees
union
select employee_id,job_id,0 from job_history
order by 2;

子査询总结:

子査询:类型、语法和准则

1
2
3
4
5
6
7
·单行子査询:
1.子査询中的组函数
2.带有子査询的HAVING子句

·多行子査询
1.使用ALL或ANY 运算符
2.子査询中的空值。

集合运算符总结:

1
2
3
4
5
1.集合运算符:类型和准则
2.UNION和UNION ALL运算符
3.INTERSEC11[ 运算符
4.MINUS运算符
5.在集合运算中使用 ORDER BY子句

SQL语句数据处理(DML)新增、修改、删除

学习目标
1.描述各个数据操纵语言(DML) 语句在表中插入行
2.更新表中的行
3.从表中删除行
4.控制事务处理

几个名称解释

1
2
3
4
1.DML(datamanipulationlanguage) : 自动提交的数据库操作语言,它们是selectupdateinsertdelete
2.DDL(datadefinitionlanguage) : 自动提交的数据库定义语言,它们是createalterdrop 等, DDL主要是用在定.义或改变表(TABLE)的结构,数据类型,表之间的链接和约東等初始化工作上, 他们大多在建立表时使用
3.DCL(DataControlLanguage) :
是数据库控制功能, 是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句

数据操纵DML

1
2
3
4
5
进行以下操作时可以执行DML语句:
1.在表中添加新行
2.修改表中的现有行
3.从表中删除现有行
4.个事务处理由一组DML (新增\修改\删除)语句组成

insert

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
insert语句语法(此语法只能插入一行)
使用insert语句可在表中添加新行:
--insert into 表名 (列名,列名,……) values(值1,值2……)
--insert into 表名 values(值1,值2……所有的列都要插入值)
【在emp表中插入雇员编号1029 雇员名tester job:test】
insert into emp values(1032,'tester1','test',' ',' ','',' ',' ');
select * from emp;

·rollback;--回退
·commit;--提交

·插入新行
1.插入一个新行, 此行的每一列都含有值
2.按照表中列的默认顺序列出这些值
3.(可选)在INSERT子句中列出这些列
4.将字符日期值放在单引号中
例如:
insert into departments(department_id, department_name, manager_id, location_id)
values(70, 'Public Relations', 100, 1700);
【在emp表中插入雇员编号1030 雇员名testuser job:manager mgr:7839 入职日期:今天系统时间 sal:8000 comm:无 deptno 40
insert into emp values(1030,'testuser','manager',7839,sysdate,8000,' ',40);

·插入带有空值的行(空也可以用‘’来表示看上面几个例题)
1.隐式方法: 在列表中省略该列。
insert into departments(department_id,,department_name)
values (30, 'Purchasing');
2.显式方法: 在values子句中指定null关键字
insert into departments '
values(100, 'Finance', null,null);

·插入特殊值
sysdate 函数用于记录当前日期和时间
例题上面有(没写ppt例题)
插入特定日期和时间值
·添加新雇员
【在emp表中插入雇员编号1031 雇员名testuser job:空 mgr:7839 入职日期:2016-7-10 sal:8000 comm:无 deptno 40】
insert into emp values(1031,'testuser','',7839,to_date('2016-7-10','yyyy-mm-dd'),8000,'',40);

·创建脚本
1.在SQL语句中使用&替代来提示用户输入值。
2.&是变量值的占符位
【在dept表中编写一个插入脚本,deptno dname,loc可以自行输入】
insert into dept values(&deptno,'&dname','&loc');
select * from dept;

·从其他表中复制行
1.编写带有子查询的insert
2.请勿使用values子句
3.使insert子句中的列数与子查询的列数匹配
4.将子查询返回的所有行插入到表salaes_reps中
例如:
【创建一个测试表】
create table test(
id number,
name varchar2(10),
location varchar2(10)
)
select * from test;
【将dept表中的所有数据插入test表中】
insert into test select * from dept;
【将emp表中的empno,ename,job字段中职位为‘CLERK’插入到 test表中】
insert into test(id,name,location)
select empno,ename,job from emp where job='CLERK';

update

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1.使用update语句修改表中的现有值
2.如果需要,可以一次更新多行
--update 表名 set 列名=新的值 where 条件
--修改emp表中工资低于1000得人,将他们的工资改为1500
update emp set sal=1500 where sal<1000;
--修改emp表中comm为空的人,给每人发100元奖金
update emp set comm=100 where comm is null and comm=0;

·更新表中的行
1.如果指定WHERE 子句, 则可以修改特定一行或多行的值:
update emp1oyees
set department_id= 50
where emp1oyee_id= 113;
2.如果省略WHERE子句, 则可以修改表中所有行的值
update copy_emp
set department_id= 110;
3.指定SET colunn _name=null可将列值更新为null

·使用子查询更新两列
根据另一个表更新行
【更新雇员7654 的职务和薪金和奖金,使其与雇员7521 的职务和薪金和奖金一样】
update emp set job=(select job from emp where empno=7521),
sal=(select sal from emp where empno=7521),
comm=(select comm from emp where empno=7521)
where empno=7654;
【将与雇员号7369相同职位的人的部门编号deptno修改和BLACK在同一部门】
update emp set deptno=(select deptno from emp where ename='BLACK')
where job=(select job from emp where empno=7369);

delete

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
delete 删除表中的数据

1.delete from 表名 where 条件 其中from可省略
delete emp where empno in(8888,8889);
rollback;
【删除没有雇员的部门】
delete from dept where deptno not in(select distinct deptno from emp);

【查询没有雇员的部门的名称、地区、部门号】
1.子查询(找到有人的部门)
select * from emp
2.主查询
select deptno,loc,dname from dept where deptno not in(select distinct deptno from emp);



子查询(各个部门中超过3个人)
select deptno,count(ename) from emp group by deptno having count(ename)>3;

·删除的另一种方式:truncate(用来清库)
truncate table 表名(不可以回退,释放空间,table不能省)
要删除就全删,无法删除某一条,删除后无法滚回
truncate语句
1.从表中删除所有行,使表为空并保留表结构不变
2.truncat比delete要快, 原因如下:
truncat语句是数据定义语言(DDL)语句, 它不会生成回退信息二者的比较
truncate:删除内容、不删除定义、释放空间、不可以回滚
delete:删除内容,不删除定义,不释放空间,可以回滚

数据库事务处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
事务: 用于保证数据的一致性 ,由一组相关的DML语句组成改的DML语句要么全部成功, 要么全部失败 。
事务和锁:
当执行事务操作(dm1)语句,oracle会'在被作用的表上加锁, 防止其它用户修改表结构
数据库事务处理由以下语句之一组成:
用于对数据进行一次一致更改的DML语句

·一条DDL语句
一条数据控制语言(DCL)语句

·数据库事务处理:开始和结束
1.在执行第一条DML SQL语句时开始
2.在发生下列事件之一时结束

·发出commitrollback语句
1.执行DDL或DCL语句 (自动提交)
2.用户退出SQL Deve1oper或SQL*Plus
3.系统崩溃

·commitrollback语句优点
使用commitrollback语句, 您可以:
1.确保数据的一致性
2.在使更改变成永久性更改之前预览数据更改
3.按逻辑关系对相关操作进行分组

·显示事务处理控制语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
·将更改回退到某个标记
1.使用 savepoint语句可在当前事务处理中创建一个标记
2.使用rollback to savepoint语句可回退到该标记
update...
savepoint update_done;
insert...
rollback to update_done;

·隐式事务处理
在下列情况下会发生自动提交:
1.发出DDL语句
2.发出DCL语句
3.从SQL Deve1oper或SQL*Plus正常退出, 而没有显式发出COMMIT或ROLLBACK语句
4.当异常终止SQL Deve1oper或SQL*Plus或者系统出现故障时会发生自动回退。

·执行COMMIT或ROLLBACK操作之前的数据状态
1.可以将数据还原到以前的状态
2.当前用户可以使用SELECT语句査看DML操作的结果
3.其他用户不能査看当前用户发出的DML语句的结果
4.受影响行已锁定,其他用户不能更改受影响行中的数据

·执行COMMIT操作之后的数据状态
1.数据更改已保存在数据库中
2.已改写以前的数据状态
3.所有用户都可以査看结果
4.受影响行上的锁已被释放, 其他用户可以对这些行进行处理,,
5.所有保存点都已被清除

·提交数据
进行更改
delete from employees
where employee_id= 99999;
insert into departments
values(290, 'Corporate Tax', nuLL, 1700);
提交更改
commit;

·执行ROLLBACK操作之后的数据状态
1.使用ROLLBACK语句放弃所有待定更改之后会有如下结果
2.数据更改已取消
3.数据已还原到以前的状态
4.受影响行上的锁已被释放
delete from copy_emp;
rollback;

·读一致性
1.读一致性可以确保用户所看到的数据始终是一致的
2.一个用户进行的更改不会与另一个用户进行的更改相冲突
3.读一致性可以确保对于同一数据:
4.读取者不必等待写入者完成操作即可读取
5.写入者不必等待读取者完成操作即可写入
6.写入者必须等待其他写入者完成操作才可写入
实施读一致性:

1
2
3
4
5
6
7
·select中的for update子句
1.锁定employees表中job_id为sa_rep的行
2.当发出rollbackcommit语句时才
3.如果select语句要锁定另一个用户已锁定的某一行,,数据库就会一直等待到该行可用为止, 然后返回 select语句的结果
select emp1oyee_id, salaf, commission_pct, job_id
from emp1oyees where job_id= 'SA_REP'
for update order by employee_id;

作业1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
基础:
1、大、中、小型数据库各有哪些?一个系统如何选择使用什么的数据库?
小型数据库:access、foxbase对安全性要求不太高、用用户量百人以内、基本无成本。
中型数据库:Mysql,SQL Server安全级别为中间级别,用户量在5000-15000以内,成本万元左右
大型数据库:Sybase、Oracle、Db2海量数据、海量用户、成本高

2.sys用户和system用户有什么区别?
(1).sys用户是超级用户,具有高权限,具有sysdba角色,有create database的权限,该用户默认密码是change_on_install
(2).system用户是管理操作员,权限也很大。具有syaoper角色,没有create database的权限,默认的密码是manager
(3).一般来讲,对数据库维护,使用system用户登录就可以了

3、如何查看数据当前连接的用户
show user;

4.给hr用户进行解锁,并且设置密码hr
alter user hr account unlock
alter user hr identified by hr;

5、请写出配置pl/sql develop客户端,使用pl/sql develop连接数据库的步骤
(1).装oracle
(2).配置两个文件
(3).安装client客户端
(4).用sys给普通用户解锁
(5).pl/sqdev(连scott)
(6)、通过DOS窗口用sys连接数据库的命令?请附截图
conn sys/hzn@orcl assysdba;

提高:
在cmd中启动、停止数据库及监听(截图)
net start OracleServerORCL
net stop OracleServerORCL
lsnrctl start
lsnrctl stop
Linux下安装oracle
Linux下启动、停止数据库及监听。
start up;
shutdown immediate;
lsnrctl stop // 关闭
lsnrctl start // 启动

作业2

一、部门表和员工表的表结构

deptno 部门编号
dname 部门名称
loc 部门地址

员工表emp的表结构:

empno 员工编号
ename 员工姓名
job 员工工作职位
mgr 上司的编号
hiredate 录用日期
sal 工资
comm 奖金
deptno 部门编号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
使用sys将scott用户解锁,使用scott用户执行下列查询语句:
基础:
解锁用户scott,使用scott用户登录oracle数据库。
sqlplus sys/hzn@orcl as sysdba;
alter user scott acconunt unlock;
conn scott/hzn@orcl;
查询部门表中的所有数据。
select * from dept;

在员工表中查询员工的姓名和员工的工作。
select ename,job from emp;
查询出员工的工资及给员工加薪200后的工资。
select sal,sal+200 from emp;
查询出每个员工的姓名,及他们的年薪,年薪的列名为Sal Of Year。(年薪=月薪*12)
select ename,sal*12“Sal Of Year” from emp;
6.创建一个由字符表达式生成的列,取别名为“Employees”。
查询结果如:SMITH likes CLERK (SMITH为员工姓名,CLERK为员工的工作)
select ename ||’ likes ’|| job”Employees” from emp;
查询出员工表中的部门编号,去掉重复行。
select deptno from emp;
select distinct deptno from emp;
查询出工作为CLERK且工资大于等于950的员工信息。
select * from emp where job=’CLERK’ and sal>=950;
查询出工资大于1000小于2000的员工的姓名及工资。
select ename,sal from emp where sal between 1000 and 2000;
查询出员工姓名为ALLEN,JONES,KING,ADAMS,MILLER的信息。
select * from emp where ename in(’ALLEN’,’JONES’,’KING’,’ADAMS’,’MILLER’);
查询姓名中第二个字母为L的员工姓名及入职日期。
select ename,hiredate from emp where ename like‘_L%’;
找出没有奖金或奖金低于100的员工。
select * from emp where comm is null or comm<100;
查询出SALESMAN或PRESIDENT职位中sal大于等于1500的员工信息。
select * from emp where ( job=’SALESMAN’ or job=’PRESIDENT’)and sal >=1500;
按入职日期倒序排列员工信息。
select * from emp order by hiredate desc;
查询出员工信息:先按部门编号顺序排列,再按员工的工资倒序排列。
select * from emp order by deptno,sal desc;
查询出工作不是MANAGER的所有员工信息。
select * from emp where ename<>‘MANAGER’;
显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面。
select ename,hiredate from emp order by hiredate;
显示不带有“R”的员工的姓名。
select ename from emp where ename not like ‘%R%’;
找出奖金高于薪金的60%的员工。
select * from emp where comm>sal*0.6;
找出收取奖金的员工的不同工作。
select job from emp;
select distinct job from emp where comm is not null;
21.根据输入的员工编号,展示员工的姓名、工资、入职日期、职位
select ename,sal,hiredate,job from emp where empno=’&aa’;


以下题目使用HR用户的employees、departments表来完成
查看DEPARTMENT_ID为80的所有员工的薪资
select salary from employees where DEPARTMENT_ID=’80’;
查看DEPARTMENT_ID为80 的部门经理的id
select manager_id from employees where DEPARTMENT_ID=’80’;
获取下employees表的表结构
SQL> desc employees;
查询出工种为'SA_REP'的所有员工的姓名、入职日期、薪资
select last_name ||first_name,hire_date,salary from employees where job_id=’SA_REP’;
给工种为'SA_REP'的所有员工加薪500,显示出员工姓名、加薪前的薪资,加薪后的薪资
select last_name ||first_name,salary,salary+500 from employees where job_id=’SA_REP’;
查询出薪资在5000--8000之前的员工信息
select * from employees where salary between 5000 and 8000;
查询薪资大于10000的员工的信息
select * from employees where salary >10000;
查询员工所在部门,DEPARTMENT_ID不重复
select department_id from employees;
select distinct department_id from employees;
查询DEPARTMENT_ID为50的员工的所有信息,按照薪资降序,雇员ID升序进行显示
select * from employees where department_id=50 order by salary desc, employee_id;
10、查询员工工资大于10000元,或者职称为AD_VP,并且入职时间在2000-1-1之后的员工信息

select * from employees where (salary>10000 or job_id=’ad_vp’) and hiredate>’1-1-2000’;
8.查询出每一位雇员的姓名、职位、以及领导的姓名。
(最高领导是没有部门的)
select e.ename,e.job,m.ename from emp e,emp m where e.mgr=m.empno;

10.要求查询出每一个雇员的编号、姓名、工资、部门名称、工资在所在公司的工资等级
(没有部门的员工也查出来)
方法一:
select e.empno,e.ename,e.sal,d.dname,s.grade from emp e,dept d, salgrade s
where (e.deptno=d.deptno or e.deptno is null) and (e.sal between losal and hisal);
方法2:
Select e.empno,e.ename,e.sal,d.dname,sg.grade
From emp e
Left Join dept d on e.deptno=d.deptno
Join salgrade sg on e.sal BETWEEN sg.losal AND sg.hisal;

12.查询出每个部门的名称、位置、部门人数、平均工资
(没有认得部门)
select d.dname,d.loc,count(ename),avg(nvl(sal,0)) from emp e
right join dept d
on e.deptno=d.deptno
group by d.dname,d.loc;

8、列出没有对应部门表信息的所有雇员的姓名、工作。
(有部门但不在部门表,没有部门)
select ename,job from emp
where deptno not in(select deptno from dept)
or deptno is null;

9、哪些部门的人数比90 部门的人数多?
select count(ename) from emp
where deptno=90;
select deptno from emp
group by deptno having count(ename)>
(select count(ename) from emp where deptno=90);

select dname from dept
where deptno
in(select deptno from emp
group by deptno having count(ename)>
(select count(ename) from emp where deptno=90));

11、列出各个部门中工资最高的员工信息:名字、部门号、薪资
select e.ename,e.deptno,e.sal
from emp e,(select max(sal) sal,deptno from emp group by deptno) a
where e.deptno=a.deptno and e.sal=a.sal;

7.用一个sql语句完成在字符串”hello”左右各添加5个‘*’,使其最终返回’*****hello*****’(禁止使用 | | 或 concate)
8.查询出每一位雇员的姓名、职位、以及领导的姓名。
(最高领导是没有部门的)
select e.ename,e.job,m.ename from emp e,emp m where e.mgr=m.empno;

10.要求查询出每一个雇员的编号、姓名、工资、部门名称、工资在所在公司的工资等级
(没有部门的员工也查出来)
方法一:
select e.empno,e.ename,e.sal,d.dname,s.grade from emp e,dept d, salgrade s
where (e.deptno=d.deptno or e.deptno is null) and (e.sal between losal and hisal);
方法2:
Select e.empno,e.ename,e.sal,d.dname,sg.grade
From emp e
Left Join dept d on e.deptno=d.deptno
Join salgrade sg on e.sal BETWEEN sg.losal AND sg.hisal;

12.查询出每个部门的名称、位置、部门人数、平均工资
(没有认得部门)
select d.dname,d.loc,count(ename),avg(nvl(sal,0)) from emp e
right join dept d
on e.deptno=d.deptno
group by d.dname,d.loc;

8、列出没有对应部门表信息的所有雇员的姓名、工作。
(有部门但不在部门表,没有部门)
select ename,job from emp
where deptno not in(select deptno from dept)
or deptno is null;

9、哪些部门的人数比90 部门的人数多?
select count(ename) from emp
where deptno=90;
select deptno from emp
group by deptno having count(ename)>
(select count(ename) from emp where deptno=90);

select dname from dept
where deptno
in(select deptno from emp
group by deptno having count(ename)>
(select count(ename) from emp where deptno=90));

11、列出各个部门中工资最高的员工信息:名字、部门号、薪资
select e.ename,e.deptno,e.sal
from emp e,(select max(sal) sal,deptno from emp group by deptno) a
where e.deptno=a.deptno and e.sal=a.sal;

7.用一个sql语句完成在字符串”hello”左右各添加5个‘*’,使其最终返回’*****hello*****’(禁止使用 | | 或 concate)

作业3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
以下作业使用scott.emp表,以及scott.dept表完成
找出EMP表员工名字中含有A和N的员工姓名
select ename from emp where ename like '%A%' and ename like '%B%';
列出部门编号为20的所有职位
select job from emp where deptno=20;
列出各个工种的最低薪资
select min(sal),job from emp group by job;
列出不属于SALES的部门
select dname from dept where dname<>'SALES';
显示出员工的工资不在1000到1500之间的员工信息:名字、工资,按工资从大到小排序
select ename,sal from emp where sal between 1000 and 1500 order by sal desc;
显示职位为MANAGER和SALESMAN,年薪在15000和20000之间的员工
的信息:名字、职位、年薪
select ename,job,sal*12 from emp where job in('MANAGER','SALESMAN') and sal*12 between 15000 and 20000;
列出至少有一个员工的所有部门
select distinct deptno from emp where ename is not null;
列出最低薪资大于1500的各种工种
select job,min(sal) from emp group by job having min(sal)>1500;
列出所有员工的年薪,并按照年薪降序排列
select sal*12 from emp order by sal*12 desc;
查出emp表中所有部门的最高薪水和最低薪水,部门编号为10的部门不显示
select max(sal),min(sal),deptno from emp where deptno<>10 group by deptno;
查询emp表中的员工号以及每个人工资的80%
select empno,sal*0.8 from emp;
查询emp表中员工编号,姓名,要求姓名输出小写
select empno,lower(ename) from emp;
查询emp表中员工姓名、工资和工种,要求将姓名列标题改为employeename,工资列标题改为salary
select ename"employeename",sal"salary",job from emp;
显示正好为5个字符的员工的姓名
select ename from emp where length(ename)=5;
15.计算工资在1000-3000之间的员工人数
select count(ename) from emp where sal between 1000 and 3000;
16.查询工作以“sales”开头的员工的平均工资,最高工资、最低工资和总工资
select avg(sal),max(sal),min(sal),sum(sal) from emp where job like 'SALES%';
17.查询30号部门的员工人数
select count(ename) from emp where deptno=30;
查询所有员工的平均奖金
select avg(sal) from emp ;
找出在(任何年份的)2月受聘的所有员工
select ename from emp where to_char(hiredate,'mm')=2;
请查询表DEPT中所有部门的情况
select deptno from DEPT;
请从表EMP中查找工种是职员CLERK或经理MANAGER的雇员姓名、工资
select ename,sal from emp where job='CLERK' or job='MANAGER';
请从表EMP中查找姓名以J开头所有雇员的姓名、工资、职位
select ename,sal,job from emp where ename like 'J%';
查询所有雇员的姓名、SAL与COMM之和
select ename,sal,sum(comm) from emp group by ename,sal;
找出平均工资最高的部门
select max(avg(sal)) from emp group by deptno;
本题以hr用户登陆,使用员工表(employees)、部门表(departments)、区域表(regions),完成以下题目
列出前五位员工的名字、工资、涨薪后的工资(涨幅为8%),以“元”为单位进行四舍五入

找出谁是最高领导,将名字按大写形式显示

求整个公司薪资最低与最高员工的薪资相差多少?
select max(salary)-min(salary) from employees;
显示整个公司的最高薪资、最低薪资、平均薪资、工资总和,并四舍五入保留到整数位
select round(max(salary),0),round(min(salary),0),round(avg(salary),0),round(sum(salary),0) from employees;
选择部门30中的所有员工
select * from employees where department_id=30;
列出所有办事员(CLERK)的姓名,编号和部门编号
select last_name||first_name,employee_id,department_id from employees where job_id like '%CLERK%';
找出奖金高于薪金的员工
select last_name||first_name from employees where commission_pct*salary>salary;
找出收取奖金的员工的不同工作
select job_id from employees where commission_pct is not null;
找出不收取奖金或收取的奖金低于100的员工
select first_name from employees where commission_pct is not null or commission_pct*salary<100;
找出早于12年前受雇的员工
select last_name||first_name from employees where months_between(sysdate,hire_date)>12;
以首字母大写的方式显示所有员工的姓名(first_name)
select upper(substr(first_name,1,1))||substr(first_name,2) from employees;
显示正好为5个字符的员工的姓名
select first_name from employees where length(first_name)=5;
显示所有员工姓名(frist_name)的前三个字符
select substr(first_name,1,3) from employees;
显示所有员工的姓名(first_name),用hzn替换所有"A"
select replace(first_name,'A','hzn') from employees;
显示员工的详细资料,按姓名排序
select * from employees order by first_name ;
显示满10年服务年限的员工的姓名和受雇日期
select last_name || first_name,hire_date from employees where months_between(sysdate,hire_date)>=10;
显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面
select last_name || first_name,hire_date from employees order by months_between(sysdate,hire_date) desc;
显示所有员工的姓名、工作和薪金,按工作的降序排序,若工作相同则按薪金排序
select last_name ||first_name,job_id,salary from employees order by job_id desc,salary;
对于每个员工,显示其加入公司的天数
select months_between(sysdate,hire_date)*30 from employees;
以年月日的方式显示所有员工的服务年限.
select months_between(sysdate,hire_date)/12 from employees;
各个部门平均、最大、最小工资、人数,按照部门号升序排序
select avg(salary),max(salary),min(salary),count(1),department_id from employees group by department_id order by department_id;
各个部门中工资大于5000的员工人数
select count(1),department_id from employees where salary>5000 group by department_id;
8.查询出每一位雇员的姓名、职位、以及领导的姓名。
(最高领导是没有部门的)
select e.ename,e.job,m.ename from emp e,emp m where e.mgr=m.empno;

10.要求查询出每一个雇员的编号、姓名、工资、部门名称、工资在所在公司的工资等级
(没有部门的员工也查出来)
方法一:
select e.empno,e.ename,e.sal,d.dname,s.grade from emp e,dept d, salgrade s
where (e.deptno=d.deptno or e.deptno is null) and (e.sal between losal and hisal);
方法2:
Select e.empno,e.ename,e.sal,d.dname,sg.grade
From emp e
Left Join dept d on e.deptno=d.deptno
Join salgrade sg on e.sal BETWEEN sg.losal AND sg.hisal;

12.查询出每个部门的名称、位置、部门人数、平均工资
(没有认得部门)
select d.dname,d.loc,count(ename),avg(nvl(sal,0)) from emp e
right join dept d
on e.deptno=d.deptno
group by d.dname,d.loc;

8、列出没有对应部门表信息的所有雇员的姓名、工作。
(有部门但不在部门表,没有部门)
select ename,job from emp
where deptno not in(select deptno from dept)
or deptno is null;

9、哪些部门的人数比90 部门的人数多?
select count(ename) from emp
where deptno=90;
select deptno from emp
group by deptno having count(ename)>
(select count(ename) from emp where deptno=90);

select dname from dept
where deptno
in(select deptno from emp
group by deptno having count(ename)>
(select count(ename) from emp where deptno=90));

11、列出各个部门中工资最高的员工信息:名字、部门号、薪资
select e.ename,e.deptno,e.sal
from emp e,(select max(sal) sal,deptno from emp group by deptno) a
where e.deptno=a.deptno and e.sal=a.sal;

7.用一个sql语句完成在字符串”hello”左右各添加5个‘*’,使其最终返回’*****hello*****’(禁止使用 | | 或 concate)

作业4

一、部门表和员工表的表结构

deptno 部门编号
dname 部门名称
loc 部门地址

员工表emp的表结构:

empno 员工编号
ename 员工姓名
job 员工工作职位
mgr 上司的编号
hiredate 录用日期
sal 工资
comm 奖金
deptno 部门编号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
将scott用户解锁,使用scott用户执行下列查询语句:
alter user scott account unlock;
conn scott/hzn@orcl;
Part one:
找出没有奖金或奖金低于100的员工
select ename from emp where comm is null or comm<100;
查询出工作不是MANAGER的所有员工信息
select * from emp where job<>'MANAGER';
查询emp表中员工的员工号和姓名,要求输出格式如下:
7369 Name is: SMITH
select empno ||' name is :'||ename from emp;
查询工资不在1000-2000之间的员工的编号、姓名、工资和部门号
select empno,ename,sal,deptno from emp where sal not between 1000 and 2000;
查询奖金不为空且工资超过1500的员工的编号、姓名、工资、奖金和部门号
select empno,ename,sal,comm,deptno from emp where comm is not null and sal>1500;
查询emp表中员工姓名、工资和工种,要求将姓名列标题改为employeename,工资列标题改为salary
select ename"employeename",sal"salary",job from emp;
查询10号部门和30号部门的所有员工的编号、姓名、工资和部门号
select empno,
select empno,ename,sal,deptno from emp where deptno in(10,30);

Prat two:使用scott用户执行下列查询语句
将员工的姓名以首字母大写,其他字母小写。
select upper(substr(ename,1,1)) ||lower(substr(ename,2),length(ename)-1)) from emp;
显示每个用户姓名的第2个和第3个字母。
select substr(ename,2,2) from emp;
将员工姓名中含有A的字母以0替换。
select replace(ename,'A',0) from emp;
查询出用户的姓名,及用户姓名字符长度。
select ename,length(ename) from emp;
计算出2096除以37的余数。
select mod(2096,27) from dual;
查询出当前的系统日期。
select sysdate from dual;
计算出员工入职到现在工作的星期数,以整数显示,不显示出小数位。
select trunc(months_between(sysdate,hiredate)*30/7,0) from emp;
查询出两列信息,员工的姓名ename和员工工资sal,以$8,00格式显示员工的工资。
select ename,to_char(sal,'$9,99') from emp;
查询出员工的姓名及员工的年薪(年薪为月薪*12加上奖金)。
select ename,sal*12+nvl2(comm,comm,0)*12 from emp;
对于每个员工,显示其加入公司的天数。
select ename,months_between(sysdate,hiredate)*30 from emp;
找出在(任何年份的)2月受聘的所有员工。
select ename from emp where to_char(hiredate,'mm')=2;
找出奖金高于薪金的员工。
select ename from emp where comm>sal;
计算工资在1000-3000之间的员工人数。
select count(ename) from emp where sal between 1000 and 3000;
查询工作以“sales”开头的员工的平均工资,最高工资、最低工资和总工资
select job,avg(sal),max(sal),min(sal),sum(sal) from emp where lower(substr(job,1,5))='sales' group by job;
where job like “
查询30号部门有奖金的员工人数
select count(ename) from emp where comm is not null and deptno=30;
查询所有员工的平均奖金
select avg(nvl(comm,0)) from emp;
查询每个部门的平均工资
select avg(sal),deptno from emp group by deptno;
查询每个部门各工种的雇员数
select count(ename),deptno,job from emp group by deptno,job;
显示在一个月为30天的情况所有员工的日薪金,忽略余数(四舍五入)
select round(sal/30,0) from emp;
显示所有员工的姓名、工作和薪金,按工作的降序排序,若工作相同则按薪金排序
select ename,job,sal from emp order by job desc,sal;
显示满33年服务年限的员工的姓名和受雇日期
select ename,hiredate from emp where months_between(sysdate,hiredate)/12>=33;
找出各月倒数第3天受雇的所有员工
select ename from emp where hiredate=last_day(hiredate)-3;
列出各个工种的最低薪资
select min(sal),job from emp group by job;
查出emp表中所有部门的最高薪水和最低薪水,部门编号为10的部门不显示
select max(sal),min(sal),deptno from emp where deptno <> 10 group by deptno;

作业5

一、部门表和员工表的表结构

deptno 部门编号
dname 部门名称
loc 部门地址

员工表emp的表结构:

empno 员工编号
ename 员工姓名
job 员工工作职位
mgr 上司的编号
hiredate 录用日期
sal 工资
comm 奖金
deptno 部门编号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
将scott用户解锁,使用scott用户执行下列查询语句:
1.找出部门10中所有经理(MANAGER),部门20中所有化验员(ANALYST),既不是经理又不是化验员员但其薪金大于或等于2000的所有员工的详细资料
select * from emp where deptno=10 and job='MANAGER' or deptno=20 and job='ANALYST' or (job not in('MANAGER','ANALYST') and sal>=2000);

请从表EMP中查找姓名以'M'开头所有雇员的姓名、工资、职位。
select ename,sal,job from emp where ename like 'M%';


3.请从表EMP中查找工资低于1500的雇员的姓名、工作、工资,并按工资升序排列。
select empno,job,sal from where sal<1500 order by sal;

4.查询列出各部门的部门名和部门经理名字
select d.dname,e.ename from emp e,dept d
where emp.deptno=dept.deptno and job='MANAGER';
或者
select dname,ename from emp join dept
on(emp.deptno=dept.deptno)
where job='MANAGER';

5.查询所有81年7月1日以前来的员工姓名、工资、所属部门的名字
select ename,sal,dname from emp,dept
where emp.deptno=dept.deptno
and hiredate<=01-7-1981;
或者
select ename,sal,dname from emp join dept
on(emp.deptno=dept.deptno)
where hiredate<=to_date('1981-07-01','yyyy-mm-dd');

查询表EMP中所有的工资大于等于2000的雇员姓名和他的经理的名字。
select work.ename,manager.ename from emp work,emp manager
where work.mgr=manager.empno(+) --左外连接
and work.sal>=2000;
或者
select work.ename,manager.ename from emp work
left join emp manager
on work.mgr=manager.empno
where work.sal>=2000;

7.请从表中查询工作是CLERK的所有人的姓名、工资、部门号、部门名称以及部门地址的信息
select ename,sal,emp.deptno,dname,loc
from emp,dept
where emp.deptno=dept.deptno and job='CLERK'
或者
select ename,sal,emp.deptno,dname,loc from emp
join dept
on(emp.deptno=dept.deptno)
where job='CLERK'

8.查询出每一位雇员的姓名、职位、以及领导的姓名。
select work.ename,work.job,manager.ename from emp work,emp manager
where work.mgr = manager.empno;
或者
select work.ename,work.job,manager.ename
from emp work
join emp manager
on(work.mgr = manager.empno);

9.查询每个雇佣的雇佣编号,姓名、基本工资、职位、领导的姓名、部门名称及位置
Select e.empno,e.ename,e.sal,e.job,m.ename,d.dname,d.loc
From emp e
Left JOIN emp m on e.mgr=m.empno
JOIN dept d on d.deptno = e.deptno;

或者
Select e.empno,e.ename,e.sal,e.job,m.ename,d.dname,d.loc
From emp e,emp m, dept d
Where e.mgr=m.empno(+) and d.deptno = e.deptno;

10.要求查询出每一个雇员的编号、姓名、工资、部门名称、工资在所在公司的工资等级
SELECT e.empno,e.ename,e.sal,d.dname,sg.grade
FROM emp e,dept d,salgrade sg
WHERE e.deptno = d.deptno
AND e.sal BETWEEN sg.losal AND sg.hisal;
或者
Select e.empno,e.ename,e.sal,d.dname,sg.grade
From emp e
Left Join dept d on e.deptno=d.deptno
Join salgrade sg on e.sal BETWEEN sg.losal AND sg.hisal;

11.查询出每个部门的名称、部门人数、平均工资
SELECT d.dname,COUNT(e.empno),NVL(AVG(e.sal),0)
FROM emp e right join dept d
on (e.deptno=d.deptno)
GROUP BY d.dname;

12.查询出每个部门的名称、位置、部门人数、平均工资
select d.dname,d.loc,count(e.empno),avg(sal)
from emp e
right join dept d
on(d.deptno = e.deptno)
group by d.dname,d.loc;

统计平均工资大于2000的部门的详细信息
SELECT d.*,avg(e.sal)
FROM emp e, dept d
WHERE d.deptno = e.deptno
GROUP BY d.deptno,d.dname,d.loc
HAVING AVG(e.sal)>2000;
或者:
SELECT d.*,avg(e.sal) FROM emp e
join dept d
on (d.deptno = e.deptno)
GROUP BY d.deptno,d.dname,d.loc
HAVING AVG(e.sal)>2000;

显示雇员姓名,根据其服务年限,将最老的雇员排在最前面
select ename, hiredate from emp order by hiredate;

作业6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
一、本习题以scott用户登陆,使用员工信息表(emp)、部门表(dept)。完成以下问题
1、找出emp表中的姓名(ENAME)第三个字母是A的员工姓名
SQL> select ename
2 from emp
3 where ename like '__A%';

2、找出emp表中姓名中含有A和N的员工姓名
SELECT ename
FROM emp
WHERE ename LIKE '%A%' and ename like '%N%’;

3、找出所有有佣金(COMM)的员工,列出姓名、工资、佣金,显示结果按工资从小到大,佣金从大到小排列
SELECT ename,sal,comm
FROM emp
WHERE comm is not null
ORDER BY sal ,comm DESC;

4、列出部门编号为20的所有的职位
SELECT DISTINCT job
FROM emp
WHERE deptno =20;
列出不属于SALES的部门的员工
SELECT ename,deptno
FROM emp
WHERE deptno <>(SELECT deptno FROM dept WHERE dname='SALES');

Select ename from emp,dept
Where emp.deptno=dept.deptno
And dept.dname<>’SALES’

Select ename from emp join depton emp.deptno=dept.deptno

where dept.dname<>’SALES’

6、显示工资不在1000~1500之间的员工信息:名字、工资,按工资从大到小排序
SELECT ENAME,sal
FROM emp
WHERE sal NOT BETWEEN 1000 AND 1500
ORDER BY sal DESC;

7、在表EMP中查询所有工资高于JONES的所有雇员姓名、工作和工资。
SELECT e.ename,e.job,e.sal
FROM emp e
WHERE e.sal >(SELECT sal
FROM emp
WHERE ename='JONES');
列出没有对应部门表信息的所有雇员的姓名、工作。
SELECT e.ename,e.job
FROM emp e
WHERE e.deptno NOT IN (SELECT deptno FROM dept);


查找工资在10003000之间的雇员所在部门的所有人员信息
SELECT *
FROM emp
WHERE deptno IN
( SELECT DISTINCT deptno
from emp
WHERE sal BETWEEN 1000 AND 3000);

二、本题以hr用户登陆,使用员工表(employees)、部门表(departments)、区域表(regions),完成以下题目
1、列出前五位员工的名字、工资、涨薪后的工资(涨幅为8%),以“元”为单位进行四舍五入
SELECT first_name,last_name,salary,round(salary+salary*0.8)
FROM employees
WHERE ROWNUM<=5;

2、找出谁是最高领导,将名字按大写形式显示
SELECT UPPER(first_name)
FROM employees
WHERE manager_id IS NULL;

3、找出first_name为David的员工的直接领导的信息
SELECT e2.*
FROM employees e1,employees e2
WHERE e1.manager_id=e2.employee_id
AND e1.first_name='David';

4、列出员工薪资高于他直接领导的员工的名称、工资,以及上司的名字和工资
--e1代表员工表,e2代表直接领导表
SELECT e1.last_name,e1.salary,e2.last_name,e2.salary
FROM employees e1,employees e2
WHERE e1.manager_id=e2.employee_id
AND e1.salary>e2.salary;

5、哪些员工和Chen(last_name)同一个部门
SELECT e.last_name
FROM employees e
WHERE e.department_id=(SELECT department_id
FROM employees
WHERE last_name='Chen');

6、显示Executive部门有哪些职位?
SELECT e.job_id,d.department_name
FROM employees e,departments d
WHERE e.department_id=d.department_id
AND d.department_name='Executive';


Select job from employees where department_id=

(select department_id from departments
where dpartment_name=’Executive’)

7、求整个公司薪资最低与最高员工的薪资相差多少?
SELECT MAX(salary)-MIN(salary)
FROM employees;
8、显示整个公司的最高薪资、最低薪资、平均薪资、工资总和,并四舍五入保留到整数位
SELECT ROUND(MAX(salary)),ROUND(MIN(salary)),ROUND(AVG(salary)),round(SUM(salary+nvl(comm,0)))
FROM employees;

9、哪些部门的人数比90 部门的人数多?
--90号部门人数
SELECT COUNT(*)
FROM employees
WHERE department_id=90
--比90号部门人数多的部门
SELECT department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*)>(SELECT COUNT(*)
FROM employees
WHERE department_id=90);
--部门名称
Select department_name from departments where department_id=

10、哪些员工的工资,在30号部门和80号部门的平均工资之间
SELECT salary
FROM employees
WHERE salary BETWEEN (SELECT MIN(nsal)FROM (SELECT AVG(salary)nsal FROM employees
WHERE department_id=30 UNION SELECT AVG(salary)nsal FROM employees
WHERE department_id=80))AND (SELECT MAX(nsal)FROM (SELECT AVG(salary)nsal FROM employees
WHERE department_id=30 UNION SELECT AVG(salary)nsal FROM employees
WHERE department_id=80));
列出各个部门中工资最高的员工信息:名字、部门号、薪资
--子查询找出工资部门中工资最高的薪水、部门号
SELECT MAX(salary) salary,department_id,ename
FROM employees where department_id is not null
GROUP BY department_id;

SELECT e1.last_name,e1.salary,e1.department_id
FROM employees e1,(SELECT MAX(salary) salary,department_id
FROM employees where department_id is not null
GROUP BY department_id)e2
where e1.salary =e2.salary
and e1.department_id = e2.department_id

所在部门的平均工资超过5000的员工的名字
--子查询:部门的平均工资超过5000
SELECT department_id,avg(salary) FROM employees
GROUP BY department_id
HAVING AVG(salary)>5000;

SELECT last_name FROM employees
WHERE department_id IN(
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary)>5000);

作业7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
基础篇(使用scott用户的emp、dept表来完成)
查询所有员工姓名的前三个字符
select substr(ename,1,3) from emp;
查询所有员工的姓名,如果包含字母S,则用s替换
select replace(ename,'S','s') from emp;
查询员工名字中不包含字母S 的员工
select ename from emp where ename not like '%S%';
查询员工姓名的第二个字母为M 的员工信息
select * from emp where substr(ename,2,1)='M';
查询员工的姓名和入职日期,并按入职日期从先到后进行排序
select ename,hiredate from emp order by hiredate;
显示所有员工的姓名、工种、工资,按工种降序排序,若工种相同则按工
资升序排序
select ename,job,sal from emp order by job desc,sal;
查询至少有一个员工的部门信息
select distinct deptno from emp;
select * from dept where deptno=any(select distinct deptno from emp);
查询工资比SMITH 员工 工资高的所有员工信息
select * from emp where sal>(select sal from emp where ename='SMITH');
查询各个部门的人数及平均工资
select count(ename),avg(sal),deptno from emp group by deptno;
查询工资SAL高于奖金COMM的员工信息
select * from emp where sal>comm;
查询各月倒数第八天入职的员工信息
select * from emp where hiredate=last_day(hiredate)-8;
显示所有雇员的姓名以及满10年服务年限的日期
select ename,hiredate from emp where months_between(sysdate,hiredate)/12>=10;
查询列出各部门的部门名和部门经理名字
select * from emp;
select distinct e.ename,d.dname from emp e join emp m on(e.empno=m.mgr) join dept d on(e.deptno=d.deptno);
显示雇员姓名,根据其服务年限,将最老的雇员排在最前面
select ename from emp order by months_between(sysdate,hiredate)/12 desc;
分组统计每个部门下,每种职位的平均奖金(包括没奖金的人)和总工资(包括奖金)
select avg(nvl2(comm,comm,0)),sum(nvl2(comm,comm+sal,sal)),job,deptno from emp group by deptno,job;
查询各个部门中不同工种的最高工资;
select max(sal),deptno from emp group by deptno;
查询10号部门员工及其领导的信息;
select e.*,m.* from emp e join emp m on(e.mgr=m.empno) where e.deptno=10 and m.deptno=10;
找出谁是最高领导,将名字按大写形式显示
select upper(ename) from emp where mgr is null;
对23.652四舍五入到小数点前1位
select round(23.652,1) from dual;
在表EMP中查询所有工资高于JONES的雇员姓名、工作和工资
select ename,job,sal from emp where sal>(select sal from emp where ename='JONES');
更新emp表中sal列名为salary

显示所有雇员的姓名,用a替换所有'A'
select replace(ename,'A','a') from emp;
显示只有首字母大写的所有雇员的姓名(两种方法)
select ename from emp
where substr(ename,1,1)=upper( substr(ename,1,1)) and substr(ename,2)=lower( substr(ename,1,1));
向emp 表中插入一条记录,员工名为MARY,号为8000,其他信息与SMITH员工的信息相同;
insert into emp(ename,empno) values('MARY',8000)
求出部门平均薪水最高的部门名称
select max(avg(sal)) from emp group by deptno;
select deptno,avg(sal) from emp group by deptno
having avg(sal)=( select max(avg(sal)) from emp group by deptno);
select dname from dept d join (select deptno,avg(sal) from emp group by deptno
having avg(sal)=( select max(avg(sal)) from emp group by deptno)) a on(d.deptno=a.deptno);

作业8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
以scott用户登陆:
基础:创建以下几张表,并且在表中增加相应的数据
student学生表,表字段:
sno varchar2(10) primary key 学号
sname varchar2(20) 姓名
sage number(2) 年龄
ssex varchar2(5) 性别
create table student (
sno varchar2(10) primary key,
sname varchar2(20),
sage number(2),
ssex varchar2(5)
);
学生表中的数据如下:


select * from student for update;
teacher教师表,表字段:
tno varchar2(10) primary key 教师编号
tname varchar2(20) 教师姓名
教师表中的数据如下:


insert into teacher values('t001','刘阳');
insert into teacher values('t002','湛燕');
insert into teacher values('t003','胡明星');
course 课程表,表字段
cno varchar2(10) 课程编号
cname varchar2(20) 课程名称
tno varchar2(20) 教师编号
其中以cno为主键,tno为外键
课程表中的数据如下
create table course(
cno varchar2(20) primary key,
cname varchar2(20),
tno varchar2(20) references teacher(tno)
);




select * from course for update;

SC成绩表,表字段:
sno varchar2(10) 学生学号
cno varchar2(10) 课程编号
score number(4,2), 成绩
select * from course for update;
create table sc(
sno varchar2(10),
cno varchar2(10),
score number(4,2)
);
成绩表中的数据如下:


select * FROM sc for update;
对sc表添加两个外键。
alter table sc modify sno references student(sno);
alter table sc modify cno references course(cno);


删除没有成绩的学生。
delete student where sno not in(select distinct sno from sc);
更新成绩为58一下的学生成绩为60.
update sc set score=60 where score<58;
查询所有学生的成绩。
select * from sc;
select sname,score from student s,sc c where s.sno=c.sno;
查询每门课程的学生数量
select count(sno),cno from sc group by cno;
对年龄加上1-150的约束。
alter table student modify sage check(sage between 1 and 150);
对性别加上约束。
alter table student modify check(ssex in('男','女')));
对学生表中其他字段设置不能为空。


alter table student modify sno not null;
alter table student modify sname not null;
创建一张包含学生姓名、学生课程名、学生成绩的视图表
conn system/hzn@orcl;
grant create view to scott;
create or replace view scc(sname,cname,score)
as select sname,cname,score from student s,course e,sc c
where s.sno=c.sno and e.cno=c.cno;
select * from scc;
conn system/hzn@orcl;
revoke create view from scott;

10、对SC 成绩表中的学生学号创建索引sno_index
create index sno_index on sc(sno);


提高:
查询“c001”课程比“c002”课程成绩高的所有学生的学号;
select s.sno from sc s,sc c where s.sno=c.sno and s.cno='c001' and c.cno='c002' and s.score>c.score;
查询平均成绩大于60 分的同学的学号和平均成绩;
select sno,avg(score) from sc group by sno having avg(score)>60;
查询所有同学的学号、姓名、选课数、总成绩;
select s.sno,sname,count(cno),sum(score) from sc s,student t where s.sno=t.sno group by s.sno,sname;
查询姓“刘”的老师的个数;
select count(tname) from teacher where tname like '刘%'
5、查询没学过“谌燕”老师课的同学的学号、姓名;
select sno,sname from student where sno not in( select distinct sno from sc
where cno
not in(select cno from course where tno=(select tno from teacher where tname='湛燕')));

6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
select t.sno,t.sname from sc s,student t where s.cno='c001' and t.sno=s.sno
intersect
select t.sno,t.sname from sc s,student t where s.cno='c002' and t.sno=s.sno;
7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;
select sno,sname from student where sno in( select distinct sno from sc
where cno
in(select cno from course where tno=(select tno from teacher where tname='湛燕')));
8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
select s.sno,s.sname from student s,sc c
where c.score<any(select score from sc where cno='c001') and c.cno='c002' and s.sno=c.sno;

9、查询所有课程成绩小于60 分的同学的学号、姓名;
(没成绩的也要算0
select t.sno,t.sname from student t,sc s where s.score<60 and s.sno=t.sno;


10、查询没有学全所有课的同学的学号、姓名
select t.sno,t.sname from sc s,student t where s.cno<>all(select cno from course) and s.sno=t.sno;


11、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数
select t.sname,c.cname,s.score from student t,course c,sc s
where s.score>70 and s.sno=t.sno and s.cno=c.cno;
12、求选了课程的学生人数
select count(distinct sno) from sc;
13、查询全部学生都选修的课程的课程号和课程名
select count(cno),cno from sc group by cno having count(cno)=(select count(distinct sno) from sc);
select c.cno,c.cname
from course c,
(select count(cno),cno from sc group by cno having count(cno)=(select count(distinct sno) from sc)) d
where c.cno=d.cno;


14、统计每门课程的学生选修人数(超过10 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select count(cno),cno from sc group by cno having count(cno)>10 order by count(cno) desc,cno;
15、删除“s002”同学的“c001”课程的成绩
delete sc where sno='s002' and cno='c001';

作业9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
基础:
以下题目在scott用户下做
习题1、
1、建立一个表(tmp_test),表结构与EMP相同,没有任何记录。
CREATE TABLE tmp_test
AS SELECT * FROM emp WHERE 1=2;
删除部门号为30的记录,并删除该部门的所有成员。
alter table emp disable constraint fk_deptno;//使外键失效
DELETE dept WHERE deptno=30;
DELETE emp where deptno=30;
3、tmp_test表中新增列性别SEX,字符型。
ALTER TABLE tmp_test ADD sex char(2);
4、修改新雇员表中的MGR列,为字符型。该列数据不能为空
ALTER TABLE tmp_test
MODIFY mgr VARCHAR2(10) NOT NULL;
5、将tmp_test表中的comm列删除
ALTER TABLE tmp_test
DROP COLUMN comm;
6、将tmp_test表删除
DROP TABLE tmp_test;
7、建立一个视图V_emp,视图包括EMP表的empno、ename、sal,并按sal从大到小排列。
grant create view to scott;//给scott用户授权
CREATE OR REPLACE VIEW v_emp AS
SELECT empno,ename,sal
FROM emp
ORDER BY sal DESC;

在myEMP表中建立ename的唯一性索引。
Create table myEMP as select * from emp;
CREATE UNIQUE INDEX in_myemp_index ON myemp(ename);


习题2、
创建一个用户(hzn),并通过plsql工具去连接,需要写出所有的命令;且在plsql中连上hzn用户后,创建一下三张表;
create user hzn identified by hzn;
conn /as sysdba; à以管理员身份登录
grant create session to hzn;
使用plsql工具,以test用户去连接
grant resource to hzn; à授予权限
grant create table to hzn; à授予用户创建表的权限
conn hzn/hzn@orcl; à以test用户登录系统
学生信息表:
学号:sid char (6) 主键
姓名:sname char/varchar2(10)
性别:ssex char (1) (M/F)
年龄:sage int
电话:sphone varchar2(12)
写法一:
Create table student(
Sid char(6) primary key,
Sname varchar2(10),
Ssex char(1) check (ssex in('M','F')),
Sage int,
Sphone varchar2(12));

写法二:
Create table student(
Sid char(6),
Sname varchar2(10),
Ssex char(1) ,
Sage int,
Sphone varchar2(12),
constraint pk_student_Sid primary key (sid),
constraint pk_student_ssex check (ssex in('M','F'))
);


课程信息表:
编号:cid char (6) 主键
名称:cname char/varchar2(20)
课时:chour int
讲师:tname varchar2(10)
Create table class(
cid char(6) primary key,
cname varchar2(20),
chour int,
tname varchar2(10) );
学生成绩表:
编号:scid int 主键
学号:sid char(6) 外键
课程:cid char(6) 外键
成绩:grade int
Create table score (
Scid int primary key,
Sid char(6),
Cid char(6),
Grade int,
constraint fk_score_sid foreign key (sid) references student(sid),
constraint fk_score_cid foreign key (cid) references
Class(cid));

并向表中构造10个学生的信息,包括成绩信息;以及5门课程信息;
2、再创建ora用户,可以访问学生信息表;
Create user ora identified by ora;
Conn /as sysdba;
Grant connect,resource to ora;
Grant create table to ora;
Grant select on hzn.student to ora;
Grant select on hzn.class to ora;
Grant select on hzn.score to ora;
3、将课程信息表导入到ora用户中
以system用户登录系统
导出:
exp userid=system/manager@orcl tables=(hzn.class) file=d:\class.dmp
导入:
Imp userid=system/manager@orcl file=d:\class.dmp fromuser=hzn touser=ora;

作业10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 1、编写一个程序块,从employees表中显示last_name为“Sullivan”的雇员的薪水和职位。
declare
v_salary number(10,2);
v_job_id varchar2(10);
begin
select salary,job_id into v_salary,v_job_id from employees where last_name='Sullivan';
dbms_output.put_line('薪水是'||v_salary||'职位是'||v_job_id);
end;

编写一个程序块,接受用户输入一个部门号,从departments表中显示该部门的名称与所在位置;如果部门编号不存在,提示:您输入的部门编号不存在,请从新输入,谢谢

declare
v_department_name varchar2(10);
v_location number(10);
begin
select department_name,location_id into v_department_name,v_location from departments where department_id=&no;
dbms_output.put_line('部门的名称是'||v_department_name||' 所在位置在'||v_location);
exception
when no_data_found then
dbms_output.put_line('请重新输入,谢谢');
end;

作业11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
一、用SCOTT用户emp dept等表:

查询emp表,显示薪水大于2000,且工作类别是MANAGER的雇员信息

查询emp表,显示年薪大于30000,工作类别不是MANAGER的雇员信息

查询emp表, 显示薪水在15003000之间,工作类别以“M”开头的雇员信息

查询emp表,显示佣金为空并且部门号为2030的雇员信息(佣金=薪水SAL+津贴COMM)

查询emp表,显示佣金不为空或者部门号为20的雇员信息,要求按照薪水降序排列 (佣金=薪水+津贴)

查询emp表,显示年薪大于30000工作类别不是MANAGER,且部门号不是1040的雇员信息,要求按照雇员姓名进行排列

7.用一个sql语句完成在字符串”hello”左右各添加5个‘*’,使其最终返回’*****hello*****’(禁止使用 | | 或 concate)

8.写一条sql语句返回”abcdefg”的后三位字符“efg”

9.在scott用户下创建表E,表字段及内容如下:


10.要求写一条sql语句统计出如下图所示结果(提示:需要用到sum函数):


提高:

11.查询EMP、DEPT表,输出的列包含员工姓名、工资、部门编号、部门名称、部门地址.

12.请从emp表中查询出所有员工的姓名及员工上司的姓名。

13.此自连接中无法显示没有老板的员工,比如king。请修改该自连接语句,使其可以显示所有员工的姓名及其老板的姓名。

14.查询EMP表,输出每个部门的部门编号及平均工资,并按部门编号降序排列.

15.查询EMP表,输出每个职位的职位名称及平均工资,按平均工资升序排列.

16.查询EMP表,输出每个部门的各个职位的平均工资,并按部门编号升序、平均工资降序排序。

17.使用子查询,找出DEPT表中哪个部门下没有员工?

18.使用子查询,找出那些工资低于各个部门所有员工的平均工资的员工

19.使用子查询,找出那些工资低于其中任意一个部门的平均工资的员工


编写一个存储过程,显示雇员总人数

21.编写一个函数,通过部门编号返回部门名称的函数





二、本题以hr用户登陆,使用员工表(employees)、部门表(departments)、区域表(regions),完成以下题目
各个部门平均、最大、最小工资、人数,按照部门号升序排序
各个部门中工资大于5000的员工人数
各个部门平均工资和人数,按照部门名字升序排序
列出同部门中工资高于1000的员工数量超过2人的部门,显示部门名字、地区名字
哪些员工的工资,高于整个公司的平均工资,列出员工的名字和工资,并按工资降序
哪些员工的工资,介于50号和80号部门平均工资之间
列出各个部门中工资最高的员工的信息:名字、部门号、工资
薪水最高的部门的平均工资是多少?
哪些部门的人数比90号部门的人数多
列出在同一部门共事,入职日期晚但工资高于其他同事的员工:名字、工资、入职日期
11.以首字母大写的方式显示所有员工的姓名(first_name)
12.显示正好为5个字符的员工的姓名
13.显示所有员工姓名(frist_name)的前三个字符
14.显示所有员工的姓名(first_name),用hzn替换所有"A"
15.显示员工的详细资料,按姓名排序
16.显示满10年服务年限的员工的姓名和受雇日期
17.显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面
18.整个公司有多少个领导




三、创建以下几张表,并且在表中增加相应的数据
student学生表,表字段:
sno varchar2(10) primary key 学号
sname varchar2(20) 姓名
sage number(2) 年龄
ssex varchar2(5) 性别
学生表中的数据如下:


teacher教师表,表字段:
tno varchar2(10) primary key 教师编号
tname varchar2(20) 教师姓名
教师表中的数据如下:



course 课程表,表字段
cno varchar2(10) 课程编号
cname varchar2(20) 课程名称
tno varchar2(20) 教师编号
其中以cno、tno创建联合主键
课程表中的数据如下




SC成绩表,表字段:
sno varchar2(10) 学生学号
cno varchar2(10) 课程编号
score number(4,2), 成绩
以学号和课程编号创建联合主键
成绩表中的数据如下:




向SC 表中插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩;


查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

查询不同老师所教不同课程平均分从高到低显示

查询每门课程被选修的学生数

查询出只选修了一门课程的全部学生的学号和姓名

查询男生、女生人数

查询姓“张”的学生名单

查询课程名称为“数据库”,且分数低于60 的学生姓名和分数

查询所有学生的选课情况;

求选了课程的学生人数

查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;





SELECT last_name, job_id, salary,
CASE sex WHEN '男' THEN 1
WHEN '女' THEN 1
ELSE 0
END
"REVISED_SALARY" FROM employees;

作业12

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
8.查询出每一位雇员的姓名、职位、以及领导的姓名。
(最高领导是没有部门的)
select e.ename,e.job,m.ename from emp e,emp m where e.mgr=m.empno;

10.要求查询出每一个雇员的编号、姓名、工资、部门名称、工资在所在公司的工资等级
(没有部门的员工也查出来)
方法一:
select e.empno,e.ename,e.sal,d.dname,s.grade from emp e,dept d, salgrade s
where (e.deptno=d.deptno or e.deptno is null) and (e.sal between losal and hisal);
方法2:
Select e.empno,e.ename,e.sal,d.dname,sg.grade
From emp e
Left Join dept d on e.deptno=d.deptno
Join salgrade sg on e.sal BETWEEN sg.losal AND sg.hisal;

12.查询出每个部门的名称、位置、部门人数、平均工资
(没有认得部门)
select d.dname,d.loc,count(ename),avg(nvl(sal,0)) from emp e
right join dept d
on e.deptno=d.deptno
group by d.dname,d.loc;

8、列出没有对应部门表信息的所有雇员的姓名、工作。
(有部门但不在部门表,没有部门)
select ename,job from emp
where deptno not in(select deptno from dept)
or deptno is null;

9、哪些部门的人数比90 部门的人数多?
select count(ename) from emp
where deptno=90;
select deptno from emp
group by deptno having count(ename)>
(select count(ename) from emp where deptno=90);

select dname from dept
where deptno
in(select deptno from emp
group by deptno having count(ename)>
(select count(ename) from emp where deptno=90));

11、列出各个部门中工资最高的员工信息:名字、部门号、薪资
select e.ename,e.deptno,e.sal
from emp e,(select max(sal) sal,deptno from emp group by deptno) a
where e.deptno=a.deptno and e.sal=a.sal;

7.用一个sql语句完成在字符串”hello”左右各添加5个‘*’,使其最终返回’*****hello*****’(禁止使用 | | 或 concate)
赞赏一下吧~