- 查询第一条记录
where语句后面跟上 and rownum=1
2.日期所相差分钟数
ceil((LOGOUT_TIME - LOGIN_TIME) * 24 * 60)
3.group by分组后,取得分组前的每一组的第一条记录
---获取15届学生第一次登录wifi的最早时间
select * from (
select wifilogin.*,row_number() over(partition by USER_ID order by rownum)cn from T_SAM_YHDLXX
) wifilogin
)where cn='1'
4.查询某用户被赋予的表权限
SELECT * FROM user_tab_privs_made where grantee='USR_GIS'
5. 按名称查询视图
select view_name from user_views where view_name like 'V%'
6. 查询数据表所占用的空间
select OWNER, t.segment_name, t.segment_type, sum(t.bytes / 1024 / 1024) mmm
from dba_segments t
where t.owner = '用户名'
and t.segment_type='TABLE'
--and t.segment_name='表名'
group by OWNER, t.segment_name, t.segment_type
order by mmm desc;
7,查询库中所含的表、列、数据行(
https://www.cnblogs.com/JohanChan/p/11718183.html)
SELECT COUNT(*) TABLES, table_schema FROM information_schema.TABLES WHERE table_schema = 'szdb'
SELECT COUNT(column_name) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'szdb';
use information_schema;
SELECT sum(table_rows) from tables where TABLE_SCHEMA = 'szdb' order by table_rows desc;
8.日期格式转换
substr(xn,1,4)>=(to_char(sysdate,'yyyy')-4)
9.
查看所有表空间的数据使用情况
SELECT Upper(F.TABLESPACE_NAME) "表空间名",
D.TOT_GROOTTE_MB "表空间大小(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空间(M)",
To_char(Round(( D.TOT_GROOTTE_MB - F.TOTAL_BYTES ) / D.TOT_GROOTTE_MB * 100, 2), '990.99')
|| '%' "使用比",
F.TOTAL_BYTES "空闲空间(M)",
F.MAX_BYTES "最大块(M)"
FROM (SELECT TABLESPACE_NAME,
Round(Sum(BYTES) / ( 1024 * 1024 ), 2) TOTAL_BYTES,
Round(Max(BYTES) / ( 1024 * 1024 ), 2) MAX_BYTES
FROM SYS.DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
Round(Sum(DD.BYTES) / ( 1024 * 1024 ), 2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 1
10. 查看表空间的名字和所属文件
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;
11.手动增加数据文件尺寸
ALTER DATABASE DATAFILE '/oracle/oradata/GAME.dbf'
RESIZE 4000M;
12,筛选时间范围
where to_date(substr(AUTHORIZE_DATE,1,10),'yyyy-mm-dd') >=to_date('2016-09-30','yyyy-mm-dd')
and to_date(substr(AUTHORIZE_DATE,1,10),'yyyy-mm-dd') <=to_date('2017-08-31','yyyy-mm-dd')
where opertime >to_date('2021-04-14 07:00','yyyy-mm-dd HH24:Mi') order by OPERTIME desc
注意是HH24:Mi 不是hh:mm否则会报ORA-01810: 格式代码出现两次的错误。
13 查看所有表空间对应的数据文件:
select sum(bytes)/1024/1024/1024||'GB' from DBA_DATA_FILES;