Sharpen Your Basic SQL Server Skills - 28 Feb 2008

Learn the distinctions between unique constraint and primary key constraint and the easiest way to get random rows from a table

Q: How does a unique constraint differ from a primary key constraint when enforcing uniqueness on applied columns?
A: A primary key constraint and a unique constraint enforce uniqueness on applied columns in the same way when NULL isn’t involved. A primary key constraint can’t be created on a column that accepts NULL attributes. Many DBAs incorrectly believe that if a column accepts NULL attributes but the column doesn’t contain any NULL attributes, a primary key constraint can be created on that column. Although this is true in other DBMS environments, it isn’t possible in SQL Server. If a SQL Server column is set to accept NULL attributes then you can’t create a primary key constraint on it. The following code tries to create a primary key constraint on a table that accepts NULL attributes.

USE AdventureWorks; GO
CREATE TABLE TestTable
 
  ( FirstID INT NULL,
  SecondID INT NULL )
GO
ALTER TABLE TestTable ADD CONSTRAINT
 
  PK_TestTable PRIMARY KEY CLUSTERED
  ( FirstID, SecondID )
GO  

This code creates a table called TestTable, but the primary key constraint creation fails with the error message that Figure 1 shows.

A unique constraint can be created on a column that accepts NULL attributes. The table and constraint will both be created successfully. The following code creates a table called TestTable, as well as a unique constraint:

USE AdventureWorks; GO
CREATE TABLE TestTable1
  ( FirstID INT NULL,
  SecondID INT
  NULL )
  GO
ALTER TABLE TestTable1 ADD
  CONSTRAINT
  IX_TestTable1 UNIQUE
  ( FirstID, SecondID )
GO

A table can have maximum of one unique, but a maximum of 249 non-unique indexes. After a NULLable column has been altered to the status of a unique index, only a single instance of NULL can be stored in the table as shown by the following example code:

insert into TestTable1 values (NULL, NULL); -- successful insert into TestTable1
values (NULL, NULL); -- throws an
 
  error, violates unique constraint
select * from TestTable1; -- look
  at the table content  

Q: What is the easiest way to retrieve random rows from a table?
A: Several efficient methods exist for retrieving random rows from a database table. One method that’s simple and easy to remember is to use the function NEWID(), as the following code shows.

USE AdventureWorks GO SELECT TOP (10) FirstName, LastName FROM Person.Contact ORDER BY NEWID();

This method works only on SQL Server 2005 and later. The function NEWID() generates a sequencer used in the ORDER BY clause that creates the random order. NEWID() returns a unique value of type uniqueidentifier.

Discuss this Article 4

bobhen
on Mar 12, 2008
It is possible to have more than one unique index in a table! What your are reffriing to is that it's only possible to have one CLUSTERED index but 249 NON-CLUSTERED indexes. And the clustered index doesn't have to be unique. If it is attached to a primary key it will of course be unique. The index attached to the primary key doesn't have to be Clustered but it is as default :)
pinaldave
on Mar 14, 2008
@daarthur, Thank you for your comment and pointing out that it will work on SQL Server 2000. @bobhen, Yes, there is error when I said that. Correct answer is it's only possible to have one CLUSTERED index but 249 NON-CLUSTERED indexes. Clustered index does not have to be unique. Again thanks for pointing out.
PETER (not verified)
on Jul 18, 2008
"A table can have maximum of one unique, but a maximum of 249 non-unique indexes" is not a true statement in SQL server. This statement only applys to clustered vs. non-clustered index and it does not apply to unique vs. non-unique constraints. A table can not more than one unique constraints.
daarthur (not verified)
on Mar 3, 2008
This works in SQL Server 2000 as well.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Michael Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 21, 2013
blog

A Common Misconception about MAXDOP

Out of the box, SQL Server is (and has been) able to take advantage of multiple processors/cores without any effort on behalf of administrators....More
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.