Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is auto incremented primary key (with int datatype) is good for for a table OR our user-defined primary key is best for a table in a database for better management.
Please specify me.
Posted
Comments
Thanks7872 23-Nov-13 2:58am    
Note: ADO.NET has nothing to do with this question.

There is nothing good or bad in this regard. What do you mean by good? That doesn't change the definition of Primary key. Thumb rule is primary key will identify the data in table,thats it. It is totally upto you how you want you primary key to be handled.

If you use column say product_id as auto increment field than database will take care of the rest.

It is better to have a INT primary key (auto increment) for your tables which you use for joins etc. and are optimized by the system for performance.

You will probably need a "user" key also which you will show to your users and will be used for searching at a high level.
 
Share this answer
 
Comments
thatraja 23-Nov-13 3:05am    
Agree 5! For Internal purpose it's good & speedy.
Mehdi Gholam 23-Nov-13 3:17am    
Thanks thatraja!
If you care deleted/missing key values then Identity is not for you. Check my past answer for more details. Re using an auto incremented primary key[^]

Based on above mentioned reason, Identity is not for display purpose. For internal purpose, Identity is good one.

Identity field is just like Index, not suggestible for searching.

User defined keys are useful when you need non-numeric values like EmployeeID. (Ex. EMP0005). So during search, we could use this value to bring particular Employee's details.

So the answer is Depends.
 
Share this answer
 
Comments
Mehdi Gholam 23-Nov-13 3:17am    
5'ed
Start from the basic. What is primary key? It is a key which identify rows uniquely in a table. So we understand that each field of primary key should be unique. It is first condition. To achieve that you can use Identity column or Sequence(sql server 2012). For both type you should declare your field data type int, bigint. You can also use guid as a primary key. If you really need in future your data will go to datawarehouse then consider guid is the first priority. in all that cases database system/application generate unique value for you.
You can also create custom unique value. But the main challenge on that is concurrency handling. So it is better instead of handling concurrency with custom code you can depend on proven system.
So my opinion is you can depend on system for generate primary key.
 
Share this answer
 
Comments
Mohd Imran Saifi 23-Nov-13 5:21am    
thnks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900