以创建学生教师表为例: 学生 id 关联教师 tid
学生表: student
教师表: teacher
sql语句 :
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;
重点: 外键关联语句,会手写才可以!
key `fktid` (`tid`), constraint `fktid` foreign key(`tid`) references `teacher` (`id`)
总结
到此这篇关于sql语句创建外键关联的文章就介绍到这了,更多相关sql创建外键关联内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!