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 > Database Server Software > MySQL > Why mysql creates an index for one foreign key and ignores the other?

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-07-10, 01:44
rico9500 rico9500 is offline
Registered User
 
Join Date: Feb 2010
Posts: 1
Question Why mysql creates an index for one foreign key and ignores the other?

*Mysql version: 5.0.51a*

I used the following create table command:
Code:
CREATE TABLE `book_loan` (`isbn` varchar(13) NOT NULL,
`student_no` varchar(12) NOT NULL,  
`borrow_date` date NOT NULL, 
`due_date` date NOT NULL,  
PRIMARY KEY  (`isbn`,`student_no`,`borrow_date`),  
CONSTRAINT `book_loan_ibfk_1`
FOREIGN KEY (`isbn`) REFERENCES `books` (`isbn`) ON DELETE CASCADE, 
CONSTRAINT `book_loan_ibfk_2`
FOREIGN KEY (`student_no`) REFERENCES `students` (`student_no`) ON
DELETE CASCADE) 
ENGINE=InnoDB;
When I use show create table, I'm expecting to get this:

Code:
CREATE TABLE `book_loan` (`isbn` varchar(13) NOT NULL,
`student_no` varchar(12) NOT NULL, 
`borrow_date` date NOT NULL, 
`due_date` date NOT NULL,  
PRIMARY KEY  (`isbn`,`student_no`,`borrow_date`), 
KEY `isbn` (`isbn`),  
KEY `student_no` (`student_no`),  
CONSTRAINT `book_loan_ibfk_1` 
FOREIGN KEY (`isbn`) REFERENCES `books` (`isbn`) ON DELETE CASCADE, 
CONSTRAINT `book_loan_ibfk_2`
FOREIGN KEY (`student_no`) REFERENCES `students` (`student_no`) ON 
DELETE CASCADE) 
ENGINE=InnoDB;
But instead I get this:

Code:
CREATE TABLE `book_loan` (`isbn` varchar(13) NOT NULL,
`student_no` varchar(12) NOT NULL,  
`borrow_date` date NOT NULL, 
`due_date` date NOT NULL,  
PRIMARY KEY  (`isbn`,`student_no`,`borrow_date`), 
KEY `student_no` (`student_no`),  
CONSTRAINT `book_loan_ibfk_1` 
FOREIGN KEY (`isbn`) REFERENCES `books` (`isbn`) ON DELETE CASCADE, 
CONSTRAINT `book_loan_ibfk_2`
FOREIGN KEY (`student_no`) REFERENCES `students` (`student_no`) ON 
DELETE CASCADE) 
ENGINE=InnoDB;
Why do I see only one index?? This Seems to contradict with MySQL docs which say MySQL automatically creates an index on the referencing column whenever you create a foreign key.
Reply With Quote
  #2 (permalink)  
Old 03-07-10, 06:43
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
there is no additional index created for the isbn FK because it can utilize the PK index
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools
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