Re: [SQL ] postgresql 複合主鍵無法被外鍵參考?
※ 引述《Bambe (肥兒)》之銘言:
: 我建立一個主資料表,有兩個以上的複合主鍵(用 constraint)
: 然後在其他的資料表裡,想參考主資料表的某一個主鍵
: 而 psql 卻回我說:
: -
: 錯誤: number of referencing and referenced columns for foreign key disagree
: SQL 狀態: 42830
: -
: 英文看不太懂 - -... 所以我去查了狀態碼
: 「42830 非法的外鍵(INVALID FOREIGN KEY) 」
: 這時我把主資料表重建變成一個主鍵
: 就可以被外鍵參考
: 想請問一下為什麼會這樣呢? @@
: 有沒有什麼辦法是可以被外鍵參考呢?謝謝各位大大 :)
create table tb_master(
"Pkey1" int not null ,
"Pkey2" int not null,
"Pkey3" int not null,
constraint pk_pkey primary key ("Pkey1","Pkey2")
);
create table tb_slave(
"Pkey1" int not null,
"Pkey2" int not null,
constraint fk_test_pkey1 foreign key ("Pkey1")
references tb_master ("Pkey1")
)
有試過pkey不constraint直接 primary key ("Pkey1","Pkey2")
還是一樣無法被 slave 資料表參考...
還有 slave 資料表直接在欄位後面接 reference 也不行QQ
還是一樣一直出現42830的錯誤... Orz
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 192.192.70.253
討論串 (同標題文章)