tags:

views:

155

answers:

6

In MS SQL is it possible to share an identity seed across tables? For example I may have 2 tables:

Table: PeopleA

  • id
  • name

Table: PeopleB

  • id
  • name

I'd like for PeopleA.id and PeopleB.id to always have unique values between themselves. I.e. 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.

A: 

No, there is nothing built into SQL Server to do this.

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.

Cade Roux
+3  A: 

No you can't and if you want to do this, your design is almost certainly flawed.

HLGEM
+1 ROFL! I guess someday everyone reaches their patience breaking point point with questions like this! ;-)
Cade Roux
@Cade Roux: Sometimes there are good reasons
Net Citizen
@Net Citizen Sure, and in those cases, you need to give more information about your requirements and the driving forces behind them.
Cade Roux
@Cade Roux: I disagree, I think I should only give an exact question on what I am looking to do. I should not litter it with a 3 page specification for example. It is not your place to put criticism on someone's question because you assume they are stupid. I have no problem with the tone of @HLGEM's post, just your comment.
Net Citizen
@Net Citizen I am sorry. Basically, this exact question about sharing an IDENTITY (with limited design context) has been asked here several times already (once just last week): http://stackoverflow.com/questions/2428262/can-i-make-an-identity-field-span-multiple-tables-in-sql-server http://stackoverflow.com/questions/294933/generate-unique-id-to-share-with-multiple-tables-sql-2008
Cade Roux
+3  A: 

No, but I guess you could create an IDENTITY(1, 2) on the one table and an IDENTITY(2, 2) on the other. 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? 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.

Mark Byers
I guess when you have a design with tables like PeopleA and PeopleB that need to share a unique identity column, that doing this even-odd identity thing won't fudge up the design any more than it already is
KM
+1  A: 

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). 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.

Kane
+1  A: 

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. Insert into the PeopleMaster and then into PeopleA or 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)

KM
+1  A: 

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, e.g.:

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.

RedFilter