Alter SQL Server table constraints

I'm migrating data from several different data sources (Access, Oracle, and SQL Server) with different data structures into a consolidated database.  Part of that process is to uniquely identify entities in the three different databases.  To do that, I've got an "Entities" table, which is hierarchical in nature and contains the following fields (among about a dozen others).

      [Entity_Type_ID] [smallint] NOT NULL,
      [DS_ID] [int] NOT NULL,
      [DS_PK_Integer] [int] NULL,
      [DS_PK_Text] [nvarchar](50) NULL,

Some of the original data sources are using text values as their primary key values, while others are using a long integer, so I believe that this structure will meet my needs.  However, I need to configure this table with a unique constraint on these 4 fields, where [Entity_Type_ID] and [DS_ID] are mandatory, and every record must contain a value in either the [DS_PK_Integer] or the [DS_PK_Text] field (the other field will be NULL).  And I also need to ensure that there are no duplicate records across those 4 fields.

What Alter Table syntax would I need to implement this?  The script to create the table as it currently exists is:

USE [myDatabase]
GO

/****** Object:  Table [dbo].[tbl_sysEntities]    Script Date: 08/13/2014 11:35:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tbl_sysEntities](
	[Entity_ID] [int] IDENTITY(1,1) NOT NULL,
	[Entity_Type_ID] [smallint] NOT NULL,
	[DS_ID] [int] NOT NULL,
	[DS_PK_Integer] [int] NULL,
	[DS_PK_Text] [nvarchar](50) NULL,
	[Entity_Name] [nvarchar](75) NULL,
	[County_ID] [int] NOT NULL,
	[API] [nvarchar](20) NULL,
	[Status_ID] [int] NOT NULL,
	[Eff_Date] [date] NULL,
	[Thru_Date] [date] NULL,
	[Parent_Entity_ID] [int] NULL,
 CONSTRAINT [PK_tbl_Entities] PRIMARY KEY CLUSTERED 
(
	[Entity_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tbl_sysEntities] ADD  CONSTRAINT [DF_tbl_Entities_Status_ID]  DEFAULT ((1)) FOR [Status_ID]
GO

Open in new window

LVL 50
Dale FyeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
I think this would do it:

CHECK(DS_PK_Integer IS NOT NULL OR DS_PK_Text IS NOT NULL)

And then build a standard unique constraint across the other 4 columns:

UNIQUE ( [Entity_Type_ID], [DS_ID], [DS_PK_Integer], [DS_PK_Text] )


You should also really consider clustering on [DS_ID] rather than on an identity column.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeAuthor Commented:
Scott,

1.  Can I add those constraints to the table without making any other changes?
ALTER TABLE tbl_sysEntities
CHECK(DS_PK_Integer IS NOT NULL OR DS_PK_Text IS NOT NULL)
UNIQUE ( [Entity_Type_ID], [DS_ID], [DS_PK_Integer], [DS_PK_Text] )

Open in new window

or is something else needed.

2.  Although I've used SQL Server for a number of years, I have not learned many of the nuances of using it.  DS_ID does not contain a unique value, it contains values (1, 2, and 3 for now) that describe which source was used for the data.  I've used identity columns (autonumber) as my PKs in Access for over 20 years, and although I understand the arguments for and against surrogate keys, what advantages are there to "clustering on [DS_ID]".

Dale
0
Scott PletcherSenior DBACommented:
>> Can I add those constraints to the table without making any other changes? <<

Yes; just be sure to put a comma between each constraint if you do them both at once.
ALTER TABLE tbl_sysEntities
CHECK(...), --<--
UNIQUE ( ... )


>>  what advantages are there to "clustering on [DS_ID]" <<

It depends on the most common lookups / joins to the table.  I would have to see missing index and index usage stats to be more sure of that, since I don't know the data.  You may already know how lookups are (almost) always done.
0
Dale FyeAuthor Commented:
Thanks, Scott.

Finally getting back to this after focusing on another hot issue in the office for a week or so.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.