Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Recuperando informações de coluna (chave composta) no SQL


No SQL Server você pode fazer
SELECT K.TABLE_CATALOG, 
       K.TABLE_NAME, 
       K.COLUMN_NAME, 
       K.ORDINAL_POSITION 
FROM   INFORMATION_SCHEMA.KEY_COLUMN_USAGE K 
       INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC 
         ON K.TABLE_CATALOG = TC.TABLE_CATALOG 
            AND K.TABLE_SCHEMA = TC.TABLE_SCHEMA 
            AND K.CONSTRAINT_NAME = TC.CONSTRAINT_NAME 
WHERE  TC.CONSTRAINT_TYPE = 'PRIMARY KEY' 

ou
SELECT Object_name(C.OBJECT_ID) TABLE_NAME, 
       C.NAME, 
       IC.INDEX_COLUMN_ID 
FROM   SYS.KEY_CONSTRAINTS K 
       INNER JOIN SYS.INDEX_COLUMNS IC 
         ON K.PARENT_OBJECT_ID = IC.OBJECT_ID 
            AND K.UNIQUE_INDEX_ID = IC.INDEX_ID 
       INNER JOIN SYS.COLUMNS C 
         ON IC.OBJECT_ID = C.OBJECT_ID 
            AND IC.COLUMN_ID = C.COLUMN_ID 
WHERE  K.TYPE = 'PK' 

EM ORACLE
SELECT K.OWNER, 
       K.TABLE_NAME, 
       K.INDEX_NAME, 
       C.COLUMN_NAME, 
       C.COLUMN_POSITION 
FROM   ALL_CONSTRAINTS K 
       INNER JOIN ALL_IND_COLUMNS C 
         ON K.OWNER = C.INDEX_OWNER 
            AND K.TABLE_NAME = C.TABLE_NAME 
            AND K.INDEX_NAME = C.INDEX_NAME 
WHERE  K.CONSTRAINT_TYPE = 'P'