If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > what column to index?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-15-08, 20:37
Panoy Panoy is offline
Registered User
 
Join Date: Mar 2007
Posts: 77
what column to index?

Hi to all,


I have 2 tables which are named "items" and "categories". Their structure is this:

items table:

item_id int not null auto_number primary key
item_name varchar(50)
item_description varchar(30)
item_category int not null

categories table:

cat_id int not null auto_number primary key
cat_name varchar(30)

If I issue a join query on those 2 tables, like this one:

SELECT
item_id,
item_name,
item_cost,
item_unit,
cat_name
FROM items
INNER JOIN categories
ON items.item_category = categories.cat_id;

Since this query is used a lot, which field do I need to create an INDEX with? Is it the item_category field in the "items" table?

Does a field have an INDEX automatically if it is a primary key field?
How about UNIQUE fields?

thanks and god bless.
__________________
Programming is fun!
Reply With Quote
  #2 (permalink)  
Old 05-15-08, 20:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Panoy
Is it the item_category field in the "items" table?
yes


Quote:
Originally Posted by Panoy
Does a field have an INDEX automatically if it is a primary key field?
yes


Quote:
Originally Posted by Panoy
How about UNIQUE fields?
yes


fyi, ansi sql knows nothing about indexes, which are dbms implementations
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-15-08, 21:20
Panoy Panoy is offline
Registered User
 
Join Date: Mar 2007
Posts: 77
Since I am using MySQL, and if I happen to put a FOREIGN KEY constraint on the "items" table, would the INDEX then be automatically created too?

From what I understand in MySQL's manual, it does seem so, but it is nice to have read it from other opinion as well.

thanks
__________________
Programming is fun!
Reply With Quote
  #4 (permalink)  
Old 05-15-08, 21:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
only for innodb tables, and only after version 4.1.2, the FK index is created automatically

still wouldn't hurt to code it yourself, because that way you can name it (in case you have to drop it)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On