Trafodion 이 메타 데이터 시트 에서 필드 정 보 를 얻 는 것 에 대한 총 결

9211 단어
Trafodion 의 모든 대상 의 메타 데이터 정 보 는 schema ' MD' 아래 에 저장 되 고 대상 의 정보, 필드 의 속성 등 입 니 다.필드 정 보 를 얻 는 데 자주 사용 되 는 SQL 방법 을 설명 합 니 다.테스트 표 는 다음 과 같 습 니 다.
--     
DROP TABLE IF EXISTS aaa;
CREATE TABLE aaa (
	a NUMERIC(18, 8),
	b varchar2(10),
	c INT,
	d largeint,
	e CLOB,
	f DATE,
	g TIMESTAMP(6),
	h NUMERIC(20, 10),
	PRIMARY KEY (a, b)
);

CREATE UNIQUE INDEX idx_aaa ON aaa (c, d);

COMMENT ON TABLE aaa IS ' aaa   ';
COMMENT ON COLUMN aaa.a IS '  a   ';
COMMENT ON COLUMN aaa.b IS '  b   ';
COMMENT ON COLUMN aaa.c IS '  c   ';
COMMENT ON COLUMN aaa.d IS '  d   ';
COMMENT ON COLUMN aaa.e IS '  e   ';
COMMENT ON COLUMN aaa.f IS '  f   ';
COMMENT ON COLUMN aaa.g IS '  g   ';
COMMENT ON COLUMN aaa.h IS '  h   ';

1. 필드 상세 정보 가 져 오기
SQL 구문
SELECT a.schema_name, a.object_name AS table_name, b.column_name, b.sql_data_type AS data_type, b.column_size AS data_length
	, b.column_precision AS data_precision, b.nullable, b.column_number AS column_id, b.column_scale AS data_scale
	, CASE 
		WHEN c.column_name IS NOT NULL THEN 'Y'
		ELSE 'N'
	END AS is_pk
	, CASE 
		WHEN g.column_name IS NOT NULL THEN 'Y'
		ELSE 'N'
	END AS is_unique
	, h.text AS comments
FROM "_MD_".objects a
	INNER JOIN "_MD_".columns b ON a.object_uid = b.object_uid
	LEFT JOIN "_MD_".keys c
	ON b.object_uid = c.object_uid
		AND b.column_name = c.column_name
	LEFT JOIN (SELECT d.base_table_uid,  rtrim(f.column_name,'@') AS column_name
		FROM "_MD_".indexes d
			INNER JOIN "_MD_".objects e ON d.index_uid = e.object_uid
			INNER JOIN "_MD_".columns f ON e.object_uid = f.object_uid
		WHERE d.is_unique = 1
		  AND f.column_name <> 'SYSKEY') g
	ON a.object_uid = g.base_table_uid
	    AND b.column_name = g.column_name
	LEFT JOIN "_MD_".text h 
	ON b.object_uid = h.text_uid
		AND b.column_number = h.sub_id
		AND h.text_type = 12
WHERE a.schema_name NOT LIKE '|_BACKUP|_%%|_' ESCAPE '|'
AND a.schema_name NOT IN ('_MD_', '_PRIVMGR_MD_', '_REPOS_', '_LIBMGR_')
AND a.object_type IN ('BT')
AND a.object_name NOT IN ('SB_HISTOGRAMS', 'SB_HISTOGRAM_INTERVALS', 'SB_PERSISTENT_SAMPLES')
AND b.column_name <> 'SYSKEY'
AND a.schema_name = 'SEABASE'
AND a.object_name = 'AAA'
;

샘플 출력
SCHEMA_NAME
TABLE_NAME
COLUMN_NAME
DATA_TYPE
DATA_LENGTH
DATA_PRECISION
NULLABLE
COLUMN_ID
DATA_SCALE
IS_PK
IS_UNIQUE
COMMENTS
SEABASE
AAA
A
SIGNED LARGEINT
8
18
0
0
8
Y
Y
필드 a 의 주석
SEABASE
AAA
H
SIGNED NUMERIC
10
20
1
7
10
N
N
필드 h 주석
SEABASE
AAA
G
DATETIME
11
0
1
6
6
N
N
필드 g 주석
SEABASE
AAA
F
DATETIME
4
0
1
5
0
N
N
필드 f 의 주석
SEABASE
AAA
E
CLOB
1024
1
1
4
1073741824
N
N
필드 e 의 주석
SEABASE
AAA
D
SIGNED LARGEINT
8
0
1
3
0
N
Y
필드 d 주석
SEABASE
AAA
C
SIGNED INTEGER
4
0
1
2
0
N
Y
필드 c 의 주석
SEABASE
AAA
B
VARCHAR
10
0
0
1
0
Y
Y
필드 b 의 주석
2. 홈 키 필드 정보 가 져 오기
SQL 구문
SELECT a.schema_name, a.object_name AS table_name, b.column_name
	, CASE 
		WHEN c.column_name IS NOT NULL THEN 'Y'
		ELSE 'N'
	END AS is_pk
FROM "_MD_".objects a
	INNER JOIN "_MD_".columns b ON a.object_uid = b.object_uid
	LEFT JOIN "_MD_".keys c
	ON b.object_uid = c.object_uid
		AND b.column_name = c.column_name
WHERE a.schema_name NOT LIKE '|_BACKUP|_%%|_' ESCAPE '|'
AND a.schema_name NOT IN ('_MD_', '_PRIVMGR_MD_', '_REPOS_', '_LIBMGR_')
AND a.object_type IN ('BT')
AND a.object_name NOT IN ('SB_HISTOGRAMS', 'SB_HISTOGRAM_INTERVALS', 'SB_PERSISTENT_SAMPLES')
AND b.column_name <> 'SYSKEY'
AND c.column_name IS NOT NULL
AND a.schema_name = 'SEABASE'
AND a.object_name = 'AAA'
;

샘플 출력
SCHEMA_NAME
TABLE_NAME
COLUMN_NAME
IS_PK
SEABASE
AAA
A
Y
SEABASE
AAA
B
Y
3. 고정 테이블 이름 이 있 는 schema 가 져 오기
SQL 구문
SELECT a.schema_name
FROM "_MD_".objects a
WHERE a.object_name = 'AAA'
;

샘플 출력
SCHEMA_NAME
SEABASE
4. 지 정 된 shema 이름 가 져 오기
SQL 구문
SELECT distinct a.schema_name
FROM "_MD_".objects a
WHERE a.schema_name = 'V7DEV'
;

샘플 출력
SCHEMA_NAME
V7DEV
5. 고정 표 이름 의 필드 에 유일한 정 보 를 가 져 올 지 여부
SQL 구문
SELECT a.schema_name, a.object_name AS table_name, b.column_name
	, CASE 
		WHEN instr(g.column_list, b.column_name) > 0 THEN 'Y'
		ELSE 'N'
	END AS is_unique
FROM "_MD_".objects a
	INNER JOIN "_MD_".columns b ON a.object_uid = b.object_uid
	LEFT JOIN "_MD_".keys c
	ON b.object_uid = c.object_uid
		AND b.column_name = c.column_name
	LEFT JOIN seabase.tbl_column_list g
	ON a.object_uid = g.base_table_uid
WHERE a.schema_name NOT LIKE '|_BACKUP|_%%|_' ESCAPE '|'
AND a.schema_name NOT IN ('_MD_', '_PRIVMGR_MD_', '_REPOS_', '_LIBMGR_')
AND a.object_type IN ('BT')
AND a.object_name NOT IN ('SB_HISTOGRAMS', 'SB_HISTOGRAM_INTERVALS', 'SB_PERSISTENT_SAMPLES')
AND b.column_name <> 'SYSKEY'
AND a.schema_name = 'SEABASE'
AND a.object_name = 'AAA'
;  

샘플 출력
SCHEMA_NAME
TABLE_NAME
COLUMN_NAME
IS_UNIQUE
SEABASE
AAA
A
Y
SEABASE
AAA
B
Y
SEABASE
AAA
C
Y
SEABASE
AAA
D
Y
SEABASE
AAA
E
N
SEABASE
AAA
F
N
SEABASE
AAA
G
N
SEABASE
AAA
H
N
6. 고정 schema 아래 표 의 줄 수 가 져 오기
SQL 구문
--    ,           
CREATE OR REPLACE FUNCTION count_rows(table_name varchar2(200), 
schema_name varchar2(200)  
) RETURN (rowcount INT)
AS
DECLARE
  rowcount number;
  stmt     varchar2(2000);
BEGIN
  if schema_name is null then
    stmt := 'select count(*) from "' || table_name || '"';
  else
    stmt := 'select count(*) from "' || schema_name || '"."' || table_name || '"';
  end if;
PREPARE s1 FROM stmt;
execute s1 into rowcount;
return rowcount;
END;
/

SELECT a.schema_name, a.object_name AS table_name, seabase.count_rows(a.object_name, a.schema_name) AS rowcount
FROM "_MD_".objects a
WHERE a.object_name NOT IN ('SB_HISTOGRAMS', 'SB_HISTOGRAM_INTERVALS', 'SB_PERSISTENT_SAMPLES')
AND a.object_name NOT LIKE 'LOBDescChunks%'
AND a.object_name NOT LIKE 'LOBMD%'
AND a.object_name NOT LIKE 'LOBDescHandle%'
AND a.object_type IN ('BT')
AND a.schema_name = 'SEABASE'
;       

샘플 출력
SCHEMA_NAME |TABLE_NAME      |ROWCOUNT 
------------|----------------|---------
SEABASE     |AAA             |0        
SEABASE     |TBL_COLUMN_LIST |1347  

7. 표 마다 고정된 schema 필드 목록 가 져 오기
SQL 구문
--         
SELECT a.schema_name, a.object_name AS table_name, pivot(b.column_name, MAX LENGTH 10240) AS column_list
FROM "_MD_".objects a
	INNER JOIN "_MD_".columns b ON a.object_uid = b.object_uid
WHERE a.schema_name NOT LIKE '|_BACKUP|_%%|_' ESCAPE '|'
AND a.schema_name NOT IN ('_MD_', '_PRIVMGR_MD_', '_REPOS_', '_LIBMGR_')
AND a.object_type IN ('BT')
AND a.object_name NOT IN ('SB_HISTOGRAMS', 'SB_HISTOGRAM_INTERVALS', 'SB_PERSISTENT_SAMPLES')
AND b.column_name <> 'SYSKEY'
AND a.schema_name = 'SEABASE'
AND a.object_name = 'AAA'
GROUP BY 1,2
;
--        
SELECT a.schema_name, a.object_name AS table_name, pivot(b.column_name, MAX LENGTH 10240, ORDER BY (b.column_number)) AS column_list
FROM "_MD_".objects a
	INNER JOIN "_MD_".columns b ON a.object_uid = b.object_uid
WHERE a.schema_name NOT LIKE '|_BACKUP|_%%|_' ESCAPE '|'
AND a.schema_name NOT IN ('_MD_', '_PRIVMGR_MD_', '_REPOS_', '_LIBMGR_')
AND a.object_type IN ('BT')
AND a.object_name NOT IN ('SB_HISTOGRAMS', 'SB_HISTOGRAM_INTERVALS', 'SB_PERSISTENT_SAMPLES')
AND b.column_name <> 'SYSKEY'
AND a.schema_name = 'SEABASE'
AND a.object_name = 'AAA'
GROUP BY 1,2
;

샘플 출력
SCHEMA_NAME
TABLE_NAME
COLUMN_LIST
SEABASE
AAA
A,B,C,D,E,F,G,H

좋은 웹페이지 즐겨찾기