?
Solved

Alter SQL Server table constraints

Posted on 2014-08-13
4
Medium Priority
?
125 Views
Last Modified: 2014-09-06
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

0
Comment
Question by:Dale Fye
  • 2
  • 2
4 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40258995
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
 
LVL 49

Author Comment

by:Dale Fye
ID: 40259084
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
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 2000 total points
ID: 40259119
>> 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
 
LVL 49

Author Closing Comment

by:Dale Fye
ID: 40307948
Thanks, Scott.

Finally getting back to this after focusing on another hot issue in the office for a week or so.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question