954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

UNIQUE vs. DISTINCT

Hi everyone,

What is the difference between UNIQUE and DISTINCT? It seems that they do the same thing.

Thanks,

Nick

stupidenator
Junior Poster
192 posts since Mar 2005
Reputation Points: 18
Solved Threads: 4
 

Hi everyone,

What is the difference between UNIQUE and DISTINCT? It seems that they do the same thing.

Thanks,

Nick

UNIQUE is a constraint applied to tables that ensures that all values in the given columns are unique. If you try to insert a existing value the you will get an error (along the lines of 'insert violates unique constraint').

The DISTINCT command is used to return one instance of each value, for example if you have a table of names

select * from names;

forename
--------
Joe
Rory
David
Lee
Joe
Andrew
Andrew
David


select distinct forename from names;

forename
--------
Joe
Rory
David
Lee
Andrew
pty
Posting Pro
530 posts since Oct 2005
Reputation Points: 64
Solved Threads: 39
 

That makes sense. Thanks for the input.

stupidenator
Junior Poster
192 posts since Mar 2005
Reputation Points: 18
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You