Home > php教程 > php手册 > body text

mysql建立外键

WBOY
Release: 2016-06-13 12:35:46
Original
956 people have browsed it

建立外键的前提:本表的列必须与外键类型相同(外键必须是外表主键)。
外键作用: 使两张表形成关联,外键只能引用外表中的列的值!
指定主键关键字: 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列 当外键的值删除本表中对应的列筛除 当外键的值改变 本表中对应的列值改变。

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!