table_constraints
information_schemaのtable_constraintsビューを参照する事で、制約を確認できます。フォーリンキー一覧やプライマリーキー一覧を取得できます。
制約一覧出力SQL
SELECT
c.constraint_schema
,c.constraint_type
,c.constraint_name,c.table_schema,c.table_name
FROM information_schema.table_constraints c
WHERE 1=1
and constraint_schema='sooni'
order by c.constraint_type
;
SELECT
c.constraint_schema
,c.constraint_type
,c.constraint_name,c.table_schema,c.table_name
FROM information_schema.table_constraints c
WHERE 1=1
and constraint_schema='sooni'
order by c.constraint_type
;
制約一覧出力例
PK一覧(構成項目含む)出力SQL
select c.constraint_type,u.table_schema,u.table_name,u.constraint_name,u.ordinal_position,u.column_name
from information_schema.table_constraints c inner join information_schema.key_column_usage u
on c.table_schema = u.table_schema and c.table_name = u.table_name and c.constraint_name = u.constraint_name
where c.table_schema='apuser'
and c.constraint_type='PRIMARY KEY'
order by c.constraint_type,u.constraint_schema,u.constraint_name,u.table_schema,u.table_name,u.ordinal_position,u.column_name
;
select c.constraint_type,u.table_schema,u.table_name,u.constraint_name,u.ordinal_position,u.column_name
from information_schema.table_constraints c inner join information_schema.key_column_usage u
on c.table_schema = u.table_schema and c.table_name = u.table_name and c.constraint_name = u.constraint_name
where c.table_schema='apuser'
and c.constraint_type='PRIMARY KEY'
order by c.constraint_type,u.constraint_schema,u.constraint_name,u.table_schema,u.table_name,u.ordinal_position,u.column_name
;
フォーリンキー一覧(構成項目含む)出力SQL
select c.constraint_type,u.table_schema,u.table_name,u.constraint_name,u.ordinal_position,u.column_name
from information_schema.table_constraints c inner join information_schema.key_column_usage u
on c.table_schema = u.table_schema and c.table_name = u.table_name and c.constraint_name = u.constraint_name
where c.table_schema='apuser'
and c.constraint_type='FOREIGN KEY'
order by c.constraint_type,u.constraint_schema,u.constraint_name,u.table_schema,u.table_name,u.ordinal_position,u.column_name
;
select c.constraint_type,u.table_schema,u.table_name,u.constraint_name,u.ordinal_position,u.column_name
from information_schema.table_constraints c inner join information_schema.key_column_usage u
on c.table_schema = u.table_schema and c.table_name = u.table_name and c.constraint_name = u.constraint_name
where c.table_schema='apuser'
and c.constraint_type='FOREIGN KEY'
order by c.constraint_type,u.constraint_schema,u.constraint_name,u.table_schema,u.table_name,u.ordinal_position,u.column_name
;