关系表
1.一对一
2.一对多
3.多对多
外键关系
1:1,外键可以放在任意一端
1:n,外键放在n端
m:n,将两张表的主键放到第三张表,形成一张新表
多表查询
笛卡尔积:笛卡尔积是指两张表的所有数据一一对应
select * from table1,table2 where 条件;
1.连接查询
2.内连接
on后面如果还有条件则在后面+where
隐式内连接
select tb1.name,tb2.name from tb1,tb2 where tb1.id=tb2.id;
//起别名
select a.name,b.name from tb1 a,tb2 b where a.id=b.id;
显式内连接
select tb1.name,tb2.name from tb1 inner join tb2 on tb1.id=tb2.id;
3.外连接
哪张表的外连接,就包含哪张表全部内容,也包含另一张表的公共内容
左右外连接,包含全部内容的表是下方的表一
4.子查询
列子查询,返回一列
select * from emp where emp_deptid in (select id from dept where name='1' or name='2')
//列子查询的in()中,是一个or条件组.否定则为not in()
行子查询,返回一行
select * from tb_emp where (entrytime,job)=(select entrytime,job from tb_emp where name='张三')
//查询与张三的入职日期、职位相同的员工
#需要注意的是,前后条件参数的位置需一一对应。
表子查询
//将查询出的结果作为一张零时表
select * from (select * from tab where name='张三');