1. <tt id="5hhch"><source id="5hhch"></source></tt>
    1. <xmp id="5hhch"></xmp>

  2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

    <rp id="5hhch"></rp>
        <dfn id="5hhch"></dfn>

      1. oracle數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí)

        時(shí)間:2023-01-21 16:24:57 Oracle認(rèn)證 我要投稿

        oracle數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí)

          20世紀(jì)約70年代 一間名為Ampex的軟件公司,正為中央情報(bào)局設(shè)計(jì)一套名叫Oracle的數(shù)據(jù)庫(kù),埃里森是程序員之一。下面是小編整理的關(guān)于oracle數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí),歡迎大家參考!

        oracle數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí)

          【1】oracle數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí)

          第一篇 基本操作

          --解鎖用戶 alter user 用戶 account unlock;

          --鎖定用戶 alter user 用戶 account lock;

          alter user scott account unlock;

          --創(chuàng)建一個(gè)用戶yc 密碼為a create user 用戶名 identified by 密碼;

          create user yc identified by a;

          --登錄不成功,會(huì)缺少create session 權(quán)限,賦予權(quán)限的語法 grant 權(quán)限名 to 用戶;

          grant create session to yc;

          --修改密碼 alter user 用戶名 identified by 新密碼;

          alter user yc identified by b;

          --刪除用戶

          drop user yc ;

          --查詢表空間

          select *from dba_tablespaces;

          --查詢用戶信息

          select *from dba_users;

          --創(chuàng)建表空間

          create tablespace ycspace

          datafile 'E:\oracle\app\product\11.2.0\dbhome_1\oradata\ycspace.dbf'

          size 2m

          autoextend on next 2m maxsize 5m

          offline ;

          --創(chuàng)建臨時(shí)表空間

          create temporary yctempspace

          tempfile 'E:\oracle\app\product\11.2.0\dbhome_1\oradata\ycspace.dbf'

          size 2m

          autoextend on next 2m maxsize 5m

          offline ;

          --查詢數(shù)據(jù)文件

          select *from dba_data_files;

          --修改表空間

          --1、修改表空間的狀態(tài)

          --默認(rèn)情況下是online,只有在非離線情況下才可以進(jìn)行修改

          alter tablespace ycspace offline ; --離線狀態(tài),不允許任何對(duì)象對(duì)該表空間的使用,使用情況:應(yīng)用需要更新或維護(hù)的時(shí)候;數(shù)據(jù)庫(kù)備份的時(shí)候

          alter tablespace ycspace read write;--讀寫狀態(tài)

          alter tablespace ycspace online;

          alter tablespace ycspace read only; --只讀,可以查詢信息,可以刪除表空間的對(duì)象,但是不能創(chuàng)建對(duì)象和修改對(duì)象 。使用情況:數(shù)據(jù)存檔的時(shí)候

          --2、修改表空間的大小

          --增加文件的大小

          alter database datafile 'E:\oracle\app\product\11.2.0\dbhome_1\oradata\ycspace.dbf' resize 10m;

          --增加數(shù)據(jù)文件

          alter tablespace ycspace add datafile 'E:\oracle\app\product\11.2.0\dbhome_1\oradata\add.dbf' size 2m;

          --刪除表空間的數(shù)據(jù)文件

          alter tablespace 表空間的名字 drop datafile 數(shù)據(jù)文件名;

          --刪除表空間

          drop tablespace ycspace;

          --刪除表空間且表空間中的內(nèi)容和數(shù)據(jù)文件

          drop tablespace ycspace including contents and datafiles;

          --指定表空間 的 創(chuàng)建用戶的語法

          create user yc1 identified by a default tablespace ycspace temporary tablespace temp;

          --刪除用戶

          drop user yc1;

          --權(quán)限

          --賦予創(chuàng)建會(huì)話的權(quán)限

          grant create session to yc1;

          --創(chuàng)建一個(gè)表

          create table studentInfo(

          sid int,

          sname varchar2(10)

          );

          --賦予yc1用戶創(chuàng)建表的權(quán)限

          grant create table to yc1;

          --賦予yc1使用表空間的權(quán)限

          grant unlimited tablespace to yc1;

          --系統(tǒng)權(quán)限

          --對(duì)象權(quán)限

          --插入

          insert into studentInfo values (2,'abcd');

          --查詢

          select *from studentInfo;

          --修改

          update studentInfo set sid=1;

          --刪除

          delete studentInfo ;

          drop table studentInfo; --系統(tǒng)權(quán)限刪除表

          --賦權(quán)的語法

          --系統(tǒng)權(quán)限

          grant 權(quán)限名(系統(tǒng)權(quán)限或?qū)ο髾?quán)限,角色,all) to 用戶(角色,public) with admin option;

          --對(duì)象權(quán)限

          grant 權(quán)限名(系統(tǒng)權(quán)限或?qū)ο髾?quán)限,角色,all) on 用戶(角色,public) with grant option;

          --收權(quán)語法

          --系統(tǒng)權(quán)限

          revoke 權(quán)限名(系統(tǒng)權(quán)限或?qū)ο髾?quán)限,角色,all) from 用戶(角色,public) with admin option;

          --對(duì)象權(quán)限

          revoke 權(quán)限名(系統(tǒng)權(quán)限或?qū)ο髾?quán)限,角色,all) from 用戶(角色,public) with grant option;

          --賦予創(chuàng)建用戶的權(quán)限并且把這個(gè)權(quán)限傳遞下去,即yc1可以給別人賦權(quán)

          grant create user to yc1 with admin option;

          --收回權(quán)限,只能收回scottd ,不能收回由scott賦權(quán)的yc1的權(quán)限

          revoke create user from scott;

          --查看用戶所具有的權(quán)限

          select *from user_sys_privs;

          --對(duì)象權(quán)限詳解

          select * from emp;

          --使用yc1來查詢scott里面的emp表

          select * from scott.emp;

          --賦予yc1查詢emp表和插入的權(quán)限

          grant select on emp to yc1;

          grant insert on emp to yc1;

          grant update(empno,ename) on emp to yc1;

          grant delete on emp to yc1;

          --對(duì)scott的emp表添加數(shù)據(jù)

          insert into scott.emp(empno,ename) value(111,'acv');

          update scott.emp set ename='yc'where empno=111;

          --賦予查詢、賦予刪除、添加、修改

          grant select on 表名 to 用戶

          --grant select,delete,update,insert on 表名 to 用戶

          grant select,delete,update,insert on emp to yc1;

          grant all on dept to yc1; --all代表所有的對(duì)象權(quán)限

          select *from scott.emp;

          select *from scott.dept;

          insert into scott.dept values(50,'企事業(yè)文化部','bumen');

          --查看角色

          --dba:數(shù)據(jù)庫(kù)管理員,系統(tǒng)最高權(quán)限,可以創(chuàng)建數(shù)據(jù)結(jié)構(gòu)(表空間等)

          --resource:可以創(chuàng)建實(shí)體(表、視圖),不可以創(chuàng)建數(shù)據(jù)庫(kù)的結(jié)構(gòu)

          --connect:連接的權(quán)限,可以登錄數(shù)據(jù)庫(kù),但是不可以創(chuàng)建實(shí)體和不可以創(chuàng)建數(shù)據(jù)庫(kù)結(jié)構(gòu)

          select *from role_sys_privs;

          grant connect to yc1;

          --將可以連接的角色賦予給yc1,則yc1就是應(yīng)該可以連接數(shù)據(jù)庫(kù)的人,類似于 create session 。

          create table StuInfos(sid int);

          select *from StuInfos;

          create table stuInfo(

          sid int primary key , --主鍵 primary key 非空且唯一 (主鍵約束)

          sname varchar2(10) not null, --姓名不能為空,(非空約束)

          sex char(2) check(sex in('男','女')), --(檢查約束),check,

          age number(3,1) constraint ck_stuInfo_age check(age>10 and age<100) , --也可以用varchar ;age between 10 and 100 ,在10和100之間,是一個(gè)閉區(qū)間

          tel number(15) unique not null, --唯一約束,

          address varchar2(200) default '什么鬼'

          )

          insert into stuInfo values(3,'大大','男',18,4321543,default);

          insert into stuInfo values(1,'張三','男',10);

          select *from stuInfo;

          drop table stuInfo;

          create table classInfo(

          cid int primary key, --班級(jí)id

          cname varchar2(20) not null unique --班級(jí)名

          )

          create table stuInfo(

          sid int primary key,

          sname varchar2(20),

          cid int constraint fofk_stuInfo_cid references classInfo(cid) on delete cascade

          )

          insert into classInfo values(1,'1班');

          insert into classInfo values(2,'2班');

          insert into classInfo values(3,'3班');

          insert into classInfo values(4,'4班');

          select *from classInfo;

          select *from stuInfo;

          insert into stuInfo values(1001,'張三',2);

          insert into stuInfo values(1002,'張四',4);

          update classInfo set cid=1 where cid=8;

          drop table stuInfo;--要先刪除這個(gè)

          drop table classInfo; --再刪除這個(gè)

          delete classInfo where cid=4 ;--同時(shí)刪除這兩個(gè)表中的4

          --刪除用戶的時(shí)候

          drop user yc1 [cascade] --刪除用戶的同時(shí)把它創(chuàng)建的對(duì)象都一起刪除

          --修改表

          --1、添加表中字段

          --alter table 表名 add 字段名 類型

          alter table classInfo add status varchar2(10) default '未畢業(yè)'

          --2、修改已有字段的數(shù)據(jù)類型

          --alter table 表名 modify 字段名 類型

          alter table classInfo modify status number(1)

          --3、修改字段名

          --alter table 表名 rename column 舊字段名 to 新的字段名

          alter table classInfo rename column cname to 班級(jí)名;

          --4、刪除字段

          --alter table 表名 drop column 字段名

          alter table classInfo drop column status ;

          --5、修改表名

          --rename 舊表名 to 新表名

          rename classInfo to 班級(jí)信息;

          --刪除表

          --1、截?cái)啾硇矢,每刪除一次會(huì)產(chǎn)生一次日志 2、截?cái)鄷?huì)釋放空間,而delete不會(huì)釋放空間

          --刪除表結(jié)構(gòu)和數(shù)據(jù)

          drop table 表名;

          --刪除表中所有數(shù)據(jù)

          truncate table classInfo;

          delete classInfo;

          create table classInfo(

          cid int primary key, --班級(jí)id

          cname varchar2(20) not null unique , --班級(jí)名

          stasuts varchar2(100)

          );

          select *from classInfo;

          --數(shù)據(jù)的操作

          --增加數(shù)據(jù)語法

          --insert into 表名[(列名,....)] values (對(duì)應(yīng)的數(shù)據(jù)的值);

          insert into classInfo values(1,'一班','未畢業(yè)');--需要按照表結(jié)構(gòu)的順序插入

          insert into classInfo values(4,'六班','未畢業(yè)');

          insert into classInfo(cname,cid) values('二班',2); --需要按照括號(hào)中的順序插入,但是 not null primary key 必須插入的。

          insert into classInfo(cname,cid) values('三班',3);

          --刪除的語法

          --delete 表名 [where 條件]

          delete classInfo where cid>=2;

          --修改記錄的語法

          --update 表名 set [字段='值' ] [where 條件]

          update classInfo set cname='三班'; --會(huì)修改所有該字段

          update classInfo set cname='四班' where cid=1;

          update classInfo set cname='五班', stasuts ='未畢業(yè)' where cid=3;

          --alter table classInfo drop constraint SYS_C0011213;

          --添加多個(gè)時(shí)可以使用序列

          --用序列來做自動(dòng)增長(zhǎng)

          create sequence seq_classInfo_cid start with 1001 increment by 1;

          insert into classInfo values(seq_classInfo_cid.Nextval,'七班','未畢業(yè)');

          insert into classInfo values(seq_classInfo_cid.Nextval,'八班','未畢業(yè)');

          insert into classInfo values(seq_classInfo_cid.Nextval,'九班','未畢業(yè)');

          insert into classInfo values(seq_classInfo_cid.Nextval,'十班','未畢業(yè)');

          create table classInfo2(

          cid int primary key, --班級(jí)id

          cname varchar2(20) not null unique , --班級(jí)名

          stasuts varchar2(100)

          );

          select *from classInfo2;

          drop table classInfo2;

          insert into classInfo2 select *from classInfo;

          insert into classInfo(cname,cid) select cname,cid from classInfo;

          alter table classInfo2 drop constraint SYS_C0011213;

          select seq_classInfo_cid.nextval from dual;

          select seq_classInfo_cid.Currval from dual;

          --直接創(chuàng)建一個(gè)新表,并拿到另一個(gè)表其中的數(shù)據(jù)

          create table newTable as select cname,cid from classInfo;

          create table newTable1 as select *from classInfo;

          select *from newTable;

          select *from newTable1;

          insert into newTable1 values(1008,'dg','');

          第二篇:高級(jí)操作

          直接在使用scott登陸,進(jìn)行查詢操作

          ----------------------------------------------------------------------------------

          --簡(jiǎn)單查詢

          select *from emp;

          select empno as id,ename as name from emp;

          select empno 編號(hào),ename 姓名 from emp;

          --去除重復(fù)

          select job from emp;

          select distinct job from emp;

          select job,deptno from emp;

          select distinct job,deptno from emp;

          --字符串的連接

          select '員工編號(hào)是' ||empno || '姓名是' ||ename ||'工作是'||job from emp;

          --乘法

          select ename,sal *12 from emp;

          --加減乘除都類似

          ---------------------------------------------------------------------

          --限定查詢

          --獎(jiǎng)金大于1500的

          select *from emp where sal>1500;

          --有獎(jiǎng)金的

          select *from emp where comm is not null;

          --沒有獎(jiǎng)金的

          select *from emp where comm is null;

          --有獎(jiǎng)金且大于1500的

          select *from emp where sal>1500 and comm is not null;

          --工資大于1500或者有獎(jiǎng)金的

          select *from emp where sal>1500 or comm is not null;

          --工資不大于1500且沒獎(jiǎng)金的

          select *from emp where sal<=1500 and comm is null;

          select *from emp where not (sal >1500 or comm is not null);

          --工資大于1500但是小于3000的

          select *from emp where sal>1500 and sal<3000;

          select *from emp where sal between 1500 and 3000; --between是閉區(qū)間,是包含1500和3000的

          --時(shí)間區(qū)間

          select *from emp where hiredate between to_date('1981-01-01','yyyy-MM-dd') and to_date('1981-12-31','yyyy-MM-dd');

          --查詢雇員名字

          select *from emp where ename='SMITH';

          --查詢員工編號(hào)

          select *from emp where empno=7369 or empno=7499 or empno=7521;

          select *from emp where empno in(7369,7499,7521);

          select *from emp where empno not in(7369,7499,7521); --排除這3個(gè),其他的都可以查

          --模糊查詢

          select *from emp where ename like '_M%'; --第2個(gè)字母為M的

          select *from emp where ename like '%M%';

          select *from emp where ename like '%%'; --全查詢

          --不等號(hào)的用法

          select * from emp where empno !=7369;

          select *from emp where empno<> 7369;

          --對(duì)結(jié)果集排序

          --查詢工資從低到高

          select *from emp order by sal asc;

          select *from emp order by sal desc,hiredate desc; --asc 當(dāng)導(dǎo)游列相同時(shí)就按第二個(gè)來排序

          --字符函數(shù)

          select *from dual;--偽表

          select 2*3 from dual;

          select sysdate from dual;

          --變成大寫

          select upper('smith') from dual;

          --變成小寫

          select lower('SMITH') from dual;

          --首字母大寫

          select initcap('smith') from dual;

          --連接字符串

          select concat('jr','smith') from dual; --只能在oracle中使用

          select 'jr' ||'smith' from dual; --推薦使用

          --截取字符串

          select substr('hello',1,3) from dual; --索引從1開始

          --獲取字符串長(zhǎng)度

          select length('hello') from dual;

          --字符串替換

          select replace('hello','l','x') from dual; --把l替換為x

          --------------------------------------------------------------------------------------------------

          --通用函數(shù)

          --數(shù)值函數(shù)

          --四舍五入

          select round(12.234) from dual;--取整的四舍五入 12

          select round (12.657,2) from dual; --保留2位小數(shù)

          select trunc(12.48) from dual;--取整

          select trunc(12.48675,2) from dual; --保留2位小數(shù)

          --取余

          select mod(10,3) from dual;--10/3取余 =1

          --日期函數(shù)

          --日期-數(shù)字=日期 日期+數(shù)字=日期 日期-日期=數(shù)字

          --查詢員工進(jìn)入公司的周數(shù)

          select ename,round((sysdate -hiredate)/7) weeks from emp;

          --查詢所有員工進(jìn)入公司的月數(shù)

          select ename,round(months_between(sysdate,hiredate)) months from emp;

          --求三個(gè)月后的日期

          select add_months(sysdate,6) from dual;

          select next_day(sysdate,'星期一') from dual; --下星期

          select last_day(sysdate) from dual; --本月最后一天

          select last_day(to_date('1997-1-23','yyyy-MM-dd')) from dual;

          --轉(zhuǎn)換函數(shù)

          select ename ,

          to_char(hiredate,'yyyy') 年,

          to_char(hiredate,'mm')月,

          to_char(hiredate,'dd') 日

          from emp;

          select to_char(10000000,'$999,999,999') from emp;

          select to_number('20')+to_number('80') from dual; --數(shù)字相加

          --查詢員工年薪

          select ename,(sal*12+nvl(comm,0)) yearsal from emp; --空和任何數(shù)計(jì)算都是空

          --Decode函數(shù),類似if else if (常用)

          select decode(1,1,'one',2,'two','no name') from dual;

          --查詢所有職位的中文名

          select ename, decode(job,

          'CLERK',

          '業(yè)務(wù)員',

          'SALESMAN',

          '銷售',

          'MANAGER',

          '經(jīng)理',

          'ANALYST',

          '分析員',

          'PRESIDENT',

          '總裁',

          '無業(yè)')

          from emp;

          select ename,

          case

          when job = 'CLERK' then

          '業(yè)務(wù)員'

          when job = 'SALESMAN' then

          '銷售'

          when job = 'MANAGER' then

          '經(jīng)理'

          when job = 'ANALYST' then

          '分析員'

          when job = 'PRESIDENT' then

          '總裁'

          else

          '無業(yè)'

          end

          from emp;

          -------------------------------------------------------------------------------------------

          --多表查詢

          select *from dept;

          select *from emp,dept order by emp.deptno;

          select *from emp e,dept d where e.deptno=d.deptno;

          select e.*,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno;

          --查詢出雇員的編號(hào),姓名,部門編號(hào),和名稱,地址

          select e.empno,e.ename,e.deptno,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno;

          --查詢出每個(gè)員工的上級(jí)領(lǐng)導(dǎo)

          select e.empno,e.ename,e1.empno,e1.ename from emp e,emp e1 where e.mgr=e1.empno;

          select e.empno,e.ename,d.dname

          from emp e,dept d ,salgrade s, emp e1

          where e.deptno=d.deptno

          and e.sal between s.losal

          and s.hisal

          and e.mgr=e1.empno;

          select e.empno,e.ename,e1.empno,e1.ename from emp e,emp e1 where e.mgr=e1.empno(+) ;

          --外連接

          select *from emp order by deptno;

          --查詢出每個(gè)部門的員工

          /*

          分析:部門表是全量表,員工表示非全量表,

          在做連接條件時(shí),全量表在非全量表的哪端,那么連接時(shí)全量表的連接條件就在等號(hào)哪斷

          */

          --左連接

          select * from dept d,emp e where d.deptno=e.deptno(+) order by e.deptno;

          --右連接

          select * from emp e,dept d where e.deptno(+)=d.deptno order by e.deptno;

          -----------------------------作業(yè)

          --查詢與smith相同部門的員工姓名和雇傭日期

          select *from emp t

          where t.deptno= (select e.deptno from emp e where e.ename='SMITH')

          and t.ename<> 'SMITH';

          --查詢工資比公司平均工資高的員工的員工號(hào),姓名和工資

          select t.empno,t.ename,t.sal

          from emp t

          where t.sal>(select avg(sal) from emp);

          --查詢各部門中工資比本部門平均工資高的員工號(hào),姓名和工資

          select t.empno,t.ename,t.sal

          from emp t, (select avg(e.sal) avgsal,e.deptno from emp e group by e.deptno) a

          where t.sal>a.avgsal and t.deptno=a.deptno;

          --查詢姓名中包含字母u的員工在相同部門的員工的員工號(hào)和姓名

          select t.empno,t.ename from emp t

          where t.deptno in( select e.deptno from emp e where e.ename like '%U%')

          and t.empno not in ( select e.empno from emp e where e.ename like '%U%') ;

          --查詢管理者是king的員工姓名和工資

          select t.ename,t.sal from emp t

          where t.mgr in

          (select e.empno from emp e where e.ename='KING');

          -------------------------------------------------------------------------------------

          ---sql1999語法

          select *from emp join dept using(deptno) where deptno=20;

          select *from emp natural join dept;

          select *from emp e join dept d on e.deptno=d.deptno;

          select *from dept;

          select *from dept d left join emp e on d.deptno=e.deptno;

          select *from dept d,emp e where d.deptno=e.deptno(+);

          ---分組

          select count(empno) from emp group by deptno;

          select deptno,job,count(*) from emp group by deptno,job order by deptno;

          select *from EMP for UPDATE;

          --group by 后面有的字段,select后才可以有,group by后面沒有的字段,select后面絕對(duì)不能有

          select d.dname, d.loc, count(e.empno) from emp e, dept d where e.deptno = d.deptno group by d.dname, d.loc ;

          ----------------------------------------------------------------------------------------------------

          --子查詢

          select *from emp t where t.sal>(select *from emp e where e.empno=7654);

          select rownum ,t.* from emp t where rownum <6 ;

          --pagesize 5

          select *from(select rownum rw,a.* from (select *from emp ) a where rownum <16) b where b.rw>10;

          select *from (select *from emp) where rownum>0;

          --索引

          create index person_index on person(p_name);

          --視圖

          create view view2 as select *from emp t where t.deptno=20;

          select *from view2;

          --------------------------------------------------------------------------------------------------------

          --pl/sql

          --plsql是對(duì)sql語言的過程化擴(kuò)展

          -----

          declare

          begin

          dbms_output.put_line('hello world');

          end;

          -------

          declare

          age number(3);

          marry boolean := true; --boolean不能直接輸出

          pname varchar2(10) := 're jeknc';

          begin

          age := 20;

          dbms_output.put_line(age);

          if marry then

          dbms_output.put_line('true');

          else

          dbms_output.put_line('false');

          end if ;

          dbms_output.put_line(pname);

          end;

          --常量和變量

          --引用變量,引用表中的字段的類型

          Myname emp.ename%type; --使用into來賦值

          declare

          pname emp.ename%type;

          begin

          select t.ename into pname from emp t where t.empno=7369;

          dbms_output.put_line(pname);

          end;

          --記錄型變量

          Emprec emp%rowtype; --使用into來賦值

          declare

          Emprec emp%rowtype;

          begin

          select t.* into Emprec from emp t where t.empno=7369;

          dbms_output.put_line(Emprec.empno || ' '||Emprec.ename||' '||Emprec.job);

          end;

          --if分支

          語法1:

          IF 條件 THEN 語句1;

          語句2;

          END IF;

          語法2:

          IF 條件 THEN 語句序列1;

          ELSE 語句序列 2;

          END IF;

          語法3:

          IF 條件 THEN 語句;

          ELSIF 條件 THEN 語句;

          ELSE 語句;

          END IF;

          --1

          declare

          pname number:=#

          begin

          if pname = 1 then

          dbms_output.put_line('我是1');

          else

          dbms_output.put_line('我不是1');

          end if;

          end;

          --2

          declare

          pname number := #

          begin

          if pname = 1 then

          dbms_output.put_line('我是1');

          elsif pname = 2 then

          dbms_output.put_line('我是2');

          else

          dbms_output.put_line('我不是12');

          end if;

          end;

          --loop循環(huán)語句

          語法2:

          Loop

          EXIT [when 條件];

          ……

          End loop

          --1

          declare

          pnum number(4):=0;

          begin

          while pnum < 10 loop

          dbms_output.put_line(pnum);

          pnum := pnum + 1;

          end loop;

          end;

          --2 (最常用的循環(huán))

          declare

          pnum number(4):=0;

          begin

          loop

          exit when pnum=10;

          pnum:=pnum+1;

          dbms_output.put_line(pnum);

          end loop;

          end;

          --3

          declare

          pnum number(4);

          begin

          for pnum in 1 .. 10 loop

          dbms_output.put_line(pnum);

          end loop;

          end;

          ----------------------------------

          --游標(biāo)

          語法:

          CURSOR 游標(biāo)名 [ (參數(shù)名 數(shù)據(jù)類型,參數(shù)名 數(shù)據(jù)類型,...)] IS SELECT 語句;

          例如:cursor c1 is select ename from emp;

          declare

          cursor c1 is

          select * from emp;

          emprec emp%rowtype;

          begin

          open c1;

          loop

          fetch c1

          into emprec;

          exit when c1%notfound;

          dbms_output.put_line(emprec.empno || ' ' || emprec.ename);

          end loop;

          close c1; --要記得關(guān)閉游標(biāo)

          end;

          --------例外

          --異常,用來增強(qiáng)程序的健壯性和容錯(cuò)性

          -- no_data_found (沒有找到數(shù)據(jù))

          --too_many_rows (select …into語句匹配多個(gè)行)

          --zero_divide ( 被零除)

          --value_error (算術(shù)或轉(zhuǎn)換錯(cuò)誤)

          --timeout_on_resource (在等待資源時(shí)發(fā)生超時(shí))

          --寫出被0除的例外程序

          declare

          pnum number(4) := 10;

          begin

          pnum := pnum / 0;

          exception

          when zero_divide then

          dbms_output.put_line('被0除了');

          when value_error then

          dbms_output.put_line('算術(shù)或轉(zhuǎn)換錯(cuò)誤');

          when others then

          dbms_output.put_line('其他異常');

          end;

          --自定義異常

          --No_data exception;

          --要拋出raise no_data;

          declare

          cursor c1 is

          select * from emp t where t.deptno = 20;

          no_data exception;

          emprec emp%rowtype;

          begin

          open c1;

          loop

          fetch c1

          into emprec;

          if c1%notfound then

          raise no_data;

          else

          dbms_output.put_line(emprec.empno || ' ' || emprec.ename);

          end if;

          end loop;

          close c1;

          exception

          when no_data then

          dbms_output.put_line('無員工');

          when others then

          dbms_output.put_line('其他異常');

          end;

          --存儲(chǔ)過程

          語法:

          create [or replace] PROCEDURE 過程名[(參數(shù)名 in/out 數(shù)據(jù)類型)]

          AS

          begin

          PLSQL子程序體;

          End;

          或者

          create [or replace] PROCEDURE 過程名[(參數(shù)名 in/out 數(shù)據(jù)類型)]

          is

          begin

          PLSQL子程序體;

          End 過程名;

          -----創(chuàng)建一個(gè)存儲(chǔ)過程helloworld

          create or replace procedure helloworld is

          begin

          dbms_output.put_line('hello world');

          end helloworld;

          ------創(chuàng)建一個(gè)漲工資的

          create or replace procedure addsal(eno in emp.empno%type) is

          emprec emp%rowtype;

          begin

          select * into emprec from emp t where t.empno = eno;

          update emp t set t.sal = t.sal + 100 where t.empno = eno;

          dbms_output.put_line('漲工資前是' || emprec.sal || ',漲工資后是' ||

          (emprec.sal + 100));

          end addsal;

          ----------------------------------------------

          --java代碼調(diào)用存儲(chǔ)過程和函數(shù)

          --存儲(chǔ)過程

          --

          create or replace procedure acc_yealsal(eno in emp.empno%type,yearsal out number) is

          pcomm emp.comm%type;

          psal emp.sal%type;

          begin

          select t.sal,t.comm into psal,pcomm from emp t where t.empno=eno;

          yearsal :=psal*12 +nvl(pcomm,0);

          end;

          ----存儲(chǔ)函數(shù)

          create or replace function 函數(shù)名(Name in type, Name in type, .. .)

          return 數(shù)據(jù)類型 is

          結(jié)果變量 數(shù)據(jù)類型;

          begin

          return(結(jié)果變量);

          end函數(shù)名;

          --存儲(chǔ)函數(shù)計(jì)算年薪

          create or replace function accf_yearsal(eno in emp.empno%type)

          return number is

          Result number;

          psal emp.sal%type;

          pcomm emp.comm%type;

          begin

          select t.sal, t.comm into psal, pcomm from emp t where t.empno = eno;

          Result := psal * 12 + nvl(pcomm, 0);

          return(Result);

          end accf_yearsal;

          -----------------------------------

          ---觸發(fā)器

          --觸發(fā)語句:增刪改:

          語法:

          CREATE [or REPLACE] TRIGGER 觸發(fā)器名

          {BEFORE | AFTER}

          {DELETE | INSERT | UPDATE [OF 列名]}

          ON 表名

          [FOR EACH ROW [WHEN(條件) ] ]

          begin

          PLSQL 塊

          End 觸發(fā)器名

          ---插入一個(gè)新員工則觸發(fā)

          create or replace trigger insert_person

          after insert on emp

          begin

          dbms_output.put_line('插入新員工');

          end;

          select *from emp;

          insert into emp values(1001,'李四','管理',7902,sysdate,100,100,20);

          --raise_application_error(-20001, '不能在非法時(shí)間插入員工')

          --==============================================================================

          SQL> @ E:\powerDesigner\A_腳本\user.sql --導(dǎo)入腳本文件

          select *from H_USER ;

          insert into h_user valuer(sequserid.nextval,'a','a',sysdate,'北京',1);

          --------------------------------------------------------------

          --數(shù)據(jù)庫(kù)建模

          --一對(duì)多:多的一端是2,箭頭指向的是表1,即少的一端

          --在實(shí)體類中一的一端的實(shí)體類有多的一端的實(shí)體類的集合屬性

          --使用powerDesiger進(jìn)行數(shù)據(jù)庫(kù)建模,然后將數(shù)據(jù)導(dǎo)入,導(dǎo)入到plsql中進(jìn)行使用

          --------------------連接遠(yuǎn)程數(shù)據(jù)庫(kù)

          --方法1,修改localhost的地址

          ORCL =

          (DESCRIPTION =

          (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))

          (CONNECT_DATA =

          (SERVER = DEDICATED)

          (SERVICE_NAME = orcl.lan)

          )

          )

          --方法2

          --或者直接在登陸界面在database中輸入遠(yuǎn)程數(shù)據(jù)庫(kù)的ip地址和端口號(hào)進(jìn)行遠(yuǎn)程登陸

        【oracle數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí)】相關(guān)文章:

        Oracle數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí):SELECT語句06-21

        Oracle數(shù)據(jù)庫(kù)語句大全05-02

        oracle數(shù)據(jù)庫(kù)基本語句02-08

        oracle數(shù)據(jù)庫(kù)密碼修改設(shè)置途徑06-14

        sqlplus如何訪問遠(yuǎn)程oracle數(shù)據(jù)庫(kù)06-04

        2017年oracle數(shù)據(jù)庫(kù)認(rèn)證考試06-17

        oracle數(shù)據(jù)庫(kù)開發(fā)規(guī)范具體條件08-29

        Oracle認(rèn)證:ORACLE綁定變量BINDPEEKING08-26

        Oracle認(rèn)證:Oracle避免全表掃描方式08-26

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码

        1. <tt id="5hhch"><source id="5hhch"></source></tt>
          1. <xmp id="5hhch"></xmp>

        2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

          <rp id="5hhch"></rp>
              <dfn id="5hhch"></dfn>