2013-10-05 9 views
5

Abbiamo una tabella chiamata asamembr con due dei suoi campi: cust_code e mbrcode.Query per verificare se la chiave primaria esiste sul tavolo in informix

C'è un altro membermessage tavolo con gli stessi campi come una chiave esterna, ma quando sto usando query seguente per creare il vincolo:

alter table 'informix'.messageclubmembership add constraint foreign key 
      (membership_number, member_code) 
      references 'informix'.asamembr 
      (cust_code, mbr_code) 
      on delete cascade 
      constraint fk_messageclubm926; 

ottengo questo errore:

Cannot find unique constraint or primary key on referenced table (informix.asamembr) 

Puoi informi come interrogare se la chiave primaria esiste sulla tabella asamembr su due campi cust_code e mbr_code?

risposta

6

Primo sguardo per il nome indice per la (colonna pk_idx) PK

select c.constrname, c.constrtype as tp , c.idxname as pk_idx , t2.tabname, c2.idxname 
from sysconstraints c, systables t, outer (sysreferences r, systables t2, sysconstraints c2) 
where t.tabname = "asamembr" 
    and t.tabid = c.tabid 
    and r.constrid = c.constrid 
    and t2.tabid = r.ptabid 
    and c2.constrid = r.constrid 

dove il constrtype:

constrtype CHAR(1) Code identifying the constraint type:
C = Check constraint
N = Not NULL
P = Primary key
R = Referential
T = Table
U = Unique

Poi, controllare le colonne di indice (cercate lo stesso indice nome del vincolo PK):

select unique 
     t.tabname 
     , i.idxname 
     , i.idxtype 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part1) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part2) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part3) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part4) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part5) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part6) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part7) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part8) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part9) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part10) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part11) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part12) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part13) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part14) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part15) 
     , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part16) 
     from sysindexes i , systables t 
     where i.tabid = t.tabid 
     and t.tabname = "asamembr"; 

dove idxtype:

idxtype CHAR(1) Index type:
U = Unique
D = Duplicates allowed
G = Nonbitmap generali
g = Bitmap generalized
u = unique, bitmap
d = nonunique, bitmap

Cerca in Informix online manuals per "sysconstraints" o "sysindexes"

+2

+1: Penso che la prima query potrebbe beneficiare di una condizione 'E c.constrtype IN ('U', 'P')' poiché l'obiettivo è trovare una chiave primaria o un vincolo univoco (specificando la tabella di destinazione come nome della tabella). Se ci sono o chiave primaria o vincoli univoci, potrebbe essere necessario cercare gli indici come mostrato. Essere consapevoli del fatto che i nomi degli indici generati dal sistema hanno degli spazi bianchi. –

+0

Solo una nota ... le query che mostro sopra sono state copiate da alcuni script che ho, quindi mostrano più di cosa hai bisogno, come la tabella di riferimento per alcuni FK sulla tabella filtrata ... e altri vincoli, cosa potrebbe essere filtrato come ha detto Jonathan. – ceinmart

+0

Grazie, ho risolto. – user2809635

2

Per vedere lo schema della tabella per la tabella asamembr è possibile utilizzare dbschema nella riga di comando:

dbschema –d yourdbname –t asamembr 

Vedi here per alcuni esempi

Oppure si può andare a dbaccess yourdbname > Table > Info > asamembr e visualizzare le informazioni tavolo c'è, ma preferisco usare dbschema perché ti mostrerà tutto in un unico posto.

Informazioni sull'errore, la colonna di riferimento (o set di colonne quando si utilizza il formato del vincolo a più colonne, che è il tuo caso) deve avere un vincolo univoco o chiave primaria. E nel tuo caso sembra che non sia il caso. Vedere ulteriori informazioni here