自动递增 http://dev.mysql.com/doc/refman/5.1/en/example-auto-increment.html不能只是字段的一部分,因此单个列可能会被清洗。那么怎么样:
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'
无需以某种方式解析那个巨大的字符串。
如果您确实需要那个巨大的字符串作为单字段主键,您将需要自己手动增加内容。不过我仍然不建议这样做,MySQLreally喜欢它的 PK 是数字的。