赞
踩
Oracle查询数据库中所有表的记录数
查询库中的表名和表中记录数:
SELECT T.TABLE_NAME, T.NUM_ROWS FROM USER_TABLES T;
查询库中记录总数:
SELECT SUM(A.NUM_ROWS)
FROM (SELECT T.TABLE_NAME, T.NUM_ROWS FROM USER_TABLES T) A;
如图所示:
1、用户操作
select * from dba_users;
create user test identified by 123456;
alter user test identified by 123456;
grant connect,resource to test ;
alter user test quota unlimited on users;
drop user test cascade;
2、用户
select * from user_users;
查看当前用户的角色select * from user_role_privs;
查看当前用户的系统权限和表级权限select * from user_sys_privs;
select * from user_tab_privs;
3、表
查看用户下所有的表select * from user_tables;
查看某表的创建时间select * from user_objects where object_name=upper('test');
查看某表的大小select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('test');
4、索引
查看索引个数和类别select index_name,index_type,table_name from user_indexes order by table_name;
查看索引被索引的字段select * from user_ind_columns where index_name=upper('&index_name');
查看索引的大小select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&index_name');
5、序列号
查看序列号,last_number
是当前值select * from user_sequences;
6、视图
select * from user_views;
7、存储函数和过程
查看函数和过程的状态select object_name,status from user_objects where object_type='FUNCTION'; select object_name,status from user_objects where object_type='PROCEDURE';
查看函数和过程的源代码select text from all_source where owner=user and name=upper('&plsql_name');
8、SQL时间计算
SELECT TO_CHAR(ADD_MONTHS(SYSDATE, -1), 'yyyyMMdd') SYY FROM DUAL; --计算上一个月
9、Oracle的connect by level的使用
获取连续数字示例代码: 1 -- 获取连续的数据(注意:level只用使用<,<=,=符号) 2 select level from dual connect by level <= 5; -- 1 2 3 4 5获取连续的日期示例代码:
1 -- 获取连续的指定时间(注意:获取连续的时间需要包含当天需要再+1天) 2 select sysdate-level+1 days from dual connect by level <= 5;统计填充示例代码:
1 /* 2 问题:查询1981年每月入职的人数,没有入职的以0补充 3 解决:1.创建一个连续的年份表进行关联 4 2.关联的条件,截取时间相等进行关联 5 3.注意:a.需要所有的时间,因此要让时间表主表 6 b.如果emp表有条件,要单独在(SELECT * FROM emp)中添加,不然会影响结果,导致时间不全 7 c.使用其他函数,如SUM求和可能为空用NVL函数,这里以count函数举例 8 */ 9 SELECT times.days 月份,NVL(COUNT(e.EMPNO),0) 入职人数 FROM (SELECT * FROM emp) e 10 RIGHT JOIN ( 11 select TO_CHAR(ADD_MONTHS(TO_DATE('2010-12-01', 'yyyy-MM-dd'),-LEVEL+1),'yyyy-MM') days 12 FROM dual CONNECT BY LEVEL <= 12 13 ) times 14 ON SUBSTR(TO_CHAR(e.HIREDATE,'yyyy-MM-dd hh24:mi:ss'), 0, 7) = times.days 15 GROUP BY times.days 16 ORDER BY times.days月份 入职人数
2010-01 0
2010-02 2
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。