How to confirm that primary key is not duplicated

Hello Experts,

I have table like this (ServiceID, ServiceName) that have multiple records duplicated and I want to check if there is one "ServiceID" associated with more  than one "ServiceName" records.

Thanks a lot.
HarreniAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>I want to check if there is one "ServiceID" associated with more  than one "ServiceName" records.

to find out duplicate records, try run a query like:

select ServiceID, ServiceName from yourTable
Group By ServiceID, ServiceName
Having count(ServiceID) > 1

Open in new window


to avoid your ServiceID to be duplicated, you could set the Is Identity property to true.
(images copied from Internet)
setting properties
Vitor MontalvãoMSSQL Senior EngineerCommented:
How to confirm that primary key is not duplicated
Just the fact to be a Primary Key is enough to assure that can't be duplicated. If there are duplicates then it means that a Primary Key doesn't exists.
HarreniAuthor Commented:
Thanks Ryan Chong.
Your query showing me if the primary is duplicated only, But I want to figure out all "ServiceName" that are associated with specific primary key.
I got the data which is not really normalized and found that there are some primary key associated with same service name.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>But I want to figure out all "ServiceName" that are associated with specific primary key.
is that mean the "ServiceName" tend to be unique and you wish to find out all ServiceID that has same "ServiceName" ?
Lord_GarfieldCommented:
select ServiceID, ServiceName from yourTable where serviceID=???

Open in new window


replace ??? with your specific serviceID value you want to check
Vitor MontalvãoMSSQL Senior EngineerCommented:
It's better you post some sample data because you're missunderstanding what's a Primary Key.
HarreniAuthor Commented:
Yes Ryan, you're right.

Here is an example of my data:

Service.PNG
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
emmmmm, you could try:

select a.* from
yourTable a
inner join
(
select ServiceID from yourTable
Group By ServiceID 
Having count(ServiceID) > 1
) b
on a.ServiceID = b.ServiceID
order by a.ServiceID, a.ServiceName

Open in new window

Vitor MontalvãoMSSQL Senior EngineerCommented:
ServiceID isn't your Primary Key (PK) otherwise it wouldn't let you have duplicate ServiceIDs. Or you don't have a PK at all or the PK is [ServiceID + ServiceName].

You can have the Service Names associated to a ServiceID as a list using the following query:
SELECT ServiceID,
	STUFF((SELECT ',' + ServiceName
		FROM YourTableNameHere T2
		WHERE T1.ServiceID= T2.ServiceID
		FOR XML PATH('') 
	), 1, 1, '' ) 
FROM YourTableNameHere T1
GROUP BY ServiceID

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HarreniAuthor Commented:
Ryan,
Thanks for your help, your way is showing what's needed but with duplicated ServiceIDs'.

Vitor,
You give me what I need exactly. Can you just do it using "join" relation instead of XML Path??
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.