Reply
Compound Primary keys - help!
Old 10-25-2007, 03:18 AM Compound Primary keys - help!
Average Talker

Posts: 22
OK I'm so confused about composite primary keys. I know exactly what they are and what they do in *theory*, but I'm having trouble implementing them.

I know that multiple columns in a table make up a composite primary key. Problem is, how do I retrieve a composite primary key if it's made up of so many columns? Or do I need an EXTRA column that combines the results of all the columns into a single string?

Please help >.<

Help very much appreciated!

P.S What's the difference between "unique" and "primary" keys? I thought they were the same thing, and then someone says to me that I need to make my primary keys unique??? I thought by declaring a column a primary key in mySQL (I use auto-increment btw) puts a primary key constraint on the column ... which ensures that no 2 PKs are the same!

PPS I know some people hate compound keys and other swear by them, but I really need to understand this
__________________
www.j7labs.com
web apps, video tutorials, resources
deltawing1 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 10-25-2007, 04:30 AM Re: Compound Primary keys - help!
chrishirst's Avatar
Super Moderator

Posts: 13,626
Location: Blackpool. UK
retrieve an index

SHOW INDEX FROM table FROM db;

Wikipedia explains primary & unique keys quite well http://en.wikipedia.org/wiki/Unique_key
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-28-2007, 12:15 AM Re: Compound Primary keys - help!
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,024
Name: Forrest Croce
Location: Seattle, WA
Quote:
Originally Posted by deltawing1 View Post
I know that multiple columns in a table make up a composite primary key. Problem is, how do I retrieve a composite primary key if it's made up of so many columns? Or do I need an EXTRA column that combines the results of all the columns into a single string?
That would be counter productive; the idea is to keep each data row as small as possible to use less disc I/O, since it's one of the slowest thing a database server has to do. An index is like an external copy of part of the data, with bookmarks to the row each entry represents. They're stored as binary trees, which are much faster to look for particular data in.

Create Index idxName On TableName ( Column_A, Column_B, Column_C )

A query that uses Column_A and Column_B in a where or join clause will benefit from this index, but a query that only uses Column_C probably won't. On the other hand, a query that only uses the columns in the index will run without ever touching the table itself ... at least in Microsoft land.

Quote:
Originally Posted by deltawing1 View Post
P.S What's the difference between "unique" and "primary" keys? I thought they were the same thing, and then someone says to me that I need to make my primary keys unique??? I thought by declaring a column a primary key in mySQL (I use auto-increment btw) puts a primary key constraint on the column ... which ensures that no 2 PKs are the same!
In practical terms, you can have a dozen unique keys, but only one PK. Also, you can have a null value in a unique key if you choose to.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Reply     « Reply to Compound Primary keys - help!
 

Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 1.04676 seconds with 12 queries