Question

If my title hurts your head... I'm with you. I don't want to get into why this table exists except that it is part of a legacy system, also the system does "record level access"(RLA) and this I know will be an issue for many tables, anyways the RLA is mentioned because adding a column will change the table format and then many very old programs will no longer work...

Apparently adding a PK has been shown not to change the table format. So I've been told that a certain set of keys is guarantied to be unique, well what do you know... it isn't. And now I need to show where they aren't.

All I can think of is:

Get the cross product where the table matches on it's primary key.

Somehow get a count column onto the result set for the number of entries where the PK matches it self.

Filter that result set for values where count id greater than 2.

I'm going to see if I expand the PK sufficiently I'll actually find something unique.

Was it helpful?

Solution

Remove the constraints / unique indexes, insert the data, and then run this query:

SELECT col1, col2, ..., coln, COUNT(*)
FROM your_table
GROUP BY col1, col2, ..., coln
HAVING COUNT(*) > 1

where col1, col2, ..., coln is the list of columns in your key (one or more columns). The result will be the list of keys that occur more than once together with a count showing how often they occur.

OTHER TIPS

select col1, ... from tab group by col1, ... having count(*)>1;
SELECT * FROM (SELECT ID, COUNT(*) CNT FROM MY_TABLE GROUP BY ID) WHERE CNT > 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top