Oracle数据库实例教程

 

1. Writing Basic SQL Select Statements

Operator

+, - , *, /, ()  parentheses

 

Nul values在算术表达式中,结果为null

 

desc tablename 查看table structure

 

2. Restricting and Sorting Data

Comparision Conidtions

=, > , >=, <, <=, <>

between ... and ...

In(set)

like     %  _

is null

 

Logical Conidtions

AND, OR, NOT

 

ASC, DESC

 

 

3. Single-Row Functions

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)

 

4. Display Data from Multiple Tables

左外连接,(+)在右边

 

自连接

 

left outer join on ...

right outer join on ...

full outer join

 

 

5. Aggregating Data Using Group Functions

 avg
count
max
min
sum
stddev:标准偏差
variance:标准方差
 
Having 子句
  

 

6. Subqueries

 Single-Row Subqueries
 =,>,>=,<,<=,<>
 Multiple-Row Subqueries
 In, Any, All

7. Producing Readable Output with iSQL*Plus

select * from fmvat where unid = &a;
替换变量参数
SQL> define a=7
SQL> select * from fmvat where unid = &a;
SQL> define
查看已定义变量
SQL>undefine
SQL>select &&b from fmvat where unid = &b; 避免重复定义
SQL> set verify on
SQL> select &&b from fmvat where unid = &b;
&b: 7
&b: 7
         7
----------
         7
SQL> set system_variable value
SQL> set echo on
SQL> show all       显示所有环境变量
SQL> show echo   显示当前环境变量
SQL> help show(set)

8. Manipulating Data

 default 值: create table的时候定义
insert into test2 values(1, default)
update test2 set xxx=default
merge into tablea : 没有就insert,有就update
commit
savepoint
rollback to savepoint

9. Creating and Managing Tables

select * from user_tables;
select * from user_objects;
select * from user_catalog;
select * from dba_tables;
select * from dba_objects;
select * from dba_catalog;
select * from all_tables;
select * from all_objects;
select * from all_catalog;
数据类型
Varchar2(size)
char(size)
number(p,s)
Date
LONG
CLOB
RAW and LONG RAW
BLOB
BFILE
bfile字段实际的文件存储在文件系统中,字段中存储的是文件定位指针.bfile对oracle来说是只读的,也不参与事务性控制和数据恢复
CREATE or REPLACE DIRECTORY MyDir as 'c:\\bfiles'
GRANT READ ON DIRECTORY MyDir  TO scott;
CREATE TABLE MyBFileTable(col1 number, col2 BFILE)
INSERT INTO MyBFileTable values ('2', BFILENAME('MyDir',"+ "'MyFile.jpg'))
ROWID         64 base number
Timestamp
create table tabelname[col1,col2...] as subquery;
alter table tablename add (column datatype defaut expr1, ...;
alter table tablename modify  (column datatype default expr1,...;
alter table tablename drop column;
alter table tablename set unused(column);
alter table tablename drop unused columns;
drop table tablename;
rename dataobject to anothername;
truncate table tablename;    DDL
comment on table/view xxx is 'xxx xxx xxx';
all_col_comments
user_col_comments
all_tab_comments
user_tab_comments
comment on column FMVAT.VATCODE is 'VAT code ';

10. Including Constraints

Not null
Unique
Primary Key
Foreign Key
Check
create table test1
(id int not null,
lname varchar(20),
constraint  uk_test1_1 unique(lname,fname));
alter table test add constraint primary key(unid);
alter table test add constraint foreign key(id) references xxx(unid);
on delete cascade
on delete set null
alter table test3 add constraint ck_test3_1 check(name like 'k%');
alter table test4 drop constraint pK_test_1 cascade;
alter table test5 disable/enable constraint pK_test_1 cascade;
alter table test6 drop pk cascade constraints;
数据字典:  user_constraints, user_cons_column

11. Create Views

set wrap off
set linesize 1000
create or replace view testview1 as select * from test;
create force view testview1 as select * from test;
create force view testview1 as select * from test
where qty > 10
with check option (constraint empvu20_ck);
with read only
Top-N  Analysis
select column_list, rownum
from (select column_list from table order by Top-N_column)
where rownum <= N;

12. Create Indexs, Sequence, Synonym

 (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;

 

 

 

13. Control user access

 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 表名@数据库链接名;

 

 

 

15. Using Set Operator

 union(排序,distinct)/union all

 

 intersect(distinct)

 

 minus (distinct)

 

 需要显示的指定Order by

 

 

16. Datatime Function

 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

 

 

17. Enhancements to the Group by Clause

 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))

 

18. Advanced Subqueries

where (a,b) in (select a,b...)

 

关联子查询: where exists

 

update,delete中也可以使用子查询

 

with Clause example:

 

with xxx as (select ...)

xxx以后就可以直接用了

 

 

19. Hierarchical Retrieval

Walking the Tree

... ...

from employees

start with employee_id = 101

connect by prior manager_id = employee_id;

 

20. Extensions to DML and DDL Statements

 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'

 

你可能感兴趣的:(oracle,sql,cache,Access)