Home > Ask the Oracle Experts > SQL Questions & Answers > Purpose of a primary key
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Purpose of a primary key

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 17 October 2002
What is the actual need of a primary key? What will happen if I don't define a primary key?

>
EXPERT RESPONSE

That's a great question. The main purpose of a primary key is to implement a relationship between two tables in a relational database; it's not called a relational database for nothing! More specifically, the primary key is the "target" which a foreign key can reference. You cannot declare a foreign key in table B to relate to table A unless the primary key in table A has been defined.

So right away, here's the first conclusion: if your database has a table which no other table relates to, then this table doesn't need a primary key. Nevertheless, the table can still have one, and it is common practice to declare one anyway.

One reason for declaring a primary key, even if no other table references it, comes from what we could call a side benefit -- the primary key gets an index. To the best of my knowledge, all databases utilize an index in order to implement the uniqueness constraint that a primary key requires. The reason is simple: when you tell the database to insert a new row, it has to check the new row's primary key value against all the existing primary key values to make sure the new value is not there yet. There's no faster way do accomplish this than by looking up the value in an index. You could say this is defensive behaviour on the part of the database, because it knows that looking up a value is always faster using an index than scanning a table.

So by declaring a primary key, you get an index, and chances are, you'll need it for retrieval anyway. Most applications have at least one query which is supposed to return a single row from the table. This is often based on the primary key, because the primary key is, by definition, capable of distinguishing each row from all the others. So when searching for a particular row using the value of the primary key, the database can uses the same index to speed up the retrieval that it set up for itself to ensure that the primary key will always be unique.

Yes, you can declare indexes yourself. In fact, you should declare an index on every column that needs one, but which ones need one is a different discussion for another day. Furthermore, you should not declare a primary key and also an index of your own on the primary key. That would be redundant, unless it's a composite primary key, in which case you might need additional indexes, at least on columns of the primary key not in the same order, or on subsets. Whew! As I said, that's another discussion. In the meantime, go ahead and declare primary keys for your tables.

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL
Which normal form is used most?
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts