Operator
+, - , *, /, () parentheses
Nul values在算术表达式中,结果为null
desc tablename 查看table structure
Comparision Conidtions
=, > , >=, <, <=, <>
between ... and ...
In(set)
like % _
is null
Logical Conidtions
AND, OR, NOT
ASC, DESC
1). Character Functions
lower
upper
initcap: 单词首字母大写
concat
substr
length
instr
lpad(salary,10,'*') *****24000
rpad
trim('H' from 'HelloWorld') elloWorld
replace
2). Number Functions
round(45.926,2) 45.93
trunc(45.926,2) 45.92
mod(1600,300) 100
3). Date Functions
default format: DD-MON-RR
select sysdate from dual;
MONTHS_BETWEEN
ADD_MONTHS
NEXT_DAY
LAST_DAY
ROUND
TRUNC
4). Conversion Functions
(1) char to number
(2) char to date
(3) number to char
(4) date to varchar
to_char(date,'format_model')
YYYY, YEAR, MM, MONTH, MON,DY,DAY,DD
HH24:MI:SS AM/PM
to_char(number,'format_model')
9: Represents a number
0: Forces a zero to be displayed
$: Places a floating dollar sign
L: Use the floating local currency symbol
.: Prints a decimal point
,: Prints a thousand indicator
to_number(char,'format_model')
to_date(char,'format_model')
5). General Functions
NVL(expr1,expr2)
NVL2(expr1,expr2,expr3)
NULLIF(expr1,expr2)
COALESCE(expr1,expr2,...,exprn)
6). Conditional Functions
CASE expr WHEN expr1 THEN return_expr1
WHEN expr2 THEN return_expr2
... ...
ELSE else_expr
END
DECODE (exp, search1, result1, search2,result2...,default)
左外连接,(+)在右边
自连接
left outer join on ...
right outer join on ...
full outer join
(1) Sequence
Create sequence se1
[increment by n]
[start with n]
[maxvalue n | nomaxvalue]
[minvalue n | nominvalue]
[cycle | nocycle]
[cache n | nocache]
数据字典:user_sequences
se1.nextvalue
se1.currval
alter sequence xxx ...
drop sequence xxx
(2) Index
create index xxx on table (col1,col2,...)
When to Create an Index
(1) A column contains a wide range of values
(2) One or more columns are frequently used together in a where clause or a join condition
(3) The table is large and most queries are expected to retrieve 20% of whole rows.
When not to create an Index
The indexed columsn are referenced as part of an expression
数据字典:user_indexes, user_ind_columns
function-Based Indexes
create index xxx idx on table(upper(au_fname)
(3) Synonyms
create [public] synonym xxx for object;
create users
create user scott identified by tiger;
grant privilege,... to user|role,PUBLIC...
create session
create table
create sequence
create view
create procedure
create role manager;
grant create table,create view to manager;
grant manager to usera,userb...;
alter user scott identified by lion;
对象权限
grant select(col1,col2) on authors to kxf;
with grant options; 允许传递
revork ... from ...
cascade constraints; 取消传递的权限
Database Links:
create public database link "linkname" using 'sales'
CREATE DATABASE LINK数据库链接名CONNECT TO 用户名 IDENTIFIED BY 密码 USING ‘本地配置的数据的实例名’;
查询远端数据库里的表
SELECT …… FROM 表名@数据库链接名;
union(排序,distinct)/union all
intersect(distinct)
minus (distinct)
需要显示的指定Order by
TZ_OFFSET
CURRENT_DATE
alter session set nls_data_format = 'DD MON YYYY HH24:MI:SS'
alter session set time_zone='-5:0'
select sessiontimezone, current_date from dual;
CURRENT_TIMESTAMP
select current_timestamp, localtimestamp from dual;
dbtimezone, sessiontimezone
group by [rollup] [cube]
grouping(expr) 结果是0,1 判断是不是聚合的结果
grouping sets example:
select departmetn_id,job_id,manager_id,avg(salary)
from employees
group by grouping sets
((department_id,job_id),(job_id,manager_id));
rollup(a, (b,c))
where (a,b) in (select a,b...)
关联子查询: where exists
update,delete中也可以使用子查询
with Clause example:
with xxx as (select ...)
xxx以后就可以直接用了
Walking the Tree
... ...
from employees
start with employee_id = 101
connect by prior manager_id = employee_id;
Insert All
into table1 values(xxx,xxx,xxx)
into table2 values(xxx,xxx,xxx)
select xxx,xxx,xxx from ...
Insert All
When sal > 1000 then
into table1 values(xxx,xxx,xxx)
When mgr > 200 then
into table2 values(xxx,xxx,xxx)
select xxx,xxx,xxx from ...
Insert First
Pivoting Insert: 旋转插入
把一个表的一行分别插入到多个表的每一行
扩展表: 数据放在文本文件里面
create diretory xxx as 'c:\oracle'