![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
Regular Coder ![]() Join Date: Jul 2002
Location: Japan
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Difference between UNIQUE and PRIMARY
Hi all,
Stupid question, but... what's the difference between Unique and Primary? I'm referring to the phpmyAdmin interface. I thought the primary field was unique. ![]() Thanks c
__________________
chesneil |
|
|
|
|
|
PM User | #2 |
|
Regular Coder ![]() Join Date: Oct 2002
Posts: 379
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
PRIMARY refers to a field or a set of fields used to identify each row in a table. UNIQUE refers to a field that cannot be repeated in a table. The primary key is, by necessity, a unique identifier, but any field may be defined as unique. The primary key can be a composite, consisting of more than one field. Each field within a composite key can be repeated, but each combination of fields must be unique.
__________________
Strategy Conscious |
|
|
|
|
|
PM User | #3 | |
|
Regular Coder ![]() Join Date: Jul 2002
Location: Japan
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Quote:
OK. Next basic question. Why do we need a 'Primary' field. I understand the need for a field that uniquely identifies each row, i.e. an 'ID' field, but why declare it as 'Primary'. What does it mean? In anticipation...
__________________
chesneil |
|
|
|
|
|
|
PM User | #4 |
|
teh Moderatorinator ![]() ![]() Join Date: Sep 2004
Location: USA
Posts: 2,435
Thanks: 2
Thanked 38 Times in 38 Posts
![]() |
You're right a primary key is something that uniquely identifies a row, and I think one major reason you declare it as primary is that in mysql primary keys are automatically indexed for you.
|
|
|
|
|
|
PM User | #5 | |
|
Regular Coder ![]() Join Date: Jul 2002
Location: Japan
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Quote:
__________________
chesneil |
|
|
|
|
|
|
PM User | #7 |
|
Regular Coder ![]() Join Date: Oct 2002
Posts: 379
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
The major reason for declaring a primary key is that it is necessary to have a mechanism for uniquely identifying each record in your table. Indexing is part of the mechanisms used to ensure you have the unique identifier.
It's important to remember that a primary key (or an index, for that matter) does not have to be based on a single field. Also, building an index does take up space: every index you create takes up roughly the same space as a table consisting of the indexed fields. Creating indexes on char and text fields is not really a good idea; they work best on fixed length number fields. And, yes, I did mean that a value in a field specified as unique cannot be repeated. Some sample code for creating a composite primary key is: PHP Code:
__________________
Strategy Conscious |
|
|
|
|
|
PM User | #8 | |
|
New to the CF scene Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Quote:
I'm confused on the space acquired when the index is created. Now my question is whether an index will occupy space or not. Can you plz explain the below statement provided in detail.Could not understand that ?? Also, building an index does take up space: every index you create takes up roughly the same space as a table consisting of the indexed fields. |
|
|
|
|
|
|
PM User | #9 |
|
Master Coder ![]() Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 5,411
Thanks: 32
Thanked 373 Times in 364 Posts
![]() ![]() ![]() ![]() ![]() |
An index is a physical list of the values in that table's column. It occupies its own space in the database. It's as real as the table, and is separate from the table.
So if you create an index on table1.create_date, for example, then a file will be created that consists of one row for every row in table1. Each row contains the create_date and a pointer back to the row in table1 that the index's row belongs to. Every time you update, delete from, or insert into table1, the index is also updated, deleted from, or inserted into.
__________________
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|