多个表中的唯一 ID [MySQL] [英] unique id in more than one table [MySQL]
问题描述
我想在两个 MySQL 表中自动增加 id(如果在第一个表中有 id=4 那么它不能在第二个表中是 id=4).我的问题是我怎样才能以最好的方式做到这一点?
I want to make auto increment id in two MySQL tables (if in first table there is id=4 then it cannot be id=4 in the second table). My question is how can I do that in the best way?
推荐答案
您需要的是一个外部生成的序列并将其链接到您的 2 个表
What you need is an external generated sequence and link it to your 2 tables
你应该看看 flickr 做了什么,看看这个链接:http://code.flickr.com/blog/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/
You should look at what flickr made, look at this link : http://code.flickr.com/blog/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/
您创建生成 id 的表:
You create the table which generate the id :
CREATE TABLE `Tickets64` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`stub` char(1) NOT NULL default '',
PRIMARY KEY (`id`),
UNIQUE KEY `stub` (`stub`)
) ENGINE=MyISAM
并获得您像这样制作的新ID:
and to get the new id you make like this :
REPLACE INTO Tickets64 (stub) VALUES ('a');
SELECT LAST_INSERT_ID();
Twitter 最近也做了一个叫雪花的东西,你应该看看他们的 github 仓库
Twitter also recently made something named snowflake, you should look in their github repository
但主要看flickr做了什么,它更简单,容易处理
But principally look at what flickr made , its simpler and can be handled easily
这篇关于多个表中的唯一 ID [MySQL]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!