473,626 Members | 3,191 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 2130
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
1405
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 setup as: "id INTEGER(11) PRIMARY KEY NOT NULL AUTO_INCREMENT," + "activities_id INTEGER(11)," + "sombody VARCHAR(60),"+ "something VARCHAR(255),"+ and I have a record I want to INSERT to this table, but only if, say, there
5
4027
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 item. Is there a simpler and efficient way of doing it (instead of traversing linearly through the whole list and comparing strings everytime I want to add new item). Thanks in advance, Abhishek
3
5735
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. problem...
1
2095
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 key constraint in DDL? I tried creating a foreign key constraint, and then making a unique index on the field in the foreign table, and vice versa, and neither has worked. My schema diff utility tells me there is still a one to many relationship at...
6
3002
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 are unique in this aspect, yet when I do the import I get this "ERROR: duplicate key violates unique constraint "werknemer_bedrijf_key". I checked the sourcetable a number of times, even COPYd the relevant columns to a textfile and did `uniq...
1
1698
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 internet delay or some other reasons, it has a chance to store the duplicated data into the database. so i realized it has to be done on the back-end side. for example, if i have three columns (office code, office id, office section) as a...
2
4026
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 wrong or give me other solution. Thanks Very much. Regards ------------------------------------------------------------------------------------------------------------------ Schema:
1
4357
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 DataSet to my Form manually and also manually populated it
1
1604
by: mageshwaran | last post by:
Difference between Primary and Unique With Notnull constraint. Please explain with example?
4
2055
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
8268
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2628
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 we have to send another system
2
1512
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.