数据库存储多种类型的数据,但需要全局唯一的id
不久前,我问了如何实现 REST api。我已经在这方面取得了进展,但我正在尝试让我的大脑适应另一个想法。
在我的 api 中,我将拥有多种类型的数据,例如人物、事件、新闻等。
现在,使用 REST,一切都应该有一个唯一的 id。我认为这个 ID 对于整个系统来说应该是唯一的,而不仅仅是每种类型的数据。
例如,不应该有 id 为 #1 的人和 id 为 #1 的新闻。最终,这两件事将被赋予不同的 id:人 #1 具有唯一 id #1,新闻项目 #1 具有唯一 id #2,因为 #1 是由一个人拍摄的。
在数据库中,我知道您可以创建自动递增的主键。问题是,通常你对每个数据“类型”都有一个表,如果你单独为每个表设置自动增量,你将得到“重复”的id(是的,id在它们自己的表中仍然是唯一的,但不是整个数据库)。
有没有简单的方法可以做到这一点?例如,是否可以将所有这些表设置为在一个增量器上工作(这是我能想到如何放置它的唯一方法),或者是否需要创建一个保存这些全局 ID 的表,并将它们绑定到一个表和该表中的唯一ID?
a while ago, i asked about how to implement a REST api. i have since made headway with that, but am trying to fit my brain around another idea.
in my api, i will have multiple types of data, such as people, events, news, etc.
now, with REST, everything should have a unique id. this id, i take it, should be unique to the whole system, and not just each type of data.
for instance, there should not be a person with id #1 and a news item with id of #1. ultimately, these two things would be given different ids altogether: person #1 with unique id of #1 and news item #1 with unique id #2, since #1 was taken by a person.
in a database, i know that you can create primary keys that automatically increment. the problem is, usually you have a table for each data "type", and if you set the auto increment for each table individually, you will get "duplicate" ids (yes, the ids are still unique in their own table, but not the whole DB).
is there an easy way to do this? for instance, can all of these tables be set to work off of one incrementer (the only way i could think of how to put it), or would it require creating a table that holds these global ids, and ties them to a table and the unique id in that table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这根本不是真的。是的,每个资源都需要有一个唯一的标识符,但是在 HTTP 系统中,这意味着一个唯一的 URI。
/people/1
和/news/1
是唯一的 URI。限制系统使得/news/1
必须改为没有任何好处(事实上,正如您发现的那样,会带来很多痛苦) /news/0983240-2309843-234802/
以避免冲突。That's simply not true. Every resource needs to have a unique identifier, yes, but in an HTTP system, for example, that means a unique URI.
/people/1
and/news/1
are unique URI's. There is no benefit (and in fact quite a lot of pain, as you are discovering) from constraining the system such that/news/1
has to instead be/news/0983240-2309843-234802/
in order to avoid conflict.您可以使用 GUID,它们在任何地方都是唯一的(无论如何,出于所有意图和目的)。
http://en.wikipedia.org/wiki/Globally_unique_identifier
You could use a GUID, they will be unique everywhere (for all intents and purposes anyway).
http://en.wikipedia.org/wiki/Globally_unique_identifier
UUID 标准 的特定 Microsoft 实现)
UUID +1(请注意,GUID是 内置函数 uuid() 用于生成文本形式的 UUID。您可能会在它前面加上表名,以便以后可以轻松识别它。
每次调用
uuid()
都会生成一个全新的值(作为文本)。因此,使用上述前缀方法,INSERT
查询可能如下所示:并且不要忘记使该列 varchar 的大小足够大,当然还要为其创建一个索引。
+1 for UUIDs (note that GUID is a particular Microsoft implementation of a UUID standard)
There is a built-in function uuid() for generating UUID as text. You may probably prefix it with table name so that you may easily recognize it later.
Each call to
uuid()
will generate you a fresh new value (as text). So with the above method of prefixing, theINSERT
query may look like this:And don't forget to make this column varchar of large enough size and of course create an index for it.