Oracle笔记(一)

一、数据库

1. 概念
数据库是一种存储管理数据的软件 , 全称 RDBMS( 关系数据库管理系统 )
常用数据库 --- oracle mysql SqlServer DB2
2. 数据库里的基本概念
1) (table) --- 实际负责存储数据的单元 ( 二维表 )
2) (row ) --- 表示了一组具体的业务数据 , 也称为记录 (recoder)
3) (column) --- 描述了业务数据里的一个具体的属性 , 也称为 字段
-- 主键 (primary key) : 唯一标识表里的一条记录 , 非空 唯一
-- 外键 (foreign key) : 用来体现两张表之间关系 , 值必须来自于另外一张表的主键

 二、简单查询

语法:select字段名1,字段2,字段3  from 表名

1.指定查询字段

-- 请打印员工的编号 , 姓名 (last_name), 工资 , 所在部门编号
select employee_id , last_name , salary , department_id from employees;

2.查询所有字段

select * from表名;

 3.数学运算

--查询员工编号,姓名,以及年薪

select emp_id, name,  salary*12 from emp;

 注意:

1)数字类型可以应用+-*/

2)日期类型可以应用+ -,计算以“天”为单位

3)字符串类型不可以+ 运算

4.字段别名

1)语法要求:紧跟在字段名后空格接着写,如果含有特殊字符或者需要区分别名大小写,可以使用双引号

2)案列:

--查询员工编号,姓名,以及年薪

select emp_id id,last_name name,salary*12 "anual salary" from emp;

 5.字符串拼接

--- 请打印员工的完整姓名

        select last_name|| ‘-’ ||first_name "name" from employees;

注意:在sql命令里出现的字符面值 或者 日期字面期 ,必须

 6.结果数据去重

1)语法要求:select ditstnct 字段名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[ 重点]

  -- 请打印工资在500010000之间的员工信息

select * from employees where salary>= 5000 and salary<= 10000 ;

 3.范围查询:between 小值 and 大值 (包括边界值) 【了解】

-- 请打印工资在 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 【重点】

语法:where 字段 like ‘格式字符串’;

格式字符串组成 :字符串面值 + 通配符(%-0~n个字符 或者 _ --1个字符)

  ----张%  ----张_

-- 请打印 last_name 是以 ’K’ 开头的员工信息
select * from employees where last_name like ‘ K%’ ;
-- 请打印 last_name 是以 ‘K_’ 开头的员工信息
select * from employees where last_name like ‘K_%’ escape ‘\’;

 转义符:使得紧跟在它之后就按普通字符处理,不做任何特殊用途,大多数操作系统默认的都是“\”,但是oracle没有默认的,使用时需要声明

五、函数--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”

 2to_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 ;

 3to_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 ) ;

八、总结

语法:select ....from .... where... group by ... having...order by...;

顺序:

        1. from ---- 确定来数据来源表

         2. where ----- 对来源表里的数据筛选

         3. group by ----对瞒足要求的数据按条件分组

         4.having --- 对分组后数据再次进行筛选

          5.order by ----- 按照一定顺序显示统计好的数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值