1
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?

SQL倉庫 - 制約の取得

1
Last updated at Posted at 2026-06-04

リファクタリングなど現状調査でER図を書くことあって、
毎回制約を取得していたので残します。
SQLSERVERです

テーブルの制約を取得する
SELECT 
    tc.TABLE_NAME AS [テーブル名],
    CASE tc.CONSTRAINT_TYPE 
        WHEN 'PRIMARY KEY' THEN 'PK'
        WHEN 'FOREIGN KEY' THEN 'FK'
        WHEN 'UNIQUE'      THEN 'UK'
        WHEN 'CHECK'       THEN 'CK'
        ELSE tc.CONSTRAINT_TYPE 
    END AS [制約タイプ],
    tc.CONSTRAINT_NAME AS [制約名],
    kcu.COLUMN_NAME AS [項目名]
FROM 
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
LEFT JOIN 
    INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu 
    ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
WHERE 
    tc.TABLE_NAME LIKE '%table_name%'
ORDER BY 
    tc.TABLE_NAME, 
    tc.CONSTRAINT_TYPE,
    kcu.ORDINAL_POSITION;
1
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
1
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?