Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Confused about table Schema...

Author  Topic 

cidr
Posting Yak Master

207 Posts

Posted - 2011-06-24 : 05:21:38
Hi there,

I've been looking at a schema that's recently came by my way. I've to write reports using two tables.

I know there's a multitude of ways to do things, however, I've never quite seen this technique. So, just wanted to ask advice.

There is a table header and a table detail. Header being parent. The tables relate to performance scores but I think I may get away without going into too much detail for you as it's just a schema query.

OK, the developer has used a composite primary key on the header and chosen two fields. He has chosen two of the same fields on the Detail table for that tables primary key along with one more. There is no foriegn key constraint. Please see the schema below for each. I've exluded a lot of the fields for easy reading.

It's fair enough that he has used a composite primary key on the header, I just don't understand the logic of the same fields in both tables being primary keys. Can someone help me understand this? Am I missing something? The developer is not available for a while for me to discuss this.

Any help would be excellent... thanks

Header::

CREATE TABLE [dbo].[tblSRVHeader](
[intInspectionNbr] [int] NOT NULL,
[intSiteID] [int] NOT NULL,
[strSiteDesc] [varchar](100) COLLATE Latin1_General_CI_AS NULL,
[intProjectMgrID] [int] NULL,
[strProjectMgr] [varchar](100) COLLATE Latin1_General_CI_AS NULL,

CONSTRAINT [PK_tblSRVHeader] PRIMARY KEY CLUSTERED
(
[intInspectionNbr] ASC,
[intSiteID] ASC)
ON [PRIMARY]
) ON [PRIMARY]

Detail::

CREATE TABLE [dbo].[tblSRVDetails](
[intInspectionNbr] [int] NOT NULL,
[intSiteID] [int] NOT NULL,
[intlineID] [int] IDENTITY(1,1) NOT NULL,
[intDivisionID] [int] NULL,
CONSTRAINT [PK_tblSRVDetails] PRIMARY KEY CLUSTERED
(
[intInspectionNbr] ASC,
[intSiteID] ASC,
[intlineID] ASC)
ON [PRIMARY]
) ON [PRIMARY]


nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-24 : 05:29:54
You are ssociating header with detail so the header PK needs to be includied in the PK of the detail.
For the detail could just be the header PK + a sequence number - actually this is almost what you have here.

The PK for the detail needs to be unique and associed with a heade row which implies the above.
The header pk columns need to come firt in the detail PK to allow the retrieval of the details for a header.

The problem with this design is that it also needs a unique index on the identity column. That could be the pk and the current pk made a unique index otherwise I would use a sequence humber within the header entry if possible.

Another option would be an identity surrogate key on the header which is used on the detail instead of the composite - I wouldn't do that and some people are religious about it.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -