数据库基础入门

2019-04-17  本文已影响0人  倒霉的小当家

首先是一些最基本的语句,

从select开始,数据库征服之旅。

以下是一些课堂笔记:
select e.ename,e.sal*12 yearsal from emp e;

select e.ename,e.sal*12 "yearsal" from emp e;  --"确定要这样命名"

select * from emp;

select distinct ename,job from emp;

select * from emp where sal <=5000;

select * from emp where sal =any(1600,1000,1500,2000);  --满足其中任意一条就可筛选出来。  或

select * from emp where sal =all(1600,1000);  --满足所有的条件。  与

select * from emp where job in ('SALESMAN','MANAGER');-- 或 

select * from emp where job =any ('SALESMAN','MANAGER'); --同上

select * from emp where ename='zzw';

select * from emp;

select * from emp where job is not null;

select * from emp where sal between 801 and 1499;  --包含关系

select * from emp e where exists(select 1 from emp);

select * from emp e where exists(select * from emp);  --exists 用于判断两表间的列项比较

select * from emp where ename like '%S%';  其中%表示0个或若干个字符。_表示一个字符

网上搜集的关于SQL关键字的区别:

           一些><=!=比较符号,不能搭配 null使用,否则只会返回false 。例如 =null,null 的搭配为

    is null  ,is not null.

        exist 和 null的区别:

                exists和in的使用方式:  

对B查询涉及id,使用索引,故B表效率高,可用大表 -->外小内大

select * from A where exists (select * from B where A.id=B.id);

对A查询涉及id,使用索引,故A表效率高,可用大表 -->外大内小

select * from A where A.id in (select id from B);

  1、exists是对外表做loop循环,每次loop循环再对内表(子查询)进行查询,那么因为对内表的查询使用的索引(内表效率高,故可用大表),而外表有多大都需要遍历,不可避免(尽量用小表),故内表大的使用exists,可加快效率;

  2、in是把外表和内表做hash连接,先查询内表,再把内表结果与外表匹配,对外表使用索引(外表效率高,可用大表),而内表多大都需要查询,不可避免,故外表大的使用in,可加快效率。

  3、如果用not in ,则是内外表都全表扫描,无索引,效率低,可考虑使用not exists,也可使用A left join B on A.id=B.id where B.id is null 进行优化。

根据上述描述对以上做了一个表格来比较:

        

    今天的学习暂时到这里,明天还有= = !

上一篇 下一篇

猜你喜欢

热点阅读