2013年3月27日星期三

【转】[SQL]MySql myisam引擎不支持外键


CREATE TABLE `t1` (                                                                                      
          `id` 
char(1NOT NULL,                                                                                 
          
PRIMARY KEY  (`id`)                                                                                    
        ) ENGINE
=InnoDB DEFAULT CHARSET=gb2312   

create table t2(
id 
char(1not null,
foreign key(id) references t1(id)
);

table t2可以正确创建,但是外键并没有建立,使用show create table t2得到如下:
CREATE TABLE `t2` (                                                                                  
          `id` 
char(1NOT NULL,                                                                             
          
KEY `id` (`id`)                                                                                    
        ) ENGINE
=MyISAM DEFAULT CHARSET=gb2312 

------------------------------------------------------------------------------------------------------------------------------------------------

指定table t2的引擎为innodb后可以正确创建
create table t2(
id 
char(1not null,
foreign key(id) references t1(id)
)engine
=innodb charset=gb2312;

使用show create table t2得到如下:
CREATE TABLE `t2` (                                                                                                                                                      
          `id` 
char(1NOT NULL,                                                                                                                                                 
          
KEY `id` (`id`),                                                                                                                                                       
          
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`id`) REFERENCES `t1` (`id`)                                                                                                       
        ) ENGINE
=InnoDB DEFAULT CHARSET=gb2312 

没有评论: