欢迎光临
我们一直在努力

sql语句创建外键关联的完整实例

以创建学生教师表为例: 学生 id 关联教师 tid

学生表: student

sql语句创建外键关联的完整实例

教师表: teacher

sql语句创建外键关联的完整实例

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

赞(0) 打赏
主机商所有内容均来自网络,若无意侵犯到您的权利,请及时与联系 QQ 2232175042,将在48小时内删除相关内容!!主机测评网 » sql语句创建外键关联的完整实例

主机商评测网 找服务器 更专业 更方便 更快捷!

专注IDC行业资源共享发布,给大家带来方便快捷的资源查找平台!

联系我们

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫