99语法
elect 数据 from 数据来源1 join 数据来源2;
笛卡尔积 对乘 cross join 交叉插入
select * from emp e cross join dept d; --99语法
select * from emp,dept; --92语法
内连接 外链接(左外连接,右外连接) 全链接
--等值连接
--自动做等值连接
--注意: 同名字段不要使用限定词
--自然连接 natural join 自动帮你做等值连接 同名字段|主外键关系
select empno,ename,deptno,dname from emp e natural inner join dept d;
--join using(字段) 指明对哪一个字段做等值连接
select empno,ename,deptno,dname from emp inner join dept using(deptno);
-- 数据来源1 join 数据来源2 on 连接条件(等值|非等值) 同名字段需要指明出处
select empno,ename,emp.deptno,dname from emp join dept on emp.deptno = dept.deptno;
--非等值连接
--员工信息和工资等级
select * from emp e inner join salgrade s on e.sal between losal and hisal;
select *
from emp
join dept
on emp.deptno = dept.deptno
join salgrade
on sal between losal and hisal
where emp.deptno = 30;
--有上级的员工信息和上级信息
select * from emp e1 join emp e2 on e1.mgr = e2.empno;
--外链接 left join | right join
--主表
--所有员工信息和上级信息
select * from emp e1 left join emp e2 on e1.mgr = e2.empno;
--全连接
select 1 no, 'a' "name" from dual union select 2 no, 'b' "name" from dual;
select 1 no, 'c' "name" from dual union select 3 no, 'd' "name" from dual;
--内连接 满足条件显示
select *
from (select 1 no, 'a' "name"
from dual
union
select 2 no, 'b' "name"
from dual) a
join (select 1 no, 'c' "name"
from dual
union
select 3 no, 'd' "name"
from dual) b
on a.no = b.no;
--左外链接 a表中的数据满不满足条件都显示
select *
from (select 1 no, 'a' "name"
from dual
union
select 2 no, 'b' "name"
from dual) a
left join (select 1 no, 'c' "name"
from dual
union
select 3 no, 'd' "name"
from dual) b
on a.no = b.no;
--右外链接 b表中的数据满不满足条件都显示
select *
from (select 1 no, 'a' "name"
from dual
union
select 2 no, 'b' "name"
from dual) a
right join (select 1 no, 'c' "name"
from dual
union
select 3 no, 'd' "name"
from dual) b
on a.no = b.no;
--全链接 a,b表中的数据满不满足条件都显示
select *
from (select 1 no, 'a' "name"
from dual
union
select 2 no, 'b' "name"
from dual) a
full join (select 1 no, 'c' "name"
from dual
union
select 3 no, 'd' "name"
from dual) b
on a.no = b.no;
view
视图是虚拟表,操作视图其实数据还是在对应的表中,使用视图最大的好处可以简化select语句代码
不是所有的账号都有权限创建视图
需要授权
1)切换到管理员账号sys
2) grant dba to 账户名;授权 回收权限revoke dba from 账号名;
创建视图
--create or replace view 视图名 as select语句 [with read only];
create or replace view vw_haha as select empno,ename,sal from emp where deptno=30 with read only;
--drop view 视图名; 删除视图
drop view vw_haha;
select * from vw_haha;
update 表名 set 字段=值 [,…] where 过滤行记录;
update vw_haha set sal=200 where empno = 7499;
实例:
grant dba to SCOTT;
--对每个部门中所有经理人求平均薪资,查询平均薪资最低的部门的部门民称
--所有的经理人
select distinct mgr from emp where mgr is not null;
--创建视图存储所有的经理人
create or replace view vw_mgr as select distinct mgr from emp where mgr is not null with read only;
select * from vw_mgr;
--对所有的经理人进行分组,查到部门编号,经理人的平均薪资
select deptno, avg(sal)
from emp
where empno in (select distinct mgr from emp where mgr is not null)
group by deptno;
--最低平均薪资
select min(avg(sal))
from emp
where empno in (select distinct mgr from emp where mgr is not null)
group by deptno;
--判断哪一个部门的薪资与最低平均薪资相等,获取到这个部门的部门编号
select deptno
from (select deptno, avg(sal) avg_sal
from emp
where empno in (select distinct mgr from emp where mgr is not null)
group by deptno)
where avg_sal =
(select min(avg(sal))
from emp
where empno in (select distinct mgr from emp where mgr is not null)
group by deptno);
--根据部门编号获取部门名称
select dname
from dept
where deptno =
(select deptno
from (select deptno, avg(sal) avg_sal
from emp
where empno in
(select distinct mgr from emp where mgr is not null)
group by deptno)
where avg_sal =
(select min(avg(sal))
from emp
where empno in
(select distinct mgr from emp where mgr is not null)
group by deptno));
--视图
select dname
from dept
where deptno = (select deptno
from (select deptno, avg(sal) avg_sal
from emp
where empno in (select * from vw_mgr)
group by deptno)
where avg_sal = (select min(avg(sal))
from emp
where empno in (select * from vw_mgr)
group by deptno));
是数据库的对象之一,是透明的,有没有索引,sql都一样
大量的数据的查询,如果数据量比较小,如果不是大量做查询,二十大量的执行增删改的操作,反而会降低效率,因为需要维护索引
可以比作字典的目录
create index 索引名 on表名 (字段列表…)
drop index 索引名
遵循三范式
表的名称, 字段,类型,要求(约束)
约束
–创建表 和添加约束
–先创建表,后续为这个表的什么字段添加约束
–创建表的同时添加约束
–创建表 字段后直接添加约束 1)默认的约束名 2)指定约束名
– 字段定义完成之后,统一为不同的字段的添加约束
–DDL 定义语句 创建表 create 删除表drop 修改alter
–创建表
–create table 表名(
–字段名 类型 约束,
–字段名 类型 约束,
– 字段名 类型 ,
–约束;
–约束…
–)
–追加约束