ITPub博客

首页 > 数据库 > Oracle > 马士兵Oracle 下

马士兵Oracle 下

Oracle 作者:immapleliu 时间:2013-12-10 17:38:59 0 删除 编辑

29、--------------主键束缚-------------

       create table stu

  (

  id number(6),

  name varchar2(20) constraint stu_name_mm not null,

  *** number(1),

  age number(3),

  sdate date,

  grade number(2) default 1,

  class number(4),

  email varchar2(50),

constraint stu_id_pk primary key (id),

constraint stu_name_email_uni unique(email, name)

);

  --------------中键束缚   被参考字段必须是主键 -------------

       create table stu

  (

  id number(6),

  name varchar2(20) constraint stu_name_mm not null,

  *** number(1),

  age number(3),

  sdate date,

  grade number(2) default 1,

  class number(4) references class(id),

  email varchar2(50),

constraint stu_class_fk foreign key (class) references class(id),

constraint stu_id_pk primary key (id),

constraint stu_name_email_uni unique(email, name)

);

红色为字段束缚的写法,蓝色为表级束缚的写法

 

create table class 

(

id number(4) primary key,

name varchar2(20) not null

);

 

31、建正表构造:

---------------添减字段--------------------------

alter table stu add(addr varchar2(29));

---------------删除字段--------------------------

alter table stu drop (addr);

---------------建正表字段的少度------------------

alter table  stu modify (addr varchar2(50));//更动后的少度必须供能包容本来的数据

----------------删除束缚前提----------------

alter table stu drop constraint  束缚名

-----------建正表构造添减束缚前提---------------

alter table stu add constraint stu_class_fk foreign key (class) references class (id);

 

32、---------------数据字典表(有user_tables、user_views、user_constraints等等)----------------

 

---------------检察当前用户下里所有的表、视图、束缚-----数据字典表user_tables---

select table_name from user_tables;

select view_name from user_views;

select constraint_name from user_constraints;

 

存储数据字典表的疑息的表:dictionary;

 //该表共有两个字段 table_name comments

 //table_name主要存放数据字典表的名字

 //comments主要是对这张数据字典表的描写

 

33、-------------索引(能劣化查询效力)------------------

create index idx_stu_email on stu (email);// 正在stu这张表的email字段上竖坐一个索引:idx_stu_email

---------- 删除索引 ------------------

drop index index_stu_email;

---------检察所有的索引----------------

select index_name from user_indexes;

---------竖坐视图-------------------

create view v$stu as selesct id,name,age from stu;

视图的作用: 简化查询,掩护我们的一些隐私数据,经由过程视图也可以用往更新数据,然则我们一般不这么用 

错误谬误:要对视图举办保护

 

34、-----------竖坐序列(sequence,oracle特有的对象,一般用往做主键)------------

create sequence seq;//竖坐序列

select seq.nextval from dual;// 检察seq序列的下一个值

drop sequence seq;//删除序列

 

35、------------数据库的三范式--------------

(1)、要有主键,列不因素

(2)、不克不及存正在部分依好:当有多个字段联合起往作为主键的时辰,不是主键的字段不克不及部分依好于主键中的某个字段

(3)、不克不及存正在传收依好

 

=======================PL_SQL(过程化SQL说话)==========================

 

38、-------------------正在客户端输出helloworld-------------------------------

set serveroutput on;//默许是off,设成on是让Oracle可以正在客户端输出数据

 

begin

dbms_output.put_line("helloworld");

end;

/

----------------pl/sql变量的赋值与输出----

declare

v_name varchar2(20);//声明变量v_name变量的声明以v_开首

begin

v_name := "myname";

dbms_output.put_line(v_name);

end;

/

39、-----------pl/sql对很是的处置处分(除数为0)-------------

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;

/

40、----------变量的声明----------

binary_integer:整数,主要用往计数而不是用往默示字段典范榜样   比number效力下

number:数字典范榜样

char:定少字符串

varchar2:变少字符串

date:日期

long:字符串,最少2GB

boolean:布我典范榜样,可以取值true,false,null//最好给一初值

----------变量的声明,使用 "%type"属性---------

declare

v_empno number(4);

v_empno2 emp.empno%type;

v_empno3 v_empno2%type;

begin

dbms_output.put_line("Test");

end;

/

//使用%type属性,可使变量的声明按照表字段的典范榜样自动变更,省往了保护的费事,并且%type属性,可以用于变量身上

41、组合变量:

---------------Table变量典范榜样(相等于java里里的数组)-------------------------------------------

declare

type type_table_emp_empno is table of emp.empno%type index by binary_integer;

v_empnos type_table type_table_empno;

begin

v_empnos(0) := 7345;

v_empnos(-1) :=9999;

dbms_output.put_line(v_empnos(-1));

end;

42、-----------------Record变量典范榜样(相等于Java里里的类)--------------------------------------------

declare

type type_record_dept is record

(

deptno dept.deptno%type,

dname dept.dname%type,

loc dept.loc%type

);

v_temp type_record_dept;

begin

v_temp.deptno:=50;

v_temp.dname:="aaaa";

v_temp.loc:="bj";

dbms_output.put_line(v_temp.deptno || " " || v_temp.dname);

end;

 

-----------使用 %rowtype声明record变量,直接参照表往声明record-------------------

declare

v_temp dept%rowtype;

begin

v_temp.deptno:=50;

v_temp.dname:="aaaa";

v_temp.loc:="bj";

dbms_output.put_line(v temp.deptno || "" || v temp.dname)

end;

 

43、 --------------select语句的使用(必须包管select语句有响应的返回记录)-------------------

declare

v_ename emp.ename%type;

v_sal emp.sal%type;

begin

select ename,sal into v_ename,v_sal from emp where empno = 7369;

dbms_output.put_line(v_ename || "" || v_sal);

end;

---------------------------select语句的使用(record)----------------------------------------

declare

v_emp emp%rowtype;

begin

select * into v_emp from emp where empno=7369;

dbms_output_line(v_emp.ename);

end;

 

------------- insert语句的使用 -----------------------------

declare

v_deptno dept.deptno%type := 50;

v_dname dept.dname%type :="aaa";

v_loc dept.loc%type := "bj";

begin

insert into dept2 values(v_deptno,v_dname,v_loc);

commit;

end;

 

------------- update 语句的使用------------------------------

declare

v_deptno emp2.deptno%type := 50;

v_count  number;

begin

update emp2 set sal = sal/2 where deptno = v_deptno;

dbms_output.put_line(sql%rowcount || ‘条记录被影响’);

commit;

end;

 

注:sql%rowcount 统计上一条sql语句更新的记录条数

 

44、-----------------ddl说话,数据界说说话-----------------------

begin

execute immediate "create table T (nnn varchar(30) default ""a"")";

end;

------------------if else语句--------------------------------------

     declare

v_sal emp.sal%type;

     begin

select sal into v_sal from emp where empno = 7369;

if(v_sal < 2000) then

dbms_output.put_line("low");

elsif(v_sal > 2000) then

dbms_output.put_line("middle");

else 

dbms_output.put_line("height");

        end if;

      end;

45、-------------------do while循环 ---------------------------

declare

i binary_integer := 1;

begin

loop

dbms_output.put_line(i);

i := i + 1;

exit when (i>=11);

end loop;

end;

---------------------while 循环 ---------------------------

declare

j binary_integer := 1;

begin

while j < 11 loop

dbms_output.put_line(j);

j:=j+1;

end loop;

end;

---------------------for 循环 ---------------------------

begin

for k in 1..10 loop

dbms_output.put_line(k);

end loop;

for k in reverse 1..10 loop

dbms_output.put_line(k);

end loop;

end;

46、-----------------------很是(1) ---------------------------

declare

v_temp number(4);

begin

select empno into v_temp from emp where deptno = 10;

exception

when too_many_rows then

dbms_output.put_line("太多记录了");

when others then

dbms_output.put_line("error");

end;

-----------------------很是(2) ---------------------------

declare

v_temp number(4);

begin

select empno into v_temp from emp where empno = 2222;

exception

when no_data_found then

dbms_output.put_line("没有该项数据");

end;

----------------弊端记录日志(用表记录:将体系日志存到数据库便于当前检察) -----------

 

竖坐序列(用往向置处分递增的ID):

create sequence seq_errorlog_id start with 1 increment by 1;

 

竖坐日志表:

create table errorlog

(

id number primary key,

errcode number,

errmsg varchar2(1024),

errdate date

);

 

示例步伐:

declare

v_deptno dept.deptno%type := 10;

v_errcode  number;

v_errmsg varchar2(1024);

begin

delete from dept where deptno = v_deptno;

   commit;

exception

when others then

rollback;

v_errcode := SQLCODE;

v_errmsg := SQLERRM;

insert into errorlog values (seq_errorlog_id.nextval, v_errcode,v_errmsg, sysdate);

commit;

end;

 

47、---------------------PL/SQL中的重点cursor(游标)和指针的不雅点差不久不多----------------------

declare

cursor c is

select * from emp; //此处的语句不会坐刻执行,而是当下里的open c的时辰,才会真正往数据库中取数据

v_emp c%rowtype;

begin

open c;

fetch c into v_emp;

dbms_output.put_line(v_emp.ename); //这样会只输出一条数据 134将使用循环的方法输出每一条记录

  close c;

end;

----------------------使用do while  循环遍历游标中的每个数据---------------------

declare

cursor c is

select * from emp;

v_emp c%rowtype;

begin

open c;

loop

fetch c into v_emp;

(1) exit when (c%notfound);  //notfound是oracle中的关头字,作用是断定能否还有下一条数据

(2) dbms_output.put_line(v_emp.ename);  //(1)(2)的逆序不克不及颠倒,不然会把最初一条成果再多打印一次。

   end loop;

   close c;

end;

------------------------使用while循环,遍历游标---------------------

declare

cursor c is

select * from emp;

v_emp emp%rowtype;

begin

open c;

fetch c into v_emp;

while(c%found) loop

   dbms_output.put_line(v_emp.ename);

   fetch c into v_emp;

   end loop;

   close c;

end;

--------------------------使用for 循环,遍历游标(最便利快捷的方法!)---------------------

declare

cursor c is

   select * from emp;

begin

for v_emp in c loop

dbms_output.put_line(v_emp.ename);

end loop;

end;

 

---------------------------带参数的游标(相等于函数)---------------------

declare

cursor c(v_deptno emp.deptno%type, v_job emp.job%type)

is

   select ename, sal from emp where deptno=v_deptno and job=v_job; 

begin

for v_temp in c(30, "CLERK") loop

dbms_output.put_line(v_temp.ename);

end loop;

end;

-------------------------可更新的游标-----------------------------

declare

cursor c 

is

   select * from emp2 for update;

begin

   for v_temp in c loop

if(v_temp.sal < 2000) then

update emp2 set sal = sal * 2 where current of c;

      elsif (v_temp.sal =5000) then

delete from emp2 where current of c;

       end if;

     end loop;

     commit;

end;

 

49、------------------------------store procedure存储过程(带驰名字的步伐块) -------------------

create or replace procedure p

is--除这两句替换declare,下里的语句齐部皆一样  

    cursor c is

select * from emp2 for update;

begin

     for v_emp in c loop

if(v_emp.deptno = 10) then

update emp2 set sal = sal +10 where current of c;

else if(v_emp.deptno =20) then

update emp2 set sal =  sal + 20 where current of c;

else

update emp2 set sal = sal + 50 where current of c;

end if;

    end loop;

  commit;

 end;

 

执行存储过程的两种方法:

(1)exec p;(p是存储过程的名称)

(2)begin

p;

 end;

/

-------------------------------带参数的存储过程

先竖坐存储过程:(in标识传入参数,out标识传出参数,默以为传入参数)

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;

 

------------------删除存储过程---------------------------

drop procedure p;

 

50、------------------------竖坐函数打定小我所得税的税率-------------------------------------

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.20;

end if;

end;

 

-----------------------------竖坐触发器(trigger) 触发器不克不及整丁的存正在,必须倚赖正在某一张表上

写 主语 谓语 宾语 游戏

竖坐触发器的倚赖表:

 

create table emp2_log

(

ename varchar2(30) ,

eaction varchar2(20),

etime date

);

 

create or replace trigger trig

after insert or delete or update on emp2 for each row --减上此句,每更新一行,触发一次,不插手则值触发一次

begin

if inserting then

insert into emp2_log values(USER, "insert", sysdate);

elsif updating then

insert into emp2_log values(USER, "update", sysdate);

elsif deleting then

insert into emp2_log values(USER, "delete", sysdate);

end if;

end;

51、--------------------触发器用法之一:经由过程触发器更新束缚的相关数据-------------------

create or replace trigger trig

after update on dept

for each row

begin

update emp set deptno =:NEW.deptno where deptno =: OLD.deptno;

end;

 

 

//////只编译不隐示的规画法子 set serveroutput on;

52、-------------------------------经由过程竖坐存储过程完成递回

create or replace procedure p(v_pid article.pid%type,v_level binary_integer) is

cursor c is select * from article where pid = v_pid;

v_preStr varchar2(1024) := "";

begin

  for i in 0..v_leave loop

v_preStr := v_preStr || "****";

  end loop;

 

  for v_article in c loop

dbms_output.put_line(v_article.cont);

if(v_article.isleaf = 0) then

p(v_article.id);

end if;

end loop;

end;

-------------------------------检察当前用户下有哪些表---

起首,用这个用户登录然后使用语句:

select * from tab;

 

-----------------------------用Oracle举办分页!--------------

由于Oracle中的隐含字段rownum不撑持">"以是:

select * from (

select rownum rn, t.* from (

select * from t_user where user_id <> "root"

) t where rownum <6

) where rn >3

------------------------Oracle下里的浑屏饬令----------------

clear screen; 或 cle scr;

 

-----------将竖坐好的guohailong的这个用户的稀码改成abc--------------

    alter user guohailong identified by abc

    当稀码使用的是数字的时辰可以也许会不行


<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21458230/viewspace-1116480/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2009-04-23