查看: 20|回复: 0

[综合] sql语句创建外键关联的完整实例

[复制链接]

2

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2011-4-5
发表于 2023-7-10 00:00:00 | 显示全部楼层 |阅读模式

以创建学生教师表为例: 学生 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

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部