3

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.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
Net Citizen
  • 5,174
  • 8
  • 38
  • 50
  • 2
    I don't know if what you ask is possible, but it sounds like this would be better served as a single table and a type code to distinguish between people who are a or b... – OMG Ponies Mar 25 '10 at 20:43
  • 1
    I have to agree with OMG. If the tables have an identical structure then they really should just be one table. Read about normalizing your database. – Mark Byers Mar 25 '10 at 20:50
  • The reason is for efficient dropping of a whole table. My actual scenario is not people names. – Net Citizen Mar 25 '10 at 21:37

6 Answers6

4

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. However, at this point in time SQL Server now has Sequences that can do what the OP wants to do. 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. 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.

HLGEM
  • 94,695
  • 15
  • 113
  • 186
  • 1
    +1 ROFL! I guess someday everyone reaches their patience breaking point point with questions like this! ;-) – Cade Roux Mar 25 '10 at 20:52
  • @Net Citizen Sure, and in those cases, you need to give more information about your requirements and the driving forces behind them. – Cade Roux Mar 25 '10 at 22:15
  • 1
    @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 Mar 25 '10 at 23:41
  • @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 Mar 26 '10 at 00:09
3

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
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 1
    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. Mar 26 '10 at 12:32
3

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.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
2

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.
  • 101,727
  • 34
  • 178
  • 212
1

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
  • 16,471
  • 11
  • 61
  • 86
0

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
  • 88,164
  • 40
  • 182
  • 265