一、关系
我们都知道MySQL是一个关系型数据库,所有一般的时候,我们都会进行多个表联合查询。这就会产生下面几种关系。
- 一对一
个人信息(常用信息、非常用信息)、登录Token等 - 一对多
一个班级对应多个学生、一个学生对应多个科目、一个分类对应多个新闻等 - 多对多
一个标签对应多个新闻,一个新闻有多个标签 - 自关联
省市区
二、创建2张关系表
创建班级表
create table tbl_classes (
id int not null primary key auto_increment,
name varchar(45) not null
)engine=innodb default charset=utf8;
创建学生表
create table tbl_students (
id int not null primary key auto_increment,
name varchar(50) not null,
gender varchar(1) not null,
age int(11) not null,
cls_id int(11)
)engine = innodb default charset=utf8;
插入数据
insert into tbl_classes values (0,"精英1班");
insert into tbl_classes values (0,"精英2班");
insert into tbl_classes values (0,"精英3班");
insert into tbl_students values (0,"小红",'女',12,1);
insert into tbl_students values (0,"小刚",'男',13,1);
insert into tbl_students values (0,"小蓝",'女',11,2);
insert into tbl_students values (0,"小赵",'男',13,3);
insert into tbl_students values (0,"小李",'女',10,3);
insert into tbl_students values (0,"小周",'男',10,4);
insert into tbl_students values (0,"小黄",'男',15,5);