Hi everyone,

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

Thanks,

Nick

Recommended Answers

All 2 Replies

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

That makes sense. Thanks for the input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.