oracle 查询所有约束

时间:2023-01-29 22:05:20

主键约束
SELECT
  USER_CONS_COLUMNS.CONSTRAINT_NAME AS 约束名,
  USER_CONS_COLUMNS.TABLE_NAME AS 表名,
  USER_CONS_COLUMNS.COLUMN_NAME AS 列名,
  USER_CONS_COLUMNS.POSITION AS 位置
FROM
  USER_CONSTRAINTS
    JOIN USER_CONS_COLUMNS
    ON (USER_CONSTRAINTS.CONSTRAINT_NAME
= USER_CONS_COLUMNS.CONSTRAINT_NAME)
WHERE
  CONSTRAINT_TYPE = 'P';

外键约束
SELECT
  USER_CONS_COLUMNS.CONSTRAINT_NAME AS 约束名,
  USER_CONS_COLUMNS.TABLE_NAME AS 子表名,
  USER_CONS_COLUMNS.COLUMN_NAME AS 子表列名,
  USER_CONS_COLUMNS.POSITION AS 位置,
  USER_INDEXES.TABLE_NAME AS 主表名,
  USER_IND_COLUMNS.COLUMN_NAME AS 主表列名
FROM
  USER_CONSTRAINTS
    JOIN USER_CONS_COLUMNS
    ON (USER_CONSTRAINTS.CONSTRAINT_NAME
        = USER_CONS_COLUMNS.CONSTRAINT_NAME)
    JOIN USER_INDEXES
    ON (USER_CONSTRAINTS.R_CONSTRAINT_NAME
        = USER_INDEXES.INDEX_NAME)
    JOIN USER_IND_COLUMNS
    ON (USER_INDEXES.INDEX_NAME = USER_IND_COLUMNS.INDEX_NAME)
WHERE
  CONSTRAINT_TYPE = 'R';

其他约束
SELECT
  USER_CONS_COLUMNS.CONSTRAINT_NAME AS 约束名,
  USER_CONS_COLUMNS.TABLE_NAME AS 表名,
  USER_CONS_COLUMNS.COLUMN_NAME AS 列名,
  USER_CONS_COLUMNS.POSITION AS 位置,
  CONSTRAINT_TYPE,
  SEARCH_CONDITION
FROM
  USER_CONSTRAINTS
    JOIN USER_CONS_COLUMNS
    ON (USER_CONSTRAINTS.CONSTRAINT_NAME
        = USER_CONS_COLUMNS.CONSTRAINT_NAME)
WHERE
  CONSTRAINT_TYPE IN ('C', 'V', 'O');