CREATE TABLE `grade_event` (
  `event_id` int(10) unsigned NOT NULL auto_increment,
  `categroy` char(1) NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY  (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `student` (
  `student_id` int(6) unsigned NOT NULL auto_increment,
  `name` varchar(15) NOT NULL,
  `sex` char(1) NOT NULL,
  PRIMARY KEY  (`student_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
创建外键的时候出现erron:150了CREATE TABLE `NewTable` (
`student_id`  int(6) NOT NULL ,
`event_id`  int(10) NOT NULL ,
`score`  int(3) NOT NULL ,
FOREIGN KEY (`event_id`) REFERENCES `grade_event` (`event_id`),
FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`),
INDEX (`student_id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8
;

解决方案 »

  1.   

    打show engine innodb status\G找LATEST FOREIGN KEY ERROR部分  有详细的报错信息
      

  2.   

    ------------------------
    LATEST FOREIGN KEY ERROR
    ------------------------
    130401 21:53:30 Error in foreign key constraint of table test1/NewTable:
    FOREIGN KEY (`event_id`) REFERENCES `grade_event` (`event_id`),
    FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`)
    ):
    Cannot find an index in the referenced table where the
    referenced columns appear as the first columns, or column types
    in the table and the referenced table do not match for constraint.
    Note that the internal storage type of ENUM and SET changed in
    tables created with >= InnoDB-4.1.12, and such columns in old tables
    cannot be referenced by such columns in new tables.
    See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
    for correct foreign key definition.
      

  3.   

    mysql> CREATE TABLE `NewTable` (
        -> `student_id`  int(6) unsigned NOT NULL ,
        -> `event_id`  int(10) unsigned NOT NULL ,
        -> `score`  int(3) NOT NULL ,
        -> FOREIGN KEY (`event_id`) REFERENCES `grade_event` (`event_id`),
        -> FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`)
        -> )
        -> ;
    Query OK, 0 rows affected (0.01 sec)