Home > Database > Mysql Tutorial > 快速理解MySQL中主键与外键的实例教程_MySQL

快速理解MySQL中主键与外键的实例教程_MySQL

WBOY
Release: 2016-05-27 13:45:39
Original
1199 people have browsed it

主键与外键的关系,通俗点儿讲,我现在有一个论坛,有两张表,一张是主贴 thread,一张是回帖 reply

先说说主键,主键是表里面唯一识别记录的字段,一般是帖子id,体现在访问的时候,例如是
thread.php?id=1   表示我要访问的是帖子id是1 的帖子~

再来说说外键,当我们删除某个帖子的时候,需要执行另一个操作,就是删除所有回帖,如果正常情况下,我们需要执行两次delete操作(thread和 reply),这时候如果存在外键,例如,在reply 表里面建立一个指向thread表的主键(id)的外键(这个外键绑的字段,必须是对应帖子的id),并指定响应 delete ,那你在删除 thread 的时候,mysql 自己会帮你把 reply 表中这个帖子的回复都删掉,而不需要你手动再去执行一次reply表的delete操作~

至于两者之间的关系,在刚才的例子中,reply 表的外键,指向的就是 thread 表的主键~~

搞个例子,简单演示一下使用,做dage和xiaodi两个表,大哥表是主键,小弟表是外键:
建表:

CREATE TABLE `dage` (
 `id` int(11) NOT NULL auto_increment,
 `name` varchar(32) default '',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `xiaodi` (
 `id` int(11) NOT NULL auto_increment,
 `dage_id` int(11) default NULL,
 `name` varchar(32) default '',
 PRIMARY KEY (`id`),
 KEY `dage_id` (`dage_id`),
 CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Copy after login

插入个大哥:

mysql> insert into dage(name) values('铜锣湾');
Copy after login

Query OK, 1 row affected (0.01 sec)
Copy after login

mysql> select * from dage;
Copy after login
Copy after login

+----+--------+
| id | name  |
+----+--------+
| 1 | 铜锣湾 |
+----+--------+
1 row in set (0.00 sec)

Copy after login

插入个小弟:

mysql> insert into xiaodi(dage_id,name) values(1,'铜锣湾_小弟A');

Copy after login

Query OK, 1 row affected (0.02 sec)
Copy after login

mysql> select * from xiaodi;
Copy after login
Copy after login

+----+---------+--------------+
| id | dage_id | name     |
+----+---------+--------------+
| 1 |    1 | 铜锣湾_小弟A |
+----+---------+--------------+

Copy after login

把大哥删除:

mysql> delete from dage where id=1;
Copy after login
Copy after login

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`bstar/xiaodi`, CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`))

Copy after login

提示:不行呀,有约束的,大哥下面还有小弟,可不能扔下我们不管呀!

插入一个新的小弟:

mysql> insert into xiaodi(dage_id,name) values(2,'旺角_小弟A');   
Copy after login


2ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`bstar/xiaodi`, CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`))
Copy after login


提示:小子,想造反呀!你还没大哥呢!

把外键约束增加事件触发限制:

mysql> show create table xiaodi;
<p>CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`)</p><p>mysql> alter table xiaodi drop foreign key xiaodi_ibfk_1; <br /></p>
Copy after login

Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 
Copy after login

mysql> alter table xiaodi add foreign key(dage_id) references dage(id) on delete cascade on update cascade;
Copy after login

Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0

Copy after login

再次试着把大哥删了:

mysql> delete from dage where id=1;
Copy after login
Copy after login

Query OK, 1 row affected (0.01 sec)

Copy after login

mysql> select * from dage;
Copy after login
Copy after login

Empty set (0.01 sec)

Copy after login

mysql> select * from xiaodi;
Copy after login
Copy after login

Empty set (0.00 sec)

Copy after login


得,这回对应的小弟也没了,没办法,谁让你跟我on delete cascade了呢!

例子说明的应该蛮清楚了吧,其他功能对应手册自己实践吧!:-)

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template