sql语句

/**********创建*************/
-----------1.创建表----------
create table stu(
s_id number(8) primary key,
s_name varchar2(20) not null,
s_sex varchar2(8),
clsid number(8),
constraint u_l unique(s_name),
constraint c_l check(s_sex in('male','female'))
);

-------------2.复制表----------
create table test as select * from emp;

----------3.创建表不复制数据-------
create table test as select * from emp where 1=2;

------------4.创建索引--------------
create index i_l on emp(empno asc);

------------5.创建同义词------------
create synonym mm for emp;

/********修改表*******/
---------1.向表中添加新字段-----------
alter table <table_name> add(
字段1 类型 [not null],
字段2 类型 [not null]
);

---------2.修改表中字段---------------
alter table <table_name> modify(
字段1 类型,
字段2 类型,
);

---------3.删除表中的字段-------------
alter table <table_name> drop(
字段1,
字段2
)

---------4.修改表名称-------------------
rename <table_name> to <new table_name>;

---------5.对已经存在的表添加约束-------
alter table emp add constraint S_F foreign key(deptno) references dept(deptno)

---------6.对表里的约束禁用-------------
alter table <table_name> disable constraint <constraint_name>;

---------7.对表里的约束从新启用----------
alter table <table_name> enable constraint <constraint_name>;

---------8.删除约束-----------------------
alter table emp drop constraint <constraint_name>;

/********删除*******/
---------删除表---------------------------
drop table emp;

---------删除索引-------------------------
drop index i_l;

---------删除同义词------------------------
drop synonym mm;

/********DML(数据操纵语言)***********/
---------插入记录--------------------------
insert into table_name (column1,column2,...)
values (value1,value2...);

---------把一张表中的数据插入另一张表中-----
insert into <table_name> <select 语句>
--eg
insert table a as select * from emp where 1=2;
insert into a select * from emp where sal>2000;

---------查询记录------------------------------
---------一般查询------------------------------
select [distinct] <column1[as new name],column2...>
from <table>
[where<条件>]
[group by <column_list>]
[having<条件>]
[order by <column_list> [asc|desc]]

------显示全部数据---------------------------
select * from emp;

/*******where语句的运算符********/
where <条件1>and<条件2> --同时满足
--eg
select * from emp deptno = 8 and sal>1000;

where <条件1> or <条件2> 满足其中一个
--eg
select * form  emp where deptno = 10 or dal>2000;

where not <条件> --不满足的
--eg
select * from emp where not deptno = 10;

where in (条件列表) --所有满足条件的记录
--eg
select * from emp where empno in(3306,9537,9513);

where between .. and .. --按范围查询
--eg
select * from emp where sal between 1000 and 300;

where 字段 link --主要用与字符类型的字段
--eg
select * from emp where ename link '_C%' --查询姓名中第二个字母是'C'的人
'_':表示任意字符;
'%':表示多个字符的序列;

where 字段 is [not] null --查询该字段是[不是]空的记录

/*****汇总数据时用的函数*********/
sum --求和
select deptno,sum(sal) as sumsal from emp
group by deptno;
avg --求平均值
max --求最大值
min --求最小值
count --求个数

/****子查询******/
select <字段列表> from <table_name> where 字段运算符(<select 语句>);
--eg
select * from  emp where sal = (select max(sal) from emp);

-----运算符-------
Any
--eg
select * from emp where sal> any (select sal from emp where deptno=30) and deptno <> 30;

all
--eg
select * from emp where sal>all(select sal from emp where deptno = 30) and deptno <> 30;

-----连接查询-------
select <字段列表> from <table1,table2> where
table1.字段[(+)] = table2.字段[(+)]
--eg
select empno,ename,dname from emp, dept where emp.deptno = dept.deptno;

-----查询指定行数的数据---------
select <字段列表> from <table_name> where rownum < 行数
--eg
select * from emp where rownum <=10;
----要查询第几行的数据----------
--eg
select * form emp where rownum<=3 and empno not in(
select empno from empno form emp where rownum <=3
);

/*****更新数据*********/
update table_name set column1 = new value, column2 = new value,...
where <条件>
--eg
update emp set sal = 1000,empno=8888 where ename = 'scott'

/******删除数据*********/
delete from <table_name> where <条件>
--eg
delete from emp where empno = '7788'

/*******数据控制语言**********/
--------1.授权----------------
grant <权限列表> to <user_name>;

--------2.收回权限------------
revoke <权限列表> from <user_name>
Oracle 的权限列表
connect 连接
resource 资源
unlimited tablespace 无限表空间
dba 管理员
session 会话

/******数据控制语言**********/
1.commit 提交;

2.rollback [to savepoint] 回滚;

3.savepoint <savepoint> 保存位置。

/*****oracle其他对象*******/
------创建视图-------------
create [or replace] view <view_name> as <select 语句>;
or replace --表示替换以有的视图

------删除视图--------------
drop view <view_name>

/******创建序列*****/
create sequence <sequence_name>
increment by n --表示序列每次增长的幅度;默认默认值为1.
start with n --表示序列开始时的序列号。默认值为1.
[maxvalue n] [minvalue n]--表示序列可以生成的最大值(最小值)
[cycle|nocycle] --表示序列到达最大之后,是否从新开始生成序列
[cache n | nocache];--允许更快的生成序列

--eg
create sequence se_1
increment by 1
start with 100
maxvlaue 99999
cycle;

----修改序列-----------
alter sequence <sequence_name>
increment by n
start with n
[maxvalue n]
[minvalue n]
[cycle|nocycle]
[cache n|nocache];

-----使用序列-------
1.currval --返回当前值
--刚建立序列后,序列的currval值为null 所以不能直接使用
--可以先初始化序列:
select <sequence_name>.nextval from dual;
--eg
select se_1.nextval from dual;
2.nextval
--返回序列下一个值
--eg
begin
for i in 1..5
loop
insert into emp(empno) values(se_1.nextval);
end loop;
end;
--查看序列当前值
select <sequence_name>.currval from dual;

/****用户*****/
------创建用户------
create user <user_name> [profile 'DEFAULT']
identified by "<password>" [default tablespace "users"]

------删除用户-------
drop user <user_name> cascade


/*******角色*******/
----创建角色--------
create role <role_name>
identified by "<password>"
drop role <role_name>

/******PL/SQL*******/
declare --声明语句
        声明语句
begin   --执行部分
        执行语句
exception --异常处理部分
		执行语句
end

/****变量声明******/
<变量名> 类型[:=初始值];
特殊类型 字段%type
--eg
name  emp.ename%type --表示name的类型和emp.ename相同

表 %rowtype
--eg
test emp%rowtype --表示test的类型为emp表的行类型

/*******常量声明**********/
<变量名> constant 类型:=初始值;
--eg
pi constant number(5,3):=3.14;

/*********全局变量声明*********/
variable <变量名> 类型;
--eg
variable num number;

/**********全局变量使用***********/
:<变量名>
--eg
:num:=100;
i=:num;
/**********查看全局变量的值***********/
print <变量名>
--eg 

/**********赋值运算符: := ***********/
--eg
num :=100;
--------使用select <列名> into <变量名> from <表名> where  <条件>------
--------注意 select into 语句返回的结果只能为一行;----------------
--eg
select emp%rowtype;
select * into test  from  emp where empno=7788;

/********用户交互输入******/
<变量> :='&变量'
--eg 
num: = #
------注意oracle的用户交互输入时先接受用户输入的所有之后在执行语句;----
------所以不能使用循环进行用户交互输入---------------

/******条件控制语句*********/
if <条件1> then
	语句 
[else if <条件2> then
	语句 
		.
		.
		.
else if <条件n> then
	语句]
[else 语句]
end if;

/*****循环控制语句*****/
------1.loop-----------
loop 
	语句;
	exit where<条件>
end loop;

------2.while loop----
while<条件>
loop
	语句;
end loop;

-------3.for---------
for <循环变量> in 下限..上限
loop
	语句;
end loop;

-------null 语句------
null;
表示没有任何操作;

-------异常处理--------
exception
	where <异常条件> then
			语句;
	where others then
			语句;
end;

/******显示游标*******/
定义:cursor <游标名> is <select 语句> [for update|for update of 字段];
[for update | for update of 字段]--给游标加锁,既是在程序中有"update","insert","delete" 语句对数据库操作时。
/*游标自动给指定的表或者字段加锁,防止同时有有别的程序对指定的表或者字段进行"update","insert","delete" 操作时.在使用"delete","update"后还可以在程序中使用current of<游标名> 子句引用当前行*/

/*****操作****/
open <游标名> --打开游标
	fetch <游标名> into 变量1,变量2,变量3,...变量n,;
		或者
		fetch <游标名> into 行对象;--取出游标当前位置的值
		close <游标名> --关闭游标

/****属性****/
%not found --如果fetch语句失败,则该属性为"true",否则为"false";
%fetch --如果fetch语句成功,则该属性为"true",否则为"false";
%row count --返回游标当前行数;
%is open  --如果游标时开的则返回"true",否则为"false";

/****使用****/
loop 循环变量
--eg
declare
	cursor c_1 is select * from emp; --定义游标
	r c_1%rowtype;--定义一个行对象,用于获取游标的值
begin
	if c_1%isopen then 
	close c_1;
	end if;
	open c_1;--判断游标是否打开.如果开了将其关然后打开
	dbms_output.put_line('行号 姓名 薪水');
	loop
	fetch c_1 into r;--取值
	exit where c_1%notfound;--如果游标没有取到值推出循环
	dbms_output.put_line(c_1%rowcount||''||r.ename||''||r.sal);--输出结果,需要 set serverout on才能显示.
	end loop;
end;

for 循环 
--eg
declare
	cursor c_1 is select ename,sal from emp; --定义游标 
begin 
	dbms_output.put_line('行号 姓名 薪水');
	for i_l in c_1 --for循环中的循环变量i为
	c_1%rowtype类型;
	loop
	dbms_output.put_line(c_1%rowcount||''||i.ename||''||i.sal); --输出结果,需要 set serverout on 才能显示。
	end loop;
end;
/*for循环使用游标是在循环开始前自动打开游标,并且自动取值到循环结束后,自动关闭游标.*/

/****游标加锁实例****/
declare
	cursor c_1 is select ename, sal from emp for update of sal;--for 循环游标emp表的sal加锁.
begin
	dbms_output.put_line('行号 姓名 薪水');
	for i in c_1 --for循环中的循环变量i为c_1%rowtype类型;
	loop
	update emp set sal = sal +100 where current of c_1; --表示对当前行的sal进行跟新。
	end loop;
	for i in c_1
	loop
	dbms_output.put_line(c_1%rowcount||''||i.ename||''||i.sal); --输出结果,需要set serverout on才能显示.
	end loop;
end;

/****代参数的游标****/
----定义----
cursor <游标名>(参数类型) is <select 语句> [for update| for update of 字段];
--eg
declare
	cursor c_1(name emp.ename%type) is select ename,sal from emp where ename = name; --定义游标
begin
	dbms_output.put_line('行号 姓名 薪水');
	for i in c_1('&name')         --for循环中的循环变量i为c_1%rowtype类型;
	 loop
     dbms_output.put_line(c_1%rowcount||' '||i.ename||' '||i.sal);    --输出结果,需要 set serverout on 才能显示.
     end loop;
end;

/****隐试游标****/
/*隐试游标游标是系统自动生成的。每执行一个DML语句就会产生一个隐试游标,起名字为SQL;*/
/*隐试游标不能进行"OPEN" ,"CLOSE","FETCH"这些操作;*/

/****属性:****/
    %notfound --如果DML语句没有影响到任何一行时,则该属性为"TRUE",否则为"FALSE";
    %found --如果DML语句影响到一行或一行以上时,则该属性为"TRUE",否则为"FALSE";
    %rowcount --返回游标当最后一行的行数;

个人认为隐试游标的作用是判断一个DML语句;
--eg
begin
    delete from emp where empno=&a;
    if sql%notfound then
        dbms_output.put_line('empno不存在');
    end if;
    if sql%ROWCOUNT>0 then
        dbms_output.put_line('删除成功');
    end if;
end; 

/****PL/SQL表****/
/*pl/sql表只有两列,其中第一列为序号列为integer类型,第二列为用户自定义列.*/

/****定义****/
type <类型名> is table of <列的类型> [not null] index by binary_integer;
/*<列的类型>可以为Oracle的数据类行以及用户自定义类型;*/

/****属性方法****/
.count --返回pl/sql表的总行数
.delect --删除pl/sql表的所有内容
.delect(行数) --删除pl/sql表的指定的行
.delct(开始行,结束行) --删除pl/sql表的多行
.first --返回表的第一个index;
.next(行数) --这个行数的下一条的index;
.last --返回表的最后一个index;

/****使用****/
--eg
declare
     type mytable is table of varchar2(20) index by binary_integer; --定义一个名为mytable的PL/sql表类型;
     cursor c_1 is select ename from emp; 
     n number:=1;
     tab_1 mytable; --为mytable类型实例化一个tab_1对象;
begin
     for i in c_1
     loop
          tab_1(n):=i.ename; --将得到的值输入pl/sql表
          n:=n+1; 
    end loop;
     n:=1;
     tab_1.delete(&要删除的行数); --删除pl/sql表的指定行
     for i in tab_1.first..tab_1.count
     loop
          dbms_output.put_line(n||' '||tab_1(n)); --打印pl/sql表的内容
          n:=tab_1.next(n);
     end loop;
exception 
     where no_data_found then   --由于删除了一行,会发生异常,下面语句可以接着删除的行后显示
          for i in n..tab_1.count+1
     loop
          dbms_output.put_line(n||' '||tab_1(n));
          n:=tab_1.next(n);
     end loop; 
end; 

/****PL/SQL记录****/
/*pl/sql表只有一行,但是有多列。*/

/****定义:****/
type <类型名> is record <列名1 类型1,列名2 类型2,...列名n 类型n,> [not null]
/*<列的类型>可以为Oracle的数据类行以及用户自定义类型;可以是记录类型的嵌套*/

/****使用****/
--eg
declare
     type myrecord is record(id emp.empno%type,
     name emp.ename%type,sal emp.sal%type); --定义一个名为myrecoed的PL/sql记录类型;
     rec_1 myrecord; --为myrecord类型实例化一个rec_1对象;
begin
	 select empno,ename,sal into rec_1.id,rec_1.name,rec_1.sal
	 from emp where empno=7788;  --将得到的值输入pl/sql记录
	 dbms_output.put_line(rec_1.id||' '||rec_1.name||' '||rec_1.sal); --打印pl/sql记录的内容
end; 

/****结合使用PL/SQL表和PL/SQL记录****/
--eg
declare
     cursor c_1 is select empno,ename,job,sal from emp;
     type myrecord is record(empno emp.empno%type,ename emp.ename%type,
job emp.job%type,sal emp.sal%type);--定义一个名为myrecoed的PL/sql记录类型;
    type mytable is table of myrecord index by binary_integer; 
    --定义一个名为mytable的PL/sql表类型;字段类型为PL/sql记录类型;
    n number:=1;
    tab_1 mytable; --为mytable类型实例化一个tab_1对象;
begin
	--赋值
	for i in c_1
	loop
		tab_1(n).empno:=i.empno;
		tab_1(n).ename:=i.ename;                     
		tab_1(n).job:=i.job;
		tab_1(n).sal:=i.sal;
		n:=n+1;
    end loop;
	  n:=1;
	  --输出
	  for i in n..tab_1.count
	  loop
		  dbms_output.put_line(i||' '||tab_1(i).empno||' '||tab_1(i).ename||' '||tab_1(i).job||' '||tab_1(i).sal);
	  end loop;
end; 
/****弱型REF游标***/
/****定义:****/
type <游标名> is ref cursor;

/****操作****/
open <游标名> for <select 语句> --打开游标
fetch <游标名> into 变量1,变量2, 变量3,... 变量n,;
	或者
fetch <游标名> into 行对象; --取出游标当前位置的值
close <游标名> --关闭游标

/****属性****/
%notfound --如果fecth语句失败,则该属性为"true",否则为"false";
%found --如果fetch语句成功,则该属性为"true",否则为"false";
%rowcount --返回当前游标行的行数;
%isopen --如果游标时开的则返回"true",否则为"false";
--eg
set autoprint on;
var c_1 refcursor;
declare
   n number;
begin
   n:=&请输入;
   if n=1 then
        open :c_1 for select * from emp;
   else 
        open :c_1 for select * from dept;
   end if;
end; 

/****过程****/
/*定义*/
create [or replace] procedure <过程名> [(参数列表)] is 
		[局部变量声明]
		begin
			可执行语句
		exception
			异常处理语句
		end[<过程名>];
/*变量的类型:in默认类型,表示输入;out 表示只输出;in out 表示即输入又输出*/

/*操作已有的过程:*/
/*在PL/SQL块中直接使用过程名;在程序外使用execute <过程名>[(参数列表)]*/

/*使用*/
--eg
create or replace procedure p_1(n in out number) is
    r emp%rowtype;
begin
     dbms_output.put_line('姓名 薪水');
     select * into r from emp where empno=n;
     dbms_output.put_line(r.ename||' '||r.sal); --输出结果,需要 set serverout on 才能显示.
    n:=r.sal; 
end;
使用过程:
declare
    n number;
begin
    n:=&请输入员工号;
    p_1(n);
    dbms_output.put_line('n的值为 '||n);
end; 

/*删除过程*/
drop procedure <过程名>

/****函数****/
/*定义*/
create [or replace] function <函数名>[(参数列表)] return 数据类型 is 
		[局部变量声明]
		begin
			可执行语句
		exception
			异常处理语句
		end[<函数名>];
/*变量的类型:in 为默认类型,表示输入; out 表示只输出;in out 表示即输入又输出;*/

/*使用*/
--eg
----创建函数----
create or replace function f_1(n number) return number is
    r emp%rowtype;
begin
     dbms_output.put_line('姓名 薪水');
     select * into r from emp where empno=n;
     dbms_output.put_line(r.ename||' '||r.sal); --输出结果,需要 set serverout on 才能显示.
     return r.sal;
end;
----使用函数----
declare
    n number;
     m number;
begin
    n:=&请输入员工号;
    m:=f_1(n);
    dbms_output.put_line('m的值为 '||m);
end; 

/*删除函数:*/
drop function <函数名>;

/*****数据包*****/
/*定义*/
----定义规范----
create [or replace] package <数据包名> as 
	--公共类型和对象声明
	--子程序说明
	end;
	
----定义包的主题----
create [or replace] package body <数据包名> as 
	--公共类型和对象声明
	--初始化主题
	begin
		--初始化语句
	end;

/*使用*/
--eg
----创建数据包规范----
create or replace package pack_1 as
	n number;
	procedure p_1;
	function f_1 return number;
end;

----创建数据包主体----
create or replace package body pack_1 as
procedure p_1 is 
       r emp%rowtype;
  begin 
       select * into r from emp where empno=7788;
       dbms_output.put_line(r.empno||' '||r.ename||' '||r.sal);
   end;

   function  f_1 return number is
       r emp%rowtype;
   begin
        select * into r from emp where empno=7788;
       return r.sal;
   end;
end;

----使用包----
declare
	n number;
begin
      n:=&请输入员工号;
      pack_1.n:=n;
      pack_1.p_1;
      n:=pack_1.f_1;
      dbms_output.put_line('薪水为 '||n);
end;

/*在包中使用ref游标*/
--eg
----创建数据包规范----
create or replace package pack_2 as 
	type c_type is ref cursor;--创立一个ref游标类型
	reocedure p_1(c1 in out c_type);--过程的参数为ref游标类型;
end;

----创建数据包主体----
create or replace package body pack_2 as
procedure p_1(c1 in out c_type) is
	begin
		open c1 for select * from emp;
	end;
end;

----使用包----
var c_1 refcursor;
set autoprint on;
execute pack_2.p_1(:c_1);

/*删除包*/
drop package <包名>;

/****触发器****/
/*创建触发器:*/
create [or replace] teigger <触发器名>
before|after
insert|delete|update[of <列名>] on <表名>
[for each row]
when(<条件>)
<pl/sql块>
关键字"before"在操作完成前触发;"after"则是在操作完成后触发;
关键字"for each row"指定触发器每行触发一次.
关键字"of <列名>" 不写表示对整个表的所有列.
where (<条件>)表达式的值必须为"true".

/*特殊变量*/
:new --为一个引用最新的列值;
:old --为一个引用以前的列值;
这些变量只有在使用了关键字 "for each row"时才存在.且update语句两个都有,而insert只有:new ,delect 只有:old;

/*使用raise_application_error*/
	语法:raise_application_error(错误号(-20000到-20999),消息[,{true|false}]);
	抛出用户自定义错误
	如果参数为'true',则错误放在先前的堆栈上.

/*instead of 触发器*/
	instead of 触发器主要针对视图(view)将触发的dml语句替换成为触发器中的执行语句,而不执行dml语句.

/*禁用某个触发器*/
	alter trigger <触发器名> disable
/*重新启用触发器*/
	alter trigger <触发器名> enable
/*禁用所有触发器*/
	alter trigger <触发器名> disable all triggers
/*启用所有触发器*/
	alter trigger <触发器名> disable all triggers
/*删除触发器*/
drop trigger <触发器名>

/****自定义对象****/
/*创建对象:*/
create [or replace] type <对象名> as object(
属性1 类型
属性2 类型
	.
	.
方法1的规范(member procedure<过程名>)
方法2的规范(member function <函数名> return 类型)
	.
	.
fragma restric_references(<方法名>,wnds/rnds/wnps/rnps)
);
关键字"fragma restric_references"通知oracle函数按以下模式之一操作;
wnds -不能写入数据库状态
rnds -不能读出数据库状态
wnps -不能写入包状态
rnps -不能读出包状态

/*创建对象主体*/
create[or replace] type body <对象名> as 
方法1的规范(member procedure<过程名> is <pl/sql块>)
方法1的规范(member function <函数名> return 类型 is <pl/sql块>)
end;

/*使用map 方法和order方法*/
  用于对自定义类型排序。每个类型只有一个map或ORDER方法。
     格式:map member function <函数名> return 类型 
          order member function <函数名> return number 

/*创建对象表*/
create table <表名> of <对象类型>

--eg
----1.创建name类型----
 create or replace type name_type as object(
          f_name varchar2(20),
          l_name varchar2(20),
          map member function name_map return varchar2); 
     
     create or replace type body name_type as
          map member function name_map return varchar2 is --对f_name和l_name排序
          begin 
               return f_name||l_name;
          end;
          end;
----2 创建address 类型 ----
     create or replace type address_type as object
       ( city varchar2(20),
           street varchar2(20),
           zip number,
      order member function address_order(other address_type) return number);

      create or replace type body address_type as
      order member function address_order(other address_type) return number is --对zip排序
      begin
           return self.zip-other.zip;
      end;
      end;

----3 创建stu对象----
       create or replace type stu_type as object (
       stu_id number(5),
       stu_name name_type,
       stu_addr address_type,
       age number(3),
       birth date, 
       map member function stu_map return number,
      member procedure update_age);

      create or replace type body stu_type as
       map member function stu_map return number is --对stu_id排序
       begin
             return stu_id;
       end;
      member procedure update_age is --求年龄用现在时间-birth
       begin 
             update student set age=to_char(sysdate,'yyyy')-to_char(birth,'yyyy') where stu_id=self.stu_id;
       end; 
       end; 
----4. 创建对象表----
      create table student of stu_type(primary key(stu_id));
	  
----5.向对象表插值----
      insert into student values(1,name_type('关','羽'),address_type('武汉','成都路',43000), null,sysdate-365*20);

----6.使用对象的方法----
     delcare
          aa stu_type;
     begin
          select value(s) into aa from student s where stu_id=1; --value()将对象表的每一行转成行对象括号中必须为表的别名
          aa.update_age();
     end; 
7.select stu_id,s.stu_name.f_name,s.stu_name.l_name from student s; --查看类型的值
8.select ref(s) from student s ; --ref()求出行对象的OID,括号中必须为表的别名;deref()将oid变成行队像;

/****其他****/
/*1.在PL/SQL中使用DDL*/
将sql语句赋给一个varchar2变量,在用execute immediate 这个varchar2变量即可;
--eg
declare
          str varchar2(200);
     begin 
          str:='create table test(id number,name varchar2(20))'; --创建表
          execute immediate str;   
          str:='insert into test values(3,''c'')'; --向表里插数据
          execute immediate str; 
     end;     

/*2.判断表是否存在*/
--eg
declare
          n tab.tname%type;
     begin
          select tname into n from tab where tname='&请输入表名';
          dbms_output.put_line('此表以存在');
     exception
          when no_data_found then
          dbms_output.put_line('还没有此表');
     end; 

/*3.查看以有的过程*/
--eg
select object_name,object_type,status from user_objects where object_type='PROCEDURE';

/*oracle的数据类型*/
varchar2(size): 可变长度的字符串,其最大长度为size个字节;size的最大值是4000,而最小值是1;你必须指定一个varchar2的size; 
nvarchar2(size): 可变长度的字符串,依据所选的国家字符集,其最大长度为size个字符或字节;size的最大值取决于储存每个字符所需的字节数,其上限为4000;你必须指定一个NVARCHAR2的size; 
number(p,s): 精度为p并且数值范围为s的数值;精度p的范围从1到38;数值范围s的范围是从-84到127;例如:number(5,2) 表示整数部分最大3位,小数部分为2位;
number(5,-2): 表示数的整数部分最大为7其中对整数的倒数2位为0,前面的取整。
number 表示使用默认值,即等同于number(5); 
long: 有效日期范围从公元前4712年1月1日到公元后4712年12月31日 
raw(size): 长度为size字节的原始二进制数据,size的最大值为2000字节;你必须为raw指定一个size; 
long raw :可变长度的原始二进制数据,其最长可达2G字节; 
char(size): 固定长度的字符数据,其长度为size个字节;size的最大值是2000字节,而最小值和默认值是1; 
nchar(size): 也是固定长度。根据Unicode标准定义 
clob:一个字符大型对象,可容纳单字节的字符;不支持宽度不等的字符集;最大为4G字节 
nclob: 一个字符大型对象,可容纳单字节的字符;不支持宽度不等的字符集;最大为4G字节;储存国家字符集 
blob: 一个二进制大型对象;最大4G字节 
bfile: 包含一个大型二进制文件的定位器,其储存在数据库的外面;使得可以以字符流I/O访问存在数据库服务器上的外部LOB;最大大小为4G字节. 

/***函数****/
/*字符函数*/
concat(字符串1,字符串2) 将字符串1和字符串2连接成一个新的字符串--eg: select concat(job,ename) from emp  
lpad:(字段,总的大小,添充字符) 左填充即向右对齐--eg: select empno,lpad(sal,10,'*') from emp 
rpad:(字段,总的大小,添充字符) 右填充即向左对齐--eg: select empno,rpad(sal,10) from emp  
lower(字符串):将字符串全部变成小写; 
upper(字符串):将字符串全部变成大写; 
initcap(字符串):将字符串变成第一个字母大写,其余都变成小写; 
length(字符串):求出字符串的长度; 
substr(字符串,开始位置,长度):从字符串中取子串;--eg: select substr(ename,2,3) from emp;--从ename的第2位开始取3位 
instr(字符串,字符):查看字符是否在字符串中存在;不存在返回0;存在则返回字符所在的的位置;如果有两个以上的字符则返回第一个的位置.--eg:select instr(ename,'S') from emp;  
trim(字符 from 字符串):去掉字符串首尾的字符;--eg: select trim('S' from ename) from emp; 
to_char():将不是其他类型转成字符类型;对于日期型可以控制其格式:to_char(日期,'格式');其中格式有: 'YYYY' --以4为显示年;'YEAR' --以标准格式显示年; 'MM' ; 'MON' ; 'DD' ; 'DAY'; 'HH' ; 'MI' ;'SS' 
replace(字符串,字符串1,字符串2) 将字符串中的字符1替换成字符2;--eg: select replace(ename,'SC','SS') from emp;  
translate(字符串,字符串1,字符串2) 替换多的字符;--eg: select translate(ename,'SH','AB') from emp; --表示将ename中的'S'换成'A','H'换成'B'; 
asciiasc(char):求字符的ascii码 
nlssort(字符串):对字符串排序. 


/*数学函数*/
abs(数字): 一个数的绝对值 
ceil(数字) :向上取整;不论小数后的书为多少都要向前进位;
floor(数字): 向下取整;不论小数后的书为多少都删除;
mod:(被除数,除数) 取余数;
round:(数字,从第几为开始取) 四舍五入;
sign:(数字) 判断是正数还是负数;正数返回1,负数返回-1,0返回0; 
sqrt:(数字) 对数字开方; 
power(m,n): 求m的n次方; 
trunc:(数字,从第几位开始) 切数字;
greatest:(数字列表) 找出数字列表中最大的数;
least:(数字列表) 找出数字列表中最小的数; 
sin(n):求n的正旋 
cos(n): 求n的余旋 
tan(n):求n的正切 
ACos(n):求n的反正切 
atan(n):求n的反正切 
exp(n):求n的指数 
ln(n):求n的自然对数,n必须大于0 
log(m,n):求n以m为底的对数,m和n为正数,且m不能为0 

/*日期函数*/
add_months(日期,数字):在以有的日期上加一定的月份;
last_day(日期):求出该日期的最后一天. 
months_between(日期1,日期2):求出两个月之间的天树(注意返回的天数为小数);
new_time(时间,时区,'gmt'):按照时区设定时间. 
next_day(d,char):返回d指定的日期之后并满足char指定条件的第一个日期 

/*其他函数*/
vsize(类型) 求出数据类型的大小; 
nvl(字符串,替换字符) 如果字符串为空则替换,否则不替换 

/****常用命令****/
desc 表名:查看表的信息. 
set serverout [on|off]:设置系统输出的状态. 
set pagesize <大小>:设置浏览中没页的大小 
set linesize <大小>:设置浏览中每行的长度 
set autoprint [on|off]:设置是否自动打印全局变量的值 
select sysdate from dual:查看当前系统时间 
alter session set:nls_date_format='格式'  设置当前会话的日期格式
show user:显示当前用户 
help topic:显示有那些命令 
save <file_name>:将buf中的内容保存成一个文件 
run <file_name>:执行已经保存的文件;也可以写成@<file_name> 
get <file_name>:显示文件中的内容 
list:显示buf中的内容 
ed: 用记事本打开buf,可以进行修改 
edl行数: 删除buf中的单行 
edl开始行:结束行 删除buf中的多行 
input字符串:向buf中插入一行 
append:字符串 将字符串追加到当前行 
connect:连接 
disconnect:断开连接 
quit:退出sql*plus 
exp:导出数据库(可以在DOS键入exp help=y 可以看到详细说明)
IMP:导入数据库(可以在DOS键入imp help=y 可以看到详细说明)

/****异常处理****/
cursor_already_open:试图"open"一个已经打开的游标 
dup_val_on_index:试图向有"unique"中插入重复的值 
invalld_cursor: 试图对以关闭的游标进行操作 
invalld_number: 在SQL语句中将字符转换成数字失败 
login_denied: 使用无效用户登陆 
no_data_found: 没有找到数据时 
not_login_no: 没有登陆Oracle就发出命令时 
program_error PL/SQL存在诸如某个函数没有"RETURN"语句等内部问题 
stoage_error PL/SQL耗尽内存或内存严重不足 
timeout_on_resource: Oracle等待资源期间发生超时 
too_many_rows: "SELECT INTO"返回多行时 
value_error: 当出现赋值错误 
zero_divide: 除数为零 

  

posted @ 2015-07-11 10:36  lhm_java  阅读(285)  评论(0编辑  收藏  举报