Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2010
    Posts
    1

    Question Unanswered: 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.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    there is no additional index created for the isbn FK because it can utilize the PK index
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •