• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

Need a Unique Constraint Example To Prevent Duplicates

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
JB4375
Asked:
JB4375
  • 7
  • 5
  • 2
1 Solution
 
chapmandewCommented:
alter table yourtablename
add constraint u_deptid unique (deptid)
0
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
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
 
chapmandewCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JB4375Author Commented:
@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
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
<< "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
 
chapmandewCommented:
did you enter the text in a new query window and run it exactly like I gave you?
0
 
JB4375Author Commented:
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
 
chapmandewCommented:
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
 
JB4375Author Commented:
Ok... now I'm getting The Query Designer does not support the ALTER TABLE SQL construct.
0
 
chapmandewCommented:
i didn't say the query designer.  go to file, new, query.  paste the code I gave you in there.
0
 
JB4375Author Commented:
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
 
chapmandewCommented:
Ahh...thought you were in management studio.  No worries.  Open up query analyzer instead and give it a shot.
0
 
JB4375Author Commented:
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
 
chapmandewCommented:
very welcome.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now