十三、Oracle 系统表


一、SYS 系统表


1. 用户

-- 当前用户名
SELECT USER AS "用户名"
FROM SYS.DUAL;

-- 当前用户ID
SELECT USERENV('SCHEMAID') AS "用名ID" 
FROM SYS.DUAL;

-- 当前用户
SELECT USERNAME             AS "用户名"
     , USER_ID              AS "用户ID"
     , DEFAULT_TABLESPACE   AS "默认表空间"
     , TEMPORARY_TABLESPACE AS "临时表空间"
     , t.*
FROM SYS.USER_USERS t;
-- `SYS.DBA_USERS`DBA
-- `SYS.ALL_USERS`所有

2. 表空间

-- 当前用户,表空间
SELECT TABLESPACE_NAME AS "表空间名", t.*
FROM SYS.USER_TABLESPACES t;

3. 数据表

  • 所有表 + 物化视图。
-- 当前用户,所有表 + 物化视图
SELECT TABLE_NAME AS "表名", TABLESPACE_NAME AS "表空间名", t.*
FROM SYS.USER_TABLES t;

  • 所有表 + 物化视图。
-- 当前用户,所有表 + 物化视图
SELECT TABLE_NAME AS "表名", TABLESPACE_NAME AS "表空间名", t.*
FROM SYS.USER_ALL_TABLES t;

  • 所有表 + 视图的注释。
-- 当前用户,所有表 + 视图的注释
SELECT TABLE_NAME AS "表名", TABLE_TYPE AS "表类型[TABLE|VIEW]", COMMENTS AS "注释", t.*
FROM SYS.USER_TAB_COMMENTS t;

4. 数据列

-- 当前用户,所有表 + 视图 + 物化视图的列。
SELECT TABLE_NAME   AS "表名"
     , COLUMN_NAME  AS "列名"
     , DATA_TYPE    AS "数据类型"
     , DATA_LENGTH  AS "数据长度"
     , NULLABLE     AS "可以为空[N|Y]"
     , DATA_DEFAULT AS "默认数据"
     , COLUMN_ID    AS "行号"
     , t.*
FROM SYS.USER_TAB_COLUMNS t
ORDER BY TABLE_NAME, COLUMN_ID;

  • 所有表 + 视图 + 物化视图的注释。
-- 当前用户,所有表 + 视图 + 物化视图的注释。
SELECT TABLE_NAME  AS "表名"
     , COLUMN_NAME AS "列名"
     , COMMENTS    AS "注释"
     , t.*
FROM SYS.USER_COL_COMMENTS t;

5. 约束

  • 所有表的约束列。
-- 当前用户,所有表的约束列。
SELECT OWNER           AS "所属用户"
     , CONSTRAINT_NAME AS "约束名"
     , TABLE_NAME      AS "表名"
     , COLUMN_NAME     AS "列名"
     , POSITION        AS "位置"
     , t.*
FROM SYS.USER_CONS_COLUMNS t
WHERE OWNER = 'QS';

  • 所有表 + 视图的约束。
-- 当前用户,所有表 + 视图的约束。
SELECT OWNER            AS "所属用户"
     , CONSTRAINT_NAME  AS "约束名"
     , CONSTRAINT_TYPE  AS "约束类型[P主键|R外键|V|C]"
     , TABLE_NAME       AS "表名"
     , SEARCH_CONDITION AS "搜索条件"
     , INDEX_OWNER      AS "所属用户"
     , R_OWNER          AS "外键所属用户"
     , INDEX_NAME       AS "索引名"
     , t.*
FROM SYS.USER_CONSTRAINTS t
WHERE OWNER = 'QS';

6. 索引

  • 所有索引。
-- 当前用户,所有索引。
SELECT INDEX_NAME      AS "索引名"
     , INDEX_TYPE      AS "索引类型[NORMAL|UNIQUE|BITMAP]"
     , TABLE_OWNER     AS "表的主人"
     , TABLE_NAME      AS "表名"
     , TABLE_TYPE      AS "表类型[TABLE]"
     , UNIQUENESS      AS "唯一的[UNIQUE|NONUNIQUE]"
     , TABLESPACE_NAME AS "表空间名"
     , t.*
FROM SYS.USER_INDEXES t;

  • 所有索引列。
-- 当前用户,所有索引列。
SELECT INDEX_NAME      AS "索引名"
     , TABLE_NAME      AS "表名"
     , COLUMN_NAME     AS "列名"
     , COLUMN_POSITION AS "列的位置"
     , COLUMN_LENGTH   AS "列的长度"
     , DESCEND         AS "排序[ASC|DESC]"
     , t.*
FROM SYS.USER_IND_COLUMNS t;

7. 视图

  • 所有视图。
-- 当前用户,所有视图
SELECT VIEW_NAME AS "视图名", t.*
FROM SYS.USER_VIEWS t;

8. 物化视图

  • 所有物化视图。
-- 当前用户,所有物化视图
SELECT OWNER AS "所属用户", MVIEW_NAME AS "物化视图名", t.*
FROM SYS.USER_MVIEWS t
WHERE OWNER = 'QS';

9. 存储过程 + 存储函数

-- 当前用户,所有存储过程 + 存储函数。
SELECT OBJECT_NAME    AS "对象名[过程名|方法名]"
     , PROCEDURE_NAME AS "过程名"
     , OBJECT_TYPE    AS "对象类型[PROCEDURE|FUNCTION]"
     , t.*
FROM SYS.USER_PROCEDURES t
WHERE OBJECT_TYPE IN ('PROCEDURE', 'FUNCTION');

-- 当前用户,所有存储过程 + 存储函数配置。
SELECT OBJECT_NAME AS "对象名[过程名|方法名]"
     , PARAM_VALUE AS "参数值"
     , OBJECT_NAME AS "对象名[存储函数]"
     , t.*
FROM SYS.USER_STORED_SETTINGS t;

7. 触发器

  • 所有触发器。
-- 当前用户,所有触发器。
SELECT TRIGGER_NAME     AS "触发器名"
     , TRIGGER_TYPE     AS "触发类型"
     , TRIGGERING_EVENT AS "触发事件"
     , TABLE_OWNER      AS "表的主人"
     , BASE_OBJECT_TYPE AS "基本对象类型"
     , TABLE_NAME       AS "表名"
     , COLUMN_NAME      AS "列名"
     , t.*
FROM SYS.USER_TRIGGERS t;

  • 所有触发器列。
-- 当前用户,所有触发器列。
SELECT TRIGGER_OWNER AS "所属用户"
     , TRIGGER_NAME  AS "触发器名"
     , TABLE_OWNER   AS "表的主人"
     , TABLE_NAME    AS "表名"
     , COLUMN_NAME   AS "列名"
     , COLUMN_LIST   AS "列清单"
     , COLUMN_USAGE  AS "列的使用"
     , t.*
FROM SYS.USER_TRIGGER_COLS t
WHERE TABLE_OWNER = 'QS';

  • 所有触发器命令。
-- 当前用户,所有触发器命令。
SELECT TRIGGER_NAME  AS "触发器名"
     , REFERENCED_TRIGGER_OWNER
     , REFERENCED_TRIGGER_NAME
     , ORDERING_TYPE AS "命令类型"
     , t.*
FROM USER_TRIGGER_ORDERING t;

8. 目录 + 对象

-- 所有用户,所有目录。
SELECT OWNER          AS "所属用户"
     , DIRECTORY_NAME AS "目录名称"
     , DIRECTORY_PATH AS "目录路径"
     , t.*
FROM SYS.ALL_DIRECTORIES t;

-- 当前用户,所有对象。
SELECT OBJECT_NAME AS "对象名称"
     , OBJECT_TYPE AS "对象类型[TABLE|INDEX|SEQUENCE]"
     , t.*
FROM SYS.USER_OBJECTS t;

-- 数据文件
SELECT TABLESPACE_NAME AS "表空间名", SUM(BYTES) SIZES
FROM SYS.DBA_DATA_FILES t
GROUP BY TABLESPACE_NAME;

-- 自由空间
SELECT TABLESPACE_NAME AS "表空间名", SUM(BYTES) FREES
FROM SYS.USER_FREE_SPACE t
GROUP BY TABLESPACE_NAME;

二、Oracle 查询表结构

-- `Oracle`查询表结构。
SELECT DECODE(t2.COLUMN_ID, 1, t.TABLE_NAME, '')    AS "表名"
     , DECODE(t2.COLUMN_ID, 1, t.COMMENTS, '')      AS "表注释"
     , t2.COLUMN_NAME                               AS "列名"
     , t2.DATA_TYPE || '(' || t2.DATA_LENGTH || ')' AS "类型"
     , DECODE(t2.NULLABLE, 'N', 'N', '')            AS "NULL"
     , t2.DATA_DEFAULT                              AS "默认数据"
     , t4.CONSTRAINT_TYPE                           AS "键"
     , t3.COMMENTS                                  AS "列注释"
FROM SYS.USER_TAB_COMMENTS t
INNER JOIN SYS.USER_TAB_COLUMNS t2 ON t2.TABLE_NAME = t.TABLE_NAME
INNER JOIN SYS.USER_COL_COMMENTS t3 ON t3.TABLE_NAME = t.TABLE_NAME AND t3.COLUMN_NAME = t2.COLUMN_NAME
LEFT JOIN (
    SELECT t.TABLE_NAME, t.COLUMN_NAME, t2.CONSTRAINT_TYPE
    FROM SYS.USER_CONS_COLUMNS t
    LEFT JOIN SYS.USER_CONSTRAINTS t2 ON t2.CONSTRAINT_NAME = t.CONSTRAINT_NAME
    WHERE t2.CONSTRAINT_TYPE = 'P'
) t4 ON t4.TABLE_NAME = t.TABLE_NAME AND t4.COLUMN_NAME = t2.COLUMN_NAME
ORDER BY t.TABLE_NAME, t2.COLUMN_ID;

你可能感兴趣的:(Oracle,oracle,数据库,sql)