我有一個簡單的鏈接表,請參閱下面的create sql。
我的問題是,如果我有amtw_u1,是否需要amtw_propnameid_idx和amt_amt_concept_id_idx的單個索引? 我使用此表來加入和過濾amtw_propnameid和amtw_amt_concept_id。我使用唯一索引來強制限制,即沒有兩行可以相同。如果在一個唯一的密鑰中,使用MYSQL是各個字段所需的密鑰?
$sql= "CREATE TABLE `refset_amt_webster` (
amtw_id int unsigned not null AUTO_INCREMENT,
amtw_propnameid int null default null comment 'links to the Webster proprameid drugs table',
amtw_amt_concept_id bigint not null comment 'links to the AMT concept id',
amtw_disabled tinyint unsigned not null default 0 comment '1=disabled',
PRIMARY KEY(amtw_id),
KEY amtw_propnameid_idx (amtw_propnameid),
KEY amtw_amt_concept_id_idx (amtw_amt_concept_id),
UNIQUE KEY amtw_u1 (amtw_propnameid,amtw_amt_concept_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT 'AMT to MIMS linking table.';";
大概不會。另外,代理鍵(amtw_id)似乎是多餘的。您的唯一密鑰可以是主密碼 – Strawberry
根據您的'amtw_u1'鍵的順序,我相信'amtw_propnameid_idx'不會錯過,因爲mysql可以使用'amtw_u1'作爲單獨的'amtw_propnameid'的索引。但是,它不能單獨使用'amtw_u1'索引查詢'amtw_amt_concept_id'。請參閱[多列索引上的MySQL文檔](http://dev.mysql.com/doc/refman/5.6/en/multiple-column-indexes.html) –
另外,草莓是正確的,您可以使用組合鍵作爲一個主鍵,然而合理的人(和企業標準)在你是否應該有'自然'主鍵方面有所不同,所以有些人仍然會使用合成的PK。 –