mysql建立外键

yipeiwu_com7年前Mysql基础

建立外键的前提:本表的列必须与外键类型相同(外键必须是外表主键)。 


外键作用: 使两张表形成关联,外键只能引用外表中的列的值! 


指定主键关键字: 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列 当外键的值删除本表中对应的列筛除 当外键的值改变 本表中对应的列值改变。                                                

相关文章

mysql4.1以上版本连接时出现Client does not support authentication protocol问题解决办法

mysql4.1以上版本连接时出现Client does not support authentication protocol问题解决...

PHP+MySQL 手工注入语句大全 推荐

暴字段长度 Order by num/* 匹配字段 and 1=1 union select 1,2,3,4,5…….n/* 暴字段位置 and 1=2 union select 1,2...

在mysql数据库原有字段后增加新内容

复制代码 代码如下:update table set user=concat(user,$user) where xx=xxx;...

mysql时区问题

用convert_tz转换时区,你可以用      show   variables  ...

php简单操作mysql数据库的类

本文实例讲述了php简单操作mysql数据库的类。分享给大家供大家参考。具体如下: <?php /** * Database class * * @version...