最近在開發數據庫元數據抽取的功能,鑑於本身剛剛開始學習元數據查詢,在開發的過程當中遇到了許多問題,針對這些問題,在網站參考了許多前輩的經驗,下面把開發中使用到的sql語句記錄下來,但願對你們有幫助。sql
1
2
3
4
5
6
|
--獲取視圖信息和視圖描述信息comments
SELECT
C.COMMENTS, T.*
FROM
ALL_VIEWS T, ALL_TAB_COMMENTS C
WHERE
1 = 1
AND
T.VIEW_NAME = C.TABLE_NAME
AND
T.VIEW_NAME =
'VIEW_NAME'
|
1
2
3
4
5
6
7
8
9
10
|
--獲取到視圖相關的字段,並查詢出字段是否可插入(insertable)、可更新(updatable)、可刪除(deletable)並查詢出注釋信息comments
SELECT
C.COMMENTS, T.*, U.UPDATABLE, U.INSERTABLE, U.DELETABLE
FROM
ALL_TAB_COLUMNS T, ALL_UPDATABLE_COLUMNS U, ALL_COL_COMMENTS C
WHERE
1 = 1
AND
U.TABLE_NAME = T.TABLE_NAME
AND
U.COLUMN_NAME = T.COLUMN_NAME
AND
C.TABLE_NAME = T.TABLE_NAME
AND
C.COLUMN_NAME = T.COLUMN_NAME
AND
T.TABLE_NAME =
'VIEWNAME'
|
1
2
3
4
5
|
--查詢出視圖和表的依賴信息dependencies
SELECT
C.*
FROM
ALL_DEPENDENCIES C
WHERE
1 = 1
AND
C.
NAME
=
'VIEWNAME'
|
1
2
3
4
5
6
|
--獲取數據庫中的表信息和表註釋comments
SELECT
C.COMMENTS, T.*
FROM
ALL_TABLES T, ALL_TAB_COMMENTS C
WHERE
1 = 1
AND
T.TABLE_NAME = C.TABLE_NAME
AND
T.TABLE_NAME =
'TABLE_NAME'
|
1
2
3
4
5
6
|
--查詢出表中的列信息,包括列的註釋comments
SELECT
T.*, C.COMMENTS
FROM
ALL_TAB_COLUMNS T, ALL_COL_COMMENTS C
WHERE
T.TABLE_NAME = C.TABLE_NAME
AND
C.COLUMN_NAME = T.COLUMN_NAME
AND
T.TABLE_NAME =
'TABLE_NAME'
|
1
2
3
4
5
6
|
--查詢出表的主鍵信息和主鍵所在的列名稱column_name
SELECT
A.COLUMN_NAME, C.*
FROM
ALL_CONSTRAINTS C, ALL_CONS_COLUMNS A
WHERE
C.CONSTRAINT_TYPE =
'P'
AND
C.CONSTRAINT_NAME = A.CONSTRAINT_NAME
AND
C.TABLE_NAME =
'TABLE_NAME'
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
--獲取到外鍵的信息,包括外鍵所在的列名稱和外鍵所參照的主鍵的表名稱和主鍵所在的列的信息
SELECT
T2.R_COLUMN_NAME, T2.R_TABLE_NAME, T1.*
FROM
(
SELECT
A1.COLUMN_NAME, C.*
FROM
ALL_CONSTRAINTS C, ALL_CONS_COLUMNS A1
WHERE
C.CONSTRAINT_TYPE =
'R'
AND
C.TABLE_NAME =
'TABLE_NAME'
AND
A1.CONSTRAINT_NAME = C.CONSTRAINT_NAME)T1,
(
SELECT
A1.COLUMN_NAME
AS
R_COLUMN_NAME,
A1.TABLE_NAME
AS
R_TABLE_NAME,
C.*
FROM
ALL_CONSTRAINTS C, ALL_CONS_COLUMNS A1
WHERE
A1.CONSTRAINT_NAME = C.CONSTRAINT_NAME) T2
WHERE
T1.R_CONSTRAINT_NAME = T2.CONSTRAINT_NAME
|
1
2
3
4
5
|
--查詢出表相關的檢查約束信息
SELECT
C.*
FROM
ALL_CONSTRAINTS C
WHERE
C.CONSTRAINT_TYPE =
'C'
AND
C.TABLE_NAME =
'TABLE_NAME'
|
1
2
3
4
5
|
--查詢出表相關的惟一約束信息
SELECT
C.*
FROM
ALL_CONSTRAINTS C
WHERE
C.CONSTRAINT_TYPE =
'U'
AND
C.TABLE_NAME =
'TABLE_NAME'
|
1
2
3
4
5
|
--表中的索引信息
SELECT
C.*
FROM
ALL_INDEXES C
WHERE
1 = 1
AND
C.TABLE_NAME =
'TABLE_NAME'
|
1
2
3
4
5
|
--表與視圖的依賴信息
SELECT
C.*
FROM
ALL_DEPENDENCIES C
WHERE
1 = 1
AND
C.REFERENCED_NAME =
'TABLE_NAME'
|
同時,數據字典中的前綴是能夠根據查詢的權限範圍進行修改的,例USER_ ALL_ DBA_ 這三種的範圍是由小到大的範圍,數據庫