ORACLE鑷鏁欑▼
--create tabletestone
( id number, --搴忓彿username varchar2(50),password varchar2(50),sjvarchar2(20));
--瀵煎叆鍛戒护
load data infile'd:\whx\testoracle.txt' append into table testone fields terminated byX'09'(id,username,password,sj);
--oracle鏃ュ織鏂囦欢
Oracle鏃ュ織鏂囦欢绠$悊涓庢煡鐪�
銆�銆�--1.鏌ヨ绯荤粺浣跨敤鐨勬槸鍝竴缁勬棩蹇楁枃浠讹細
銆�銆�select * from v$log;
銆�銆�--2.鏌ヨ姝e湪浣跨敤鐨勭粍鎵�瀵瑰簲鐨勬棩蹇楁枃浠讹細
銆�銆�select * from v$logfile;
銆�銆�--3.寮哄埗鏃ュ織鍒囨崲锛�
銆�銆�alter system switch logfile;
銆�銆�--4.鏌ヨ鍘嗗彶鏃ュ織锛�
銆�銆�select * from v$log_history;
銆�銆�--5.鏌ヨ鏃ュ織鐨勫綊妗fā寮忥細
銆�銆�select dbid,name,created,log_mode from v$database;
銆�銆�--6.鏌ヨ褰掓。鏃ュ織鐨勪俊鎭細
銆�銆�select recid,stamp,thread#,sequence#,name fromv$archived_log;
銆�銆�--7.澧炲姞涓庡垹闄ゆ棩蹇楁枃浠剁粍
銆�銆�alter database add logfile group 1('/home1/oracle/oradata/ora8i/log1a.log'),'/home2/oracle/oradata/ora8i/log1b.log')size 100M;
銆�銆�alter database drop logfile group 1;
銆�銆�--8.澧炲姞涓庡垹闄ゆ棩蹇楁垚鍛�
銆�銆�alter database add logfile member'/home1/oracle/oradata/ora8i/log1a.log' to group1,'/home1/oracle/oradata/ora8i/log2a.log' to group 2;
銆�銆�alter database drop logfile member'/home1/oracle/oradata/ora8i/log1a.log' ;
銆�銆�--9.鏃ュ織鏂囦欢绉诲姩
銆�銆�alter database rename file'/home1/oracle/oradata/ora8i/log1a.log' to'/home2/oracle/oradata/ora8i/log1a.log';
銆�銆�--鎵ц璇ュ懡浠や箣鍓嶅繀椤讳繚璇佽鏃ュ織鏂囦欢鐗╃悊涓婂凡缁忕Щ鍔ㄥ埌鏂扮洰褰�
銆�銆�--10.娓呴櫎鏃ュ織鏂囦欢
銆�銆�alter database clear logfile'/home1/oracle/oradata/ora8i/log1a.log';
銆�銆�--璇ュ懡浠ょ敤浜庝笉鑳界敤鍒犻櫎缁勫強缁勬垚鍛樺懡浠ゅ垹闄ゆ棩蹇楁椂浣跨敤
--鏂囨湰鍐呭瀵煎叆鏁版嵁搴�
---灏嗘枃鏈枃浠�/(excel琛ㄤ腑)鐨勫唴瀹瑰鍏ュ埌oracle鏁版嵁搴撲腑---鍙互鍒╃敤PL/SQL developer
--棣栧厛鏌ョ湅oracle鏁版嵁搴撶殑缂栫爜SQL> select * fromnls_database_parameters where parameter ='NLS_CHARACTERSET';
PARAMETERVALUE------------------------------ --------------------------------------------------------------------------------NLS_CHARACTERSETZHS16GBK
--杩欏叾鏉ユ簮浜巔rops$锛岃繖鏄〃绀烘暟鎹簱鐨勫瓧绗﹂泦銆�--oracle瀹㈡埛绔紪鐮丼QL> select * from nls_instance_parameters whereparameter='NLS_LANGUAGE';
PARAMETERVALUE--------------------------------------------------------------------------------------------------------------------------------------------NLS_LANGUAGEAMERICAN
--鍏舵潵婧愪簬v$parameter锛岃〃绀哄鎴风鐨勫瓧绗﹂泦鐨勮缃紝鍙兘鏄弬鏁版枃浠讹紝鐜鍙橀噺鎴栬�呮槸娉ㄥ唽琛ㄤ細璇濆瓧绗﹂泦鐜SQL>select *
from nls_session_parameters;--鍏舵潵婧愪簬v$nls_parameters锛岃〃绀轰細璇濊嚜宸辩殑璁剧疆锛屽彲鑳芥槸浼氳瘽鐨勭幆澧冨彉閲忔垨鑰呮槸alter session瀹屾垚锛屽鏋滀細璇濇病鏈夌壒娈婄殑璁剧疆锛屽皢涓巒ls_instance_parameters涓�鑷淬��--鍐嶆潵璇翠竴涓嬫�庝箞淇敼oracle鐨勫瓧绗﹂泦锛�--鐩墠鎴戠殑鏁版嵁搴撶幆澧冪殑瀛楃闆嗘槸AL32UTF8锛岄偅涔堟妸瀹冩敼鎴怹HS16GBK --1.棣栧厛浠ysdba鐨勮韩浠界櫥褰曚笂鍘� conn /as sysdba SQL>conn sys/ych as
sysdba; --2.鍏抽棴鏁版嵁搴搒hutdown immediate; --3.浠ount鎵撴潵鏁版嵁搴擄紝startup mount --4.璁剧疆session SQL>ALTER SYSTEM ENABLE RESTRICTED
SESSION;SQL> ALTER SYSTEM SET JOB_QUEUE_PROCESSES=0;--PL/SQL Developer涓嶰racle11g杩滅▼杩炴帴闂http://zhumeng8337797.blog.163.com/blog/static/10076891420111115104023136/
--Oracle11g缂栫爜闂
SQL> ALTER
SYSTEM SET AQ_TM_PROCESSES=0; --5.鍚姩鏁版嵁搴揝QL>alter
database open; --6.淇敼瀛楃闆哠QL>ALTER DATABASE CHARACTER SET
ZHS16GBK;--杩欎細鍙兘浼氭姤閿欙紝鎻愮ず鎴戜滑鐨勫瓧绗﹂泦锛氭柊瀛楃闆嗗繀椤讳负鏃у瓧绗﹂泦鐨勮秴闆嗭紝杩欐椂鎴戜滑鍙互璺宠繃瓒呴泦鐨勬鏌ュ仛鏇存敼锛歋QL>ALTER
DATABASE character set INTERNAL_USE ZHS16GBK;--杩欐潯璇彞灏卞彲浠ヤ簡锛孴ERNAL_USE鎻愪緵鐨勫府鍔╁氨浼氫娇oracle缁曡繃浜嗗瓙闆嗕笌瓒呴泦鐨勯獙璇侊紝杩欐潯璇彞鍜屼笂闈㈢殑璇彞鍐呴儴鎿嶄綔鏃跺畬鍏ㄧ浉鍚岀殑銆� --7.鍏抽棴锛岄噸鏂板惎鍔⊿QL>shutdown immediate;SQL>startup----------------------------------------------------------------------------------------------------/==============================================================//*DBMS name: MySQL 4.0 *//* Created on: 2007-6-28 15:25:46 *//==============================================================/
drop table ifexists admin;
drop table ifexists answer;
drop table ifexists subitem;
drop table ifexists item;
drop table ifexists question;
drop table ifexists user;
/==============================================================//*Table: admin *//==============================================================/
SQL>createsequence s_admin increment by 1;SQL>create table admin( id INT PRIMARY KEY ,adminid VARCHAR2(50) not null, adminpwd VARCHAR2(50)) ;
/==============================================================//*Table: question *//==============================================================/SQL>createsequence s_question increment by 1;SQL>create table question( qid int notnull, title VARCHAR2(50), content clob, itemid int, subid int, useridVARCHAR2(50), grade VARCHAR2(50), offerscore int, status int, questiontimedate, clickcount int, acceptflag int, commenflag int, primary key (qid)) ;
/==============================================================//*Table: answer *//==============================================================/SQL>createtable answer( aid int not null, quesans VARCHAR2(50), userid VARCHAR2(50),grade VARCHAR2(50), anstime date, status int, qid int, primary key (aid) ,foreign key (qid) references question(qid) on delete cascade ) ;
/==============================================================//*Table: item *//==============================================================/createtable item( itemid int not null, itemname VARCHAR2(50), itemcode int, primarykey (itemid)) ;
/==============================================================//*Table: subitem *//==============================================================/createtable subitem( subid int not null, subname varchar2(50), itemid int, subcodeint, primary key (subid) , foreign key (itemid) references item(itemid) ondelete cascade) ;
/==============================================================//*Table: user *//==============================================================/createtable table_user( id INT PRIMARY KEY , userid VARCHAR2(50) not null , userpwdVARCHAR2(50), userques VARCHAR2(50), userans VARCHAR2(50), usermailVARCHAR2(50), integral int, grade int, sex VARCHAR2(2), realname VARCHAR2(50));
----------------------------------------------------------------------------------------------------
SQL>setserveroutput on;SQL> create table t_student (id number(10,0) not null, namevarchar2(255) not null, age number(10,0), sex varchar2(255), good char(1),primary key (id, name));SQL> create table t_teacher (id number(10,0) notnull, birthDate date, gender varchar2(255), good number(1,0) not null, namevarchar2(255), title varchar2(255), primary key (id));SQL> delete fromteacher where id=2;SQL> update use setemail='[email protected]'whereusername='xiaowang';SQL> drop table userba2;SQL> create tablet1_student(id varchar2(255),name varchar2(255),age varchar2(255),good char(1));SQL> create table userba2(id number,username varchar2(20),passwordvarchar2(20));SQL> drop table t_group;sql> drop table t_user;
--1. 鏌ヨOracle涓墍鏈夌敤鎴蜂俊鎭疭QL> select*from dba_users;
--2. 鍙煡璇㈢敤鎴峰拰瀵嗙爜sql>select username,password from dba_users;
--3. 鏌ヨ褰撳墠鐢ㄦ埛淇℃伅select*from dba_ustats;
--閫氳繃pl/sql涓殑鈥滄祻瑙堝櫒鈥濅篃鍙互鏌ョ湅user淇℃伅
--鏌ョ湅褰撳墠鐢ㄦ埛鐨勭己鐪佽〃绌洪棿
銆�銆�SQL>select username,default_tablespace fromuser_users;
---鏌ョ湅褰撳墠鐢ㄦ埛鐨勭郴缁熸潈闄愬拰琛ㄧ骇鏉冮檺
銆�銆�SQL>select * from user_sys_privs;
銆�銆�SQL>select * from user_tab_privs;
---鏌ョ湅鐢ㄦ埛涓嬫墍鏈夌殑琛�
銆�銆�SQL>select * from user_tables;
--1銆佺敤鎴�
----鏌ョ湅褰撳墠鐢ㄦ埛鐨勭己鐪佽〃绌洪棿
銆�銆�SQL>select username,default_tablespace fromuser_users;
----鏌ョ湅褰撳墠鐢ㄦ埛鐨勮鑹�
銆�銆�SQL>select * from user_role_privs;
----鏌ョ湅褰撳墠鐢ㄦ埛鐨勭郴缁熸潈闄愬拰琛ㄧ骇鏉冮檺
銆�銆�SQL>select * from user_sys_privs;
銆�銆�SQL>select * from user_tab_privs;
----鏄剧ず褰撳墠浼氳瘽鎵�鍏锋湁鐨勬潈闄�
銆�銆�SQL>select * from session_privs;
----鏄剧ず鎸囧畾鐢ㄦ埛鎵�鍏锋湁鐨勭郴缁熸潈闄�
銆�銆�SQL>select * from dba_sys_privs where grantee='GAME';
--2銆佽〃
----鏌ョ湅鐢ㄦ埛涓嬫墍鏈夌殑琛�
銆�銆�SQL>select * from user_tables;
----鏌ョ湅鍚嶇О鍖呭惈log瀛楃鐨勮〃
銆�銆�SQL>select object_name,object_id from user_objects
銆�銆�where instr(object_name,'LOG')>0;
----鏌ョ湅鏌愯〃鐨勫垱寤烘椂闂�
銆�銆�SQL>select object_name,created from user_objects whereobject_name=upper('&table_name');
----鏌ョ湅鏌愯〃鐨勫ぇ灏�
銆�銆�SQL>select sum(bytes)/(1024*1024) as"size(M)" from user_segments wheresegment_name=upper('&table_name');
----鏌ョ湅鏀惧湪ORACLE鐨勫唴瀛樺尯閲岀殑琛�
銆�銆�SQL>select table_name,cache from user_tables whereinstr(cache,'Y')>0;
--3銆佺储寮�
----鏌ョ湅绱㈠紩涓暟鍜岀被鍒�
銆�銆�SQL>select index_name,index_type,table_name fromuser_indexes order by table_name;
----鏌ョ湅绱㈠紩琚储寮曠殑瀛楁
銆�銆�SQL>select * from user_ind_columns whereindex_name=upper('&index_name');
----鏌ョ湅绱㈠紩鐨勫ぇ灏�
銆�銆�SQL>select sum(bytes)/(1024*1024) as"size(M)" from user_segments
銆�銆�where segment_name=upper('&index_name');
--4銆佸簭鍒楀彿
銆�銆�鏌ョ湅搴忓垪鍙凤紝last_number鏄綋鍓嶅��
銆�銆�SQL>select * from user_sequences;
銆�銆�5銆佽鍥�
----鏌ョ湅瑙嗗浘鐨勫悕绉�
銆�銆�SQL>select view_name from user_views;
----鏌ョ湅鍒涘缓瑙嗗浘鐨剆elect璇彞
銆�銆�SQL>set view_name,text_length from user_views;
銆�銆�SQL>set long 2000; 璇存槑锛氬彲浠ユ牴鎹鍥剧殑text_length鍊艰瀹歴et long 鐨勫ぇ灏�
銆�銆�SQL>select text from user_views whereview_name=upper('&view_name');
--6銆佸悓涔夎瘝
----鏌ョ湅鍚屼箟璇嶇殑鍚嶇О
銆�銆�SQL>select * from user_synonyms;
--7銆佺害鏉熸潯浠�
----鏌ョ湅鏌愯〃鐨勭害鏉熸潯浠�
銆�銆�SQL>select constraint_name,constraint_type,search_condition, r_constraint_name
銆�銆�from user_constraints where table_name =upper('&table_name');
銆�銆�SQL>selectc.constraint_name,c.constraint_type,cc.column_name
銆�銆�from user_constraints c,user_cons_columns cc
銆�銆�where c.owner = upper('&table_owner') andc.table_name = upper('&table_name')
銆�銆�and c.owner = cc.owner and c.constraint_name =cc.constraint_name
銆�銆�order by cc.position;
--8銆佸瓨鍌ㄥ嚱鏁板拰杩囩▼
----鏌ョ湅鍑芥暟鍜岃繃绋嬩互鍙婂寘鐨勭姸鎬�
銆�銆�SQL>select object_name,status from user_objects where
object_type='FUNCTION';銆�銆�SQL>select object_name,status fromuser_objects where object_type='PROCEDURE'; SQL>select object_name,statusfrom user_objects where object_type='PACKAGE';
----鏌ョ湅鍑芥暟鍜岃繃绋嬩互鍙婂寘鐨勬簮浠g爜
銆�銆�SQL>select text from all_source where owner=user andname=upper('&plsql_name');
---ORACLE涓暟鎹瓧鍏歌鍥惧垎涓�3澶х被,鐢ㄥ墠缂�鍖哄埆锛屽垎鍒负锛歎SER锛孉LL 鍜� DBA锛岃澶氭暟鎹瓧鍏歌鍥惧寘鍚浉浼肩殑淇℃伅銆�--USER_*:鏈夊叧鐢ㄦ埛鎵�鎷ユ湁鐨勫璞′俊鎭紝鍗崇敤鎴疯嚜宸卞垱寤虹殑瀵硅薄淇℃伅--ALL_*锛氭湁鍏崇敤鎴峰彲浠ヨ闂殑瀵硅薄鐨勪俊鎭紝鍗崇敤鎴疯嚜宸卞垱寤虹殑瀵硅薄鐨勪俊鎭姞涓婂叾浠栫敤鎴峰垱寤虹殑瀵硅薄浣嗚鐢ㄦ埛鏈夋潈璁块棶鐨勪俊鎭�--DBA_*锛氭湁鍏虫暣涓暟鎹簱涓璞$殑淇℃伅--锛堣繖閲岀殑*鍙互涓篢ABLES锛� INDEXES锛� OBJECTS锛� USERS绛夈��
--1.鏌ョ湅鎵�鏈夌敤鎴凤細SQL>select * from dba_user;SQL>select * from
all_users;SQL>select * from user_users;2.鏌ョ湅鐢ㄦ埛绯荤粺鏉冮檺锛歴elect * from
dba_sys_privs;select * from all_sys_privs;select * from user_sys_privs;3.鏌ョ湅鐢ㄦ埛瀵硅薄鏉冮檺锛歴elect * from dba_tab_privs;select * from
all_tab_privs;select * from user_tab_privs;4.鏌ョ湅鎵�鏈夎鑹诧細select * from
dba_roles;5.鏌ョ湅鐢ㄦ埛鎵�鎷ユ湁鐨勮鑹诧細select * from dba_role_privs;select *from user_role_privs;
6.鏌ョ湅褰撳墠鐢ㄦ埛鐨勭己鐪佽〃绌洪棿select username,default_tablespace fromuser_users;
7.鏌ョ湅鏌愪釜瑙掕壊鐨勫叿浣撴潈闄愶紝濡俫rant connect,resource,create
session,create view to TEST;鏌ョ湅RESOURCE鍏锋湁閭d簺鏉冮檺锛岀敤SELECT * FROMDBA_SYS_PRIVS WHERE GRANTEE='RESOURCE';
渚嬶細闄愬埗user1鐢ㄦ埛鍙厑璁�100涓苟鍙戣繛鎺QL> altersystem set resource_limit=true;
System altered
SQL> createprofile profile_user1 limit sessions_per_user 100;
Profile created
SQL> ALTERUSER user1 profile profile_user1;
User altered
------------------------------------------------------------------------------------------妗堜緥create or replace procedure sp_pro2 is begin--鎵ц閮ㄥ垎delete from mytest where name='鐜嬬孩绁�';end;
--鏈�绠�鍗曠殑蹇�
---------鍒嗛〉绠楁硶--1.0SQL>select * from (select rownum rn,t.* from empt) where rn>&minnum and rn<&maxmun;
--1.1SQL>select* from (select rownum rn,t.* from emp t rownum<=&maxnum) wherern>&minnum;
---鐪嬩技鐩镐技鐨勫垎椤佃鍙ワ紝鍦ㄥ搷搴旈�熷害涓婂叾瀹炴湁寰堝ぇ鐨勫樊鍒�傛潵鐪嬩竴涓祴璇曡繃绋嬶紝棣栧厛绌夸欢涓�涓祴璇曡〃銆係QL>createtable test as select * from emp;
----鍙嶅鎻掑叆鏁版嵁SQL>insert into test select * from test;
---first_rows瀵瑰垎椤电殑褰卞搷----鍒涘缓涓�涓祴璇曡〃锛屽苟浣滃叧鑱旀煡璇㈠垎椤礢QL>createtable page_test as select rownum id,t.* from test t;
----1.鍏堝垎鏋愯琛⊿QL>analyze table page_test compute statistics fortable for all columns;
----2.鐢ㄦ櫘閫氬垎椤礢QL>select * from(select rownum rn,a.object_name from
page_test a,page_test b,page_test c where a.id=b.id and b.id=c.id and
rownum<=5) where
rn>0;-----------------------------------------------------------------------------------------------------------------------------begin
dbms_output.put_line('hello'); end;--鏈夊畾涔夊拰鎵ц閮ㄥ垎鐨勫潡declare--瀹氫箟鍙橀噺v_ename varchar2(5);begin --鎵ц閮ㄥ垎 select ename into v_ename from emp where empno=&aa;
--鍦ㄦ帶鍒跺彴鏄剧ず鐢ㄦ埛鍚� dbms_output.put_line('鐢ㄦ埛鍚�:'||v_ename); end; --鏈夊畾涔夊拰鎵ц閮ㄥ垎鐨勫潡 --鎶婄敤鎴风殑宸ヨ祫涔熸樉绀哄嚭鏉eclare--瀹氫箟鍙橀噺v_ename varchar2(5);v_sal
number(7,2);begin --鎵ц閮ㄥ垎 select ename into v_ename from emp
where empno=&aa; select sal into v_sal from emp where emp empno=&aa; --鍦ㄦ帶鍒跺彴鏄剧ず鐢ㄦ埛鍚� dbms_output.put_line('鐢ㄦ埛鍚�:'||v_ename);
end; --鏈夊畾涔夊拰鎵ц閮ㄥ垎鐨勫潡 --鎶婄敤鎴风殑宸ヨ祫涔熸樉绀哄嚭鏉eclare--瀹氫箟鍙橀噺v_ename varchar2(5);v_sal number(7,2);begin --鎵ц閮ㄥ垎 select ename,sal into v_ename,v_sal from emp where
empno=&aa; --鍦ㄦ帶鍒跺彴鏄剧ず鐢ㄦ埛鍚� dbms_output.put_line('鐢ㄦ埛鍚�:'||v_ename); dbms_output.put_line('钖噾:'||v_sal); end; declare--瀹氫箟鍙橀噺v_ename
varchar2(5);v_sal number(7,2);begin --鎵ц閮ㄥ垎 select
ename,sal into v_ename,v_sal from emp where empno=&aa; --鍦ㄦ帶鍒跺彴鏄剧ず鐢ㄦ埛鍚� dbms_output.put_line('鐢ㄦ埛鍚�:'||v_ename||'钖噾:'||v_sal); exception when no_data_found then
dbms_output.put_line('鏈嬪弸浣犵殑缂栧彿杈撳叆鏈夎锛岃閲嶆柊杈撳叆'); end; --婕旂ず4 create procedure sp_pro4(spName varchar2,neSal number)
is begin --鎵ц閮ㄥ垎---鏍规嵁鐢ㄦ埛鍚嶄慨鏀瑰伐璧� update emp set
sal=newSal where ename spName; end; --鍑芥暟妗堜緥 --杈撳叆闆囧憳鐨勫悕瀛楋紝杩斿洖璇ラ泧鍛樼殑骞磋柂 create function sp_fun2(spName varchar2)
return number is yearSal number(7,2); begin --鎵ц閮ㄥ垎select sal12+nvl(comm,0)12into yearSal from emp where ename=spName; return yearSal; end; --(1)鎴戜滑鍙互浣跨敤create package鍛戒护鏉ュ垱寤� ----鍒涘缓鍖� ---鍒涘缓涓�涓寘sp_package ---澹版槑璇ユ姳鏈変竴涓繃绋媢pdate_sal ---杩樺0鏄庝簡璇ュ寘鏈変竴涓嚱鏁癮nnual_incomecreate
package sp_package isprocedure update_sal(name varchar2,newsal number);function
annual_income(name varchar2) return number;end;--(2)寤虹珛鍖呬綋鍙互浣跨敤create package body鍛戒护 ---缁欏寘sp_packeage瀹炵幇鍖呬綋create or
replace package body sp_package isprocedure update_sal(name varchar2,newsal
number) isbeginupdate emp set sal=newsal where ename=name;end;function annual_income(name
varchar2) return number is annual_salary number;beginselect sal*12+nvl(comm,0)
into annual_salary from where ename=name;return annual_salary;end;end; --(3)濡備綍璋冪敤鍖呯殑杩囩▼鎴栨槸鍑芥暟--褰撹皟鐢ㄥ寘鐨勮繃绋嬫垨鏄嚱鏁版椂锛屽湪杩囩▼鍜屽嚱鏁板墠闇�瑕佸甫涓婂寘鍚嶏紝濡傛灉瑕佽闂叾瀹冩柟妗堢殑鍖咃紝杩橀渶瑕佸湪鍖呭悕鍓嶅姞涓婃柟妗堝悕.--濡�:sql>call sp_package.update_sal('SCOTT',1500);
--涓嬮潰宸茶緭鍏ュ憳宸ュ彿锛屾樉绀哄憳宸ュ鍚嶃�佸伐璧勩�佷釜浜烘墍寰楃◣(绋庣巼涓�0.03)涓轰緥 --妗堜緥 declare c_tax_rate number(3,2):=0.03;
--鐢ㄦ埛鍚�,宸ヨ祫锛岀◣鏀� v_ename
varchar2(5); v_sal number(7,2); v_tax_sal number(7,2); begin --鎵ц閮ㄥ垎 select ename,sal into v_ename,v_sal from emp where
empno=&em; ---璁$畻鎵�寰楃◣ v_tax_sal:=v_sal*c_tax_rate; --杈撳嚭 dbms_output.put_line('濮撳悕鏄�:'||v_ename||'宸ヨ祫'||v_sal||'鎵�寰楃◣'||v_tax_sal);
end; declare c_tax_rate number(3,2):=0.03; --鐢ㄦ埛鍚�,宸ヨ祫锛岀◣鏀� v_ename emp.ename%type; v_sal number(7,2); v_tax_sal
number(7,2); begin --鎵ц閮ㄥ垎 select ename,sal into v_ename,v_sal
from emp where empno=&em; ---璁$畻鎵�寰楃◣v_tax_sal:=v_sal*c_tax_rate; --杈撳嚭 dbms_output.put_line('濮撳悕鏄�:'||v_ename||'宸ヨ祫'||v_sal||'鎵�寰楃◣'||v_tax_sal); end; declare c_tax_rate number(3,2):=0.03;
--鐢ㄦ埛鍚�,宸ヨ祫锛岀◣鏀� v_ename
emp.ename%type; v_sal emp.sal%type; v_tax_sal number(7,2); begin --鎵ц閮ㄥ垎 select ename,sal into v_ename,v_sal from emp where
empno=&em; ---璁$畻鎵�寰楃◣ v_tax_sal:=v_sal*c_tax_rate; --杈撳嚭 dbms_output.put_line('濮撳悕鏄�:'||v_ename||'宸ヨ祫'||v_sal||'鎵�寰楃◣'||v_tax_sal);end;
---pl/sql璁板綍瀹炰緥declare--瀹氫箟涓�涓猵l/sql璁板綍绫诲瀷emp_record_type,绫诲瀷鍖呭惈涓変釜鏁版嵁name,salary,title;type emp_record_type is record(name
emp.ename%type,salary emp.sal%type,title emp.job%type);--瀹氫箟浜嗕竴涓猻p_record鍙橀噺锛岃繖涓彉閲忕殑绫诲瀷鏄痚mp_record_typesp_record
emp_record_type;begin select ename,sal,job into sp_record from emp where
empno=7788; dbms_output.put_line('鍛樺伐鍚�'||emp_record.name);
end;--澶嶅悎绫诲瀷----pl/sql琛ㄧ殑瀹炰緥declare--瀹氫箟浜嗕竴涓猵l/sql琛ㄧ被鍨媠p_table_type(鏄竴涓被鍨�,涓嶆槸涓�涓彉閲�)锛岃绫诲瀷鏄敤浜庡瓨鏀緀mp.ename%type绫诲瀷鐨勬暟鎹�--index by binary_integer;琛ㄧず涓嬭〃鏄暣鏁皌ype
sp_table_type is table of emp.ename%type index by binary_integer;--瀹氫箟浜嗕竴涓猻p_table鍙橀噺,鍙橀噺鐨勭被鍨嬫槸sp_table_type;pl/sql涓畾涔夋椂鍙橀噺鍦ㄥ墠锛岀被鍨嬪湪鍚庨潰sp_table sp_table_type;beginselect ename
into sp_table(0) from emp where empno=778;dbms_output.putline('鍛樺伐鍚�:'||sp_table(0));end;--鍑洪敊锛屼笅鏍囧嚭鐜伴敊璇痙eclare--瀹氫箟浜嗕竴涓猵l/sql琛ㄧ被鍨媠p_table_type(鏄竴涓被鍨�,涓嶆槸涓�涓彉閲�)锛岃绫诲瀷鏄敤浜庡瓨鏀緀mp.ename%type绫诲瀷鐨勬暟鎹�--index by binary_integer;琛ㄧず涓嬭〃鏄暣鏁皌ype
sp_table_type is table of emp.ename%type index by binary_integer;--瀹氫箟浜嗕竴涓猻p_table鍙橀噺,鍙橀噺鐨勭被鍨嬫槸sp_table_type;pl/sql涓畾涔夋椂鍙橀噺鍦ㄥ墠锛岀被鍨嬪湪鍚庨潰sp_table sp_table_type;beginselect ename
into sp_table(-1) from emp where empno=778;dbms_output.putline('鍛樺伐鍚�:'||sp_table(0));end;--鍑洪敊锛屽疄闄呰繑鍥炵殑琛屾暟瓒呭嚭浜嗚姹傜殑琛屾暟declare--瀹氫箟浜嗕竴涓猵l/sql琛ㄧ被鍨媠p_table_type(鏄竴涓被鍨�,涓嶆槸涓�涓彉閲�)锛岃绫诲瀷鏄敤浜庡瓨鏀緀mp.ename%type绫诲瀷鐨勬暟鎹�--index by binary_integer;琛ㄧず涓嬭〃鏄暣鏁皌ype
sp_table_type is table of emp.ename%type index by binary_integer;--瀹氫箟浜嗕竴涓猻p_table鍙橀噺,鍙橀噺鐨勭被鍨嬫槸sp_table_type;pl/sql涓畾涔夋椂鍙橀噺鍦ㄥ墠锛岀被鍨嬪湪鍚庨潰sp_table sp_table_type;beginselect ename
into sp_table(0) from emp;dbms_output.putline('鍛樺伐鍚�:'||sp_table(0));end;--鍙傜収鍙橀噺-ref cursor娓告爣鍙橀噺--(1)璇蜂娇鐢╬l/sql缂栧啓涓�涓潡,鍙互杈撳叆閮ㄩ棬鍙�,骞舵樉绀鸿閮ㄩ棬鎵�鏈夊憳宸ョ殑濮撳悕鍜屼粬鐨勫伐璧勩�俤eclare--瀹氫箟娓告爣绫诲瀷sp_emp_cursortype
sp_emp_cursor is ref cursor;--瀹氫箟涓�涓父鏍囧彉閲弔est_cursor sp_emp_cursor;v_ename
emp.ename%type;v_sal emp.sal%type;begin --鎵ц閮ㄥ垎 --鎶妕est_cursor鍜屼竴涓猻elect缁撳悎 open test_cursor for select ename,sal from emp where
deptno=&no;--寰幆鍙栧嚭loop fetch test_cursor into
v_ename,v_sal; --鍒ゆ柇閫�鍑烘潯浠讹紝鏄惁test_cursor鏄惁涓虹┖ exit when test_cursor%notfound dbns_output.put_line('鍚嶅瓧涓�'||v_ename||'宸ヨ祫'||v_sal); end
loop;end;--(2)鍦�(1)鐨勫熀纭�涓婏紝濡傛灉鏌愪釜鍛樺伐鐨勫伐璧勪綆浜�200鍏冿紝灏卞鍔�100鍏冦�俤eclare--瀹氫箟娓告爣绫诲瀷sp_emp_cursortype
sp_emp_cursor is ref cursor;--瀹氫箟涓�涓父鏍囧彉閲弔est_cursor sp_emp_cursor;v_ename
emp.ename%type;v_sal emp.sal%type;begin --鎵ц閮ㄥ垎 --鎶妕est_cursor鍜屼竴涓猻elect缁撳悎 open test_cursor for select ename,sal from emp where
deptno=&no;--寰幆鍙栧嚭loop fetch test_cursor into
v_ename,v_sal; --鍒ゆ柇宸ヨ祫楂樹綆锛屽喅瀹氭槸鍚︽洿鏂� if v_sal<200
then v_sal=v_sal+100 where deptno=no; --鍒ゆ柇閫�鍑烘潯浠讹紝鏄惁test_cursor鏄惁涓虹┖ exit when test_cursor%notfound dbns_output.put_line('鍚嶅瓧涓�'||v_ename||'宸ヨ祫'||v_sal); endloop;end;
--缂栧啓涓�涓繃绋嬶紝鍙互杈撳叆涓�涓泧鍛樺悕锛屽鏋滆闆囧憳鐨勫伐璧勪綆浜�2000锛屽氨缁欒闆囧憳宸ヨ祫澧炲姞10%
create or
replace procedure sp_pro6(spName varchar2) is --瀹氫箟v_sal
emp.sal%type;begin --鎵ц select sal into v_sal from emp where
ename=spName; --鍒ゆ柇 if v_sal<2000 then update emp set
sal=sal+sal*0.1 where ename=spName; end if; end; --缂栧啓涓�涓繃绋嬶紝鍙互杈撳叆涓�涓泧鍛樺悕锛屽鏋滆闆囧憳鐨勮ˉ鍔╀笉鏄�0锛屽氨缁欒闆囧憳琛ュ姪鍦ㄥ師鏉ョ殑鍩虹涓婂鍔�100;濡傛灉琛ュ姪涓�0灏辨妸琛ュ姪璁剧疆涓�200;create or replace procedure
sp_pro7(spName varchar2) is --瀹氫箟v_comm emp.comm%type;begin --鎵ц select comm into v_comm from emp where ename=spName; --鍒ゆ柇 if v_sal<>0 then update emp set comm=comm+100
where ename=spName; else update emp set comm=comm+200 where ename=spName; end
if; end; --缂栧啓涓�涓繃绋嬶紝鍙互杈撳叆涓�涓泧鍛樼紪鍙凤紝濡傛灉璇ラ泧鍛樼殑鑱屼綅鏄疨RESIDENT锛屽氨缁欒浠栧伐璧勫鍔�1000;濡傛灉璇ラ泧鍛樻槸MANAGER,灏辩粰浠栫殑宸ヨ祫澧炲姞500;鍏朵粬鑱屼綅鐨勯泧鍛樺伐璧勫鍔�200;create or replace procedure sp_pro8(spNO number) is
--瀹氫箟v_job emp.job%type;begin --鎵ц閮ㄥ垎select job into v_job from emp where empno=spNO; if v_job='PERSIDENT'then update emp set sal=sal+1000 where empno=spNO; elsif v_job='MANAGER' thenupdate emp set sal=sal+500 where empno=spNO; else update emp set sal=sal+100where empno=spNO; end if;end;
sql>exec
sp_pro8(7983)---寰幆璇彞loop,鏂板缓涓�寮犺〃create table
usersq(num1 number锛孨ame1 varchar2(40));--璇风紪鍐欎竴涓繃绋嬶紝鍙緭鍏ョ敤鎴峰悕锛屽苟寰幆娣诲姞10涓敤鎴峰埌users琛ㄤ腑锛岀敤鎴风紪鍙蜂粠1寮�濮嬪鍔犮�俢reate or replace procedure
sp_prol(spName varchar2) is--瀹氫箟:=琛ㄧず璧嬪�紇_num
number:=1;begin loop insert into usersq values(v_num,spName); --鍒ゆ柇鏄惁瑕侀��鍑哄惊鐜� exit when v_num=10; --鑷 v_num:=v_num+1;
end loop; end;sql>exec sp_pro1('浣犲ソ');sql>select
* from usersq;---璇风紪鍐欎竴涓繃绋嬶紝鍙緭鍏ョ敤鎴峰悕锛屽苟寰幆娣诲姞10涓敤鎴峰埌users琛ㄤ腑锛岀敤鎴风紪鍙蜂粠1寮�濮嬪鍔犮�俢reate or
replace procedure sp_pro2(spName varchar2) is--瀹氫箟:=琛ㄧず璧嬪�紇_num number:=11;beginwhile v_num<=20 loop --鎵ц閮ㄥ垎 insert into usersq values(v_num,spName); --鑷 v_num:=v_num+1; end loop; end;--for寰幆create or replace procedure sp_pro3(spName varchar2) is--瀹氫箟:=琛ㄧず璧嬪�紇_num number:=11;begin for i in reverse1..10 loopinsert into usersq values(i,spName);end loop;end;
sql>exec
sp_pro3('灏忕帇');
goto table,鍏朵腑table鏄凡缁忓畾涔夊ソ鐨勬爣鍙峰悕declarei
int:=1;begin loop dbms_output.put_line('杈撳嚭i='||i); if i=10
then goto end_loop; end if; i:=i+1; end loop; <>
dbms_output.put_line('寰幆缁撴潫'); end;
sql>setserveroutput on;
declarei
int:=1;begin loop dbms_output.put_line('杈撳嚭i='||i); if i=10
then goto end_loop; end if; i:=i+1; end loop; dbms_output.put_line('寰幆缁撴潫'); <> end;---鏂板缓琛�---book琛╟reate table book (bookIdnumber,bookName varchar2(50),publishHouse varchar2(50));
---缂栧啓杩囩▼---spBookId (in) number,--in浠h〃寰�瀛樺偍鍌ㄨ繃绋嬭緭鍏ワ紝榛樿涓篿n--out浠h〃涓�涓緭鍑哄弬鏁癱reate orreplace procedure sp_pro4(spBookId in number,spbookName invarchar2,sppulishHouse varchar2) isbegin insert into bookvalues(spBookId,spbookName,sppulishHouse); end;
public classTESTfenye;
--妗堜緥:缂栧啓涓�涓繃绋嬶紝鍙互杈撳叆闆囧憳鐨勭紪鍙凤紝杩斿洖璇ラ泧鍛樼殑濮撳悕create or
replace procedure sp_pro8(spno in number,spName out varchar2) isbegin select
ename into spName from emp where empno=spno; end;--妗堜緥鎵╁睍:缂栧啓涓�涓繃绋嬶紝鍙互杈撳叆闆囧憳鐨勭紪鍙凤紝杩斿洖璇ラ泧鍛樼殑濮撳悕銆佸伐璧勩�佸拰宀椾綅create orreplace procedure sp_pro9(spno in number,spName out varchar2,spSal out number,spJobout varchar2) isbegin select ename,sal,job into spName,spSal,spJob from empwhere empno=spno; end;
---3.杩斿洖缁撴灉闆嗙殑杩囩▼--1.鍒涘缓鍖�,鍦ㄨ鍖呬腑瀹氫箟浜嗕竴涓猼est_cursor绫诲瀷create or replace package testpackage as type test_cursor
is ref cursor; end testpackage;--2.鍒涘缓瀛樺偍杩囩▼create or
replace procedure sp_pro10(spNo in number,p_cursor out testpackage.test_cursor)
isbegin open p_cursor for select * from emp where deptno=spNo; end; --3.濡備綍鍦↗AVA涓皟鐢ㄨ瀛樺偍杩囩▼
--oracle鍒嗛〉--鍙互鎶婁笅闈ql璇彞褰撳仛涓�涓ā鏉夸娇鐢╯electt1.*,rownum rn from (select * from emp) t1;
selectt1.*,rownum rn from (select * from emp) t1 where rownum<=10;
selectt1.*,rownum rn from (select * from emp) t1 where rownum<=10;
select * from(select t1.*,rownum rn from (select * from emp) t1 where rownum<=10) wherern>=6;
--寮�鍙戜竴涓寘--浣跨敤涓婇潰鐨勫寘create or replace package testpackage as
type test_cursor is ref cursor; end testpackage;--寮�濮嬬紪鍐欏垎椤电殑杩囩▼create or replace procedure fenye(tablename in
varchar2,Psize in number,--3銆�4銆�5銆佷竴椤垫樉绀鸿褰昉Now in number,--1銆�2銆�3myrows out number,--鎬昏褰曟暟myPageCount out
number,--鎬婚〉鏁皃_cursor out testpackage.test_cursor---杩斿洖鐨勮褰�) is--瀹氫箟閮ㄥ垎--瀹氫箟sql璇彞瀛楃涓瞯_sql varchar2(1000);--v_begin
number:=(PNow-1)*psize+1;v_end number:=PNow*psize;begin--鎵ц閮ㄥ垎v_sql:='select * from (select t1.*,rownum rn from (select
* from '||tablename ||') t1 where rownum<='||v_end||') where
rn>='||v_begin;--鎶婃父鏍囧拰sql璇彞鍏宠仈open p_cursor
for v_sql;--鍏抽棴娓告爣--璁$畻myrows鍜宮yPageCount--缁勭粐浜嗕竴涓猻ql璇彞v_sql:='select count(*) from'||tablename;--鎵цsql璇彞锛屽苟鎶婅繑鍥炲�艰祴鍊肩粰myrows;execute
immediate v_sql into myrows;--璁$畻myPageCountif mod(myrows,psize)=0 then
myPageCount:=myrows/psize;else myPageCount:=myrows/psize+1;end if;--鍏抽棴娓告爣close p_cursor;end;--浣跨敤java娴嬭瘯
---闂---鎺掑簭闇�姹俢reate or replace procedure
fenye(tablename in varchar2,Psize in number,--3銆�4銆�5銆佷竴椤垫樉绀鸿褰昉Now in number,--1銆�2銆�3myrows out number,--鎬昏褰曟暟myPageCount out number,--鎬婚〉鏁皃_cursor out
testpackage.test_cursor---杩斿洖鐨勮褰�) is--瀹氫箟閮ㄥ垎--瀹氫箟sql璇彞瀛楃涓瞯_sql varchar2(1000);--v_begin
number:=(PNow-1)*psize+1;v_end number:=PNow*psize;begin--鎵ц閮ㄥ垎v_sql:='select * from (select t1.*,rownum rn from (select
* from '||tablename ||'order by sal) t1 where rownum<='||v_end||') where
rn>='||v_begin;--鎶婃父鏍囧拰sql璇彞鍏宠仈open p_cursor
for v_sql;--鍏抽棴娓告爣--璁$畻myrows鍜宮yPageCount--缁勭粐浜嗕竴涓猻ql璇彞v_sql:='select count(*) from'||tablename;--鎵цsql璇彞锛屽苟鎶婅繑鍥炲�艰祴鍊肩粰myrows;execute
immediate v_sql into myrows;--璁$畻myPageCountif mod(myrows,psize)=0 then
myPageCount:=myrows/psize;else myPageCount:=myrows/psize+1;end if;--鍏抽棴娓告爣close p_cursor;end;
---妗堜緥:缂栧啓涓�涓繃绋嬶紝鍙帴鍙楅泧鍛樼殑缂栧彿锛屽苟鏄剧ず璇ラ泧鍛樼殑鍚嶅瓧--闂鏄紝濡傛灉杈撳叆鐨勯泧鍛樼殑缂栧彿涓嶅瓨鍦紝鎬庢牱鍘诲鐞嗐�俤eclarev_ename emp.ename%type;begin--瀹氫箟select ename into v_ename from emp where empno=&no;dbms_output.put_line('濮撳悕涓�:'||v_ename);end;
declarev_ename
emp.ename%type;--瀹氫箟beginselect ename into v_ename from emp
where empno=&no;dbms_output.put_line('濮撳悕涓�:'||v_ename);exception
when no_data_found then dbms_output.put_line('涓嶅瓨鍦�');end;
---鑷畾涔変緥澶朿reate or replace procedure ex_test(spNo number)is begin
--鏇存柊鐢ㄦ埛sal update emp set sal=sal+1000 whereempno=spNo; end;
--濡備綍鑷畾涔変緥澶朿reate or replace procedure ex_test(spNo number)is --瀹氫箟涓�涓緥澶杕yex exception;begin --鏇存柊鐢ㄦ埛sal update emp
set sal=sal+1000 where empno=spNo; --sql%notfound杩欓噷琛ㄧず娌℃湁uodate鎴愬姛 --raise myex瑙﹀彂渚嬪 if sql%notfound
then raise myex; end if; exception when myex then dbms_output.put_line('娌℃湁浠讳綍鐢ㄦ埛鏇存柊'); end;
---鍒涘缓瑙嗗浘锛屾妸emp琛╯al<1000鐨勯泧鍛樻槧灏勫埌璇ヨ鍥�(view)--瑙嗗浘鍒涘缓浠ュ悗锛屽彲浠ュ皢瑙嗗浘褰撴垚涓�寮犳櫘閫氱殑琛╟reate viewmyview as select * from emp where sal<1000;
--涓轰簡绠�鍖栨搷浣滐紝鐢ㄤ竴涓鍥捐В鍐筹紝鏄剧ず闆囧憳缂栧彿锛屽鍚嶅拰閮ㄩ棬鍚嶇Оcreate view myview2 as selectemp.ename,emp.sal,dept.deptno from emp,dept where emp.deptno=dept.deptno;select* from myview2;
--瑙嗗浘鍜岃鍥句箣闂村彲浠ヨ繘琛岃仈鍚堟煡璇�-------------------------------student鏁版嵁搴�------------------------------------------
SQL> create
table course( course_no number(8,0) not null,description varchar2(50),cost
number(9,2),prerequisite number(8,0),create_by varchar2(30) not
null,create_date date,modify_by varchar2(30) not null,modified_date
date);SQL>insert into course values(6300039,'璇剧▼璁捐濂�',123.52,00000001,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300040,'璇剧▼璁捐濂�',121.32,00000002,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300041,'璇剧▼璁捐濂�',173.52,00000003,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300042,'璇剧▼璁捐濂�',123.52,00000004,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300043,'璇剧▼璁捐濂�',123.52,00000005,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300044,'璇剧▼璁捐濂�',121.32,00000006,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300045,'璇剧▼璁捐濂�',173.52,00000007,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300046,'璇剧▼璁捐濂�',123.52,00000008,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300047,'璇剧▼璁捐濂�',123.52,00000009,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300048,'璇剧▼璁捐濂�',121.32,00000010,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300049,'璇剧▼璁捐濂�',173.52,00000011,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300050,'璇剧▼璁捐濂�',123.52,00000012,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300051,'璇剧▼璁捐濂�',123.52,00000013,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300052,'璇剧▼璁捐濂�',121.32,00000014,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300053,'璇剧▼璁捐濂�',173.52,00000015,'scott',sysdate,'scott',sysdate);SQL>insert
into course values(6300054,'璇剧▼璁捐濂�',123.52,00000016,'scott',sysdate,'scott',sysdate);
--------------鍒涘缓涓�涓繃绋媍reate or replace procedure sp_pro01 isbegininsert into
course values(6300039,'瑗挎瀛楀箷瓒�',123.54,00000001,user,sysdate,user,sysdate);
insert into course values(6300040,'璇剧▼璁捐濂�',121.32,00000002,'scott',sysdate,'scott',sysdate);insert
into course values(6300041,'璇剧▼璁捐濂�',173.52,00000003,'scott',sysdate,'scott',sysdate);insert
into course values(6300042,'璇剧▼璁捐濂�',123.52,00000004,'scott',sysdate,'scott',sysdate);insert
into course values(6300043,'璇剧▼璁捐濂�',123.52,00000005,'scott',sysdate,'scott',sysdate);insert
into course values(6300044,'璇剧▼璁捐濂�',121.32,00000006,'scott',sysdate,'scott',sysdate);insert
into course values(6300045,'璇剧▼璁捐濂�',173.52,00000007,'scott',sysdate,'scott',sysdate);insert
into course values(6300046,'璇剧▼璁捐濂�',123.52,00000008,'scott',sysdate,'scott',sysdate);insert
into course values(6300047,'璇剧▼璁捐濂�',123.52,00000009,'scott',sysdate,'scott',sysdate);insert
into course values(6300048,'璇剧▼璁捐濂�',121.32,00000010,'scott',sysdate,'scott',sysdate);insert
into course values(6300049,'璇剧▼璁捐濂�',173.52,00000011,'scott',sysdate,'scott',sysdate);insert
into course values(6300050,'璇剧▼璁捐濂�',123.52,00000012,'scott',sysdate,'scott',sysdate);insert
into course values(6300051,'璇剧▼璁捐濂�',123.52,00000013,'scott',sysdate,'scott',sysdate);insert
into course values(6300052,'璇剧▼璁捐濂�',121.32,00000014,'scott',sysdate,'scott',sysdate);insert
into course values(6300053,'璇剧▼璁捐濂�',173.52,00000015,'scott',sysdate,'scott',sysdate);insert
into course values(6300054,'璇剧▼璁捐濂�',123.52,00000016,'scott',sysdate,'scott',sysdate);insert
into course values(6300055,'璇剧▼璁捐濂�',123.52,00000017,'scott',sysdate,'scott',sysdate);end;
--------------------鎵ц璇ヨ繃绋婼QL>exec sp_pro01;
-----------------------------------------------------------------------------------------
SQL>create
table section( section_id number(8,0) primary key not null, course_no
number(8,0) not null, section_no number(3) not null, start_date_time date,
locate varchar2(50), instructor_id number(8,0) references
instructor(instructor_id) not null, capacity number(3,0), create_by
varchar2(30) not null, created_date date not null, modified_by varchar2(30) not
null, modified_date date not null);--------------鍒涘缓涓�涓繃绋媍reate or
replace procedure sp_pro02 isbegin insert into section
values(191,6300040,1,sysdate,'鍖椾含',150,100,'scott',sysdate,'scott',sysdate);
insert into section values(192,6300041,2,sysdate,'鍖椾含',190,100,'scott',sysdate,'scott',sysdate); insert into
section values(193,6300042,3,sysdate,'澶╂触',100,100,'scott',sysdate,'scott',sysdate);
insert into section values(194,6300043,4,sysdate,'涓婃捣',170,100,'scott',sysdate,'scott',sysdate); insert into
section values(195,6300044,5,sysdate,'骞垮窞',130,100,'scott',sysdate,'scott',sysdate);
insert into section values(196,6300045,6,sysdate,'姝︽眽',160,100,'scott',sysdate,'scott',sysdate); insert into
section values(197,6300046,7,sysdate,'鎴愰兘',110,100,'scott',sysdate,'scott',sysdate);
insert into section values(198,6300047,8,sysdate,'閲嶅簡',134,100,'scott',sysdate,'scott',sysdate);end;
--------------------鎵ц璇ヨ繃绋婼QL>exec sp_pro02;
-----------------------------------------------------------------------------------------SQL>createtable stud( student_id number(8,0) primary key, salutation varchar2(5),first_name varchar2(25), last_Name varchar2(25) not null, street_addressvarchar2(50) not null, zip varchar2(10) not null, phone char(11),employervarchar2(50),registration_date date not null,create_by varchar2(30) notnull,created_date date not null,modified_by varchar2(30) not null,modified_datedate not null);
--------------鍒涘缓涓�涓繃绋媍reate or replace procedure sp_pro03 isbegin insert into
stud values(80605301,'鏈�','娴╂ˉ','鏈�','婀栧寳','100001','02887720301','瑗垮崕澶у',sysdate,'scott',sysdate,'sys',sysdate); insert into
stud values(80605302,'鏉�','榫�','鏉�','鍚夋灄','100002','02887720302','瑗垮崕澶у',sysdate,'scott',sysdate,'system',sysdate); insert into
stud values(80605303,'绁�','鏅哄彂','绁�','鍥涘窛','100003','02887720303','瑗垮崕澶у',sysdate,'scott',sysdate,'sys',sysdate); insert into
stud values(80605304,'鏉�','鐜�','鏉�','閲嶅簡','100004','02887720304','瑗垮崕澶у',sysdate,'scott',sysdate,'system',sysdate); insert into
stud values(80605305,'瀹�','闆壙','瀹�','娴欐睙','100005','02887720305','瑗垮崕澶у',sysdate,'scott',sysdate,'sys',sysdate); insert into
stud values(80605310,'鏉�','蹇嗕笢','鏉�','鍥涘窛','100003','02887720306','瑗垮崕澶у',sysdate,'scott',sysdate,'system',sysdate); insert into
stud values(80605311,'寮�','鑸�','寮�','鍥涘窛','100003','02887720307','瑗垮崕澶у',sysdate,'scott',sysdate,'scott',sysdate); insert into
stud values(80605312,'鑾�','閫告潹','鑾�','鍥涘窛','100003','02887720308','瑗垮崕澶у',sysdate,'scott',sysdate,'system',sysdate); insert into
stud values(80605314,'鎴�','瀛愪华','鎴�','婀栧寳','100001','02887720309','瑗垮崕澶у',sysdate,'scott',sysdate,'scott',sysdate); insert into
stud values(80605315,'鏉�','鍕�','鏉�','鍚夋灄','100002','02887720310','瑗垮崕澶у',sysdate,'scott',sysdate,'system',sysdate); insert into
stud values(80605316,'閮�','鑽e崕','閮�','鍥涘窛','100003','02887720313','瑗垮崕澶у',sysdate,'scott',sysdate,'sys',sysdate); insert into
stud values(80605317,'鏉�','閾惰緣','鏉�','閲嶅簡','100004','02887720314','瑗垮崕澶у',sysdate,'scott',sysdate,'system',sysdate); insert into
stud values(80605318,'鏉�','鏄嗛湒','鏉�','娴欐睙','100005','02887720315','瑗垮崕澶у',sysdate,'scott',sysdate,'scott',sysdate); insert into
stud values(80605319,'璧�','铏�','璧�','鍥涘窛','100003','02887720316','瑗垮崕澶у',sysdate,'scott',sysdate,'system',sysdate); insert into
stud values(80605320,'榛�','鍙嬪織','榛�','鍥涘窛','100003','02887720317','瑗垮崕澶у',sysdate,'scott',sysdate,'sys',sysdate); insert into
stud values(80605321,'闄�','鍏夋槬','闄�','鍥涘窛','100003','02887720318','瑗垮崕澶у',sysdate,'scott',sysdate,'system',sysdate);
end;
--------------------鎵ц璇ヨ繃绋婼QL>exec sp_pro03;
create tableenrollment(student_id number(8,0) references stud(student_id) notnull,section_id number(8,0) references section(section_id) not null,enroll_datedate not null,final_date number(3,0) not null,create_byvarchar2(30),create_date date not null,modified_by varchar2(30) notnull,modified_date date not null);-----------------------------------------------------------------------------------------------------------------------------SQL>create table enrollment(student_id number(8,0) referencesstud(student_id) not null,section_id number(8,0) references section(section_id)not null,enroll_date date not null,final_grade number(3,0),create_byvarchar2(30) not null,created_date date not null,modified_by varchar2(30) notnull,modified_date date not null);
--------------鍒涘缓涓�涓繃绋媍reate or replace procedure sp_pro04 isbegin insert intoenrollment values(80605301,191,sysdate,98,'scott',sysdate,'sys',sysdate);insert into enrollmentvalues(80605302,192,sysdate,78,'scott',sysdate,'sys',sysdate); insert intoenrollment values(80605303,193,sysdate,68,'scott',sysdate,'sys',sysdate); insertinto enrollment values(80605304,194,sysdate,90,'scott',sysdate,'sys',sysdate);insert into enrollmentvalues(80605305,195,sysdate,77,'scott',sysdate,'sys',sysdate); insert intoenrollment values(80605310,196,sysdate,76,'scott',sysdate,'system',sysdate); insertinto enrollment values(80605311,197,sysdate,98,'scott',sysdate,'sys',sysdate);insert into enrollmentvalues(80605312,198,sysdate,78,'scott',sysdate,'scott',sysdate);end;
--------------------鎵ц璇ヨ繃绋婼QL>exec sp_pro04;
-----------------------------------------------------------------------------------------------------------------------------
SQL>create
table instructor(instructor_id number(8,0) primary key not null,salutation
varchar2(5),first_name varchar2(25),last_Name varchar2(25) not null,street_address
varchar2(50) not null,zip varchar2(10) not null,phone char(11),create_by
varchar2(30) not null,created_date date not null,modified_by varchar2(30) not
null,modified_date date not null);--------------鍒涘缓涓�涓繃绋媍reate or
replace procedure sp_pro05 isbegin insert into instructor values(150,'鐭�','绾�','鐭�','鍥涘窛','100001','02887720011','scott',sysdate,'sys',sysdate);
insert into instructor values(190,'楂�','鍕�','楂�','鍖椾含','100002','02887720012','scott',sysdate,'sys',sysdate);
insert into instructor values(170,'鍛�','鐜�','鍛�','鍥涘窛','100001','02887720013','scott',sysdate,'sys',sysdate);
insert into instructor values(100,'鏈�','闆�','鏈�','涓婃捣','100004','02887720014','scott',sysdate,'sys',sysdate);
insert into instructor values(130,'绋�','濞�','绋�','鍥涘窛','100001','02887720011','scott',sysdate,'sys',sysdate);
insert into instructor values(110,'寤�','鐕�','寤�','鍖椾含','100002','02887720012','scott',sysdate,'sys',sysdate);
insert into instructor values(160,'鍛�','鐞�','鍛�','鍥涘窛','100001','02887720013','scott',sysdate,'sys',sysdate);
insert into instructor values(134,'涓�','甯搁緳','涓�','涓婃捣','100004','02887720014','scott',sysdate,'sys',sysdate);end;
--------------------鎵ц璇ヨ繃绋婼QL>exec sp_pro05;
-----------------------------------------------------------------------------------------------------------------------------
SQL>createtable zipcode(zip varchar2(10) not null,city varchar2(25) not null,statevarchar2(2),create_by varchar2(30) not null,created_date date notnull,modified_by varchar2(30) not null,modified_date date not null);
SQL>createtable grade_type(grade_type_code char(2),description varchar2(50),create_byvarchar2(30) not null,created_date date not null,modified_by varchar2(30) notnull,modified_date date not null);
SQL>createtable grade_type_wight(section_id char(2) not null,grade_type_code char(2) notnull,number_per_section number(3) not null,percent_of_final_grade number(3) notnull,drop_lowest char(1) not null,create_by varchar2(30) not null,created_datedate not null,modified_by varchar2(30) not null,modified_date date not null);
SQL>createtable grade(student_id number(8,0) references stud(student_id) notnull,section_id number(8,0) references section(section_id) notnull,grade_type_code char(2),grade_code_occurence number(38) notnull,numeric_grade number(3) not null,comments varchar2(2000),create_by varchar2(30)not null,created_date date not null,modified_by varchar2(30) notnull,modified_date date not null);
SQL>create
table gradte_conversion(letter_grade varchar2(2) not null,grade_point
number(3,2) not null,max_grade number(3) not null,min_grade number(3) not
null,create_by varchar2(30) not null,created_date date not null,modified_by
varchar2(30) not null,modified_date date not
null);------------------------------------------------------------------------------------------------------绗�11绔� 娓告爣绠�浠媎eclarev_first_name stud.first_name%type; v_last_name stud.last_name%type;begin selectfirst_name,last_name into v_first_name,v_last_name from stud where student=123;dbms_output.put_line('student name:'||v_first_name||''||v_last_name);exceptionwhen no_data_found then dbms_output.put_line('there is no student with studentID 123');end;
----------璁板綍绫诲瀷declare vr_student stud%rowtype;-------鍒涘缓涓�涓熀浜庤〃鐨勬父鏍嘼egin select * into vr_stud from stud wherestudent_id=156; dbms_output.put_line(vr_student.first_name||''||vr_student.last_name||'hasan ID of 156'); exception when no_data_found thenraise_application_erroe(-2001,'the student'||'is not in the database'); end;
---===================================================================================================---============================绗�1绔� PL/SQL姒傚康declaredeclaration statementsbegin Exectable statementsexception Exception-handlingstatementsend;
---1.1declarev_first_name varchar2(35); v_last_name varchar2(35); c_counter constantnumber:=0;---1.2declare v_first_name varchar2(35); v_last_name varchar2(35);c_counter constant number:=0;begin select first_name,last_name intov_first_name,v_last_name from student where student_id=123;dbms_output.put_line('student name:'||v_first_name||''||v_last_name);end;
---1.3
declarev_first_name varchar2(35); v_last_name varchar2(35); c_counter constantnumber:=0;begin select first_name,last_name into v_first_name,v_last_name fromstudent where student_id=123; dbms_output.put_line('studentname:'||v_first_name||''||v_last_name);exception when no_data_found thendbms_output.put_line('there is not student with'||'student id equals 123');end;
--------------------------------鏃跺皻璐墿缃�------------------------------------------drop database
testdbgocreate database testdb;gouse testdb;go---瀛樺偍杩囩▼create Sequencesequ_stationinfo_idincrement by 1start with 1nomaxvaluenocyclecache
-------------------鍒涘缓涓�涓簭鍒梒reate sequence seqmax increment by 1;-------------------鏅�氱敤鎴风殑琛╟reate table users(userid number(10) primary key,--鐢ㄦ埛idusername varchar2(30) not null unique,--鐢ㄦ埛鍚峵ruename varchar2(30) not null,--鐪熷疄濮撳悕passwd varchar2(30) not null,--瀵嗙爜email varchar2(40) not null,--鐢靛瓙閭欢phone varchar2(20) not null, --鐢佃瘽鍙风爜address varchar2(30) not null, --鐢ㄦ埛鍦板潃postcode char(6) not null,--閭紪grade int default 5 --鐢ㄦ埛鐨勭骇鍒�)
--绠$悊鍛樿〃admin
--璐х墿琛╟reate table goods (goodsId number(10) primary key
identity(1,1),--璐х墿idgoodsName varchar2(40) ,--鍚嶇ОgoodsIntro varchar2(500),--浠嬬粛goodsPrice float
,--浠锋牸goodsNum int ,--鏁伴噺publisher varchar2(40),--鍙戣鍟唒hoto
varchar2(40),--鐓х墖type varchar2(10)--绫诲瀷)
--鐩存帴鍚戞暟鎹簱娣诲姞璐х墿insert into goods values(seqmax.nextval, '榛戠櫧妫灄','杩欐槸涓�閮ㄥソ鐗�', 59, 1, '棣欐腐鍢夌鍑哄搧','01.jpg','棣欐腐鐢靛奖')insert into
goods values(seqmax.nextval, '閲戦浮II','杩欐槸涓�閮ㄥソ鐗�', 45, 1, '棣欐腐鍢夌鍑哄搧','02.jpg','棣欐腐鐢靛奖')insert into
goods values(seqmax.nextval, '闈撳コ鑿滈', '杩欐槸涓�閮ㄥソ鐗�',99, 1, '棣欐腐鍢夌鍑哄搧','03.jpg','棣欐腐鐢靛奖')insert into
goods values(seqmax.nextval, '甯冭。绁炵浉','杩欐槸涓�閮ㄥソ鐗�', 10, 1, '棣欐腐鍢夌鍑哄搧','04.jpg','棣欐腐鐢靛奖')insert into
goods values(seqmax.nextval, '娲涚', '杩欐槸涓�閮ㄥソ鐗�',68, 1, '棣欐腐鍢夌鍑哄搧','05.jpg','棣欐腐鐢靛奖')insert into
goods values(seqmax.nextval, '榛戠櫧妫灄', '杩欐槸涓�閮ㄥソ鐗�',56, 1, '棣欐腐鍢夌鍑哄搧','01.jpg','棣欐腐鐢靛奖')insert into
goods values(seqmax.nextval, '榛戠櫧妫灄', '杩欐槸涓�閮ㄥソ鐗�',56, 1, '棣欐腐鍢夌鍑哄搧','01.jpg','棣欐腐鐢靛奖')insert into
goods values(seqmax.nextval, '閲戦浮II', '杩欐槸涓�閮ㄥソ鐗�',55, 1, '棣欐腐鍢夌鍑哄搧','02.jpg','棣欐腐鐢靛奖')
-----------------瀛樺偍杩囩▼create or replace procedure sp_pro18 isbegin insert into
goods values(seqmax.nextval, '榛戠櫧妫灄','杩欐槸涓�閮ㄥソ鐗�', 59, 1, '棣欐腐鍢夌鍑哄搧','01.jpg','棣欐腐鐢靛奖');insert into
goods values(seqmax.nextval, '閲戦浮II','杩欐槸涓�閮ㄥソ鐗�', 45, 1, '棣欐腐鍢夌鍑哄搧','02.jpg','棣欐腐鐢靛奖');insert into
goods values(seqmax.nextval, '闈撳コ鑿滈', '杩欐槸涓�閮ㄥソ鐗�',99, 1, '棣欐腐鍢夌鍑哄搧','03.jpg','棣欐腐鐢靛奖');insert into
goods values(seqmax.nextval, '甯冭。绁炵浉','杩欐槸涓�閮ㄥソ鐗�', 10, 1, '棣欐腐鍢夌鍑哄搧','04.jpg','棣欐腐鐢靛奖');insert into
goods values(seqmax.nextval, '娲涚', '杩欐槸涓�閮ㄥソ鐗�',68, 1, '棣欐腐鍢夌鍑哄搧','05.jpg','棣欐腐鐢靛奖');insert into
goods values(seqmax.nextval, '榛戠櫧妫灄', '杩欐槸涓�閮ㄥソ鐗�',56, 1, '棣欐腐鍢夌鍑哄搧','01.jpg','棣欐腐鐢靛奖');insert into
goods values(seqmax.nextval, '榛戠櫧妫灄', '杩欐槸涓�閮ㄥソ鐗�',56, 1, '棣欐腐鍢夌鍑哄搧','01.jpg','棣欐腐鐢靛奖');insert into
goods values(seqmax.nextval, '閲戦浮II', '杩欐槸涓�閮ㄥソ鐗�',55, 1, '棣欐腐鍢夌鍑哄搧','02.jpg','棣欐腐鐢靛奖');
end;------------------------鎵ц瀛樺偍杩囩▼sql>exec sp_pro18;
--鍚戠敤鎴疯〃涓垵濮嬪寲涓�涓敤鎴凤紝灏嗘潵鏄�氳繃娉ㄥ唽鐣岄潰鍔犲叆鐨�
insert into
users values('shunping','闊╅『骞�','shunping','[email protected]','010-88888888','鏄熸槦灏忓尯鍝堝搱妤煎樆鍢诲崟鍏�123鎴块棿','123456',1);
-------------------鍒涘缓涓�涓簭鍒梒reate sequence seqmax1 increment by 1;-----------------鍒涘缓涓�涓瓨鍌ㄨ繃绋媍reate or replace procedure sp_pro19 isbegin insert into
users values(seqmax1.nextval,'shunping','鐜嬬孩绁�','shunping','[email protected]','028-87720557','鏄熸槦灏忓尯鍝堝搱妤煎樆鍢诲崟鍏�123鎴块棿','123456',1); insert into users
values(seqmax1.nextval,'whx','whx','wxh','[email protected]','028-87720033','鏄熸槦灏忓尯鍝堝搱妤煎樆鍢诲崟鍏�123鎴块棿','123456',1)end;
---------鎵ц瀛樺偍杩囩▼sql>exec sp_pro19;sql>
--瀛︾敓鍙兘杩欐牱璁捐璁㈠崟琛�(閿欒)--create table orders(ordersId bigint
primary key identity(1,1),--璁㈠崟鍙穟serId bigint constraint fk_client_id
references users(userid),--鍝釜鐢ㄦ埛璁㈢殑goodsId bigint constraint fk_shangpin_id
references goods(goodsId),--鍟嗗搧鍙穘ums int not null,--鏁伴噺orderDate datetime default getdate(),--涓嬭鍗曠殑鏃堕棿payMode varchar2(20)check (payMode in('璐у埌浠樻','鏀粯瀹濅粯娆�')) default '璐у埌浠樻',--浠樻鐨勬柟寮廼sPayed bit check ( isPayed in (0
,1)),--(0,琛ㄧず杩樻病鏈変粯娆� 1:琛ㄧず宸茬粡浠樻浜�)totalPrice
float not null,--鎬讳环鏍�)
--搴旇杩欐牱鍘昏璁¤鍗曡〃create table orders( ordersId number(10) primary key,--璁㈠崟鍙� userId number(10) constraint fk_client_id references
users(userid),--鍝釜鐢ㄦ埛璁㈢殑 orderDate datetime default getdate(),--涓嬭鍗曠殑鏃堕棿 payMode varchar2(20) check (payMode in('璐у埌浠樻','鏀粯瀹濅粯娆�')) default '璐у埌浠樻',--浠樻鐨勬柟寮� isPayed bit check ( isPayed in (0
,1)),--(0,琛ㄧず杩樻病鏈変粯娆� 1:琛ㄧず宸茬粡浠樻浜�) totalPrice
float not null--鎬讳环鏍�)
--璁㈠崟缁嗚妭琛╟reate table orderDetail( ordesIid bigint constraint
fk_order_id references orders(ordersId),--璁㈠崟鍙�(骞舵槸涓�涓閿�) 鎸囧悜orders琛ㄧ殑涓婚敭 goodsId bigint
constraint fk_shangpin_id references goods(goodsId),--鍟嗗搧鍙�(骞舵槸涓�涓閿�) 鎸囧悜goods琛ㄧ殑涓婚敭 nums int not null--鏁伴噺)
--鏄剧ず涓�涓嬪悇涓〃鐨勫垵濮嬪寲淇℃伅select * from users;select * fromgoods;select * from orders
select * fromorderDetail
go
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------sql>createtable t_student(id number(3) primary key,name varchar2(20),age number(3))
-------Hibernate
HelloWorld---------------------------寤虹珛鏂癹ava 椤圭洰,鍚嶄负hibernate_0100_HelloWorld--瀛︿範寤篣ser-library-hibernate,骞跺姞鍏ョ浉搴旂殑jar鍖�--椤圭洰鍙抽敭-buildpath-configure build path-add
library鈥�--閫夋嫨User-library,鍦ㄥ叾涓柊寤� libraray,鍛藉悕涓� hibernate--鍦ㄨlibrary涓姞鍏ibernate鎵�闇�jar鍖�--hibernate
core--/required--slf-nop jar--寮曞叆mysql鐨凧DBC椹卞姩鍖�--鍦╩ysql涓缓绔嬪搴旂殑鏁版嵁搴撲互鍙婅〃--create
database hibernate锛�--use hibernate锛�--create table Student (id int primary key,
namevarchar(20), age int)锛�--寤虹珛hibernate 閰嶇疆鏂囦欢hibernate.cfg.xml--浠庡弬鑰冩枃妗d腑copy--淇敼瀵瑰簲鐨勬暟鎹簱杩炴帴--娉ㄩ噴鎺夋殏鏃剁敤涓嶄笂鐨勫唴瀹�--寤虹珛Student 绫�--寤虹珛Student 鏄犲皠鏂囦欢 Student.hbm.xml--鍙傝�冩枃妗�--灏嗘槧灏勬枃浠跺姞鍏ュ埌hibernate.cfg.xml涓�--鍙傝�冩枃妗�--鍐欐祴璇曠被Main,鍦∕ain涓Student瀵硅薄杩涜鐩存帴鐨勫瓨鍌ㄦ祴璇� --鍙傝�冩枃鎸�--FAQ锛�--瑕佽皟鐢� new
Configuration().configure().buildSessionFactory(),鑰屼笉鏄�-- 瑕佺渷鐣� configure锛屽惁鍒欎細鍑� hibernate
dialect must be set 鐨勫紓甯�--Note:--璇峰姟蹇呭缓绔嬭嚜宸卞姩鎵嬫熁鏂囨尅鐨勮兘鍔�--閲嶈鐨勬槸:--瑕佸缓绔嬭嚜宸卞姩鎵嬫煡涓�鎵嬫枃妗g殑淇″績--杩樻湁寤虹珛鑷繁鍔ㄦ墜鏌ヤ竴鎵嬫枃妗g殑涔犳儻锛�--涓诲姩瀛︿範锛岀爫寮冭鍔ㄦ帴鍙楃亴杈撶殑涔犳儻锛�--寤虹珛鑳藉姏--閿欒璇诲畬鏁�--璇烩�旀様璇殑鍏抽敭琛�--鎺掗櫎娉�--姣旇緝娉�--google
-----------------寤虹珛 Annotation 鐗堟湰鐨凥elloWorld---------------------鍒涘缓teacher 琛紝sql>create
table teacher (id number(3) primary key,name varchar2(20),title
varchar2(10));--鍒涘缓Teacher 绫�--鍦╤ibernate lib 涓姞鍏nnotation鐨刯ar鍖�--hibernate annotaion jar--ejb3
persistence jar--hibernate common-annotations.jar--娉ㄦ剰鏂囪涓病鏈夋彁鍒癶ibernate-common-annotations.jar 鏂囦欢--鍙傝�傾nnotaion鏂囨。寤虹珛瀵瑰簲鐨勬敞瑙�--鍦╤ibernate.cfg.xml涓缓绔嬫槧灏�
class:.../銆�--鍙傝�冩枃瑁嗚繘琛屾祴璇曪紙娉ㄦ剰鏂囪涓己灏慶onfigure()鐨勫皬bug) --FAQ: @涓嶇粰鎻愮ず--閰嶇疆eclipse灞炴�т俊鎭痗ontent assist-activation--鍔犱笂@--------------------------------------------------------------------------------------------------------------------------------------------------绗�19绔�--ch19_01.sqlSQL>create or replace
procedure Discount as cursor c_group_discount is select distinct
s.course_no,c.description from section s,enrollment e,course c where
s.section_id=e.section_id and c.course_no=s.course_no group by
s.course_no,c.description,e.section_id,s.section_id having count(*)>=8;
begin for r_group_discount in c_group_discount loop update course set
cost=cost*.95 where course_no=r_group_discount.course_no;
dbms_output.put_line('A_5% discount has been given
to'||r_group_discount.course_no||''||r_group_discount.description); end loop;
end;SQL>execute Discount;--缂栧啓涓�涓猻elect璇彞锛屼互渚夸簬浣跨敤user_source瑙嗗浘鏉ユ樉绀篸iscount杩囩▼鐨勬簮浠g爜SQL>column text format a770 selectto_char(line,99)||'>',text from user_source wherename='DISCOUNT';--ch19_02.sqlSQL>create or replace procedurefind_name(i_student_id in number,o_first_name out varchar2,o_last_name outvarchar2) as begin select first_name,last_name into o_first_name,o_last_namefrom stud where student_id=i_student_id; exception when others thendbms_output.put_line('error in finding student_id'||i_student_id); endfind_name;SQL>declare v_local_first_name stud.first_name%type;v_local_last_name stud.last_name%type; beginfind_name(80605304,v_local_first_name,v_local_last_name);dbms_output.put_line('student 80605304is:'||v_local_first_name||''||v_local_last_name); end;
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------绗�20绔� 鍑芥暟-------------------------------------------------------
--ch20_01.sqlSQL>createor replace function show_description(i_course_no course.course_no%type) returnvarchar2 as v_description varchar2(50); begin select description intov_description from course where course_no=i_course_no; return v_description;exception when no_data_found then return('the course is not in the database');when others then return('error in running show_description'); end;
--ch20_01b.sqlSQL>createor replace function id_is_good(i_student_id in number) return boolean asv_id_cnt number; begin select count(*) into v_id_cnt from stud wherestudent_id=i_student_id; return 1=v_id_cnt; exception when others then returnfalse; end id_is_good;
set serveroutputon;SQL>declare v_description varchar2(50); beginv_description:=show_description(&sv_number);dbms_output.put_line(v_description); end;
--娴嬭瘯id_is_good鍑芥暟鐨勪竴涓柟娉昐QL>declare
v_id number; begin v_id:=&id; if id_is_good(v_id) then
dbms_output.put_line('student id:'||v_id||'is a valid'); else
dbms_output.put_line('student id:'||v_id||'is not valid'); end if;
end;--ch20_01c.sql version 1.0SQL>create or replace function
new_instructor_id return instructor.instructor_id%type as v_new_instid
instructor.instructor_id%type; begin select instructor_id_SEQ.nextval into
v_new_instid from dual where v_new_instid; exception when others then declare
v_sqlerrm varchar2(250):=substr(SQLERRM,1,250); begin
raise_application_error(-20003,'error in instructor_id:'||v_sqlerrm); end; end
new_instructor_id;---------------------------------------------------------------------------------------SQL>declare
cons_zip constant zipcode.zip%type:='&sv_zipcode'; e_zipcode_is_not_valid
exception; begin if zipcode_does_not_exist(cons_zip) then raise
e_zipcode_is_not_valid; else null; end if; exception when e_zipcode_is_not_valid
then raise_application_error(-20003,'could not find zipcode'||cons_zip||'.');
end;---=============================================================================================---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------绗�21绔� 鍖�------------------------------------------------------------------------------------------------------------------------------------------------------------ch21_1.sqlSQL>create
or replace package manage_students as procedure find_name(i_student_id in
stud.student_id%type,o_first_name out stud.first_name%type,o_last_name out
stud.last_name%type); function id_is_good(i_student_id in stud.student_id%type)
return boolean; end manage_students;-----ch21_2.sqlSQL>declare v_first_name
stud.first_name%type; v_last_name stud.last_name%type; begin
manage_students.find_name(125,v_first_name,v_last_name); dbms_output.put_line(v_first_name||''||v_last_name);
end;-------ch21_4a.sqlSQL>create or replace package body manage_students as
procedure find_name(i_student_id in stud.student_id%type, o_first_name out
stud.first_name%type, o_last_name out stud.last_name%type) is v_student_id
stud.student_id%type; begin select first_name,last_name into
o_first_name,o_last_name from stud where student_id=i_student_id; exception
when others then dbms_output.put_line('error in finding
student_id:'||v_student_id); end find_name; function id_is_good(i_student_id in
stud.student_id%type) return boolean is v_id_cnt number; begin select count(*)
into v_id_cnt from stud where stud.student_id=i_student_id; return 1=v_id_cnt;
exception when others then return false; end id_is_good; end manage_students;------------------------------------------------------------------------------绗竴姝ュ垱寤哄寘澶碨QL>create or replace package school_api as procedure
discount_cost; function new_instructor_id return instructor.instructor_id%type;
end school_api;---绗簩姝ュ疄鐜板寘浣�--------ch21_5a.sqlSQL>create orreplace package body school_api as procedure discount_cost is cursorc_group_discount is select distinct s.course_no,c.description from sections,enrollment e,course c where s.section_id=e.section_id andc.course_no=s.course_no group by s.course_no,c.description,e.section_id,s.section_idhaving count(*)>=8; begin for r_group_discount in c_group_discount loopupdate course set cost=cost*.95 where course_no=r_group_discount.course_no;dbms_output.put_line('A_5% discount has been given to' ||r_group_discount.course_no||''||r_group_discount.description); end loop; end discount_cost; functionnew_instructor_id return instructor.instructor_id%type is v_new_instidinstructor.instructor_id%type; begin select instructor_id_SEQ.nextval intov_new_instid from dual; return v_new_instid; exception when others then declarev_sqlerrm varchar2(250):=substr(SQLERRM,1,250); beginraise_application_error(-20003,'error in instructor_id:'||v_sqlerrm); end; endnew_instructor_id; end school_api;--------------ch21_6a.SQLSQL>setserveroutput on;SQL>declare v_first_name stud.first_name%type; v_last_namestud.last_name%type; begin if manage_students.id_is_good(&&v_id); thenmanage_students.find_name(&&v_id,v_first_name,v_last_name); dbms_output.put_line(v_first_name||''||v_last_name);else dbms_output.put_line('Student ID:'||&&v_id||'is not in thedatabase'); end if; end;
---ch21_7a.sqlSQL>declare
v_instructor_id instructor.instructor_id%type; begin school_api.discount_cost;
v_instructor_id:=school_api.new_instructor_id; dbms_output.put_line('the new id
is :'||v_instructor_id); end;--鈥斺�攃h21_9a.sql--绗竴姝ュ垱寤哄寘澶碨QL>create or
replace package course_pkg as type course_rec_typ is record(first_name
stud.first_name%type,last_name stud.last_name%type,course_no
course.course_no%type,description course.description%type,section_no
section.section_no%type); type course_cur is ref cursor return course_rec_typ;
procedure get_course_list(p_student_id number,p_instructor_id
number,course_list_cv in out course_cur); end course_pkg;---绗簩閮ㄥ垱寤哄寘浣揝QL>create or replace package body course_pkg as
procedure get_course_list(p_student number,p_instructor number,course_lsit_cv
in out course_cur) is begin if p_student_id is null and p_instructor_id is null
then open course_list_cv for select 'please choose a
student-'first_name,'instructor combination' last_name,null course_no,null
description,null section_no from dual; elsif p_student_id is null then open
course_list_cv for select s.first_name first_name,s.last_name
last_name,c.course_no course_no,c.description description,se.section_no
section_no from instructor i,stud s,section se,course c,enrollment e where
i.instructor_id=p_instructor_id and i.instructor_id=se.instructor_id and
se.course_no=c.course_no and e.student_id=s.student_id and e.section_id=se.section_id
order by c.course_no,se.course_no; elsif p_instructor_id is null then open
course_list_cv for select i.last_name last_name,c.course_no
course_no,c.description description,se.section_no section_no from instructor
i,stud s,section se,course c,enrollment e where s.student_id=p_student_id and
i.instructor_id=se.instructor_id and se.course_no=c.course_no and
e.student_id=s.student_id and e.section_id=se.section_id order by
c.course_no,se.section_no; end if; end get_course_lsit; end course_pkg; ---ch21_10a.sql--绗竴姝ュ垱寤哄寘澶碨QL>create or replace package student_info_pkg as type
student_details is ref cursor; procedure get_student_info(p_student_id
number,p_choice number,details_cv in out stud_details); end student_info_pkg;--绗簩閮ㄥ垱寤哄寘浣揝QL>create or replace package body student_info_pkg asprocedure get_student_info(p_student_id number,p_choice number,detail_cv in outstud_details) is begin if p_choice=1 then open details_cv for selects.first_name first_name,s.last_name last_name,s.street_address address,z.citycity,z.state state,z.zip zip from stud s,zipcode z wheres.student_id=p_student_id and z.zip=s.zip; elsif p_choice=2 then opendetails_cv for select from stud s,section se,course c,enrollment e wherese.course_no=c.course_no and e.student_id=s.student_id ande.section_id=se.section_id and se.section_id in(select e.section_id from studs,enrollment e where s.student_id=p_student_id and s.student_id=e.student_id)order by c.course_no; elsif p_choice=3 then open details_cv for selecti.first_name first_name,i.last_name last_name,c.course_nocourse_no,c.description description,se.section_no section_no from instructori,stud s,section se,course c,enrollment e where s.student_id=p_student_id andi.instructor_id=se.instructor_id and se.course_no=c.course_no and e.student_id=s.student_idand e.section_id=se.section_id order by c.course_no,se.section_no; end if; endget_student_info; end student_info_pkg;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------鏁版嵁搴撲綔涓�---------绗竴姝ュ垱寤哄洓寮犺〃SQL>create table J(JNO varchar2(20)
primary key,JNAME varchar2(20) not null,CITY varchar2(20) not
null);SQL>create table S(SNO varchar2(20) primary key,SNAME varchar2(20) not
null,STATUS numeric(3,0) not null,CITY varchar2(20) not null);SQL>create
table P(PNO varchar2(20) primary key,PNAME varchar2(20) not null,COLOR char(2)
not null,WEIGHT numeric(3,0) not null);SQL>create table SPJ(SNO varchar2(20)
references S(SNO),PNO varchar2(20) references P(PNO),JNO varchar2(20)
references J(JNO),QTY numeric(3,0) not null);---------绗簩姝ユ彃鍏ユ暟鎹紝寤虹珛杩囩▼create or replace procedure sp_pro20
isbegin insert into S values('S1','绮� 鐩�',20,'澶╂触'); insert into S values('S2','鐩� 閿�',10,'鍖椾含'); insert into S values('S3','涓滄柟绾�',30,'鍖椾含'); insert into S values('S4','涓版嘲鐩�',20,'澶╂触'); insert into S values('S5','涓� 姘�',30,'涓婃捣');end;
create or
replace procedure sp_pro_21 isbegin insert into J values('J1','涓� 寤�','鍖椾含'); insert into J values('J2','涓� 姹�','闀挎槬'); insert into J values('J3','寮圭哀 鍘�','澶╂触'); insert into J values('J4','閫犺埞 鍘�','澶╂触'); insert into J values('J5','鏈鸿溅 鍘�','鍞愬北'); insert into J values('J6','鏃犵嚎鐢靛巶','甯稿窞'); insert into J values('J7','鍗婂浣撳巶','鍗椾含');end;
create or
replace procedure sp_pro22 isbegin insert into P values('P1','铻� 姣�','绾�',12); insert into P values('P2','铻� 鏍�','缁�',17); insert into P values('P3','铻轰笣鍒�','钃�',14); insert into P values('P4','铻轰笣鍒�','绾�',14); insert into P values('P5','鍑� 杞�','钃�',40); insert into P values('P6','榻� 杞�','绾�',30);end;
create or
replace procedure sp_pro23 isbegin insert into SPJ values('S1','P1','J1',200);
insert into SPJ values('S1','P1','J3',100); insert into SPJ
values('S1','P1','J4',700); insert into SPJ values('S1','P2','J2',100); insert
into SPJ values('S1','P3','J1',400); insert into SPJ
values('S2','P3','J2',200); insert into SPJ values('S2','P3','J4',500); insert
into SPJ values('S2','P3','J5',400); insert into SPJ
values('S2','P5','J1',400); insert into SPJ values('S2','P5','J2',100); insert
into SPJ values('S3','P1','J1',200); insert into SPJ
values('S3','P3','J1',200); insert into SPJ values('S4','P5','J1',100); insert
into SPJ values('S4','P6','J3',300); insert into SPJ
values('S4','P6','J4',200); insert into SPJ values('S5','P2','J4',100); insert
into SPJ values('S5','P3','J1',200); insert into SPJ
values('S5','P6','J2',200); insert into SPJ
values('S5','P6','J4',500);end;------------------------------------------------------------------------------------------------------绗笁姝xec sp_pro20;exec sp_pro21;exec sp_pro_22;exec
sp_pro23;-------------------------------------------------------------绗洓姝QL>select * from S;SQL>select * fromJ;SQL>select * from P;SQL>select * from SPJ;
---------------------------------------绗簲姝ワ紝瀹屾垚浣滀笟--1.鎵惧嚭鎵�鏈変緵搴斿晢鐨勫鍚嶅拰鎵�鍦ㄥ煄甯係QL>select
SNAME,CITY from S;--2.鎵惧嚭鎵�鏈夐浂浠剁殑鍚嶇О銆侀鑹层�侀噸閲廠QL>select
PNAME,COLOR,WEIGHT from P;--3.鎵惧嚭浣跨敤渚涘簲鍟哠1鎵�渚涘簲闆朵欢鐨勫伐绋嬪彿鐮丼QL>select
distinct JNO from SPJ where SNO='S1' order by JNO;--4.鎵惧嚭宸ョ▼椤圭洰J2浣跨敤鐨勫悇绉嶅悕绉板強鍏舵暟閲廠QL>selectPNAME,WEIGHT from P where P.PNO in(select PNO from SPJ whereJNO='J2');SQL>select P.PNAME,count(*) from P where P.PNO in(select PNO fromSPJ where JNO='J2');
--5.鎵惧嚭涓婃捣鍘傚晢渚涘簲鐨勬墍鏈夐浂浠跺彿鐮丼QL>select distinct SPJ.PNO from SPJ
where SPJ.SNO in (select S.SNO from S where S.CITY='涓婃捣');--6.鎵惧嚭浣跨敤涓婃捣浜х殑闆朵欢鐨勫伐绋嬪悕绉癝QL>select
J.JNAME from J where J.JNO in (select distinct SPJ.JNO from SPJ where SPJ.SNO
in (select S.SNO from S where S.CITY='涓婃捣'));---7.鎵惧嚭娌℃湁浣跨敤澶╂触鍘傜殑闆朵欢鐨勫伐绋嬪彿鐮丼QL>select distinct SPJ.JNO from SPJ
where SPJ.SNO in(select S.SNO from S where S.CITY!='澶╂触')---8.鎶婂叏閮ㄧ孩鑹查浂浠剁殑棰滆壊鏀规垚钃濊壊SQL>update P
set P.COLOR='钃�'where P.COLOR='绾�';--9.鐢盨5渚涘簲J4鐨勯浂浠禤6鏀规垚S3渚涘簲锛岃鍋氬繀瑕佺殑淇敼SQL>selectSPJ.JNO from SPJ where SPJ.SNO='S5';SQL>select * from (select SPJ.JNO fromSPJ where SPJ.SNO='S5') where JNO='J4';SQL>select SPJ.PNO,SPJ.SNO from SPJwhere SPJ.JNO in(select * from (select SPJ.JNO from SPJ where SPJ.SNO='S5')where JNO='J4') and SPJ.PNO='P6';SQL>select SPJ.SNO from SPJ group bySPJ.SNO;SQL>select SPJ.SNO from SPJ group by SPJ.SNO havingSPJ.SNO='S5';SQL>select SPJ.JNO from SPJ where SPJ.SNO=(select SPJ.SNO fromSPJ group by SPJ.SNO having SPJ.SNO='S5');
---绛旀SQL>select * from SPJ where SPJ.PNO='P6';SQL>select
* from (select * from SPJ where SPJ.PNO='P6') where JNO='J4';SQL>select *
from (select * from (select * from SPJ where SPJ.PNO='P6') where JNO='J4')
where SNO='S5';SQL>update (select * from (select * from (select * from SPJ
where SPJ.PNO='P6') where JNO='J4') where SNO='S5') set SNO='S3';----鎴栬�匰QL>update spj set sno='S3' where sno='S5' AND
JNO='J4' AND PNO='P6'; ---10.浠庝緵搴斿晢鍏崇郴涓垹闄2鐨勮褰曪紝骞朵粠渚涘簲鎯呭喌鍏崇郴涓垹闄ゅ搴旂殑璁板綍SQL>deletefrom SPJ where SPJ.SNO='S2';SQL>delete from S where S.SNO='S2';
---璇峰皢(S2,J6,P4,200)鎻掑叆渚涘簲鎯呭喌鍏崇郴SQL>insertinto SPJ values('S2','J6','P4',200);
---鏍囧噯绛旀--1select sname,city from s;--2select pname,color,weight
from p;--3select jno from spj where sno='s1';--4select p.pname,spj.qty from
spj,p where jno='j2' and p.pno=spj.pno; --5select pno from spj where sno
in(select sno from s where city='涓婃捣');--6select
jname from j where jno in (select jno from spj where sno in(select sno from s
where city='涓婃捣')); --7select jno from j where jno not in
(select jno from spj where city='澶╂触');--8select *
from p;update p set color='钃�' where color='绾�';select * from p;--9update spj set sno='s3' where sno='s5'
and pno='p6' and jno='j4';--10select * from spj;delete from s where
sno='s2';delete from spj where sno='s2';select * from
spj;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ORA-08002:搴忓垪 INSTRUCTOR_ID_SEQ.CURRVAL 灏氭湭鍦ㄦ浼氳瘽涓畾涔塐RA-8002 WhenSelecting CURRVAL From Sequence [ID 1019173.102]--------------------------------------------------------------------------------Modified 09-SEP-2010 Type PROBLEM Status PUBLISHED Problem Description:--------------------You have created the following sequence: CREATE SEQUENCEsome_sequence START WITH 1000;Later, in your code, you try to access thecurrent value of the sequence using the CURRVAL pseudo column: SELECTsome_sequence.CURRVAL from dual;You get the following error: ORA-08002:sequence SOME_SEQUENCE.CURRVAL is not yet defined in this session
SolutionDescription: ---------------------The NEXTVAL function acts as a sequenceinitializer. This can be misleading since in our example when we create thesequence we START WITH 1000. This does not however initialize the sequence. Thefirst call to NEXTVAL initializes the sequence to the START WITH value. (Note thatit does NOT increment the value.)
See the outputbelow for a complete example: SQL> CREATE SEQUENCE some_sequence 2 STARTWITH 1000; Sequence created. /* Use CURRVAL here before NEXTVAL and the erroris thrown */ SQL> SELECT some_sequence.CURRVAL "Value" 2 FROMDUAL; FROM DUAL * ERROR at line 2: ORA-08002: sequence SOME_SEQUENCE.CURRVAL isnot yet defined in this session /* Now call NEXTVAL and initialize the sequence.. */ SQL> SELECT some_sequence.NEXTVAL "Value" 2 FROM DUAL; Value--------- 1000 /* Now we have access to the current value CURRVAL */ SQL>SELECT some_sequence.CURRVAL "Value" 2 FROM DUAL; Value ---------1000 /* Now that the sequence has been initialized with the first call toNEXTVAL, the 2nd call to NEXTVAL increments as it should.. */ SQL> SELECTsome_sequence.NEXTVAL "Value" 2 FROM DUAL; Value --------- 1001SQL> SELECT some_sequence.CURRVAL "Value" 2 FROM DUAL; Value--------- 1001
SolutionExplanation: ---------------------
Before you canaccess CURRVAL for a sequence, you must first initialize the sequence withNEXTVAL.
References:-----------Oracle Server SQL Reference, Pseudocolumns CURRVAL and NEXTVAL.
Search Words:
-------------ORA-8002-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------绗洓娆′綔涓�---鍒涘缓涓ゅ紶琛⊿QL>create table employer( empno
varchar2(20) primary key, empname varchar2(20) not null, empage numeric(3,0)
not null check(empage>=16 and empage <=100), empjob varchar2(20) not
null, empsalary numeric(9,2) not null check(empsalary>=0), empdeptno
varchar2(20) not null, foreign key(empdeptno) references department(deptno)
);SQL>create table department( deptno varchar2(20) primary key, deptname
varchar2(20) not null unique, deptmanager varchar2(20) not null, deptaddress
varchar2(20) not null, deptphone varchar2(11) not null unique );
----------------------------------------------------------------------------------------------------------------------------------鍒涘缓鐢ㄦ埛SQL>create user wangming with
resource,connect;SQL>create user liyong with resource,connect;SQL>create
user liuxing with resource,connect;SQL>create user zhangxin with
resource,connect;SQL>create user zhouping with
resource,connect;SQL>create user yanglan with resource,connect;--鍒涘缓杩囩▼SQL>create or replace procedure sp_pro21 is begin
create user wangming with resource,connect; create user liyong with
resource,connect; create user liuxing with resource,connect; create user
zhangxin with resource,connect; create user zhouping with resource,connect;
create user yanglan with resource,connect;
end;-------------------------------------------------------------------------------------------------a.鐢ㄦ埛鐜嬫槑瀵逛袱寮犺〃鏈塻elect鏉冨姏SQL>grant select on
employer,department to wangming;--b.鐢ㄦ埛鏉庡媷瀵逛袱寮犺〃鏈塱nsert鍜宒elete鏉冨姏SQL>grant insert,delete on
employer,department to liyong;--c.姣忎釜鑱屽伐瀵硅嚜宸辩殑璁板綍鏈塻elect鏉冨姏SQL>grant select on employer where empname=user to
public;--d.鐢ㄦ埛鍒樻槦瀵硅亴宸ヨ〃鏈塻elect鏉冨姏锛屽宸ヨ祫瀛楁鍏锋湁鏇存柊鏉冨姏SQL>grant
select,update(empsalary) on employer to liuxing; --e.鐢ㄦ埛寮犳柊鍏锋湁淇敼杩欎袱寮犺〃鐨勭粨鏋勭殑鏉冨姏SQL>grant alter on
employer,department to zhangxin;--f.鐢ㄦ埛鍛ㄥ钩鍏锋湁瀵逛袱寮犺〃鎵�鏈夋潈鍒�(璇汇�佹彃銆佹敼銆佸垹鏁版嵁),骞跺叿鏈夌粰鍏朵粬鐢ㄦ埛鎺堟潈鐨勬潈鍔汼QL>grant all
privileges on employer,department to zhouping with grant option;--g.鐢ㄦ埛鏉ㄥ叞鍏锋湁浠庢瘡涓儴闂ㄨ亴宸ヤ腑select鏈�楂樺伐璧勶紝鏈�浣庡伐璧勶紝骞冲潎宸ヨ祫鐨勬潈鍔�,浠栦笉鑳芥煡鐪嬫瘡涓汉鐨勫伐璧凷QL>create view deptsalary as selectdepartment.deptname,max(empsalary),min(empsalary),avg(empsalary) fromemployer,department where employer.empdeptno=department.deptno group byemployer.empdeptno;SQL>grant select on deptsalary to yanglan;
---鎾ら攢涓敤鎴锋墍鎺堜簣鐨勬潈鍔�--aSQL>revoke select on employer,department fromwangming;--bSQL>revoke insert,delete on employer,department fromliyong;--cSQL>revoke select on employer where empname=user frompublic;--dSQL>revoke select,update(empsalary) on employer fromliuxing;--eSQL>revoke all privileges on employer,department from zhoupingcascade;--fSQL>revoke select on deptsalary from yanglan;SQL>drop viewdeptsalary;
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------鏍囧噯绛旀 --a 銆佺敤鎴风帇鏄庡涓や釜琛ㄦ湁SELECT 鏉冨姏銆� SQL>GRANT SELECT ON 鑱屽伐,閮ㄩ棬 TO 鐜嬫槑 --b 銆佺敤鎴锋潕鍕囧涓や釜琛ㄦ湁INSERT 鍜孌ELETE 鏉冨姏銆� SQL>GRANT INSERT,DELETE ON 鑱屽伐,閮ㄩ棬 TO 鏉庡媷 --c 銆佹瘡涓亴宸ュ彧瀵硅嚜宸辩殑璁板綍鏈塖ELECT 鏉冨姏銆� SQL>GRANT
SELECT ON 鑱屽伐 WHEN USER()=NAME TO ALL; --d 銆佺敤鎴峰垬鏄熷鑱屽伐琛ㄦ湁SELECT 鏉冨姏锛屽宸ヨ祫瀛楁鍏锋湁鏇存柊鏉冨姏銆� SQL>GRANT
SELECT,UPDATE(宸ヨ祫) ON 鑱屽伐 TO 鍒樻槦 --e 銆佺敤鎴峰紶鏂板叿鏈変慨鏀硅繖涓や釜琛ㄧ殑缁撴瀯鐨勬潈鍔涖�� SQL>GRANT
ALTER TABLE ON 鑱屽伐,閮ㄩ棬 TO 寮犳柊; --f 銆佺敤鎴峰懆骞冲叿鏈夊涓や釜琛ㄦ墍鏈夋潈鍔涳紙璇伙紝鎻掞紝鏀癸紝鍒犳暟鎹級锛屽苟鍏锋湁缁欏叾浠栫敤鎴锋巿鏉冪殑鏉冨姏銆� SQL>GRANT
ALL PRIVILIGES ON 鑱屽伐,閮ㄩ棬 TO 鍛ㄥ钩 WITH GRANT OPTION; --g銆佺敤鎴锋潹鍏板叿鏈変粠姣忎釜閮ㄩ棬鑱屽伐涓璖ELECT 鏈�楂樺伐璧勩�佹渶浣庡伐璧勩�佸钩鍧囧伐璧勭殑鏉冨姏锛屼粬涓嶈兘鏌ョ湅姣忎釜浜虹殑宸ヨ祫銆� SQL>CREATE VIEW 閮ㄩ棬宸ヨ祫 AS SELECT 閮ㄩ棬.鍚嶇О,MAX(宸ヨ祫),MIN(宸ヨ祫),AVG(宸ヨ祫) FROM 鑱屽伐,閮ㄩ棬 WHERE 鑱屽伐.閮ㄩ棬鍙�=閮ㄩ棬.閮ㄩ棬鍙� GROUP BY 鑱屽伐.閮ㄩ棬鍙� GRANT SELECT ON 閮ㄩ棬宸ヨ祫 TO 鏉ㄥ叞; --9 锛庢妸涔犻8 涓紙1锛�---锛�7锛夌殑姣忎竴绉嶆儏鍐碉紝鎾ら攢鍚勭敤鎴锋墍鎺堜簣鐨勬潈鍔� --1
SQL>REVOKE SELECT ON 鑱屽伐锛岄儴闂� FROM 鐜嬫槑;--2
SQL>REVOKE INSERT , DELETE ON 鑱屽伐锛岄儴闂� FROM 鏉庡媷;--3SQL>REOVKE SELECT ON 鑱屽伐 WHEN USER ( ) =NAME FROM ALl; --4SQL>REVOKE SELECT ,
UPDATE ON 鑱屽伐 FROM 鍒樻槦;
--5SQL>REVOKE ALTER TABLE ON 鑱屽伐,閮ㄩ棬 FROM 寮犳柊; --6 SQL>REVOKE ALL PRIVILIGES ON 鑱屽伐,閮ㄩ棬 FROM 鍛ㄥ钩; --7
SQL>REVOKE SELECT ON 閮ㄩ棬宸ヨ祫 FROM 鏉ㄥ叞; SQL>DROP
VIEW 閮ㄩ棬宸ヨ祫;
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------5.4瀹屾暣鎬х害鏉熷懡鍚嶅瓙鍙�---渚�10SQL>create table stu( sno
numeric(6) constraint c1 check (sno between 90000 and 99999), sname
varchar2(20) constraint c2 not null, sage numeric(3) constraint c3
check(sage<30), ssex char(2) constraint c4 check(ssex in ('鐢�','濂�')), constraint studkey primary key
(sno) );---渚�11SQL>create table teach( eno numeric(4)priamry key, ename varchar2(20), job varchar2(8), sal numeric(7,2), deductnumeric(7,2), deptno numeric(2), constraint empkey foreign key(deptno)references dept(deptno), constraint c1 check(sal+deduct>=30000));---SQL>alter table stu drop constraint c1;SQL>alter table stu addconstraint c1 check(sno between 900000 and 999999999);SQL>alter table studrop constraint c3;SQL alter table stu add constraint c3 check(sage<40);
---------5.6瑙﹀彂鍣�-----瀹氫箟瑙﹀彂鍣⊿QL>create trigger <瑙﹀彂鍣ㄥ悕> {before|after} <瑙﹀彂浜嬩欢> on <琛ㄥ悕> for each {row|statement} [when <瑙﹀彂鏉′欢>] <瑙﹀彂鍔ㄤ綔浣�>
SQL>create or replace trigger insert_or_uodate_sal before insert or update
on teach for each row as begin if (new.job='鏁欐巿')and(new.sal<4000) then new.sal:=4000; end if; end;SQL>create or replacetrigger insert_sal after insert teach for each row as begin insert into sal_logvalues(new.eno,new.sal,current_user,sysdate); end;
SQL>create or
replace trigger uodate_sal after uodate on tach for each row as begin if
new.sal<>old.sal) then insert into sal_log values(new.eno,new.sal,current_user,sysdate);
end if;
end;----------------------------------------------------------------------------------------------------------------------------------------------------------------------绗�5娆′綔涓�--鍋囪鏈変笅闈㈢殑鍏崇郴妯″紡锛�
-- 绉戝锛堢鍚嶏紝绉戝湴鍧�锛岀鐢佃瘽锛�-- 鐥呮埧锛堢梾鎴垮彿锛屽簥浣嶅彿锛岀瀹ゅ悕锛�
-- 鍖荤敓锛堝伐浣滆瘉鍙凤紝濮撳悕锛岃亴绉帮紝绉戝鍚嶏紝骞撮緞锛�
-- 鐥呬汉锛堢梾鍘嗗彿锛屽鍚嶏紝鎬у埆锛岃瘖鏂紝涓荤鍖荤敓锛岀梾鎴垮彿锛�
--鍏朵腑锛屼竴涓瀹ゆ湁澶氫釜鐥呮埧銆佸涓尰鐢燂紝涓�涓梾鎴垮彧鑳藉睘浜庝竴涓瀹わ紝涓�涓尰鐢熷彧灞炰簬涓�涓瀹わ紝--浣嗗彲璐熻矗澶氫釜鐥呬汉鐨勮瘖娌伙紝涓�涓梾浜虹殑涓荤鍖荤敓鍙湁涓�涓�備娇鐢⊿QL璇█瀹氫箟涓婇潰鐨勫叧绯绘ā寮忥紝--瑕佹眰鍦ㄦā寮忎腑浣跨敤瀹屾暣鎬х害鏉熷懡鍚嶅瓙鍚嶅畬鎴愪互涓嬪畬鏁存�х害鏉熷懡鍚嶆潯浠剁殑瀹氫箟锛�
--1锛� 瀹氫箟姣忎釜妯″紡鐨勪富鐮侊紱
--2锛� 瀹氫箟鐩稿叧鍏崇郴妯″紡鐨勫疄浣撳畬鏁存�у拰鍙傜収瀹屾暣鎬э紱
--3锛� 瀹氫箟鐥呬汉鐨勬�у埆鍙兘鏄�濈敺鈥濇垨鈥濆コ鈥濓紱
--4锛� 瀹氫箟鍖荤敓鐨勫勾榫勫彧鑳藉湪20鍒�80宀佷箣闂达紱
--5锛� 瀵圭梾浜鸿〃瀹氫箟鍒涘缓涓�涓猘fter琛岀骇瑙﹀彂鍣紝褰撹繘琛屾彃鍏ユ搷浣滄椂鏈寚瀹氫富绠″尰鐢熸椂锛岃嚜鍔ㄦ敼涓虹帇鍖荤敓銆�
-- 绉戝锛堢鍚嶏紝绉戝湴鍧�锛岀鐢佃瘽锛塖QL>create table 绉戝( 绉戝悕 varchar2(20) primary key, 绉戝湴鍧� varchar2(20) not null unique, 绉戠數璇� varchar2(20) not null unique );-- 鐥呮埧锛堢梾鎴垮彿锛屽簥浣嶅彿锛岀瀹ゅ悕锛塖QL>create table 鐥呮埧( 鐥呮埧鍙� varchar2(20) primary key, 搴婁綅鍙� numeric(2) not null unique, 绉戝鍚� varchar2(20) not null unique, constraint c11 foreign
key(绉戝鍚�) references 绉戝(绉戝悕) );-- 鍖荤敓锛堝伐浣滆瘉鍙凤紝濮撳悕锛岃亴绉帮紝绉戝鍚嶏紝骞撮緞锛塖QL>create
table 鍖荤敓( 宸ヤ綔璇佸彿 varchar2(20)
primary key, 濮撳悕 varchar2(10) not null, 鑱岀О varchar2(10) not null, 绉戝鍚� varchar2(20)
not null, constraint c21 foreign key(绉戝鍚�) references 绉戝(绉戝悕), 骞撮緞 numeric(3)
check (骞撮緞 between 20 and 80) );-- 鐥呬汉锛堢梾鍘嗗彿锛屽鍚嶏紝鎬у埆锛岃瘖鏂紝涓荤鍖荤敓锛岀梾鎴垮彿锛塖QL>create table 鐥呬汉( 鐥呭巻鍙� varchar2(20) primary key, 濮撳悕 varchar2(20) not null, 鎬у埆 char(2) default
'鐢�' check (鎬у埆 in ('鐢�','濂�')), 璇婃柇 varchar2(255)
not null, 涓荤鍖荤敓 varchar2(10) not null, constraint c31
foreign key(涓荤鍖荤敓) references 鍖荤敓(濮撳悕), 鐥呮埧鍙� varchar2(20)
not null, constraint c41 foreign key(鐥呮埧鍙�) references 鐥呮埧(鐥呮埧鍙�) );
--5锛� 瀵圭梾浜鸿〃瀹氫箟鍒涘缓涓�涓猘fter琛岀骇瑙﹀彂鍣紝褰撹繘琛屾彃鍏ユ搷浣滄椂鏈寚瀹氫富绠″尰鐢熸椂锛岃嚜鍔ㄦ敼涓虹帇鍖荤敓銆係QL>create or
replace trigger after_insert after insert on 鐥呬汉 for each row as
begin if new.涓荤鍖荤敓 is null then new.涓荤鍖荤敓='鐜嬪尰鐢�'; end if; end;