建表
create table column_name1 column_type [not null] [check (expression)] [default value] [primary key][references column_name2 column_type [not null] [check (expression)] [default value] [primary key][references .... [constraint primary_name primary key(column_name1,column_name2...) ] [unique( ); create table as select * from --------------------------------------------------------------------------------------- 改表结构 1 alter table 2 alter table --------------------------------------------------------------------------------------- 删表 drop table truncate table --------------------------------------------------------------------------------------- 表的重命名 rename --------------------------------------------------------------------------------------- 插入数据 1 insert into values (column_value1, column_value2,.... ); 2 insert into values (column_value1, column_value2,.... ); 3 insert into select expression1,expression2, .... from [where --------------------------------------------------------------------------------------- 表的定义与数据插入 create table [ as child_select]; --------------------------------------------------------------------------------------- 修改数据 1 update set column_name1= expression1,column2=expression2, .... [where 2 update set column_name1,column2, .... =child_select [where --------------------------------------------------------------------------------------- 删除数据 delete from [where --------------------------------------------------------------------------------------- 数据查询 select [all| distinct] <[table_name.*]|expression1,expression2,...> from table_name1.* [other_name1],table_name2.* [other_name2],... [where [connect by [group by expression1,expression2,....] [having [{union|intersect|minus} select...] [ order by {expression1|labor1} [asc|edsc],{expression2|labor2} [asc|edsc] for update of column_name1,column_name2,...[no wait] ]; --------------------------------------------------------------------------------------- 索引 create [unique] index tabspace tabspace_name; drop index --------------------------------------------------------------------------------------- 视图 create or replace view as [with check option;] [with read only;] drop view --------------------------------------------------------------------------------------- 存储过程 ====================存储过程主要用于处理复杂的业务,而且易于维护 ====================创建存(储过程/函数/包)的用户必须具有 CREATE PROCEDURE ==================== 或 CREATE ANY PROCEDURE 的权限 create [or replace] procedure ( as/is var_name var_type begin ... exception ... end; execute procedure_name(value1,value2...); 其中exception中:可用 when when when OTHERS then .......; 其中 condition value sqlcode condition CURSOR_ALREADY_OPEN (-6511) 试图打开一个已打开的光标 DUP_VAL_ON_INDEX (-1) 唯一索引中的数据重复 INVALID_CURSOR (-1001) 使用未打开的光标 INVALID_NUMBER (-1722) 字符串转换成数据时出错 LOGIN_DENIDE (-1017) NO_DATE_FOUND (-1403) select语句基于的条件检索数据不存在 NOT_LOGGED_ON (-1012) PROGRAM_ERROR (-6501) STORAGE_ERROR (-6500) STORAGE_ERROR (-0051) TOO_MANY_ROWS (-1427) 使用隐式光标时一次检测到多行数据 TRANSACTION_BACKED_OUT (-0061) VALUE_ERROR (-1476) 指定目标域的长度小于待放入其中的数据长度 ZERO_DIVID OTHERS EXCEPTION raise_application_error ( 其中error_code 的编码范围在 -20001 到 -20999 之间 可用 grant procedure on 运行 execute 重新编译:alter procedure -------------------------------------------------------------------------------------------- 用connect by 遍历家族树 { 使用connect by的顺序,select ,from ,where ,start with ,connect by ,order by } //where子句将从树中删掉单个节点,但保留它的后代, 而connect by 的限定将删除单个节点 //及其后代。 level 是表示接点层数的伪列(从1开始) 例: 1) select node_value from t_tree start with node_value='root_nod' connect by parent_node=prior node_value //遍历节点root_nod 的子节点,不包括 and node_value !='no_use_nod' //node_value 等于'no_use_nod'节点及其子节点 2) select node_value from t_tree where node_value !='no_use_nod' //遍历节child_nod的父节点,不包括 start with node_value='child_nod' //node_value 等于'no_use_nod'节点 connect by node_value=prior parent_node ----------------------------------------------------------------------------------------------- 定义函数: create or replace function return ... begin .... exception end; --------------------------------------------------------------------------------------- 包: 包是集中到单独一单元的一组过程、函数、变量、常量、数据指针、例外列表和sql语句。 dbms_output 包括三个调用函数 put , put_link ,new_link 在使用dbms_output 之前,必须 首先发出命令 set serveroutput on 建立包的定义: create [or replace] package <[user].package_name> {is|as} end <[user].package_name> 建立包的内容: create [or replace] package body <[user].package_name> {is|as} end <[user].package_name> 或 create [or replace] package body <[user].package_body_name> {is|as} begin sql_command; //初始化包的命令 end <[user].package_body_name> 调用方式:<[user].package_name>.<[user].package_body_name> --------------------------------------------------------------------------------------- 数据拷贝: copy from [ {append | create | insert | replace } table name using subquery; 例: set copycommit 1 set arraysize 1000 //每一千条记录提交一次 copy from t_branch@dhzx - //???????It is wrong create t_new_branch - using - select * from t_new_branch //每行中的 - 表示与下一行连接 -------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
触发器: 触发器的执行对用户来说是透明的。 建立触发器需具有表的alter 或 alter any table 的权限 合法的触发器类型有14种 BEFORE INSERT row BEFORE INSERT statement AFTER INSERT row AFTER INSERT statement BEFORE UPDATE row BEFORE UPDATE statement AFTER UPDATE row AFTER UPDATE statement BEFORE DELETE row BEFORE DELETE statement AFTER DELETE row AFTER DELETE statement INSTEAD OF row INSTEAD OF statement 创建触发器的语法: --------------------------------------------------------------------------------------------- 显示光标:declare cursor open fetch close oracle的隐式游标是sql,所有游标都有四个属性:%NOT FOUND,%FOUND,%ROWCOUNT,%ISOPEN 这些属性可以存取有关的insert,update,delete,insert into的语句执行信息。 例:游标名%ROWCOUNT,或sql%ISOPEN ---------------------------------------------------------------------------------------- 伪列 rowid, rownum, user --------------------------------------------------------------------------------------- pb 端调用:declare (:var_1,:var_2,....); execute fetch CLOSE --------------------------------------------------------------------------------------- 同义词 create synonym for create public synonym for drop [public] synonym --------------------------------------------------------------------------------------- 序列 create sequence [schema.]sequence_name [option] ; option: INCREMENT BY, MINVALUE, NOMINVALUE ,MAXVALUE ,NOMAXVALUE , START WITH ,CYCLE ,NOCYCLE ,CACHE ,NOCACHE ,ORDER ,NOORDER access: CURRVAL ,NEXTVAL 调用方式:[schema.]sequence_name.nextval //取下一个序列号 --------------------------------------------------------------------------------------- 创建数据库连接: create [public] database link connect to using 删除 drop [public] database link 调用方式: 或建立远程连接的同义词:例: create synonym 应避免在使用 connect by,start with 和 prior等关键字查询中使用数据库连接, 使用数据连接进行树状结构的查询,多数回失败。 ---------------------------------------------------------------------------------------- 创建用户 create user alter user --------------------------------------------------------------------------------------- 授权与回收 database: grant connect|resouce|dba to [with grant/admin option]; revoke connect|resouce|dba from table: grant all|alter|delete|index|insert|select on [user_name.] revoke all|alter|delete|index|insert|select on grant {system privilege|role} [,{system privilege|role},...] [ on |