sql语句创建外键关联的完整实例
以创建学生教师表为例: 学生 id 关联教师 tid
学生表: student
教师表: teacher
sql语句 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
use school;
create table student(
id int (10) not null primary key ,
name varchar (30) default null ,
tid int (10) default null ,
key `fktid` (`tid`),
constraint `fktid` foreign key (`tid`) references `teacher` (`id`)
)engine=innodb default charset=utf8
insert into student values (1, '小明' ,1);
insert into student values (2, '小红' ,1);
insert into student values (3, '小刚' ,1);
insert into student values (4, '小王' ,1);
insert into student values (5, '小智' ,1);
select * from student;
create table teacher (
id int (10) primary key not null ,
name varchar (30) default null
)engine=innodb default charset=utf8
insert into teacher values (1, '陈老师' );
select * from teacher;
|
重点: 外键关联语句,会手写才可以!
1
2
|
key `fktid` (`tid`),
constraint `fktid` foreign key (`tid`) references `teacher` (`id`)
|
总结
到此这篇关于sql语句创建外键关联的文章就介绍到这了,更多相关sql创建外键关联内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/SoULikeMe/article/details/113368984
本文由主机测评网发布,不代表主机测评网立场,转载联系作者并注明出处:https://zhuji.jb51.net/shujuku/2759.html