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?
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 ChongCommented:
>>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
0
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.
0
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ryan ChongCommented:
>>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" ?
0
Lord_GarfieldCommented:
select ServiceID, ServiceName from yourTable where serviceID=???

Open in new window


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

Here is an example of my data:

Service.PNG
0
Ryan ChongCommented:
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

1
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

1

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??
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.