`

oracle数据库命令大全

阅读更多
sqlplus打开记事本命令 ed;

循环插入数据
declare 
i NUMBER;
begin
for i in4..100 loop
INSERT INTO rfid_fixed_heart VALUES(SYSDATE+i,i);
end LOOP;
END;


1、索引: (索引分为:1.B数索引(bitmap位图索引,反向键索引) 2、unique唯一索引(非唯一索引)  3、单列索引(组合索引、基于函数索引)(在组合中单列和多列创建索引位单列和多列索引) )

create index index_name on table(字段);
create unique index index_n on table('name');---唯一索引
create unique index index_n1 on table(name desc);---降序唯一索引
create bitmap index index_a on table('name');---位图索引
create index index_char on table(to_char(time,'yyyy')) ;---函数索引
create index index_m on table(id) reverse;---反向键索引

创建索引后 读取数据较快  但是修改数据时候会变慢  因为它还要增加一个索引到索引表里面

2、视图:(1、简化查询,2.保护私有数据)

create view view_name as 查询内容
视图创建的越多越不好维护(表结构改了视图也得跟着改)

3、三范式(设计数据库的时候需要遵守的一些规则)
	第一范式:第一范式的目标是确保每列的原子性,如果每列都是不可再分割的最小数据单元,则满足第一范式。
	第二范式:在第一范式的基础上更进一层,目标是确保表中的每列都和主见相关
	第三范式:在第二范式的基础上更近一层,目标是确保每列和主键都有直接相关

4、oracle输出
	begin 
	dbms_output.put_line('helloWorld');
	end;
	并且还需要写一句话
	set serveroutput on;(默认serveroutput 是off的)
	然后在执行一边就会输出helloWorld

5、declare(声明变量)
	declare
	v_name varchar(20);
	begin
	v_name :='myname';
	dbms_output.put_line(v_name);
	end;
	执行结果是myname

6、declare(声明变量)
    declare 
    v_num number :=0;
    begin
    v_num :=2/v_num;
    dbms_output.put_line(v_num);
    exception
    when others then     --当出现问题时
    dbms_output.put_line('error');
    end;      
	执行结果error

7、oracle常用变量类型:
	1、binary_integer:整数,主要是用来计数而不是用来表示字段类型
	2、number :数字类型
	3、char:定长字符串
	4、varchar2 :变长字符串
	5、date :日期
	6、long:长字符串,最长2GB
	7、boolean:布尔类型,可以取值为true,false和null值	

8、Sql语句的运用:
	declare
	v_ename emp.ename%type;   --v_ename指定的是emp表中的ename的类型并且别emp中ename引用
	v_sal  emp.sal%type;      --v_sal指定的是emp表中的sal的类型
	begin
	select ename,sal into v_ename,v_sal from emp where empno=7369;
	dbms_output.put_line(v_name || '' ||v_sal);
	end;
	输出800 (返回800行数据)

9、继续演戏%type
	declare 
	v_deptno dept.deptno%type :=5;
	v_dname dept.dname%type :='aaaaa';
	v_loc dept.loc%type :='bj';
	begin
	insert into dept2 values(v_deptno,v_dname,v_loc);
	commit;
	end;
	表dept2中就会多一行5,aaaaa,bj数据

10、if else的用法
	declare 
	v_sal emp.sal%type;
	begin
	select sal into v_sal from emp
	where empno=7369;
	if(v_sal<1200)then
	dbms_output.put_line('low');
	elsif(v_sal<2000)then
	dbms_output.put_line('middle');
	else
	dbms_output.put_line('high');
	end if;
	end;

11、do  while循环以loop开始再以loop结束
	declare 
	i binary integer :=1;
	begin
		loop
	dbms_output.put_line(i);
	i:=i+1;
	exit when(i>=11);
	end loop;
	end; 
	打印 1到10的数据

12、while循环   以loop开始再以loop结束
	declare
	j binary integer :=1;
	begin
	while j<11 loop
	dbms_output.put_line(j);
	j :=j+1;
	end loop;
	end;
	打印1到10的数据;

13、for循环
	begin
	for k in 1..10 loop
		dbms_output.put_line(k);
	end loop;
	for k in 10..1 loop
		dbms_output.put_line(k);
	end loop;
	end;
	打印1到10的数据和10到1的数据


14、游标《重点》
    declare
    cursor c is select * from emp;  --声明游标
          v_emp c%rowtype;
     begin 
     open c                         --打开游标后p_sql才能正真的取数据
     fetch c into v_emp;
     dbms_output.put_line(v_emp.ename);
     close c;
     end;

15、游标
    declare
    cursor c is select * from emp;  --声明游标
          v_emp c%rowtype;
     begin 
     open c          --打开游标后p_sql才能正真的取数据
     loop                         
     fetch c into v_emp;
     exit when(c%notfound);		    
     dbms_output.put_line(v_emp.ename);
     end loop;
     close c;
     end;

16、存储过程:当你写好存储过程以后在procedures下面会显示你刚写的存储过程名  然后右键---测试----上面有个开始调试器点一下-----在把旁边的执行点一下存储过程就执行了

	下面是一个简单的例子:
	创建表 create table emp(
            empno number(4) primary key,
            empname varchar(20) not null,
            empmessage varchar(50) not null
            sal number(6) not null
     		)
    	 insert into emp(empno,empname,empmessage,sal)values(1,'accp','accp',60);
   	 insert into emp(empno,empname,empmessage,sal)values(6,'accp','accp',800);
   	 insert into emp(empno,empname,empmessage,sal)values(2,'accp','accp',50);
   	 insert into emp(empno,empname,empmessage,sal)values(3,'accp','accp',30);
       	 insert into emp(empno,empname,empmessage,sal)values(4,'accp','accp',100);
   	 insert into emp(empno,empname,empmessage,sal)values(5,'accp','accp',200);
	   --存储过程
	create or replace procedure p
        is
        cursor c is
        select * from emp for update;
        begin
        for emp in c loop
       if(emp.empno=4)then
       update emp set sal=sal*2 where current of c;
       elsif(emp.empno=6)then
       update emp set sal=sal*3 where current of c;
       else
       update emp set sal=sal-1 where current of c;
       end if;
       end loop;
       commit;
       end;	

17、带参数的存储过程:
	create or replace procedure p
(v_a in number, v_b number, v_ret out number, v_temp in out number)
  is
  begin
  if(v_a>v_b) then
  v_ret :=v_a;
  else
  v_ret :=v_b;
  end if;
  v_temp :=v_temp+1;
  end;
  
  declare 
  v_a number :=3;
  v_b number :=4;
  v_ret number;
  v_temp number :=5;
  begin p
  (v_a,v_b,v_ret,v_temp);
  dbms_output.put_line(v_ret);
  dbms_output.put_line(v_temp);
  end;

	输出4,6

18、函数function
首先创建表
	create table emp(
            empno number(4) primary key,
            empname varchar(20) not null,
            empmessage varchar(50) not null
            sal number(6) not null
     )
插入数据
    insert into emp(empno, empname, empmessage, sal)values(5, 'accp', 'accp', 200);
    insert into emp(empno, empname, empmessage, sal)values(11, 'accp', 'accp', 3200);
    insert into emp(empno, empname, empmessage, sal)values(23, 'accp', 'accp', 2400);
    insert into emp(empno, empname, empmessage, sal)values(15, 'accp', 'accp', 2200);
    insert into emp(empno, empname, empmessage, sal)values(18, 'accp', 'accp', 9200);
创建函数:
create or replace function sal_tax(v_sal number) --个人所纳税
  return number
  is
  begin
  if(v_sal<2000)then
  return 0.10;
  elsif (v_sal<2750)then
  return 0.15;
  else
  return 0.2;
  end if;
  end;
调用函数:
  select lower(empname),sal_tax(sal)from emp;
执行结果
lower(empname) 		sal_tax(sal)
accp			0.1
accp			0.2
accp			0.15
accp			0.15
accp			0.2


--带参数的存储过程
create or replace procedure pro_class_t (p_class_id in number,to_add in number) 
is 
v_class_name_zh Rebase_class_t.class_name_zh%type; 
v_status Rebase_class_t.status%type; 
begin 
select class_name_zh,status into v_class_name_zh,v_status from Rebase_class_t where class_id=p_class_id; 
update Rebase_class_t set status = status + to_add where class_id=p_class_id; 
dbms_output.put_line('员工'||v_class_name_zh||'的状态给更改为'||v_status); 
commit; 
exception 
when others then 
dbms_output.put_line('发生错误!'); 
rollback; 
end; 

call pro_class_t(43,30) 


select min(attrib_id),count(data_type),data_type from base_attrib_t group by data_type having min(attrib_id) > 20 


字符转义符模糊查询
<isNotEmpty prepend="and" property="value0">
lower(a.assignment)like lower('%'|| trim(#value0#) ||'%')	
</isNotEmpty>

不允许崩溃的td
<TD align="left" nowrap="nowrap" class="bg_white">
<div  title='<bean:write name="CIlist" property="monitering_item" />' style="float:left; padding-top: 1px; height: 14px; width: 80px; white-space:nowrap;word-break:keep-all;overflow:hidden;text-overflow:ellipsis;">
	<bean:write name="CIlist" property="monitering_item" />
</div>
</TD>

	          
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics