sql 知识点整理

SQL 基础

数据操纵语言: DML(Data Manipulation Language)

select insert update delete merge

数据库模式定义语言: DDL(Data Definition Language)

create alert drop truncate

事务控制语言: TCL(Transaction Control Language)

commit rollback savepoint

数据控制语言: DCL (Data Control Language)

grant revoke

数据类型

1.字符类型 2.数字类型 3.日期类型 4.二进制及大文本数据

常用数据类型转换

参考:Oracle 数据类型转换

虚表 dual

1、查看当前用户,可以在 SQL Plus中执行下面语句 select user from dual;
2、用来调用系统函数

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;--获得当前系统时间
select SYS_CONTEXT('USERENV','TERMINAL') from dual;--获得主机名
select SYS_CONTEXT('USERENV','language') from dual;--获得当前 locale
select dbms_random.random from dual;--获得一个随机数

3、得到序列的下一个值或当前值,用下面语句

select your_sequence.nextval from dual;--获得序列your_sequence的下一个值
select your_sequence.currval from dual;--获得序列your_sequence的当前值

4、可以用做计算器 select 7*9 from dual;

常用函数

参考:Oracle数据库常用函数

集合查询操作

参考:彻底理解Oracle中的集合操作与复合查询

子查询

参考:ORACLE复杂查询之子查询

高级用法

-- 创建表
create table table> as select * from <exists table>
-- 插入表
insert into table2(f1, f2...) select v1,v2... from table1
-- merge into 用法
merge into 表A
using xxx
on 和表A关联
    when matched then
        update set...
    when not matched then
        insert(...) value(...)
-- 递归查询
select * from table1 where xxx
start with (从某个节点开始)
connect by prior(子节点id和父节点pid直接的关系需要)
分析函数
  • sum(xxx) over()
  • sum(xxx) over(order by xxx)
  • sum(xxx) over(partitjion by xxx order by xxxx)
  • group by rollup xxx
  • group by cube xxx
  • grouping(xxx)
  • rank() densc_rank()

  • for update
  • for update nowait
  • for update wait 5
  • for update skip locked

索引

索引分为B树索引和位图索引。
参考:各种Oracle索引类型介绍

索引碎片

首先要对索引进行分析:

analyze index ind_1 validate structure;  ind_1为你自己建立的索引

分析后查询几个主要的参数判断是否需要整理碎片:

select name,HEIGHT,PCT_USED,DEL_LF_ROWS/LF_ROWS from index_stats;

这里主要通过几个标准来判断是否需要整理碎片:
1.HEIGHT>=4
2.PCT_USED<50%
3.DEL_ROWS/LF_ROWS>0.2
如果查询到的值符合以上三种情况的任意一种,就说明我们需要进行碎片整理工作了。
碎片整理语句:

alter index ind_1 rebuild [online] [tablespace name];

一般情况下都是要加上online参数的,不必加tablespace name。

物化视图

参考:
1.Oracle物化视图的一般使用
2.fast物化视图的刷新方式

同义词

参考:oracle同义词

参考:Oracle之Dblink

分区

参考:oracle分区技术提高查询效率

你可能感兴趣的:(数据库)