Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Unique values in field

Author  Topic 

skiabox
Posting Yak Master

169 Posts

Posted - 2008-01-03 : 06:58:53
Is there any way I can check if the values of a field are unique(so the field is a primary key candidate).
Thank you for your time.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-03 : 07:03:16
SELECT COUNT(Field) -Count(Distinct Field) FROM Table

This will be zero if its values are all unique.
Go to Top of Page

skiabox
Posting Yak Master

169 Posts

Posted - 2008-01-03 : 07:26:07
Thnx a lot!Is there a shortcut to find which fields are primary key candidates or should I search one by one with the above query?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-03 : 07:36:28
You need to do this for each field
Go to Top of Page

skiabox
Posting Yak Master

169 Posts

Posted - 2008-01-03 : 07:40:27
Ok!thnx a lot for helping me!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-03 : 07:47:21
quote:
Originally posted by skiabox

Thnx a lot!Is there a shortcut to find which fields are primary key candidates or should I search one by one with the above query?


select * from information_schema.table_constraints
where table_name='your table' and constraint_type='primary key'


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

skiabox
Posting Yak Master

169 Posts

Posted - 2008-01-05 : 17:29:13
madhivanan : Can you give a specific example?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-07 : 00:59:00
quote:
Originally posted by skiabox

madhivanan : Can you give a specific example?


What did you understand from the query?
Replace your table by actual table used

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -