473,500 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check for MultiColumn Unique before Adding Unique Constraint

dlite922
1,584 Recognized Expert Top Contributor
I'm writing two scripts to upgrade a database to make it more robust (basically adding lots of constraints). The first script is the "validator" that you run first to see if you can upgrade or not. The second script does the Alter Table commands.

i.e. I need a way to check uniqueness of two columns before adding the unique constraint on them.

I think this is common, but I'm not googling the right terms guess.

Any help greatly appreciated, guys!






Dan
May 14 '10 #1
1 2120
dlite922
1,584 Recognized Expert Top Contributor
Here's how I solved it:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT count FROM (SELECT COUNT(*) as count FROM mytable GROUP BY col1, col2, col3) WHERE count > 1;
  3.  
  4.  
This says give me a count of all rows that have all three columns the same (two or more rows with identical values for those three columns).

If there are duplicate (non-unique) rows, this count would be greater than 1, hence why I put it in a wrapper function and asked if there are any count greater than 1.

If there are any, this query should give me duplicates, else return empty if there are none.





Dan
May 14 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1396
by: Ike | last post by:
How can I do an update to a table where I dont want to allow records to have a field(s) which occur in another record, whenm each record has a unique integer id. For example, suppose I have a table...
5
4015
by: Abhishek Pandey | last post by:
Hi, I have a vector of strings. I want to hold it only unique strings. That is, before adding any new string, I want to check if it is already present, and if yes, then I will not add the new...
3
5729
by: Alex | last post by:
Acc 97 Hi, I am in need of some help here.... I have a query which is based upon time, where i need to plot the hours a job has been working.. therefore, oN & off in a union query. ...
1
2085
by: Jonathan Scott via AccessMonster.com | last post by:
I have an application who's backend has a relationship defined one to one. I need to update the LIVE version of the database to reflect this for the new version. How can I express such a foreign...
6
2992
by: Joolz | last post by:
Hi everyone, When importing a bunch of data (> 85000 rows) I get an error I can't explain. The table into which I'm importing has a unique clause on (code, bedrijf). The rows in the source-table...
1
1691
by: HandersonVA | last post by:
Would anyone please instruct how to prevent the duplicate record by setting the unique keys on the ms sql server? i've been checking the duplicate record as front-end and i found out if there is an...
2
4011
by: pstachy | last post by:
Hi again! I have another issue. I would like the attribute of the tag <invoice> to be unique. Made the following schema but unfortunately it doesn't validate. Could someone please indicate what is...
1
4345
by: =?Utf-8?B?VGFz?= | last post by:
Hi, first of all, I'm using: Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) Microsoft .NET Framework Version 2.0.50727 Installed Edition: C# Express. I have added a...
1
1593
by: mageshwaran | last post by:
Difference between Primary and Unique With Notnull constraint. Please explain with example?
4
2048
by: sanQUEST | last post by:
hi, can anybody can tell me how to add pry constraint to a table if it has already duplicated records ? san
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.