Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« SSRS 2005 MatrixesDo not use spaces or other invalid characters in your column names »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

By definition, primary keys must contain unique data. They are implemented in the database through the use of a unique index. If there is not already a clustered index on the table, then the primary key's index will be clustered. It's not always true, but most of the time, you want your primary keys to be clustered because it is usually the key criteria in your requests to the data. This includes join conditions and where clause criteria. Clustered indexes give you exceptional performance because it allows SQL Server to create optimal execution plans.

How to detect this problem:

  1. SELECT AllTables.Name
  2. FROM   (
  3.        SELECT Name, id
  4.        FROM   sysobjects
  5.        WHERE  xtype = 'U'
  6.        ) AS AllTables
  7.        LEFT Join (
  8.          SELECT parent_obj
  9.          FROM   sysobjects
  10.          WHERE  xtype = 'PK'
  11.          ) AS PrimaryKeys
  12.          ON AllTables.id = PrimaryKeys.parent_obj
  13. WHERE  PrimaryKeys.Parent_Obj IS NULL
  14. ORDER BY AllTables.Name

How to correct it: Identify tables without a primary key using the query above. Examine each table and identify what makes each row in the table unique. Modify the table to include a primary key.

Level of severity: Moderate
Level of difficulty: Easy

About the Author

George has been developing software professionally for 19 years, first for the department of defense, and then for various other companies. In 1998, George started his software company, Orbit Software, specializing in School Bus Transportation software. His specialty is refining SQL Server queries to deliver optimal performance.
Social SitingsTwitterLTD RSS Feed
365 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

1 comment

Comment from: SQLDenis [Member] Email
I agree with this....can't remember how many times I had to clean up tables and even worse with child tables (but no pk --> fk) running group by having count(*) > 1

figuring out how to merge the data etc etc

try deleting a row that has no PK with dups from the GUI
11/11/09 @ 19:41

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)