2014-03-31 43 views
1

我有一個表,主鍵的一部分是另一個表的外鍵。當fk列是pk的子集時,主鍵索引是否會用作外鍵的索引?

create table player_result (
    event_id  integer not null, 
    pub_time  timestamp not null, 
    name_key  varchar(128) not null, 
    email_address varchar(128), 
    withdrawn  boolean not null, 
    place   integer, 
    realized_values hstore, 
    primary key (event_id, pub_time, name_key), 
    foreign key (email_address) references email(address), 
    foreign key (event_id, pub_time) references event_publish(event_id, pub_time)); 

將主鍵足以回到event_idpub_time外鍵生成的索引?

回答

2

是的。

指數A,B,C

是良好的:

A
A,B
A,B,C(和完整的3場的任何其他組合,如果默認順序並不重要)

但不適用於其他組合(如B,CC,A等)。

2

這將是參考側有用,使得在引用DELETEUPDATE可用於參考的行或跑步級聯的存在進行檢查時所使用的引用側的PRIMARY KEY作爲指標更新/刪除。 PostgreSQL不需要這個索引就可以存在,它只是使外鍵約束檢查更快。

它是而不是足以作爲對這些列的參考的唯一約束。您無法創建FOREIGN KEYREFERENCES player_result(event_id, pub_time),因爲這些列上沒有unique約束。只要每一對具有不同的name_key,該對可以在表格中出現多次。

正如@xagyg準確地指出的那樣,由外鍵引用創建的唯一B樹索引也僅對索引左側的列的引用有用。例如,它不能用於查找pub_time, name_key或僅用於name_key