一对多(1方建主表(id为主键字段), 多方建外键字段)
一个实体的某个数据与另外一个实体的多个数据有关联关系, 一对多的关系在设计的时候,需要设计表的外键。
班级表和学生表设计
部门表和员工表设计
创建数据库表
constraint 约束
foreign key就是表与表之间的某种约定的关系,由于这种关系的存在,能够让表与表之间的数据,更加的完整,关连性更强。
foreign key语句的式例:FOREIGN KEY(Sno) REFERENCES Student(Sno)
注意:表的外键必须是另一张表的主键
//创建班级表
create tableclass(idintprimary key auto_increment,namevarchar(20));
//创建学生表
create tablestudent(idintprimary key auto_increment,namevarchar(20),sexvarchar(20),class_idint,constraint foreignkey(class_id)referencesclass(id));
//插入班级数据
insertintoclassvalues(1,'ceshiban');
insertintoclassvalues(2,'kaifa');
//插入学生数据insertintostudentvalues(1,'zhangsan','nan',1);insertintostudentvalues(2,'lisi','nan',2);
insertintostudentvalues(3,'jingjing','nan',2);
//联查
select*fromstudentwhereclass_id=(selectidfromclasswhereid=2);
补一个外键的注意(默认是约束): 删除主键信息时,当该主键字段值在外键表中存在时,该记录是不能删除的。---要把外键表是的相关信息删除之后,才能删除。
子查询:嵌套在其他查询中的查询。
多对多( 3个表= 2个实体表 + 1个关系表 )
一个实体的数据对应另外一个实体的多个数据,另外实体的数据也同样对应当前实体的多个数据。
一个学生可以有多个老师,一个老师可以教多个学生
解决方案:创建一个中间表,专门用来维护多表之间的对应关系,通常是能够唯一标识出数据的字段(主键)
create tableteacher(idintprimary key,namevarchar(100));
create table student(idintprimary key,namevarchar(100));
create tableteacher_student(teacher_idint,student_idint,constraint foreignkey(teacher_id)referencesteacher(id),constraint foreignkey(student_id)referencesstudent(id));
insertintoteachervalues(1,'梁老师');
insertintoteachervalues(2,'李老师');
insertintostudentvalues(1,”张三”);
insertintostudentvalues(2,”四”);
insertintoteacher_studentvalues(1,1);
insertintoteacher_studentvalues(1,2);
insertintoteacher_studentvalues(2,1);
insertintoteacher_studentvalues(2,2);
//查询李老师所教的学生
selectidfromteacherwherename=’李老师’selectstudent_idfromteacher_studentwhereteacher_id=idselect*fromstudentwhereidin(selectstudent_idfromteacher_studentwhereteacher_id=(selectidfromteacherwherename='李老师'));
//查询张三的所有老师select*fromteacherwhereidin(selectteacher_idfromteacher_studentwherestudent_id=(selectidfromstudentwherename='张三'));
五、 连表查询
分类:内连接、外连接、交叉连接
初始定义表结构
create tablecustomer(idintprimary key auto_increment,namevarchar(20),cityvarchar(20));create tableorders(idintprimary key auto_increment,good_namevarchar(20),pricefloat(8,2),customer_idint);insertintocustomer(name,city)values('李老师','东北');insertintocustomer(name,city)values('崔老师','山西');insertintocustomer(name,city)values('张老师','内蒙');insertintocustomer(name,city)values('闫老师','天津');insertintoorders(good_name,price,customer_id)values('电脑',59,1);insertintoorders(good_name,price,customer_id)values('笔记本',88,2);insertintoorders(good_name,price,customer_id)values('吹风机',99,1);insertintoorders(good_name,price,customer_id)values('香水',300,3);insertintoorders(good_name,price,customer_id)values('牛奶',100,6);
交叉查询
交叉查询,又叫笛卡尔积查询,会将左表和右表的信息,做一个乘积将所有信息查询出来,会产生临时表,比较占用内存,生成的记录数=表1 X表2
select*fromcustomer,orders;select*fromcustomer crossjoinorders;
内连接查询
内连接,inner join on 查询两张表,设定条件,将两张表中对应的数据查询出来
不会产生笛卡尔积,不会产生临时表,性能高
select*fromcustomer c innerjoinorders o on c.id=o.customer_id;select*fromcustomer,orderswherecustomer.id=orders.customer_id;select*fromcustomer c,orders owherec.id=o.customer_id;
左外连接
左外连接 left join on 设定条件,将两张表对应的数据查询出来,同时将左表自己没有关联的数据也查询出来
注意:join前面是左,后面是右
select*fromcustomer c leftjoinorders o on c.id=o.customer_id;
右外连接
右外连接 right join on 设定条件,将两张表对应的数据查询出来,同时将右表自己没有关联的所有数据查询出来
select*fromcustomer c rightjoinorders o on c.id=o.customer_id;
联合查询
select*fromcustomer leftjoinorders on customer.id=orders.customer_idhaving price>20;