Obsidian/Recognition/Work Related/프로젝트/스마트계류장/1.데이터베이스/DB 정의서 추출.md

4.9 KiB

-- Table 목록
-- select distinct ETT_NO , ETT_GBN, ETT_ID , TableNm , ETT_DESC , TableID , TableNm
select distinct ETT_NO , ETT_GBN, ETT_ID , TableNm , ETT_DESC , '','','', TableID , TableNm
from Table_items
order by ETT_SEQ, cast(ETT_NO as integer)


-- Table 상세
-- select distinct ETT_NO , ETT_GBN, ETT_ID , TableNm , ETT_DESC , TableID , TableNm
select SEQ , no, TableID , tablenm, CollumnID , CollumnNm , Dong , `Type` , Len , NullYn , Pkyn , Fkyn , Idx , Def , Jeay , bigo 
from Table_items
order by ETT_SEQ, cast(ETT_NO as integer), cast(no as double)


-- 공통 항목 찾기

select CollumnID , CollumnNm  , count(*) as cnt
from table_items 
where CollumnID like '%#_AT'escape '#'
group by CollumnID , CollumnNm 
order by cnt desc 

select *
from table_items ti 
where nvl(CollumnNm,'') like '%%'
and nvl(CollumnID,'') like '%%'
and TableID like '%TIC_GROUND_VHCLE%'
order by cast(no as integer)



-- 테이블명(물리) 테이블명(논리) 테이블설명 컬럼ID 컬럼(물리) 컬럼(논리) 타입 타입길이 NotNull, PK, FK, INX, 기본갑, 제약조건 비고
WITH tbl AS (
	SELECT A.OWNER
	    , B.COMMENTS TABLE_COMMENTS
	    , A.TABLE_NAME TABLE_NAME
	    , C.COMMENTS COLUMN_COMMENTS
	    , A.COLUMN_NAME COLUMN_NAME
	    , CASE A.NULLABLE WHEN 'Y'THEN 'ㅇ' ELSE '' END NULL_FLAG
	    , CASE WHEN A.DATA_TYPE = 'TIMESTAMP(6)' THEN 'TIMESTAMP' ELSE A.DATA_TYPE END DATA_TYPE		
	    , CASE WHEN A.DATA_TYPE IN ('CHAR', 'VARCHAR2')
	            THEN '' || A.DATA_LENGTH || ''
	            WHEN A.DATA_TYPE = 'NUMBER' AND A.DATA_SCALE = 0 AND A.DATA_PRECISION IS NOT NULL
	            THEN '' || A.DATA_PRECISION || ''
	            WHEN A.DATA_TYPE = 'NUMBER' AND A.DATA_SCALE <> 0
	            THEN '' || A.DATA_PRECISION || ',' || A.DATA_SCALE || ''
	            END DATA_LENGTH
	    , A.COLUMN_ID
	    , A.DATA_DEFAULT
	FROM DBA_TAB_COLUMNS A
	  , DBA_TAB_COMMENTS B
	  , DBA_COL_COMMENTS C
	WHERE A.OWNER = B.OWNER
	AND A.TABLE_NAME = B.TABLE_NAME
	AND A.TABLE_NAME = C.TABLE_NAME
	AND A.COLUMN_NAME = C.COLUMN_NAME
	AND A.owner in ('UCM', 'UIC', 'UEAI', 'UTRACK', 'UDT')
),
cons AS (
	SELECT A.OWNER
	     , A.TABLE_NAME
	     , A.COLUMN_NAME
	     , CASE WHEN B.CONSTRAINT_TYPE = 'P' THEN 'ㅇ' ELSE '' END as PK
	     , CASE WHEN B.CONSTRAINT_TYPE = 'R' THEN 'ㅇ' ELSE '' END as FK
--	     , A.CONSTRAINT_NAME
	     , CASE WHEN B.CONSTRAINT_TYPE = 'P' THEN 'ㅇ' ELSE '' END as CONSTRAINT_NAME
	FROM DBA_CONS_COLUMNS A, DBA_CONSTRAINTS B
	WHERE A.OWNER = B.OWNER
	AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
	AND B.CONSTRAINT_TYPE IN ('P', 'R')
	AND A.OWNER in ('UCM', 'UIC', 'UEAI', 'UTRACK', 'UDT')
)
select t.table_name
     , nvl(trim(substr(t.table_comments, 1,  instr(t.table_comments,'(', 1) -1)), nvl(table_comments, 'Temp Table')) table_comments
     , trim(substr(t.table_comments, instr(t.table_comments,'(', 1) +1,  length(t.table_comments) - instr(t.table_comments,'(', 1) -1)) table_desc
     , t.column_id
     , t.column_name
     , case when instr(t.column_comments, 'CT') > 0
            then nvl(trim(substr(t.column_comments, 1,  instr(t.column_comments,'(', 1) -1)), column_comments)
            else t.column_comments 
       end as column_comments
     , t.data_type
     , t.data_length
     , decode(c.pk, 'ㅇ', 'ㅇ', t.null_flag ) as null_flag
     , c.pk
     , c.fk
     , c.constraint_name
     , t.data_default
     , '' cons
     , case when instr(t.column_comments, 'CT') > 0
            then trim(substr(t.column_comments, instr(t.column_comments,'(', 1) +1,  length(t.column_comments) - instr(t.column_comments,'(', 1) -1))
            else ''
       end as column_comments
     , nvl(t.table_comments, 'TEMP Table') as table_comments
from tbl t, cons c
where t.owner = c.owner(+)
and t.table_name = c.table_name(+)
and t.column_name = c.column_name(+)
AND t.owner in ( 'NONE'
--, 'UCM'
--, 'UIC'
--, 'UEAI'
--, 'UTRACK'
, 'UDT'
)
order by t.owner, t.table_name, t.column_id


-- 테이블 목록

-- 테이블명(물리) 테이블명(논리) 테이블설명 컬럼ID 컬럼(물리) 컬럼(논리) 타입 타입길이 NotNull, PK, FK, INX, 기본갑, 제약조건 비고
WITH tbl AS (
	SELECT B.OWNER
	    , B.COMMENTS TABLE_COMMENTS
	    , B.TABLE_NAME TABLE_NAME
	FROM DBA_TAB_COMMENTS B
	WHERE  B.owner in ('UCM', 'UIC', 'UEAI', 'UTRACK', 'UDT')
),
cons AS (
	SELECT A.OWNER
	     , A.TABLE_NAME
	     , A.CONSTRAINT_NAME
	FROM DBA_CONS_COLUMNS A
	WHERE A.OWNER in ('UCM', 'UIC', 'UEAI', 'UTRACK', 'UDT')
	and position = 0
)
select t.table_name
     , nvl(trim(substr(t.table_comments, 1,  instr(t.table_comments,'(', 1) -1)), nvl(table_comments, 'Temp Table')) table_comments
     , c.CONSTRAINT_NAME
--     , trim(substr(t.table_comments, instr(t.table_comments,'(', 1) +1,  length(t.table_comments) - instr(t.table_comments,'(', 1) -1)) table_desc
from tbl t, cons c
where t.owner = c.owner(+)
and t.table_name = c.table_name(+)
and t.owner in ( 'NONE'
--, 'UCM'
, 'UIC'
--, 'UEAI'
--, 'UTRACK'
--, 'UDT'
)
order by t.table_name