久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

分享:我的oracle9i學(xué)習(xí)筆記(三)

瀏覽:131日期:2023-11-21 16:55:42
######### Managing Privileges ############# grant create table,create session to user_name; grant create any table to user_name; revoke create any table from user_name; /*授予權(quán)限語法,public 標(biāo)識(shí)所有用戶,with admin option答應(yīng)能將權(quán)限授予第三者的權(quán)限*/ grant system_privs,[......] to [user/role/public],[....] [with admin option]; select * from v$pwfile_users; /*當(dāng) O7_dictionary_Accessiblity參數(shù)為True時(shí),標(biāo)識(shí)select any table時(shí),包括系統(tǒng)表也能select ,否則,不包含系統(tǒng)表;缺省為false*/ show parameter O7; /*由于 O7_dictionary_accessiblity為靜態(tài)參數(shù),不能動(dòng)態(tài)改變,故加scope=spfile,下次啟動(dòng)時(shí)才生效*/ alter system set O7_dictionary_accessiblity=true scope=spfile; /*授予對(duì)象中的某些字段的權(quán)限,如select 某表中的某些字段的權(quán)限*/ grant [object_privs(column,....)],[...] on object_name to user/role/public,... with grant option; /*Oracle不答應(yīng)授予select某列的權(quán)限,但可以授insert ,update某列的權(quán)限*/ grant insert(column_name1,column_name2,...) on table_name to user_name with grant option; select * from dba_sys_privs/session_privs/dba_tab_privs/user_tab_privs/dba_col_privs/user_col_privs; /*db/os/none 審計(jì)被記錄在 數(shù)據(jù)庫/操作系統(tǒng)/不審計(jì) 缺省是none*/ show parameter audit_trail; /*啟動(dòng)對(duì)表的select動(dòng)作*/ audit select on user.table_name by session; /*by session在每個(gè)session中發(fā)出command只記錄一次,by access則每個(gè)command都記錄*/ audit [create table][select/update/insert on object by session/access][whenever sUCcessful/not successful]; desc dbms_fga;---進(jìn)一步設(shè)計(jì),則可使用dbms_fgs包 /*取消審計(jì)*/ noaudit select on user.table_name; /*查被審計(jì)信息*/ select * from all_def_audit_opts/dba_stmt_audit_opts/dba_priv_audit_opts/dba_obj_audit_opts; /*獲取審計(jì)記錄*/ select * from dba_audit_trail/dba_audit_exists/dba_audit_object/dba_audit_session/dba_audit_statement; ########### Managing Role ################# create role role_name; grant select on table_name to role_name; grant role_name to user_name; set role role_name; create role role_name; create role role_name identified by passWord; create role role_name identified externally; set role role_name ; ----激活role set role role_name identified by password; alter role role_name not identified; alter role role_name identified by password; alter role role_name identified externally; grant priv_name to role_name [WITH ADMIN OPTION]; grant update(column_name1,col_name2,...) on table_name to role_name; grant role_name1 to role_name2; /*建立default role,用戶登錄時(shí),缺省激活default role*/ alter user user_name default role role_name1,role_name2,...; alter user user_name default role all; alter user user_name default role all except role_name1,...; alter user user_name default role none; set role role1 [identified by password],role2,....; set role all; set role except role1,role2,...; set role none; revoke role_name from user_name; revoke role_name from public; drop role role_name; select * from dba_roles/dba_role_privs/role_role_privs/dba_sys_privs/role_sys_privs/role_tab_privs/session_roles;########### Basic SQL SELECT ################ select col_name as col_alias from table_name ; select col_name from table_name where col1 like '_o%'; ----'_'匹配單個(gè)字符 /*使用字符函數(shù)(右邊截取,字段中包含某個(gè)字符,左邊填充某字符到固定位數(shù),右邊填充某字符到固定位數(shù))*/ select substr(col1,-3,5),instr(col2,'g'),LPAD(col3,10,'$'),RPAD(col4,10,'%') from table_name; /*使用數(shù)字函數(shù)(往右/左幾位四舍五入,取整,取余)*/ select round(col1,-2),trunc(col2),mod(col3) from table_name ; /*使用日期函數(shù)(計(jì)算兩個(gè)日期間相差幾個(gè)星期,兩個(gè)日期間相隔幾個(gè)月,在某個(gè)月份上加幾個(gè)月,某個(gè)日期的下一個(gè)日期, 某日期所在月的最后的日期,對(duì)某個(gè)日期的月分四舍五入,對(duì)某個(gè)日期的月份進(jìn)行取整)*/ select (sysdate-col1)/7 week,months_between(sysdate,col1),add_months(col1,2),next_day(sysdate,'FRIDAY'),last_day(sysdate), round(sysdate,'MONTH'),trunc(sysdate,'MONTH') from table_name; /*使用NULL函數(shù)(當(dāng)eXPr1為空取expr2/當(dāng)expr1為空取expr2,否則取expr3/當(dāng)expr1=expr2返回空)*/ select nvl(expr1,expr2),nvl2(expr1,expr2,expr3),nullif(expr1,expr2) from table_name; select column1,column2,column3, case column2 when '50' then column2*1.1 when '30' then column2*2.1 when '10' then column3/20 else column3 end as ttt from table_name ; ------使用case函數(shù) select table1.col1,table2.col2 from table1 [CROSS JOIN table2] -----笛卡兒連接 [NATURAL JOIN table2] -----用兩個(gè)表中的同名列連接 [JOIN table2 USING (column_name)] -----用兩個(gè)表中的同名列中的某一列或幾列連接 [JOIN table2 ON (table1.col1=table2.col2)] [LEFTRIGHTFULL OUTER JOIN table2 ------相當(dāng)于(+)=,=(+)連接,全外連接 ON (table1.col1=table2.col2)]; ------SQL 1999中的JOIN語法; example: select col1,col2 from table1 t1 join table2 t2 on t1.col1=t2.col2 and t1.col3=t2.col1 join table3 t3 on t2.col1=t3.col3; select * from table_name where col1 < any (select col2 from table_name2 where continue group by col3); select * from table_name where col1 < all (select col2 from table_name2 where continue group by col3); insert into (select col1,col2,col3 form table_name where col1>50 with check option) values (value1,value2,value3); MERGE INTO table_name table1 USING table_name2 table2 ON (table1.col1=table2.col2) WHEN MATCHED THEN UPDATE SET table1.col1=table2.col2, table1.col2=table2.col3, ... WHEN NOT MATCHED THEN INSERT VALUES(table2.col1,table2.col2,table2.col3,...); -----合并語句 ##################### CREATE/ALTER TABLE ####################### alter table table_name drop column column_name ;---drop column alter table table_name set unused (col1,col2,...);----設(shè)置列無效,這個(gè)比較快。 alter table table_name drop unused columns;---刪除被設(shè)為無效的列 rename table_name1 to table_name2; ---重命名表 comment on table table_name is 'comment message';----給表放入注釋信息 create table table_name (col1 int not null,col2 varchar2(20),col3 varchar2(20), constraint uk_test2_1 unique(col2,col3))); -----定義表中的約束條件 alter table table_name add constraint pk_test2 primary key(col1,col2,...); ----創(chuàng)建主鍵 /*建立外鍵*/ create table table_name (rid int,name varchar2(20),constraint fk_test3 foreign key(rid) references other_table_name(id)); alter table table_name add constraint ck_test3 check(name like 'K%'); alter table table_name drop constraint constraint_name; alter table table_name drop primary key cascade;----級(jí)聯(lián)刪除主鍵 alter table table_name disable/enable constraint constraint_name;----使約束暫時(shí)無效 /*刪除列,并級(jí)聯(lián)刪除此列下的約束條件*/ alter table table_name drop column column_name cascade constraint; select * from user_constraints/user_cons_columns;---約束條件相關(guān)視圖############## Create Views ##################### CREATE [OR REPLACE] [FORCENOFORCE] VIEW view_name [(alias[,alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint_name]] [WITH READ ONLY [CONSTRAINT constraint_name]]; ------創(chuàng)建視圖的語法 example: Create or replace view testview as select col1,col2,col3 from table_name; ------創(chuàng)建視圖 /*使用別名*/ Create or replace view testview as select col1,sum(col2) col2_alias from table_name; /*創(chuàng)建復(fù)雜視圖*/ Create view view_name (alias1,alias2,alias3,alias4) as select d.col1,min(e.col1),max(e.col1),avg(e.col1) from table_name1 e,table_name2 d where e.col2=d.col2 group by d.col1; /*當(dāng)用update修改數(shù)據(jù)時(shí),必須滿足視圖的col1>;10的條件,不滿足則不能被改變.*/ Create or replace view view_name as select * from table_name where col1>;10 with check option; /*改變視圖的值.對(duì)于簡單視圖可以用update語法修改表數(shù)據(jù),但復(fù)雜視圖則不一定能改。如使用了函數(shù),group by ,distinct等的列*/ update view_name set col1=value1; /*TOP-N分析*/ select [column_list],rownum from (select [column_list] from table_name order by Top-N_column) where rownum<=N; /*找出某列三條最大值的記錄*/ example: select rownum as rank ,col1 ,col2 from (select col1 ,col2 from table_name order by col2 desc) where rownum<=3; ############# Other database Object ############### CREATE SEQUENCE sequence_name [INCREMENT BY n] [START WITH n] [{MAXVALUE n NOMAXVALUE}] [{MINVALUE n NOMINVALUE}] [{CYCEL NOCYCLE}] [{CACHE n NOCACHE}]; -----創(chuàng)建SEQUENCE example: CREATE SEQUENCE sequence_name INCREMENT BY 10 START WITH 120 MAXVALUE 9999 NOCACHE NOCYCLE; select * from user_sequences ;---當(dāng)前用戶下記錄sequence的視圖 select sequence_name.nextval,sequence_name.currval from dual;-----sequence的引用 alter sequence sequence_name INCREMENT BY 20 MAXVALUE 999999 NOCACHE NOCYCLE; -----修改sequence,不能改變起始序號(hào) drop sequence sequence_name; ----刪除sequence CREATE [PUBLIC] SYNONYM synonym_name FOR object; ------創(chuàng)建同義詞 DROP [PUBLIC] SYNONYM synonym_name;----刪除同義詞 CREATE PUBLIC DATABASE LINK link_name USEING OBJECT;----創(chuàng)建DBLINK select * from object_name@link_name; ----訪問遠(yuǎn)程數(shù)據(jù)庫中的對(duì)象 /*union 操作,它將兩個(gè)集合的交集部分壓縮,并對(duì)數(shù)據(jù)排序*/ select col1,col2,col3 from table1_name union select col1,col2,col3 from table2_name; /*union all 操作,兩個(gè)集合的交集部分不壓縮,且不對(duì)數(shù)據(jù)排序*/ select col1,col2,col3 from table1_name union all select col1,col2,col3 from table2_name; /*intersect 操作,求兩個(gè)集合的交集,它將對(duì)重復(fù)數(shù)據(jù)進(jìn)行壓縮,且排序*/ select col1,col2,col3 from table1_name intersect select col1,col2,col3 from table2_name; /*minus 操作,集合減,它將壓縮兩個(gè)集合減后的重復(fù)記錄, 且對(duì)數(shù)據(jù)排序*/ select col1,col2,col3 from table1_name minus select col1,col2,col3 from table2_name; /*EXTRACT 抽取時(shí)間函數(shù). 此例是抽取當(dāng)前日期中的年*/ select EXTRACT(YEAR FROM SYSDATE) from dual; /*EXTRACT 抽取時(shí)間函數(shù). 此例是抽取當(dāng)前日期中的月*/ select EXTRACT(MONTH FROM SYSDATE) from dual; ########################## 增強(qiáng)的 group by 子句 ######################### select [column,] group_function(column)... from table [WHERE condition] [GROUP BY [ROLLUP] group_by_expression] [HAVING having_expression]; [ORDER BY column]; -------ROLLUP操作字,對(duì)group by子句的各字段從右到左進(jìn)行再聚合 example: /*其結(jié)果看起來象對(duì)col1做小計(jì)*/ select col1,col2,sum(col3) from table group by rollup(col1,col2); /*復(fù)合rollup表達(dá)式*/ select col1,col2,sum(col3) from table group by rollup((col1,col2)); select [column,] group_function(column)... from table [WHERE condition] [GROUP BY [CUBE] group_by_expression] [HAVING having_expression]; [ORDER BY column]; -------CUBE操作字,除完成ROLLUP的功能外,再對(duì)ROLLUP后的結(jié)果集從右到左再聚合 example: /*其結(jié)果看起來象對(duì)col1做小計(jì)后,再對(duì)col2做小計(jì),最后算總計(jì)*/ select col1,col2,sum(col3) from table group by cube(col1,col2); /*復(fù)合rollup表達(dá)式*/ select col1,col2,sum(col3) from table group by cube((col1,col2)); /*混合rollup,cube表達(dá)式*/ select col1,col2,col3,sum(col4) from table group by col1,rollup(col2),cube(col3); /*GROUPING(expr)函數(shù),查看select語句種以何字段聚合,其取值為0或1*/ select [column,] group_function(column)...,GROUPING(expr) from table [WHERE condition] [GROUP BY [ROLLUP] group_by_expression] [HAVING having_expression]; [ORDER BY column]; example: select col1,col2,sum(col3),grouping(col1),grouping(col2) from table group by cube(col1,col2); /*grouping sets操作,對(duì)group by結(jié)果集先對(duì)col1求和,再對(duì)col2求和,最后將其結(jié)果集并在一起*/ select col1,col2,sum(col3) from table group by grouping sets((col1),(col2))
標(biāo)簽: Oracle 數(shù)據(jù)庫
主站蜘蛛池模板: 正宁县| 聊城市| 陆河县| 绥江县| 石棉县| 门头沟区| 建昌县| 太湖县| 彩票| 嘉峪关市| 丽江市| 克山县| 绥棱县| 南华县| 邹城市| 左权县| 青冈县| 西华县| 灵台县| 庆元县| 城口县| 安泽县| 余江县| 香格里拉县| 宁晋县| 普宁市| 湾仔区| 仪征市| 石台县| 紫金县| 浙江省| 泾源县| 西乌| 茂名市| 简阳市| 龙井市| 高雄市| 南华县| 四平市| 华容县| 舟山市|