简体   繁体   English

MS SQL在表中共享标识种子

[英]MS SQL share identity seed amongst tables

In MS SQL is it possible to share an identity seed across tables? 在MS SQL中,是否可以跨表共享身份种子? For example I may have 2 tables: 例如,我可能有2个表:

Table: PeopleA 表:PeopleA

  • id ID
  • name 名称

Table: PeopleB 表:PeopleB

  • id ID
  • name 名称

I'd like for PeopleA.id and PeopleB.id to always have unique values between themselves. 我希望PeopleA.idPeopleB.id之间始终拥有独特的价值观。 Ie I want them to share the same Identity seed. 即我希望他们共享相同的身份种子。

Note : I do not want to hear about table partitioning please, only about if it's possible to share a seed across tables. 注意 :我不想听到表分区,只是关于是否可以在表之间共享种子。

Original answer 原始答案

No you can't and if you want to do this, your design is almost certainly flawed. 不,你不能,如果你想这样做,你的设计几乎肯定是有缺陷的。

When I wrote this in 2010 that was true. 当我在2010年写这篇文章时,这是真的。 However, at this point in time SQL Server now has Sequences that can do what the OP wants to do. 但是,此时SQL Server现在具有可以执行OP想要执行的操作的序列。 While this may not help the OP (who surely has long since solved his problem), it may help some one else looking to do the same thing. 虽然这可能无助于OP(他肯定早已解决了他的问题),但它可能有助于其他人寻求做同样的事情。 I do still think that wanting to do this is usually a sign of a design flaw but it is possible out of the box now. 我仍然认为想要这样做通常是设计缺陷的标志,但现在可以开箱即用。

No, but I guess you could create an IDENTITY(1, 2) on the one table and an IDENTITY(2, 2) on the other. 不,但我猜你可以在一张桌子上创建一个IDENTITY(1,2),在另一张桌子上创建一个IDENTITY(2,2)。 It's not a very robust design though. 但它并不是一个非常强大的设计。

Could you instead refer to your entities as 'A1', 'A2', ... if they come from TableA and 'B1', 'B2', etc... if they come from TableB? 您是否可以将您的实体称为'A1','A2',......如果它们来自TableA和'B1','B2'等......如果它们来自TableB? Then it's impossible to get duplicates. 然后就不可能得到重复。 Obviously you don't actually need to store the A and the B in the database as it is implied. 显然,您实际上并不需要将A和B存储在数据库中,因为它是隐含的。

Not sure what your design is, but sometimes it is useful to use an inheritance-type model, where you have a base table and then sub-tables with substantially different attributes, eg: 不确定您的设计是什么,但有时使用继承类型模型是有用的,其中您有一个基表,然后是具有显着不同属性的子表,例如:

Person
------
PersonID <-- PK, autoincrement
FirstName
LastName
Address1
...

Employee
--------
PersonID <-- PK (not autoincrement), FK to Person
JobRoleID
StartDate
Photo
...

Associate
---------
PersonID <-- PK (not autoincrement), FK to Person
AssociateBranchID
EngagementTypeID
...

In this case you would insert the base values to Person, and then use the resulting PersonID to insert into either Employee or Associate table. 在这种情况下,您将基值插入Person,然后使用生成的PersonID插入Employee或Associate表。

If you really need this, create a third table PeopleMaster, where the identity(1,1) exists, make the two other tables just have int FKs to this identity value. 如果你真的需要这个,创建第​​三个表PeopleMaster,其中存在标识(1,1),使另外两个表只有int FKs到这个标识值。 Insert into the PeopleMaster and then into PeopleA or PeopleB. 插入PeopleMaster,然后插入PeopleA或PeopleB。

I would really consider this a bad design though. 我真的认为这是一个糟糕的设计。 Create one table with a PeopleType flag ("A" or "B") and include all common columns, and create child tables if necessary (for any different columns between the PeopleA and PeopleB) 使用PeopleType标志(“A”或“B”)创建一个表并包含所有公共列,并在必要时创建子表(对于PeopleA和PeopleB之间的任何不同列)

No. 没有。

But I have worked on projects where a similar concept was used. 但我曾参与过使用类似概念的项目。 In my case what we did was have a table called [MasterIdentity] which had one column [Id] (an identity seed). 在我的情况下,我们所做的是有一个名为[MasterIdentity]的表,它有一列[Id] (身份种子)。 No other table in the database had any columns with an identity seed and when Identities were required a function/stored proc was called to insert a value into the [MasterIdentity] table and return the seed. 数据库中没有其他表具有任何具有标识种子的列,并且当需要标识时,调用函数/存储过程以将值插入[MasterIdentity]表并返回种子。

No, there is nothing built into SQL Server to do this. 不,SQL Server没有内置任何功能。

Obviously there are workarounds such as both using an FK relationship to a table which does have a single IDENTITY and having some fancy constraints or triggers. 显然有一些解决方法,例如使用FK关系到一个表,它具有单个IDENTITY并具有一些奇特的约束或触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM