视图
create view 视图名 as select……
索引
create index 索引名 on 表名(字段名)
高级
数据类型
? 基本
数值型:number(长度,精度),decimal、int 字符串:char,varchar,nvarchar,nchar,clob,blob 日期:date,timestram ? 表
变量名 表名.字段名%type 表的某个字段类型 变量名 表名%rowtype 某个表的记录类型 ? 复合类型
type 类型名 is record( 变量名 数据类型, …… )
变量名 类型名 ? 游标
type 游标名 is ref cursor 定义游标类型 游标变量 游标名
open 游标变量 for select…… 打开游标 fech 游标变量 into 循环取值 close 游标变量 关闭游标 游标变量%notdate
控制结构
? 判断
·if 条件 then …… end if
·if 条件 then …… else …… end if
·if 条件 then …… elsif 条件 then…… else …… end if ? 循环
·when 条件 loop …… end loop ·loop …… exit when 条件 end loop
存储过程
? 语法
create procedure 过程名(变量名 in 数据类型,变量名 out 数据类型) as
declare 变量名 数据类型 --定义变量,可以用:=赋初值 begin 处理语句
exception --异常处理
when 异常处理 then …… when others then…… end
? 分页存储过程
·create package my_package is
type my_cursor is ref cursor end
·create procedure cut(v_in_table in varchar,v_in_size in number,v_in_page in number,
v_out_count out number,v_out_ye out number,v_out_rs out my_package.my_cursor) as
declare v_sql varchar(2000); declare v_start number; declare v_end number; begin
execute immeteate ?select count(*) into?||v_out_count||? from ?||v_in_table; if mod(v_out_count,v_in_size)=0 then v_out_ye:=v_out_count/v_in_size; else
v_out_ye:=v_out_count/v_in_size+1; end if;
v_start:=v_in_size*(v_in_page-1)+1; v_end:=v_in_size*v_in_page
v_sql:=?select * from (select t1.*,rownum num from (select * from ?||v_in_table||? )) t where num?>=v_start;
open v_out_rs for v_sql; end
函数
create function 函数名(参数列表) return 返回值类型 as
declare 变量名 数据类型; begin dml……
return 变量名; end
包
包用于把同一对象的操作放一块,方便管理 ? 创建包(用于定义类型) create package 包名 is
type 类型名 is record; type 游标名 is ref cursor;
function 函数名(参数列表) return 返回值类型; procedure 过程名(参数列表); end
? 创建包体
create package body 包名 is
变量名 类型名; 变量名 游标名;
function 函数名(参数列表)return 返回值类型 is begin …… end;
procedure 过程名(参数列表) is begin …… end; end
触发器
? dml
create trigger 触发器名
before/after --在操作之前/之后执行 之前的值为:new.字段名 之后的值为:old.字段名 delete [or update or insert of 字段名] on 表名
[for each row] --加上此句为行级触发,每dml一行都会触发,不加为表级触发,不考虑多少行,只执行一次 begin
--处理语句 (可以抛异常 调用函数 raise_application_error(错误号,‘错误信息’)
错误号在-20001到-29000之间;
可以用deleting updating inserting 判断操作类型)
end;
? ddl
create trigger 触发器名 after 事件
on 用户名.shame begin end
? 系统
create trigger 触发器名 before/after
事件(logon logoff startup shutdown) on database begin end
? 管理触发器
alter trigger 触发器名 diable锁定触发器 alter trigger 触发器名 enable解锁触发器 drop trigger 触发器名 删除触发器
Java调用数据库 jdbc
? 直接调用
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub Connection conn = null;
PreparedStatement ps = null; Statement stmt = null; ResultSet rs = null; try {
Class.forName(\); conn
=
DriverManager.getConnection(\, \, \);
}
//预定义类型
ps = conn.prepareStatement(\); ps.setString(1, \);
ps.executeUpdate();//执行增删改
rs = ps.executeQuery();//执行查询,需要定义结果集来接收结果 while(rs.next()){ }
//普通类型
stmt = conn.createStatement(); stmt.executeUpdate(\);//执行增删改
rs = stmt.executeQuery(\);//执行查询,需要定义结果集来接收结while(rs.next()){ }
// TODO Auto-generated catch block e.printStackTrace(); if(rs!=null){ }
if(stmt!=null){ }
if(ps!=null){ }
if(conn!=null){ }
conn.close(); ps.close(); stmt.close(); rs.close();
System.out.println(rs.getString(1)); System.out.println(rs.getString(1));
果
}
} catch (Exception e) {
}finally{