Oracle数据库知识小结
2019独角兽企业重金招聘Python工程师标准>>>
1. 对于日期型数据, 做 *, / 运算不合法
2. 包含空值的数学表达式的值都为空值
3. oracle 中连接字符串使用 "||", 而不是 java 中的 "+"
4. 日期和字符只能在单引号中出现.
5. WHERE 子句紧随 FROM 子句
6. 日期必须要放在单引号中, 且必须是指定的格式
7. 查询 LAST_NAME 中有 'o' 字符的所有员工信息.
select *from employees where last_name like '%o%'
8. 查询 LAST_NAME 中第二个字符是 'o' 的所有员工信息.
select *from employees where last_name like '_o%'
9. 查询 LAST_NAME 中含有 '_' 字符的所有员工信息
使用 escape 说明转义字符.
select *from employees where last_name like '%\_%' escape '\'
10. ORDER BY:desc(降序),asc(升序,默认的)
1). 若查询中有表达式运算, 一般使用别名排序
2). 按多个列排序: 先按第一列排序, 若第一列中有相同的, 再按第二列排序.
11. 打印出 "2009年10月14日 9:25:40" 格式的日期和时间.
select to_char(sysdate, 'YYYY"年"MM"月"DD"日" HH:MI:SS')
from dual
注意: 使用双引号向日期中添加字符
12. 格式化数字: 1234567.89 为 1,234,567.89
select to_char(1234567.89, '999,999,999.99')
from dual
20. 字符串转为数字时
1). 若字符串中没有特殊字符, 可以进行隐式转换:
select '1234567.89' + 100
from dual
2). 若字符串中有特殊字符, 例如 '1,234,567.89', 则无法进行隐式转换, 需要使用 to_number() 来完成
select to_number('1,234,567.89', '999,999,999.99') + 100
from dual
21. 对于把日期作为查询条件的查询, 一般都使用 to_date() 把一个字符串转为日期, 这样可以不必关注日期格式
select last_name, hire_date
from employees
where hire_date = to_date('1998-5-23', 'yyyy-mm-dd')
22. 转换函数: to_char()把日期转为字符串
select to_char(sysdate, 'yyyy-mm-dd')
from dual
25. 查询部门号为 10, 20, 30 的员工信息, 若部门号为 10, 则打印其工资的 1.1 倍, 20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数
--使用 case-when-then-else-end
select last_name, department_id, salary, case department_id when 10 then salary * 1.1
when 20 then salary * 1.2
when 30 then salary * 1.3
end new_sal
from employees
where department_id in (10, 20, 30)
--使用 decode
select last_name, department_id, salary, decode(department_id, 10, salary * 1.1,
20, salary * 1.2,
30, salary * 1.3
) new_sal
from employees
where department_id in (10, 20, 30)
26. 多表连接查询时, 若两个表有同名的列, 必须使用表的别名对列名进行引用, 否则出错!
27. 查询出 last_name 为 'Chen' 的 manager 的信息. (员工的 manager_id 是某员工的 employee_id)
0). 例如: 老张的员工号为: "1001", 我的员工号为: "1002",
我的 manager_id 为 "1001" --- 我的 manager 是"老张"
1). 通过两条 sql 查询:
select manager_id
from employees
where lower(last_name) = 'chen' --返回的结果为 108
select *
from employees
where employee_id = 108
2). 通过一条 sql 查询(自连接):
select m.*
from employees e, employees m
where e.manager_id = m.employee_id and e.last_name = 'Chen'
3). 通过一条 sql 查询(子查询):
select *
from employees
where employee_id = (
select manager_id
from employees
where last_name = 'Chen'
)
28. 左外连接和右外连接
select last_name, e.department_id, department_name
from employees e, departments d
where e.department_id = d.department_id(+)
select last_name, d.department_id, department_name
from employees e, departments d
where e.department_id(+) = d.department_id
理解 "(+)" 的位置: 以左外连接为例, 因为左表需要返回更多的记录,
右表就需要 "加上" 更多的记录, 所以在右表的链接条件上加上 "(+)"
注意: 1). 两边都加上 "(+)" 符号, 会发生语法错误!
2). 这种语法为 Oracle 所独有, 不能在其它数据库中使用.
29. SQL 99 链接 Employees 表和 Departments 表
1).
select *
from employees join departments
using(department_id)
缺点: 要求两个表中必须有一样的列名.
2).
select *
from employees e join departments d
on e.department_id = d.department_id
3).多表链接
select e.last_name, d.department_name, l.city
from employees e join departments d
on e.department_id = d.department_id
join locations l
on d.location_id = l.location_id
30. SQL 99 的左外连接, 右外连接, 满外连接
1).
select last_name, department_name
from employees e left join departments d
on e.department_id = d.department_id
2).
select last_name, department_name
from employees e right join departments d
on e.department_id = d.department_id
3).
select last_name, department_name
from employees e full join departments d
on e.department_id = d.department_id
31. 子查询注意:
1). 子查询要包含在括号内
2). 将子查询放在比较条件的右侧
3). 在 SELECT 列表中所有未包含在组函数中的列都应该包含在 GROUP BY 子句中
例: 按 department_id 进行分组
select department_id, avg(salary)
from employees
group by department_id
32. 利用子查询创建表 myemp, 该表中包含 employees 表的 employee_id(id), last_name(name), salary(sal), email 字段
1). 创建表的同时复制 employees 对应的记录
create table myemp
as
select employee_id id, last_name name, salary sal, email from employees
2). 创建表的同时不包含 employees 中的记录, 即创建一个空表
create table myemp
as
select employee_id id, last_name name, salary sal, email from employees where 2 = 3
33. 对现有的表进行修改操作
1). 添加一个新列
ALTER TABLE myemp ADD(age number(3))
2). 修改现有列的类型
ALTER TABLE myemp MODIFY(name varchar2(30));
3). 修改现有列的名字
ALTER TABLE myemp RENAME COLUMN sal TO salary;
4). 删除现有的列
ALTER TABLE myemp DROP COLUMN age;
5). 添加约束(主外键约束)
ALTER TABLE EMPLOYEES ADD
CONSTRAINT DEPARTMENT_ID_FK FOREIGN KEY(DEPARTMENT_ID) REFERENCES DEPARTMENTS(DEPARTMENT_ID)
34. 清空表(截断: truncate), 不能回滚!!
35.
1). 创建一个表, 该表和 employees 有相同的表结构, 但为空表: create table emp2 as select * from employees where 1 = 2;
2). 把 employees 表中 80 号部门的所有数据复制到 emp2 表中: insert into emp2 select * from employees where department_id = 80;
36. 定义非空约束
1). 非空约束只能定义在行级.
2). 不指定约束名
create table emp2 (name varchar2(30) not null, age number(3));
3). 指定约束名
create table emp3(name varchar2(30) constraint name_not_null not null, age number(3));
37. 唯一约束
1). 行级定义
①. 不指定约束名
create table emp2 (name varchar2(30) unique, age number(3));
②. 指定约束名
create table emp3 (name varchar2(30) constraint name_uq unique, age number(3));
2). 表级定义: 必须指定约束名
①. 指定约束名
create table emp3 (name varchar2(30), age number(3), constraint name_uq unique(name));
38. 外键约束
1). 行级定义
①. 不指定约束名
create table emp2(
emp_id number(6),
name varchar2(25),
dept_id number(4) references dept2(dept_id))
②. 指定约束名
create table emp3(
emp_id number(6),
name varchar2(25),
dept_id number(4) constraint dept_fk3 references dept2(dept_id))
2). 表级定义: 必须指定约束名
①. 指定约束名
create table emp4(
emp_id number(6),
name varchar2(25),
dept_id number(4),
constraint dept_fk2 foreign key(dept_id) references dept2(dept_id))
39 约束需要注意的地方
1). ** 非空约束只能定义在列级
2). ** 唯一约束的列值可以为空
3). ** 外键引用的列起码要有一个唯一约束
40. 建立外键约束时的级联删除问题:
1). 级联删除:
create table emp2(
id number(3) primary key,
name varchar2(25) unique,
dept_id references dept2(dept_id) on delete cascade)
2). 级联置空
create table emp3(
id number(3) primary key,
name varchar2(25) unique,
dept_id references dept2(dept_id) on delete set null)
41. 查询员工表中 salary 前 10 的员工信息.
select last_name, salary
from (select last_name, salary from employees order by salary desc)
where rownum <= 10
说明: rownum "伪列" ---- 数据表本身并没有这样的列, 是 oracle 数据库为每个数据表 "加上的" 列. 可以标识行号.
默认情况下 rownum 按主索引来排序. 若没有主索引则自然排序.
注意: **对 ROWNUM 只能使用 < 或 <=, 而是用 =, >, >= 都将不能返回任何数据.(用别名)
42. 查询员工表中 salary 10 - 20 的员工信息.
select *
from(
select rownum rn, temp.*
from (
select last_name, salary
from employees e
order by salary desc
) temp
)
where rn > 10 and rn < 21
43. 对 oralce 数据库中记录进行分页: 每页显示 10 条记录, 查询第 5 页的数据
select employee_id, last_name, salary
from (
select rownum rn, employee_id, last_name, salary
from employees
) e
where e.rn <= 50 and e.rn > 40
注意: **对 oracle 分页必须使用 rownum "伪列"!
select employee_id, last_name, salary
from (
select rownum rn, employee_id, last_name, salary
from employees
) e
where e.rn <= pageNo * pageSize and e.rn > (pageNo - 1) * pageSize
对MySql的分页: select * from employees limit (pageNo - 1) * pageSize,pageSize
44. 序列通常用来生成主键:
INSERT INTO emp2 VALUES (emp2_seq.nextval, 'xx', ...)
45.
序列的创建
create sequence seq_newsId
increment by 1
start with 1
maxvalue 999999999;
得到序列的SQL语句
select seq_newsid.nextval from sys.dual;
删除序列的SQL
DROP SEQUENCE seq_newsId;
转载于:https://my.oschina.net/u/1453975/blog/204963
Oracle数据库知识小结相关推荐
- SQL那些事儿(九)--oracle数据库知识体系
十月份花了一个星期的时间看了下oracle数据库,一直没有总结. 今儿抽空总结了下oracle的知识点,其实不同数据库基本类似,自己也用过sqlserver.mysql.sqlite.postsql, ...
- Oracle数据库知识要点
一.卸载安装(来自百度经验) 完全卸载: 1. 停止相关服务 2. 运行Universal Installer,卸载产品 3. 清理注册表 4. 重启电脑,删除目录(Oracle文件夹和app文件夹) ...
- oracle 存储同步,Oracle数据库知识——存储过程篇
在线QQ客服:1922638 专业的SQL Server.MySQL数据库同步软件 存储过程是一组用于完成特定功能的SQL语句,该语句已编译并存储在数据库中.用户通过指定存储过程的名称并提供参数(如果 ...
- 关系型数据库知识小结
一.基础术语 DML(data manipulation language): 如SELECT.UPDATE.INSERT.DELETE,主要用来对数据库里的数据进行操作的语言 DDL(data de ...
- oracle 与indexc函数,oracle索引知识小结
索引概述 索引是关系数据库中用于存放每一条记录的一种对象,主要目的是加快数据的读取速度和完整性检查.建立索引是一项技术性要求高的工作.一般在数据库设计阶段的与数据库结构一道考虑.应用系统的性能直接与索 ...
- mysql 9.0创建数据库_数据库基础学习——MySQL数据库知识小结(9)
1 MySQL 中的约束 1.1约束类型 • 非空约束(not null) • 唯一性约束(unique) • 主键约束(primary key) PK • 外键约束(foreign key) FK ...
- oracle的约束什么作用,Oracle数据库知识之约束
1.先了解什么是约束 约束是表级的强制约定.有5种约束:not null , unique, primary key ,foreign key, check 2.表级约束和列级约束 作用范围: 列级约 ...
- Oracle数据库的备份与恢复技术
Oracle数据库的备份与恢复技术 郑永生 (华能德州电厂信息中心,德州 253024) 摘 要 确保数据安全是每一位数据库DBA的必要工作,根据数据的重要性和软硬件条件制定可行的备份恢复方案 ...
- 国考最热岗位报录比20602:1?还是数据库知识挑战赛适合我
什么?212万人报名2022年国考? 最热门的西藏阿里地区某岗位 报录比竟然高达20602:1? 千军万马过独木桥 都是勇士 当然,网上国考气氛组也不能少 有人对两万里挑一的人充满了好奇 有人担心考试 ...
最新文章
- 使用.net调用java的Web Services
- redis cluster集群模式总结
- Unknown initial character set index '255' received from server.
- 光流 | 金字塔迭代(Iterative Pyramidal)LK光流算法(Matlab源代码)
- 国外方案 组件化_网页webp解决方案
- ZYAR20A 亚克力2驱 蓝牙 298寻迹避障机器人 —— 小车黑线循迹实验 四驱
- Mac远程连接服务器
- Java中七个潜在的内存泄露风险,你知道几个?
- Python小项目(一)-----------计算体脂率
- php转jsp,阿里西西Html多功能代码转换器(html转js/jsp/php工具)
- ROS 2 Humble Hawksbill 环境基础
- JUR 项目评级:BB ,展望稳定 | TokenInsight
- python界面显示图片更换背景_用python制作一个简陋的证件照换底色的桌面控制台应用...
- JDK8 下载与安装教程,超简单版(Windows)
- 使用Entitas构建游戏框架(一)
- 百万投资血本无归,细数外汇资金盘《云腾科技》的八宗罪
- 崔莺莺要明媒正娶,张生你怎么看?
- 图神经网路入门(1)
- 马加爵 谭卓 牛顿等人在天堂的对话
- [ORACLE] 免安装的Oracle客户端 - Oracle Instant Client介绍与配置
热门文章
- 千年服务器角色信息在哪个文件,千年服务端文件详解
- 神策数据曹犟:神策数据产品矩阵与技术体系
- Haproxy配置文件详解
- lintcode:形状工厂
- MySQL性能调优的14板斧
- How to publish in an open world?
- PEEL!!!!!!!! it is the easiest way for English arguments
- 剑桥大学Raven系统
- 洗被套的时候洗衣机里面不要再放其他东西
- 避免资源放在收藏夹里面吃灰的方法(如从typora上直接能导出html并且无缝连接到微信公众号的神奇网站)