auto_increment不能是一個字段的只是一部分,所以一列可能是一個洗。因此,如何:
CREATE TABLE xxxxx (
id int unsigned not null auto_increment,
rest_of_that_key char(xx) not null,
// other goodies
PRIMARY KEY (id)
);
然後你可以SELECT CONCAT(rest_of_that_key, LPAD(id, 7, '0')) AS full_key FROM xxxxx
。
甚至更好,這樣你就可以使用這些辦公代碼和日期與過濾數據:
CREATE TABLE xxxxx (
id int unsigned not null auto_increment,
country_code char(1) not null,
sysdate date not null,
office_code smallint unsigned not null,
// other goodies
PRIMARY KEY (id),
KEY country_code (country_code)
// etc, index the useful stuff
);
然後你可以使用SELECT CONCAT(country_code, DATE_FORMAT(sysdate, '%Y%m%d'), office_code, LPAD(id, 7, '0')) AS full_key FROM xxxxx
,甚至在WHERE office_code = 1256 AND sysdate >= '2010-01-01'
拋出,而不必以某種方式解析巨大串。
如果你真的需要這個巨大的字符串作爲單字段主鍵,你將手動增加自己的東西。我仍然不會推薦這樣做,MySQL 真的喜歡它的PK是數字。
有人不理解標準化... – DCookie 2010-03-04 16:40:08