5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ORACLEのテーブル定義を確認する

Last updated at Posted at 2016-03-18

Oracleではテーブル定義がuser_tab_columnsに保存されています。
ただし、格納されているのは型名や主キー,indexといった情報で制約に関しては取得することは出来ません。
なので制約定が必要な場合はuser_constraintsとJOINすれば制約ごと参照することが可能になります。

SELECT DISTINCT
    main.column_name AS カラム名,
    main.data_type AS データ型,
    main.data_length AS カラム長,
    CASE main.nullable
        WHEN  'N' THEN '×'
        WHEN  'Y' THEN '○'
    END AS NULL許可,
    sub.constraint_type AS 主キー,
    sub.last_change AS 最終更新日,
    sub.index_name AS インデックス名
    
FROM 
    user_tab_columns main
LEFT JOIN
    
       (SELECT
        col.column_name AS column_name, 
            con.constraint_type AS constraint_type,
            con.last_change AS last_change,
            con.index_name AS index_name,
            con.table_name AS table_name
        FROM 
            user_constraints con
        JOIN
            user_cons_columns col 
            ON
            con.constraint_name = col.constraint_name 
            AND
            con.table_name = col.table_name 
        WHERE
            con.constraint_type = 'P'  
        ORDER BY 
           con.table_name) sub
      
    ON 
    sub.column_name = main.column_name
    AND
    sub.table_name = main.table_name
WHERE 
    main.table_name = :table_name;

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?