General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Laplink Gold 2008 - $15 Off with Coupon Code CJM9NE
  #1  
Old July 8th, 2003, 03:12 AM
rathaur rathaur is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Malaysia
Posts: 81 rathaur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 31 sec
Reputation Power: 7
primary key and unique

Hi

Hope someone would be generous to explainme the following

1.
Difference between a primary key and Unique in Mysql. Why there are table where there is a field which has been defined as primary key contain another field defeined as unique key.

2.
Index key ? are they same with primary key ..

3. I did a sample table with syntax:

CREATE TABLE `keys` (
`id` INT( 3 ) NOT NULL AUTO_INCREMENT,
`matric` VARCHAR( 20 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
UNIQUE (
`matric`
)
);


The table is succesfully created but when i say 'describe keys' the mysql returns an error :

mysql> describe keys;
ERROR 1064: You have an error in your SQL syntax near 'keys' at line 1


Thanks,

Rathaur
__________________
Rathaur
======================
Knowledge is Power

Reply With Quote
  #2  
Old July 8th, 2003, 09:03 AM
iahmed iahmed is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: USA
Posts: 171 iahmed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 58 sec
Reputation Power: 5
Index:

An index is a structure in a table that orders the data. It allows the database to access data quickly (In MySQL its implemented using B-tree algorithms).

Primary Key:

This is an index that cannot be NULL, Primary Keys are used in building relationships between tables in a database. (an index is automatically created on the primary key). The difference between primary and ordinary keys is that there can be multiple keys, but only one primary key.

Unique:
Unique and Index are same, the difference is, in Unique, duplicate are not allowed in any circumstances and that is enforced by database server.

Thanks

Last edited by iahmed : July 8th, 2003 at 09:26 AM.

Reply With Quote
  #3  
Old July 8th, 2003, 08:50 PM
rathaur rathaur is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Malaysia
Posts: 81 rathaur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 31 sec
Reputation Power: 7
Thanks, I am confused again, pls do clear me. You said there is only 1 primary key in a table, but I do have a table with 2 primary keys.

I created the table with 1 primary key, but later I alter the table to have 2 primary key

look at this 2 primary keys are here


mysql> describe k2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(3) | | PRI | 0 | |
| name | varchar(33) | | | | |
| name2 | varchar(30) | | | | |
| bins | varchar(30) | | PRI | | |
| trial | varchar(5) | | | | |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql>

Reply With Quote
  #4  
Old July 9th, 2003, 12:12 AM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to avit
Your table with 2 primary keys means that the 2 fields together form one unique key. Each field by itself may have repeating values, but both keys put together must be unique. Take a look:

Code:
CREATE TABLE phonenumbers (
  firstname varchar(20) NOT NULL,
  lastname varchar(20) NOT NULL,
  phone char(10),
  PRIMARY KEY (firstname, lastname)
);

Bob  |  Jones  |  6665554444
Bob  |  Smith  |  7776668888
Joe  |  Smith  |  8885552222


You see, there are two Bobs and two Smiths, but each record has a unique key.

Reply With Quote
  #5  
Old July 1st, 2007, 01:52 PM
manuswami manuswami is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 1 manuswami User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 57 sec
Reputation Power: 0
Is it a composite primary key????

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > primary key and unique


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway