代码之家  ›  专栏  ›  技术社区  ›  David Oneill

Oracle查找约束

  •  143
  • David Oneill  · 技术社区  · 16 年前

    我有一个约束条件叫 users.SYS_C00381400

    3 回复  |  直到 16 年前
        1
  •  299
  •   APC    6 年前
    select * from all_constraints
    where owner = '<NAME>'
    and constraint_name = 'SYS_C00381400'
    /
    

    与所有数据字典视图一样,如果您只想检查当前模式和管理用户的DBA\U约束视图,那么这将是一个用户\U约束视图。

    SQL> create table t23 (id number not null primary key)
      2  /
    
    Table created.
    
    SQL> select constraint_name, constraint_type
      2  from user_constraints
      3  where table_name = 'T23'
      4  /
    
    CONSTRAINT_NAME                C
    ------------------------------ -
    SYS_C00935190                  C
    SYS_C00935191                  P
    
    SQL>
    

    'C' 请查收, 'P' 初选。

    通常,给关系约束一个明确的名称是一个好主意。例如,如果数据库为主键创建一个索引(如果该列还没有索引,它将这样做),它将使用约束名称oo命名索引。您不希望数据库中充满名为 SYS_C00935191 .

    老实说,大多数人并不费心命名非空约束。

        2
  •  28
  •   Thiago    12 年前

    要获得更详细的说明(哪个表/列引用哪个表/列),可以运行以下查询:

    SELECT   uc.constraint_name||CHR(10)
       ||      '('||ucc1.TABLE_NAME||'.'||ucc1.column_name||')' constraint_source
       ,       'REFERENCES'||CHR(10)
       ||      '('||ucc2.TABLE_NAME||'.'||ucc2.column_name||')' references_column
    FROM user_constraints uc ,
      user_cons_columns ucc1 ,
      user_cons_columns ucc2
    WHERE uc.constraint_name = ucc1.constraint_name
    AND uc.r_constraint_name = ucc2.constraint_name
    AND ucc1.POSITION        = ucc2.POSITION -- Correction for multiple column primary keys.
    AND uc.constraint_type   = 'R'
    AND uc.constraint_name   = 'SYS_C00381400'
    ORDER BY ucc1.TABLE_NAME ,
      uc.constraint_name;
    

    从…起 here .

        3
  •  6
  •   Van Gogh    12 年前

    SELECT constraint_name, constraint_type, column_name
    from user_constraints natural join user_cons_columns
    where table_name = "my_table_name";