Solved

Need a Unique Constraint Example To Prevent Duplicates

Posted on 2009-04-14
14
464 Views
Last Modified: 2012-05-06
I have a table and I need to prevent duplicate rows on insert and update. The stipulations are as follows:
*The field DeptID can never contain a duplicate.
*Other fields such as contact or requestor may/will have duplicate data
*Need to avoid duplicate rows due to hitting submit twice or anything else a user might do.

Thanks in advance.
SQL-Table.png
0
Comment
Question by:JB4375
  • 7
  • 5
  • 2
14 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
Comment Utility
alter table yourtablename
add constraint u_deptid unique (deptid)
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
If DeptID Will / Should not contain Duplicates then you can have that as the primary key of the table right.
What's the use in having another column Sequence as a Primary key.
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
Well, one thing is that it is a bad idea to have your clustered index on a column that is an nvarchar(50).

BTW...you really should consider designing your data types a little more carefully
0
 
LVL 1

Author Comment

by:JB4375
Comment Utility
@rrjegan17: DeptID will never have a duplicate, where as other fields may. Sequence is the primary key because I have updating turned on. I needed to have all fields editable in the event that someone made a mistake entering a Dept ID.
@Chapmandew: Not really sure about the "Clustered Index" or "Data Types" comment. I'm new to the SQL game. All of the data goes in as text, and I wanted to be sure I had enough room for anything that was entered.
I'm getting "Error Validating Constraint... Do you want to edit?
 
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
<< "Error Validating Constraint... >>

That means you have data existing in that table and it violated the Unique constraint which you are trying to create.

Remove the duplicates in the existing data and try recreating it again.
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
did you enter the text in a new query window and run it exactly like I gave you?
0
 
LVL 1

Author Comment

by:JB4375
Comment Utility
I went to Design Table, Manage Constraints, Check Contraints Tab, New.
From there the Constraint Name is generated and there's a blank window for Constraint Expression.
I copied what you gave me in the window, and when I attempt to close it, that's when I get the error.
 
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
yeah, don't do that.  Just post what I gave you in a new query window, swap in your tables name, run it and you're done.
0
 
LVL 1

Author Comment

by:JB4375
Comment Utility
Ok... now I'm getting The Query Designer does not support the ALTER TABLE SQL construct.
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
i didn't say the query designer.  go to file, new, query.  paste the code I gave you in there.
0
 
LVL 1

Author Comment

by:JB4375
Comment Utility
I'm in SQL Server Entrprise Manager. When I select file, my choices are options --> disk cleanup, and exit. Any Query option I select takes me to Query Designer.
Sorry to be so clueless.
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
Ahh...thought you were in management studio.  No worries.  Open up query analyzer instead and give it a shot.
0
 
LVL 1

Author Comment

by:JB4375
Comment Utility
My bad.... We have both versions.... switched over to Management Studio, and it worked like a charm.
Thanks for your assistance, and infinite patience.
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
very welcome.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now