一. 引言
1. 概念
数据库是一种存储管理数据的软件, 全称RDBMS(关系数据库管理系统)
常用数据库 — oracle mysql SqlServer DB2
2. 数据库里的基本概念
1) 表(table) — 实际负责存储数据的单元(二维表)
2) 行(row ) — 表示了一组具体的业务数据 , 也称为记录(recoder)
3) 列(column) — 描述了业务数据里的一个具体的属性 , 也称为”字段”
– 主键(primary key) : 唯一标识表里的一条记录 , 非空 唯一
– 外键(foreign key) : 用来体现两张表之间关系 , 值必须来自于另外一张表的主键
3. oracle的工作方式 — C/S(控制台/数据库)
二. 简单查询( select )
语法
select 字段1,字段2...from 表名;
1. 指定查询字段
-- 请打印员工的编号,姓名(last_name),工资,所在部门编号
select employee_id , last_name , salary , department_id from employees;
2. 查询所有字段
--- 查询所有
select * from employees;
3. 数学运算
-- 查询员工的编号,姓名,以及年薪
select employee_id,last_name, salary*12 from employees;
注意:
1) 数字类型可以应用 + - * /
2) 日期类型可以应用 + - , 计算时以”天”为单位
3) 字符串类型不可以 + 运算
4. 字段别名
1) 别名语法要求:紧跟在字段名后空格接着写,如果含有特殊字符或者需要区分别名的大小写,可以使用双引号
2)案例:
-- 查询员工的编号,姓名,以及年薪
select employee_id id,last_name name, salary*12 "anual salary" from employees;
[外链图片转存失败(img-79px33Yv-1562823476502)(assets/1562141262207.png)]
5. 字符串拼接
--- 请打印员工的完整姓名
select last_name|| ‘-’ ||first_name "name" from employees;
注意: 在sql命令里出现的字符串字面值 或者 日期字面值 , 必须用单引号引起来
6. 结果数据去重
1) 语法要求: select distinct 字段名1,字段名2 … from 表;
2)案例
-- 请打印公司里有哪些职位
select distinct job_id from employees;
三. 排序
语法:
select ... from...order by 字段名 asc(升序默认)|desc(降序);
1. 单排序条件
-- 请打印员工信息,并按照工资降序排列
-- 1. 按照字段名排序
select * from employees order by salary desc ;
-- 2. 按照别名排序
select employee_id,last_name name,salary from employees order by name;
-- 3. 按照结果里的字段下标来排序
select employee_id,last_name name,salary*12+10000 “aa bbb” from employees order by 3;
2. 多排序条件
--- 先按部门升序排列,如果部门一样在按照工资降序排列
select * from employees order by department_id asc, salary desc ;
**注意:null空值在oracle数据库中默认为最大值 **
四. 条件判断 ( where ) ---- 逐行筛选
语法:
select … from…where 条件筛选 order by…;
1. 等值条件判断 = != 【重点】
-- 请打印30部门的员工信息
select * from employees where department_id = 30;
-- 请打印’King’的详细信息
select * from employees where last_name=’King’ ;
--对于字面值的判断区分大小写
2. 一般条件判断 > >= < <= and or 【重点】
-- 请打印工资在5000到10000之间的员工信息
select * from employees where salary>=5000 and salary<=10000;
3. 范围查询 : between 小值 and 大值 (包括边界值) 【了解】
~~~sql
– 请打印工资在5000到10000之间的员工信息
select * from employees where salary between 5000 and 10000;
~~~
4. 枚举查询 : in( value1, value2 , …… ) not in(…) 【了解】
-- 请打印30,50,70部门的员工信息
select * from employees where department_id=30 or department_id=50 or department_id=70 ;
select * from employees where department_id in( 30,50,70) ;
5. 空值处理 : is null , is not null 【重点】
-- 请打印部门编号为null记录
select * from employees where department_id = null ; --error
-- 注意: 如果表达式里含有null值,则结果一定为null
select * from employees where department_id is null;
6. 模糊查询 — like ,not like 【重点】
-- 请打印last_name是以’K’开头的员工信息
select * from employees where last_name like ‘ K%’ ;
-- 请打印last_name是以‘K_’开头的员工信息
select * from employees where last_name like ‘\K_%’ escape ‘\’;
五. 函数 — oracle提供的功能函数( 单行函数 组函数)
1. 单行函数 : 作用于指定表里的每一行数据执行一次 , 产生一个结果数据
1)基础概念
-- abs(num) : 计算num的绝对值
select abs(salary) from employees; --作用于表里的107行记录,得到107个执行结果
select abs(-2) from employees; --107个执行结果(表里107行)
-- dual:oracle提供的“单行单列表”,只是为了维护select语句的完整性
select abs(-2) from dual;
-- sysdate,systimestamp : 代表当前系统时间
select sysdate from dual; --注意:oracle里默认的日期格式为“dd-mon-rr”
2)to_char() 函数
--请打印当前系统时间( 显示四位年,月日,小时分钟秒 , 星期)
select to_char(sysdate,’yyyy-mm-dd,day,hh24:mi:ss’) from dual;
--可以截取日期中的某一个组成部分
select to_char(sysdate,’day’) from dual;
--请打印员工的编号,姓名,工资以及入职月份
select employee_id,last_name,salary,to_char(hire_date,’mm’) “月份” from employees order by 4 ;
--请用两种方式打印1997年入职的员工信息
select * from employees where hire_date like ‘%97’;
select * from employees where to_char(hire_date,’yyyy’) = 1997 ;
3)to_date() 函数
–请打印 02-03-04 是星期几
select to_char(to_date(’02-03-04’,’’'dd-mm-rr’), ‘day’) from dual;
~~~
2. 组函数 — 作用于每一组数据产生一个结果
--请打印公司的平均工资,最高工资,最低工资
select avg(salary) , max(salary),min(salary) from employees;
-- 1. count(字段) : 统计指定字段里非空值的个数
select count(department_id) from employees;
-- 2. count(*) : 统计结果里非空行的数量 , 其他等价写法【count(1) count(0)】
-- 请打印1997年入职的员工人数
select count(*) from employees where to_char(hire_date,’yyyy’) = 1997 ;
六. 分组语句 — group by
-- group by last_name 按照姓分组( 姓一样的分到一组 )
-- group by last_name,first_name 按照姓名联合分组( 姓名完全一致)
--请打印部门编号,以及部门的最高工资
-- 思路:
-- 1) 来源表 --- from employees 107行
-- 2) 筛选条件 ---- 没有 107行
-- 3) 分组条件 ---- group by department_id
-- 合并:
select department_id,max(salary) from employees group by department_id;
--请统计1997年各部门入职的员工人数
-- 思路:
-- 1) from employees 107行
-- 2) where to_char(hire_date,’yyyy’)=1997 28行
-- 3) group by department_id
-- 4) select department_id , count(*)
-- 合并:
select department_id,count(*) from employees where to_char(hire_date,’yyyy’)=1997
group by department_id ;
--请统计1997年各月入职的员工人数
-- 思路:
-- 1) from employees 107行
-- 2) where to_char(hire_date,’yyyy’)=1997 28行
-- 3) group by to_char(hire_date,’mm’)
-- 4) select to_char(hire_date,’mm’) , count(*)
-- 合并:
select to_char(hire_date,’mm’),count(*) from employees
where to_char(hire_date,’yyyy’)=1997 group by to_char(hire_date,’mm’) ;
七. 分组数据的条件筛选 — having
--请打印部门编号,人数 ( 显示人数大于2的结果 按人数排序)
-- 思路:
-- 1) from employees 2) where 没有 3) group by department_id
-- 4) having count() >2 5) select department_id,count() 6) order by 2
-- 合并 :
select department_id,count(*) from employees group by department_id
having count(*)>2 order by 2;
select department_id , avg(salary) from employees where department_id in(30,50)
group by department_id ;
select department_id , avg(salary) from employees group by department_id
having department_id in(30,50) ;