it-source

열에 고유한 제약 조건이 있는지 검색

criticalcode 2023. 9. 13. 22:41
반응형

열에 고유한 제약 조건이 있는지 검색

가상 시나리오에서 나는 테이블 생성 권한을 가진 사용자입니다.테이블의 열에 고유 제약 조건이 있는지 알고 싶습니다.사전에서 찾아 볼 수 있습니까?제가 어떻게 해야 할까요?

여기에 주어진 두 답변 모두 열에 고유한 제약 조건을 정의하지 않고 고유한 인덱스를 만들어 열에 고유성을 적용하는 한 가지 방법이 없습니다.이 옵션에 익숙하지 않은 경우 이 두 링크(1, 2)를 참조하십시오.

이 검사는 고유 제약 조건 검사에 추가로 수행해야 합니다.

select count(*) from
USER_IND_COLUMNS cols
where cols.table_name='YOUR_TABLE_NAME'
and cols.COLUMN_NAME='YOUR_COLUMN';

고유한 제약 조건을 확인하려면 이미 제공된 방법을 사용합니다.

select count(*) cnt 
from user_constraints uc
where uc.table_name='YOUR_TABLE_NAME'
and uc.constraint_type='U';

다른 방법으로 당신은 또한ALL_CONSTRAINTS그리고.ALL_IND_COLUMNS견해

고유한 제약 조건의 경우 다음과 같은 작업을 수행할 수 있습니다.

select cons.constraint_type, 
       all_cols.owner, all_cols.constraint_name, 
       all_cols.table_name, 
       all_cols.column_name, 
       all_cols.position
  from all_cons_columns col
       inner join all_cons_columns all_cols
               on col.owner = all_cols.owner
              and col.constraint_name = all_cols.constraint_name
       inner join all_constraints cons
               on col.owner = cons.owner
              and col.constraint_name = cons.constraint_name
 where col.owner = 'SCHEMA'
   and col.table_name = 'FOO'
   and col.column_name = 'ID'
   and cons.constraint_type in ('U', 'P')
 order by owner, constraint_name, position;

소유자, 테이블 및 관심 열을 설정하면 해당 열을 포함하는 모든 제약 조건이 표시됩니다.

열에 고유 인덱스가 있는 경우(제약 조건이 없는 고유 인덱스를 사용할 수 있음)가 모두 표시되지는 않습니다.

예:

SQL> create table foo(id number, id2 number, constraint foo_con unique(id, id2), constraint foo_con2 unique(id));

Table created.

이제 포함하는 모든 제약 조건을 나열합니다.id:

SQL> col column_name format a20
SQL> col constraint_name format a20
SQL> col table_name format a15
SQL> select cons.constraint_type,
  2         all_cols.owner, all_cols.constraint_name,
  3         all_cols.table_name,
  4         all_cols.column_name,
  5         all_cols.position
  6    from all_cons_columns col
  7         inner join all_cons_columns all_cols
  8                 on col.owner = all_cols.owner
  9                and col.constraint_name = all_cols.constraint_name
 10         inner join all_constraints cons
 11                 on col.owner = cons.owner
 12                and col.constraint_name = cons.constraint_name
 13   where col.owner = user
 14     and col.table_name = 'FOO'
 15     and col.column_name = 'ID'
 16     and cons.constraint_type in ('U', 'P')
 17   order by owner, constraint_name, position;

C OWNER                          CONSTRAINT_NAME      TABLE_NAME      COLUMN_NAME            POSITION
- ------------------------------ -------------------- --------------- -------------------- ----------
U DTD_TRADE                      FOO_CON              FOO             ID                            1
U DTD_TRADE                      FOO_CON              FOO             ID2                           2
U DTD_TRADE                      FOO_CON2             FOO             ID                            1
select count(*) cnt 
from user_constraints 
where table_name=your_table_name 
and constraint_type='U';

카운트가 = 0인 경우, 존재하지 않습니다.UNIQUE그 외의 제약이 있습니다.UNIQUE테이블에 제약을 가합니다.

여기 제가 방금 시도한 질문이 있습니다.각 고유성 제약 조건은 이 제약 조건을 적용하는 인덱스와 고유한 열로 식별됩니다.

select x.index_name, c.column_name, c.column_position
from USER_INDEXES x join USER_IND_COLUMNS c
     on x.index_name = c.index_name and x.table_name = c.table_name
     left join USER_CONSTRAINTS uc
     on x.index_name = uc.index_name and x.table_name = uc.table_name
where x.status = 'VALID' and
      (x.uniqueness = 'UNIQUE' or
       uc.constraint_type = 'U' and uc.status = 'ENABLED' and uc.validated = 'VALIDATED')
      and x.table_name='<your table name_in_caps>'
order by x.index_name, c.column_position;

기본 키, 고유 인덱스 및 추가된 고유성 제약 조건에 적합한 것으로 보입니다.

언급URL : https://stackoverflow.com/questions/15520994/find-if-a-column-has-unique-constraint

반응형