patternMinor
Database Schema with a shared identity Field
Viewed 0 times
identityfieldsharedwithdatabaseschema
Problem
I'm a developer treading into the database world so bear with me please.
I'm creating a Contract database with separate tables for various types of contracts.
I have 5 tables but would like to share a common 'Identity' field between them, so that there will be a unique contract # for any given contract regardless of type.
What is the recommended way to do this? Something that won't over complicate simple crud transactions?
My application will be using a Sql Server 2008 R2 server..the app itself is Silverlight 4 using WCF Ria services for CRUD transactions.
A schema idea would be great, a tutorial of something similar ideas and technology would be even better.
Thanks for looking
Robert
I'm creating a Contract database with separate tables for various types of contracts.
I have 5 tables but would like to share a common 'Identity' field between them, so that there will be a unique contract # for any given contract regardless of type.
What is the recommended way to do this? Something that won't over complicate simple crud transactions?
My application will be using a Sql Server 2008 R2 server..the app itself is Silverlight 4 using WCF Ria services for CRUD transactions.
A schema idea would be great, a tutorial of something similar ideas and technology would be even better.
Thanks for looking
Robert
Solution
Until SQL Server supports sequences (next version "Denali") then you'll have to have a common table.
However, if I understand you, I think you're looking at the subtype/supertype pattern. A sequence would be nice but if you designed using, say, Object Role Modelling then you'd generate this pattern/schema
Basically, you have a common "Contract" table:
Notes:
Then you have sub-tables with specific attributes for each contract type
Notes:
However, if I understand you, I think you're looking at the subtype/supertype pattern. A sequence would be nice but if you designed using, say, Object Role Modelling then you'd generate this pattern/schema
Basically, you have a common "Contract" table:
ContractID, PK, UQ1 (Identity)
ContractType, FK, UQ1
InsertedDateTime
UpdatedDateTime
CounterPartyNotes:
- the PK is
ContractIDto give a unique value
ContractTypedefines your "various types". Let's go for Tom, Dick and Harry as types
- The unique key UQ1 is on both
ContractID, ContractType(this is a "super key")
InsertedDateTime,CounterPartyetc are common attributes
Then you have sub-tables with specific attributes for each contract type
- ContractTom:
ContractID, ContractType, TomAttrib1, TomAttrib2, ..., TomAttribn
- ContractDick:
ContractID, ContractType, DickAttrib1, DickAttrib2
- ContractHarry:
ContractID, ContractType, HarryAttrib1, HarryAttrib2, HarryAttrib3
Notes:
ContractID, ContractTypeare both the PK and FK.
ContractTypehas CHECK constraint to restrict to Tom or Dick or Harry
Code Snippets
ContractID, PK, UQ1 (Identity)
ContractType, FK, UQ1
InsertedDateTime
UpdatedDateTime
CounterPartyContext
StackExchange Database Administrators Q#4105, answer score: 6
Revisions (0)
No revisions yet.