본문 바로가기

개발(합니다)/DB

[DB] 테이블 스페이스, 테이블, 컬럼 정보 조회하기(pg, mysql, oracle, tibero)

반응형

 

개발하다보면 테이블 스페이스의 용량이 얼마나 잡혀있는지, 사용 되어 있는지 확인하는 경우가 생깁니다.

혹은 테이블에 있는 컬럼 리스트를 뽑아봐야 하는 경우도 생기는데 필요한 쿼리를 정리합니다.

테이블, 컬럼 정보

Postgresql

1. 테이블 목록 조회

SELECT RELNAME AS TABLE_NAME
  FROM PG_STAT_USER_TABLES

2. 컬럼 목록 조회

select *
  from information_schema.columns
 where table_catalog = '데이터베이스명'
   and table_name    = '테이블명'
 order by ordinal_position;

3. 기본키 조회

select cc.column_name as column_name
  from information_schema.table_constraints       tc
      ,information_schema.constraint_column_usage cc
 where tc.table_catalog   = '데이터베이스명'
   and tc.table_name      = '테이블명'
   and tc.constraint_type = 'primary key'
   and tc.table_catalog   = cc.table_catalog
   and tc.table_schema    = cc.table_schema
   and tc.table_name      = cc.table_name
   and tc.constraint_name = cc.constraint_name

4. 테이블 comment 조회

select ps.relname    as table_name
      ,pd.description as table_comment
  from pg_stat_user_tables ps
      ,pg_description      pd
 where ps.relname  = '테이블명'
   and ps.relid   = pd.objoid
   and pd.objsubid  = 0


5. 컬럼 comment 조회

select ps.relname    as table_name
      ,pa.attname     as column_name
      ,pd.description as column_comment
  from pg_stat_all_tables ps
      ,pg_description     pd
      ,pg_attribute       pa
 where ps.schemaname = (select schemaname
                            from pg_stat_user_tables
                           where relname = '테이블명')
   and ps.relname  = '테이블명'
   and ps.relid   = pd.objoid
   and pd.objsubid <> 0
   and pd.objoid    = pa.attrelid
   and pd.objsubid  = pa.attnum
 order by ps.relname, pd.objsubid

Mysql

1. 테이블 정보 조회

select * 
from infomation_schema.tables 
where table_schema='계정명';

=> show tables;



2. 테이블에 해당하는 컬럼명  조회

select * 
 from infomation_schema.columns 
where table_schema='계정명' 

=> show full columns from 테이블명 ;

Tibero

1. 테이블 조회

SELECT * FROM ALL_TAB_COMMENTS;
SELECT * FROM ALL_TAB_COMMENTS WHERE TABLE_NAME = '테이블명';

2. 티베로 컬럼명 조회

SELECT * FROM ALL_COL_COMMENTS WHERE TABLE_NAME = '테이블명';

3. 티베로 테이블 상세정보 조회

SELECT * FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = '테이블명'; 

Oracle

1. 테이블 조회 

SELECT A.TABLE_NAME 
       , B.COMMENTS 
    FROM USER_TABLES A 
반응형