Oracle
 sql >> Database >  >> RDS >> Oracle

Come vedere i vincoli di una tabella, ad esempio se è una chiave primaria o una chiave univoca?

Puoi unire le due viste, USER_CONSTRAINTS e USER_CONS_COLUMNS .

SELECT a.*,
  b.constraint_type
FROM user_cons_columns A,
  user_constraints b
WHERE A.owner        =b.owner
AND A.constraint_name=b.constraint_name
AND A.table_name     = b.table_name
and a.table_name='CLASS'
/

Ad esempio,

SQL> SELECT a.*,
  2    b.constraint_type
  3  FROM user_cons_columns A,
  4    user_constraints b
  5  WHERE A.owner        =b.owner
  6  AND A.constraint_name=b.constraint_name
  7  AND A.table_name     = b.table_name
  8  and a.table_name='EMP'
  9  /

OWNER      CONSTRAINT_NAME      TABLE_NAME COLUMN_NAME            POSITION C
---------- -------------------- ---------- -------------------- ---------- -
SCOTT      PK_EMP               EMP        EMPNO                         1 P
SCOTT      FK_DEPTNO            EMP        DEPTNO                        1 R

SQL>

In un altro modo, usa DBMS_METADATA.GET_DDL per generare il DDL per la tabella. Avrà informazioni complete sulla tabella -

SQL> set long 200000 pages 0 lines 131
SQL> column txt format a121 word_wrapped
SQL> select dbms_metadata.get_ddl('TABLE', 'EMP') from dual;

  CREATE TABLE "SCOTT"."EMP"
   (    "EMPNO" NUMBER(4,0),
        "ENAME" VARCHAR2(10),
        "JOB" VARCHAR2(9),
        "MGR" NUMBER(4,0),
        "HIREDATE" DATE,
        "SAL" NUMBER(7,2),
        "COMM" NUMBER(7,2),
        "DEPTNO" NUMBER(2,0),
         CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"  ENABLE,
         CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
          REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"


SQL>

Quindi, puoi vedere che il DDL generato ha informazioni complete sulla tabella.