alter profile profile_name limit cpu_per_call 1000 logical_reads_per_call 10;
desc dbms_resouce_manager;---资源管理器包
/*获取资源信息的表或视图*/ select * from dba_users/dba_profiles;
###### managing users ############
show parameter os;
create user testuser1 identified by kxf_001;
grant connect,createtable to testuser1;
alter user testuser1 quota 10m on tablespace_name;
/*创建用户*/ create user user_name identified by password default tablespace tablespace_name temporary tablespace tablespace_name quota 15m on tablespace_name password expire;
/*数据库级设定缺省临时表空间*/ alter database default temporary tablespace tablespace_name;
/*制定数据库级的缺省表空间*/ alter database default tablespace tablespace_name;
/*创建os级审核的用户,需知道os_authent_prefix,表示oracle和os口令对应的前缀,'ops$'为此参数的值,此值可以任意设置*/ create user user_name identified by externally default ops$tablespace_name tablespace_name temporary tablespace tablespace_name quota 15m on tablespace_name password expire;
/*修改用户使用表空间的限额,回滚表空间和临时表空间不允许授予限额*/ alter user user_name quota 5m on tablespace_name;
/*删除用户或删除级联用户(用户对象下有对象的要用cascade,将其下一些对象一起删除)*/ drop user user_name [cascade];
/*每个用户在哪些表空间下有些什么限额*/ desc dba_ts_quotas;select * from dba_ts_quotas where username='...';
/*改变用户的缺省表空间*/ alter user user_name default tablespace tablespace_name;
######### managing privileges #############
grant create table,create session to user_name;
grant create any table to user_name; revoke create any table from user_name;
/*授予权限语法,public 标识所有用户,with admin option允许能将权限授予第三者的权限*/ grant system_privs,[......] to [user/role/public],[....] [with admin option];
select * from v$pwfile_users;
/*当 o7_dictionary_accessiblity参数为true时,标识select any table时,包括系统表也能select ,否则,不包含系统表;缺省为false*/ show parameter o7;
/*由于 o7_dictionary_accessiblity为静态参数,不能动态改变,故加scope=spfile,下次启动时才生效*/ alter system set o7_dictionary_accessiblity=true scope=spfile;
/*授予对象中的某些字段的权限,如select 某表中的某些字段的权限*/ grant [object_privs(column,....)],[...] on object_name to user/role/public,... with grant option;
/*oracle不允许授予select某列的权限,但可以授insert ,update某列的权限*/ 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 审计被记录在 数据库/操作系统/不审计 缺省是none*/ show parameter audit_trail;
/*启动对表的select动作*/ audit select on user.table_name by session;
/*by session在每个session中发出command只记录一次,by access则每个command都记录*/ audit [create table][select/update/insert on object by session/access][whenever successful/not successful];
desc dbms_fga;---进一步设计,则可使用dbms_fgs包
/*取消审计*/ noaudit select on user.table_name;
/*查被审计信息*/ select * from all_def_audit_opts/dba_stmt_audit_opts/dba_priv_audit_opts/dba_obj_audit_opts;
/*获取审计记录*/ 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,用户登录时,缺省激活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%'; ----'_'匹配单个字符
/*使用字符函数(右边截取,字段中包含某个字符,左边填充某字符到固定位数,右边填充某字符到固定位数)*/ select substr(col1,-3,5),instr(col2,'g'),lpad(col3,10,'$'),rpad(col4,10,'%') from table_name;
/*使用数字函数(往右/左几位四舍五入,取整,取余)*/ select round(col1,-2),trunc(col2),mod(col3) from table_name ;
/*使用日期函数(计算两个日期间相差几个星期,两个日期间相隔几个月,在某个月份上加几个月,某个日期的下一个日期, 某日期所在月的最后的日期,对某个日期的月分四舍五入,对某个日期的月份进行取整)*/ 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函数(当expr1为空取expr2/当expr1为空取expr2,否则取expr3/当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函数
select table1.col1,table2.col2 from table1 [cross join table2] | -----笛卡儿连接 [natural join table2] | -----用两个表中的同名列连接 [join table2 using (column_name)] | -----用两个表中的同名列中的某一列或几列连接 [join table2 on (table1.col1=table2.col2)] | [left|right|full outer join table2 ------相当于(+)=,=(+)连接,全外连接 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,...); -----合并语句
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;----级联删除主键
alter table table_name disable/enable constraint constraint_name;----使约束暂时无效
/*删除列,并级联删除此列下的约束条件*/ alter table table_name drop column column_name cascade constraint;
select * from user_constraints/user_cons_columns;---约束条件相关视图
############## create views #####################
create [or replace] [force|noforce] view view_name [(alias[,alias]...)] as subquery [with check option [constraint constraint_name]] [with read only [constraint constraint_name]]; ------创建视图的语法
example: create or replace view testview as select col1,col2,col3 from table_name; ------创建视图 /*使用别名*/ create or replace view testview as select col1,sum(col2) col2_alias from table_name; /*创建复杂视图*/ 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; /*当用update修改数据时,必须满足视图的col1>10的条件,不满足则不能被改变.*/ create or replace view view_name as select * from table_name where col1>10 with check option;
/*改变视图的值.对于简单视图可以用update语法修改表数据,但复杂视图则不一定能改。如使用了函数,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}]; -----创建sequence
example: create sequence sequence_name increment by 10 start with 120 maxvalue 9999 nocache nocycle;
select * from user_sequences ;---当前用户下记录sequence的视图
select sequence_name.nextval,sequence_name.currval from dual;-----sequence的引用
alter sequence sequence_name increment by 20 maxvalue 999999 nocache nocycle; -----修改sequence,不能改变起始序号
drop sequence sequence_name; ----删除sequence
create [public] synonym synonym_name for object; ------创建同义词
drop [public] synonym synonym_name;----删除同义词
create public database link link_name useing object;----创建dblink
/*union 操作,它将两个集合的交集部分压缩,并对数据排序*/ select col1,col2,col3 from table1_name union select col1,col2,col3 from table2_name;
/*union all 操作,两个集合的交集部分不压缩,且不对数据排序*/ select col1,col2,col3 from table1_name union all select col1,col2,col3 from table2_name;
/*intersect 操作,求两个集合的交集,它将对重复数据进行压缩,且排序*/ select col1,col2,col3 from table1_name intersect select col1,col2,col3 from table2_name;
/*minus 操作,集合减,它将压缩两个集合减后的重复记录, 且对数据排序*/ select col1,col2,col3 from table1_name minus select col1,col2,col3 from table2_name;
/*extract 抽取时间函数. 此例是抽取当前日期中的年*/ select extract(year from sysdate) from dual; /*extract 抽取时间函数. 此例是抽取当前日期中的月*/ select extract(month from sysdate) from dual;
########################## 增强的 group by 子句 #########################
select [column,] group_function(column)... from table [where condition] [group by [rollup] group_by_expression] [having having_expression]; [order by column]; -------rollup操作字,对group by子句的各字段从右到左进行再聚合
example: /*其结果看起来象对col1做小计*/ select col1,col2,sum(col3) from table group by rollup(col1,col2); /*复合rollup表达式*/ 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的功能外,再对rollup后的结果集从右到左再聚合
example: /*其结果看起来象对col1做小计后,再对col2做小计,最后算总计*/ select col1,col2,sum(col3) from table group by cube(col1,col2); /*复合rollup表达式*/ select col1,col2,sum(col3) from table group by cube((col1,col2)); /*混合rollup,cube表达式*/ select col1,col2,col3,sum(col4) from table group by col1,rollup(col2),cube(col3);
/*grouping(expr)函数,查看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操作,对group by结果集先对col1求和,再对col2求和,最后将其结果集并在一起*/ select col1,col2,sum(col3) from table group by grouping sets((col1),(col2));