0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PostgreSQLでとある列名を使っているテーブルを調べる

Last updated at Posted at 2024-11-12

たくさんのテーブルで同じ列名を使っている時なんかに、
 
「あれ?あの項目ってどのテーブルで使ってるんだっけ?」
 
とか思ったことがあると思います。そんた時に使います。
とある列名がどこのテーブルで使われているのかを調べるSQLです。意外と便利です。
コメントアウトしているところは必要に応じて生かせばいろいろな情報が出力されます。

--
-- とある列名を使っているテーブルを調べるSQL
--
SELECT
    table_catalog                               -- table_catalog
    , table_schema                              -- table_schema
    , table_name                                -- table_name
    , column_name                               -- column_name
--     , ordinal_position                          -- ordinal_position
--     , column_default                            -- column_default
--     , is_nullable                               -- is_nullable
--     , data_type                                 -- data_type
--     , character_maximum_length                  -- character_maximum_length
--     , character_octet_length                    -- character_octet_length
--     , numeric_precision                         -- numeric_precision
--     , numeric_precision_radix                   -- numeric_precision_radix
--     , numeric_scale                             -- numeric_scale
--     , datetime_precision                        -- datetime_precision
--     , interval_type                             -- interval_type
--     , interval_precision                        -- interval_precision
--     , character_set_catalog                     -- character_set_catalog
--     , character_set_schema                      -- character_set_schema
--     , character_set_name                        -- character_set_name
--     , collation_catalog                         -- collation_catalog
--     , collation_schema                          -- collation_schema
--     , collation_name                            -- collation_name
--     , domain_catalog                            -- domain_catalog
--     , domain_schema                             -- domain_schema
--     , domain_name                               -- domain_name
--     , udt_catalog                               -- udt_catalog
--     , udt_schema                                -- udt_schema
--     , udt_name                                  -- udt_name
--     , scope_catalog                             -- scope_catalog
--     , scope_schema                              -- scope_schema
--     , scope_name                                -- scope_name
--     , maximum_cardinality                       -- maximum_cardinality
--     , dtd_identifier                            -- dtd_identifier
--     , is_self_referencing                       -- is_self_referencing
--     , is_identity                               -- is_identity
--     , identity_generation                       -- identity_generation
--     , identity_start                            -- identity_start
--     , identity_increment                        -- identity_increment
--     , identity_maximum                          -- identity_maximum
--     , identity_minimum                          -- identity_minimum
--     , identity_cycle                            -- identity_cycle
--     , is_generated                              -- is_generated
--     , generation_expression                     -- generation_expression
--     , is_updatable                              -- is_updatable
FROM
    information_schema.columns 
WHERE
1 = 1
-- AND table_schema = 'hoge'          -- スキーマ名で絞りたい時はスキーマ名を書く
AND column_name like '%fuga%'      -- ここに列名を書く
;

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?