1、什么是外键?
如果一个表的某个字段指向另一个表的主键,就称之为外键;被指向的表之为主表,也叫父表;那么另一个表就是从表,也叫子表。
2、那么,如果设置和使用一个外键呢?
我们通过一个例子来演示,两个表:一个是作者表 author_table;一个是文章表 article_table;
准备数据:
# 建表 author_table
create table author_table(
author_id int(4) not null primary key auto_increment,
author_name char(20) not null);
# 建表 article_table 并设置外键,指向作者表的author_id
create table article_table(
article_id int(4) not null primary key auto_increment,
article_title char(20)