2013-10-05 7 views
5

Mamy tabelę o nazwie asamembr z dwoma jej polami: cust_code i mbrcode.Zapytanie, aby sprawdzić, czy klucz podstawowy istnieje w tabeli w informix

Jest inna membermessage stół z tych samych pól jako klucz obcy, ale kiedy używam następujące kwerendy, aby utworzyć ograniczenie:

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; 

otrzymuję ten błąd:

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

Czy możesz powiedzieć jak zapytać, czy klucz podstawowy istnieje w tabeli asamembr na dwóch polach cust_code i mbr_code?

Odpowiedz

6

Pierwsze spojrzenie na nazwę indeksu dla (kolumna 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 

gdzie constrtype:

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

Następnie sprawdź kolumny indeksu (poszukaj tego samego indeksu nazwa przymusu 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"; 

gdzie idxtype:

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

Szukaj w Informix online manuals dla "sysconstraints" lub "sysindexes"

+2

+1: Myślę, że pierwszy kwerenda może korzystać z warunku 'I c.constrtype IN ('U', 'P')' ponieważ celem jest znalezienie klucza podstawowego lub ograniczenia unikalnego (określenie tabeli docelowej jako nazwy tabeli). Jeśli istnieje klucz podstawowy lub ograniczenia unikatowe, może być konieczne przejrzenie indeksów w przedstawiony sposób. Należy pamiętać, że nazwy indeksu generowane przez system mają pierwsze znaki puste. –

+0

Po prostu wiadomość ... Zapytania, które pokazałem powyżej, zostały skopiowane z niektórych skryptów, które mam, więc pokazują więcej potrzebnych informacji, np. Tabelę referencyjną dla niektórych plików FK nad tabelą filtrowaną ... i inne ograniczenia, co być filtrowane, tak jak powiedział Jonathan. – ceinmart

+0

Dzięki temu rozwiązałem. – user2809635

2

Aby wyświetlić schematu tabeli dla tabeli asamembr można użyć dbschema w wierszu poleceń:

dbschema –d yourdbname –t asamembr 

Zobacz here dla niektórych przykładów

czy można przejść do dbaccess yourdbname > Table > Info > asamembr i zobaczyć tam informacje o tabeli, ale Wolę używać dbschema, ponieważ pokaże ci wszystko w jednym miejscu.

Informacje o błędzie: kolumna z odniesieniami (lub zestaw kolumn w przypadku używania formatu ograniczenia wielu kolumn, który jest przypadkiem) musi mieć ograniczenie unikatowe lub klucza podstawowego. A w twoim przypadku wygląda na to, że tak nie jest. Zobacz więcej informacji here

Powiązane problemy