Solved

Column Uniqueness

Posted on 2012-03-14
8
538 Views
Last Modified: 2012-03-14
I had a look at the Northwind database and found that I could add a new "Product" row having the same "ProductName". Is this how the table is supposed to work? The only uniqueness is on the identity column, which of cause is the primary key.

One would think that the "ProductName" should be unique, or am I not understanding the roles of these two columns correctly.

As far as I can see though, a mistake can be made if a data entry clerk captures a new row but a duplicate "ProductName". This would probably not be detected since the primary key is the identity column.

Is my reasoning correct, if so, then should I design a database making the "ProductName" column a primary key column?

When is using an identity column as the primary key not okay?
0
Comment
Question by:mitdaniels
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 100 total points
ID: 37722115
You can just add a unique constraint which will do the same thing.

Primary keys create physical indexes (sorted on the hard drive) for searching, and is by far the fastest way to access a table.

If you were to add that to your text column, then any time the data changed it would physically re-sort the info.  

EG:

    ID ONLY                           TEXT ONLY

1  Banana                          1    Banana
2  Orange                          2   Orange

becomes (after update tbl set name = 'Apple' where id = 2)

1  Banana                          2  Apple    
2  Apple                            1  Orange
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 37722142
>> should I design a database making the "ProductName" column a primary key column?

No.  A primary key is typically the fastest way to access a column.  If you're joining the table to another table by primary key, you want that to be as fast as possible.  Joining by a contrived integer (an identity) is faster than joining by data (a varchar).

>> When is using an identity column as the primary key not okay?

Almost always.  The entire point of an identity column is to be the primary key.

That said, there may be special circumstances where there is no need for a true primary key, but there is a need for uniqueness.  In that case, omitting the identity column and placing a unique index on the column should suffice.


Kent
0
 

Author Comment

by:mitdaniels
ID: 37722209
Thanks, how do I add a unique constraint to a column in SQL Server 2008R2?
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 100 total points
ID: 37722221
ALTER TABLE <table>
ADD CONSTRAINT <NAME_OF_CONSTRAINT> UNIQUE (<col 1>,<col 2>)

note: can be just 1 column.
0
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 50 total points
ID: 37722256
Hi Mit,

How many rows do you have in the table?  

The unique constraint is fine if you've got a small number of rows, but if you have a significant amount of data, and/or there are a lot of updates/inserts to the table, you should consider adding a UNIQUE INDEX on that column instead.

Both will ensure uniqueness, but the only way that the DBMS can validate uniqueness is to check the contents of the table.  If an appropriate index is available the DBMS will use it and the validation requires just an index search.  If there is no index available, the constraint will have to read an awful lot of data, which can be a performance killer.

Also, with the UNIQUE INDEX you'll have the added benefit of being able to use the index in your own queries if you need one.


Kent
0
 

Author Closing Comment

by:mitdaniels
ID: 37722309
Thanks, that was really quick and very helpful.
0
 

Author Comment

by:mitdaniels
ID: 37722380
Not that it's a problem, but I see that the Contraint was added to the Indexes folder not the Constraints folder??
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 37722396
Hi Mit,

SQL Server implements a unique contraint by placing a unique index on the column(s).  I must admit that I don't know if the index is general enough to be used by user queries though.


Kent
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
If in a where clause in t-sql 7 49
Estimating my database size 7 48
Question about Common Table Expressions 3 39
Intermittent OleDbConnection Error 20 44
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question