mysql 创建单表外键关联多表

【mysql 创建单表外键关联多表】创建students表关联country,position,gender三张表

create table country( cid int primary key auto_increment, cname varchar(10) NOT NULL )ENGINE=INNODB DEFAULT CHARSET=utf8; create table `position`( pid int primary key auto_increment, pname varchar(10) NOT NULL )ENGINE=INNODB DEFAULT CHARSET=utf8; create table Gender( gid int primary key auto_increment, gname varchar(10) NOT NULL )ENGINE=INNODB DEFAULT CHARSET=utf8; create table students( sid int primary key auto_increment, sname varchar(100) not null, countryId int, positionId int, GenderId int, Capability int, constraint fk_stu_cid foreign key(countryId) references country(cid), constraint fk_stu_pid foreign key(positionId) references `position`(pid), constraint fk_stu_gid foreign key(GenderId) references Gender(gid) )ENGINE=INNODB DEFAULT CHARSET=utf8; insert into students(sname,countryId,positionId,GenderId,Capability) values('刘备',3,2,1,80); insert into students(sname,countryId,positionId,GenderId,Capability) values('曹操',1,1,1,76); idname countryId positionId GenderId Capability 刘备蜀打野男80 曹操魏top男76 孙策 孙尚香吴AD女88 诸葛亮蜀ap男87 周瑜 黄忠 关羽 张飞 赵云 小乔 大乔 貂蝉 吕布 夏侯惇insert into country(cname) values('魏'); idcountry 1魏 2蜀 3吴insert into `position`(pname) values('top'); insert into `position`(pname) values('jungle'); insert into `position`(pname) values('ap'); insert into `position`(pname) values('ad'); insert into `position`(pname) values('sup'); idposition 1top 2jungle 3ap 4ad 5supinsert into Gender(gname) values('男'); insert into Gender(gname) values('女'); idGender 1female 2male查询select * from students s,country c,position p,Gender g where s.countryId = c.cid and s.positionId = p.pid and s.GenderId = g.gid;


    推荐阅读