1.多表操作
1.1添加外键约束
//创建表的时候
create table if not exists 表名(
字段名1 数据类型 约束条件,
字段名2 数据类型 约束条件,
字段名3 数据类型 约束条件,
......
[constraint 外键名] foreign key (从表字段名) references dept(主表字段名)
);
//创建表后
alter table 从表名 add constraint 外键名 foreign key(从表字段名) references 主表名(主表字段名);
1.2删除外键约束
alter table 从表名 drop foreign key 外键约束名
2.多表查询
2.1交叉连接查询
select * from 表1, 表2,...........;
2.2内连接
//隐式内连接
select * from 表1,表2 where 条件;
//显示内连接
select * from 表1 inner join 表2 on 条件
2.3外连接
左外连接
select * from 表1 left join 表2 on 条件;
右外连接
select * from 表1 right join 表2 on 条件;
满外连接
select * from 表1 full join 表2 on 条件;
2.4子查询
all:
select .... from 表名 where 字段名 > all(查询语句);
any
select .... from 表名 where 字段名 > any(查询语句);
in
select .... from 表名 where 字段名 in (查询语句);
exists
select .... from 表名 where exists (查询语句);
自关联查询
select 字段列表 from 表1 a, 表2 b where 条件;
select 字段列表 from 表1 a left join 表1 b 条件;