no foreign key in table if engine is MyISAM

2019-03-04 06:55发布

问题:

why i cannot create foreign key in my table payments.

crate table students(
text char(5)NOT NULL,
id int(11)NOT NULL AUTO_INCREMENT,
name varchar(250),
level varchar(250),
PRIMARY KEY (text,id)
)ENGINE=MyISAM;

the oder table is

 crate table payments(
    p_id int(11)NOT NULL AUTO_INCREMENT,
    amount varchar(250),
    id int
    PRIMARY KEY (p_id)
FOREIGN KEY (id) REFERENCES students(id)
    )ENGINE=MyISAM;

回答1:

Because MyISAM does not support foreign keys. The FK declarations are parsed, but otherwise ignored. You need to use InnoDB tables for real FK support.