创建多表关系:
方法一(建表时):
FOREIGN KEY(o_buyer_id) REFERENCES s_user(u_id),
FOREIGN KEY(o_seller_id) REFERENCES s_user(u_id)
方法二(建表后):
ALTER TABLE EMP ADD CONSTRAINT FOREIGN KEY EMP(DEPTNO) REFERENCES DEPT (DEPTNO);
隐式内连接:没有出现`join`关键字
格式:`select 显示字段1, 显示字段2 from 表1, 表2 where 表1.gid=表2.gid;`
显式内连接:SQL语句中出现了`join`关键字
格式:` select 显示字段1, 显示字段2 from 表1 join 表2 on 表1, 表2 where 表1.gid=表2.gid;`
说明:join前可以添加`inner`或`cross`关键字,可以省略
左外连接:以左表为主,`left [outer]`
格式:` select 显示字段1, 显示字段2 from 表1 left [outer] join 表2 on 表1, 表2 where 表1.gid=表2.gid;`
说明:显示左表所有数据,右表有对应就显示数据,没有对应显示NULL
左外连接:以左表为主,`left [outer]`
格式:` select 显示字段1, 显示字段2 from 表1 right [outer] join 表2 on 表1, 表2 where 表1.gid=表2.gid;`
说明:显示左表所有数据,右表有对应就显示数据,没有对应显示NULL
子(嵌套)查询
select * from 表1/表2 where 字段 in (1,3,5);
select * from 表1/表2 where 字段 in (select 字段 from 表1/表2);
记录联合
格式:`select 语句1 union select 语句2`
说明:
union all:将两边查询结果直接拼在一起
union:是union all的去重结果
联合更新数据
格式:`update 表1, 表2 set 表1.gid=0, 表2.price=表2.price-200 where 表1.gid=表2.gid and 表1.id=7;`
示例:`update user u, goods g set u.gid=0, g.price=g.price-200 where u.gid=g.gid and u.id=7;`
说明:可以给操作的表起别名,还可以对字段的原有只进行运算