Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 01-25-2005, 02:18 PM   PM User | #1
chesneil
Regular Coder

 
Join Date: Jul 2002
Location: Japan
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
chesneil is an unknown quantity at this point
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
chesneil is offline   Reply With Quote
Old 01-25-2005, 08:16 PM   PM User | #2
Kiwi
Regular Coder

 
Join Date: Oct 2002
Posts: 379
Thanks: 0
Thanked 0 Times in 0 Posts
Kiwi is an unknown quantity at this point
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
Kiwi is offline   Reply With Quote
Old 01-26-2005, 01:17 AM   PM User | #3
chesneil
Regular Coder

 
Join Date: Jul 2002
Location: Japan
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
chesneil is an unknown quantity at this point
Quote:
Originally Posted by Kiwi
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.
Thanks for the explanation, Kiwi. When you say "...a field that cannot be repeated in a table" you mean that entries in that column cannot be repeated. Not that the column names can't be repeated (which, of course, they can't).


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
chesneil is offline   Reply With Quote
Old 01-26-2005, 01:24 AM   PM User | #4
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,435
Thanks: 2
Thanked 38 Times in 38 Posts
Brandoe85 will become famous soon enough
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.
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 01-26-2005, 03:57 AM   PM User | #5
chesneil
Regular Coder

 
Join Date: Jul 2002
Location: Japan
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
chesneil is an unknown quantity at this point
Quote:
Originally Posted by Brandoe85
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.
And 'indexing' is what exactly?
__________________
chesneil
chesneil is offline   Reply With Quote
Old 01-26-2005, 04:06 AM   PM User | #6
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,435
Thanks: 2
Thanked 38 Times in 38 Posts
Brandoe85 will become famous soon enough
Indexes allow for quick searches. Heres an article that explains indexing.
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 01-26-2005, 04:34 AM   PM User | #7
Kiwi
Regular Coder

 
Join Date: Oct 2002
Posts: 379
Thanks: 0
Thanked 0 Times in 0 Posts
Kiwi is an unknown quantity at this point
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:
CREATE TABLE invoice_ln (
 
invoice_number INTEGER(4UNSIGNED ZEROFILL NOT NULL,
 
invoice_line_number INTEGER(2UNSIGNED ZEROFILL NOT NULL,
 
descr TEXT,
 
PRIMARY KEY (invoice_numberinvoice_line_number)
); 
__________________
Strategy Conscious
Kiwi is offline   Reply With Quote
Old 07-17-2008, 09:41 AM   PM User | #8
Trilok
New to the CF scene

 
Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Trilok is an unknown quantity at this point
Quote:
Originally Posted by Kiwi View Post
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:
CREATE TABLE invoice_ln (
 
invoice_number INTEGER(4UNSIGNED ZEROFILL NOT NULL,
 
invoice_line_number INTEGER(2UNSIGNED ZEROFILL NOT NULL,
 
descr TEXT,
 
PRIMARY KEY (invoice_numberinvoice_line_number)
); 
Hi sir
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.
Trilok is offline   Reply With Quote
Old 07-17-2008, 05:59 PM   PM User | #9
Fumigator
Master Coder

 
Fumigator's Avatar
 
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
Fumigator is just really niceFumigator is just really niceFumigator is just really niceFumigator is just really niceFumigator is just really nice
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.
__________________
Fumigator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:16 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.