怎样用SQL语句在MYSQL中创建外键约束关系

发布网友

我来回答

4个回答

热心网友

应该是这样子的,
Alter table 表名
add foreign key (外键属性) references 表名(表属性)

热心网友

alter table table_name add constraint fk_column_id foreign key(column) references 

主键表 (column_id);

热心网友

alter table table_name add constraint fk_column_id foreign key(column) references 主键表 (column_id);

热心网友

给你个创建了外间约束的例子
看下,这个是我写的,字段多了些,不影响
create table if not exists entity(id int auto_increment primary key, hostname char(255), hostip char(32), type int, caption char(255), INDEX (id)) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci
create table if not exists hvhost(en_id int, username char(255), password char(255), memory_size bigint, cpu_count int, cpu_frequency bigint, power_state char(16), time char(), FOREIGN KEY(en_id) REFERENCES entity(id) on delete cascade on update cascade) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com