Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 September Best practice for Primary Key in Database Management System

Best practice for Primary Key in Database Management System

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Hello Guys,
The discussion on Primary Key is very common and even not require so much attention because each and every database professionals have excellent knowledge on the Database Primary Key.

I agree that you all are using Primary Key in your database system, but still, I want to write theory about: what should be your best practice with Primary Key.

This is a very familiar concept, but then also sometimes database professionals are making a mistake, and even they don’t know about it.

Let me start this theory,

First, I would like to define the Primary Key.

A Primary key is a unique identifier for each record, but this is the only half story. Another main purpose is to relate records for different tables and playing the leading role as a pointer.

The best data type for Primary Key.

I suggest choosing a numeric data type because storage of numeric type is very compact than any character data type and even most of the primary key is a foreign key for another table, so smaller key performs faster than the other.

Are you updating your primary key?

Primary key should never change or update. The updating primary key might create more problem because it used by multiple indexes and used as a foreign key.

Are you using Natural Key as a Primary Key?

What is Natural Key?
A natural key is a key that formed of attributes that already exist in the real world. For example, a US citizen’s social security number could be used as a natural key.

I suggest to avoid natural key because of one main reason which is human error. The Application or Human has to enter SSN (social security number) into a database, and there might be chance for the wrong insertion, and maybe your system cannot detect this error at the same time, but once you find this error then you will face more problems for changing the data of the primary key.

You can use this, but you should make sure about the correct data and also using of Natural Key can prevent some additional joins.

Are you using Surrogate Key as a Primary Key?

What is Surrogate Key?
A surrogate key in a database is a unique identifier for either an entity in the modelled world or an object in the database. The surrogate key is not derived from application data, unlike a natural key which is derived from application data.

I suggest to use Surrogate Key as Primary Key which is generated by database management system and not derived from any application data. In the background, Your primary key handled by database program and application user.

Are you creating a Primary Key for each and every table?

If you are doing this, then you are doing right. But, If you are not doing this then your database may have many HEAP tables which are not good because primary key also works as cluster index and also responsible for the correct order of the data.
Your best practice is to create an auto_increment column for each and every table.

Now, a task is for you, please check your database primary key and judge yourself.
In above theory whatever I have written is my viewpoint and concern, but you might have a different view so take decision base on your system requirement.

For further suggestions, your comments are most welcome.

Sep 17, 2015Anvesh Patel
Enable and Disable Foreign key and Check constraint in SQL ServerFind duplicate records in MySQL
Comments: 2
  1. Dinesh
    April 7, 2016 at 6:37 pm

    Your posting article really usfull to sql developer/dba

  2. Ratnesh
    June 20, 2016 at 1:29 pm

    This blog is very useful for me. I got lots of benefits like remove duplicate records in mysql.
    Yes Anvesh ! I agree to have a Primary key for each table.

Anvesh Patel
Anvesh Patel

Database Engineer

September 17, 2015 Database DesigningAnvesh Patel, database, database concept, database research and development, database standards, Database Theory, database topic, dbrnd, natural key, Primary key, RDBMS, surrogate key
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....