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
 General SQL Server Forums
 New to SQL Server Programming
 Avoiding duplicate data

Author  Topic 

SIRHACK
Starting Member

5 Posts

Posted - 2011-02-05 : 21:15:20
Hi,

I have a real beginners question.

I have a table that has following Definition:
ForeID int Don't allow null
P1 nvarchar(30) allow null
P2 nvarchar(30) allow null
P3 nvarchar(30) allow null
P4 nvarchar(30) allow null

ForeID is the Primary Key

The values stored into P1, P2, P3 and P4 should all be unique within the table. Except for "NULL" which is valid all the time in any position.

For example:
ForeID P1 P2 P3 P4
1 Bill Carlos Jim James
2 NULL Juan Ted NULL
3 Jim William Mary Doug
4 Dave Pete Sue Clem
5 Teddy Julio NULL Hank
6 NULL NULL NULL NULL

Would be a valid table. However,

ForeID P1 P2 P3 P4
1 Bill Carlos Jim James
2 Mike Bill Ted Howard
3 Jim William Mary Doug
4 Dave Pete Ted Clem
5 Teddy Julio NULL Hank
6 NULL NULL NULL NULL


Would not be valid since "Ted" appears twice as well as "Bill"

When performing an INSERT or UPDATE using the below SQL statements I can obviously get duplicates in any row or column or both.

UpdateCommand="UPDATE Fours$ SET [P1] = @P1, [P2] = @P2, [P3] = @P3, [P4] = @P4 WHERE ( [FourID] = @FourID) "
InsertCommand="INSERT INTO Fours$(P1, P2) VALUES (@P1, @P2)"

Is there a way to prevent duplicates from ever getting into the data. This is just in development so no dups exit as yet and I would like to prevent them rather than having to clean out the data once created.

Thanks.



dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-05 : 21:48:21
To avoid duplicates you need CONSTRAINT

All Primary Keys are unique constraints. The constraint only applies to the columns specified.

In your example, Bill would not violate any constraint, or present a duplicate because it occurs in two different columns. Ted by itself is seen twice, but the ROWS of data are entirely unique (but they do not have to be).

quote:

The values stored into P1, P2, P3 and P4 should all be unique within the table. Except for "NULL" which is valid all the time in any position.



Those two statements are not true together as you would only be allowed 1 NULL per COLUMN if a unique constraint were applied on the column. (TWO nulls in one column would violate any unique constraint on that column.

In your example, the only enforced constraint is on the COLUMN ForeID, not each ROW where that primary key exists. By definition a primary key is what is used to identify the uniqueness of a row.

If you want to enforce unique constraints on the other columns, you could do this by way of a UNIQUE NONCLUSTERED INDEX on each column individually, or the composite of all four. I am not saying you actually would do that in practice however.





Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

SIRHACK
Starting Member

5 Posts

Posted - 2011-02-05 : 22:40:09
Using UNIQUE NONCLUSTED INDEX on any particular column (i.e., P1, P2, P3, P4) now disallows NULL to appear in multiple rows. Is there any way around this and still have any non-NULL entry be contrained?
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-06 : 07:17:21
No. You can't have a unique index that allows duplicates. You can't have a unique constraint that does that. You would have to specify some kind of default constraint on the column that would always be unique, like the a string of PK + 'missing' or something.



Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2011-02-06 : 12:17:05
if you changed the way you stored the data this would be easier. Instead of putting names in 4 different columns, put it in ONE column, and have a unique constraint on that. something like this:

create table foo
(
ForeID int not null
,Sequence int not null -- this tells you whether the name is 1st, 2nd, 3rd or 4th in your original table
,FirstName varchar(30) not null unique
,constraint PK_FOO primary key clustered (ForeID,Sequence)
)


elsasoft.org
Go to Top of Page
   

- Advertisement -