Auto-generated primary key Versus user defined primary key?
-
i am designing a database using sql server 2008 r2. In around 90% of the tables i have added an identifier column (i set the Identity specification to Yes) that will contain an auto-generated number that can uniquely indentify the table rows. But in some tables i have a unique value that the users can enter which can also uniquely indentify the table rows without that need to have an auto-generated ID column. for example i have a table named Document_Status which have a Description column that include values such as (new, draft, published, canceled,etc); now in the Document_status table no two rows can have the same Description value. so should I in this case set the Description column as the primary key and there will be no need to set an auto-generated ID field in the Document_status table.? OR it is always better to have an auto-generated column as the primary key? BR
-
Answer:
Varchar columns make lousy targets for indexes and primary keys in particular. Remember that a primary key is indexed by default and this means every entry in Description would be duplicated in the index, thus wasting tablespace. In addition, comparisons between varchars is REALLY slow as opposed to those between numbers, so queries are likey to be much slower to process. Keep your primary key something else and just have an UNIQUE constraint on the Description column if you really must enforce uniqueness for the column.
Qaqaqwq W at Yahoo! Answers Visit the source
Other answers
You will live in hell setting Description as your primary key field. They will provide as many as description possibilities they can input. As soon as possible set a field as primary key that users could never touch and your system would take care of.
Itamar R
What Itamar said. I'd also add that for the purpose of recalling the data it's much faster to pull up a record based on an integer field as opposed to a varchar.
Me
Related Q & A:
- Why different key exhange techniques for SSL key exchange?Best solution by Information Security
- How to remove namespace from generated JAXB?Best solution by Stack Overflow
- How to add primary key from multiple table as a foreign key in a table in sql server 2008?Best solution by stackoverflow.com
- How do you change the name on a primary user?
- What's the difference between a chroma key and a digital chroma key?Best solution by blog.rosebrand.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.