Home  >  Article  >  php教程  >  mysql建立外键

mysql建立外键

WBOY
WBOYOriginal
2016-06-13 12:35:46946browse

建立外键的前提:本表的列必须与外键类型相同(外键必须是外表主键)。
外键作用: 使两张表形成关联,外键只能引用外表中的列的值!
指定主键关键字: foreign key(列名)
引用外键关键字: references (外键列名)
事件触发限制: on delete和on update , 可设参数cascade(跟随外键改动), restrict(限制外表中的外键改动),set Null(设空值),set Default(设默认值),[默认]no action
例如:
outTable表 主键 id 类型 int
创建含有外键的表:
create table temp(
id int,
name char(20),
foreign key(id) references outTable(id) on delete cascade on update cascade);
说明:把id列 设为外键 参照外表outTable的id列 当外键的值删除本表中对应的列筛除 当外键的值改变 本表中对应的列值改变。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn