模糊查询,分页,序列

关于模糊查询, 被查询的字符串不能是  双引号 引起来的。只能是 单引号,否则会不执行的。

 

Select * from emp where name like '%A%' 这里面 是区分 单双引号的。在执行模糊查询的时候不能使用 单引号。

 

关于 oracle 数据库,查询的语句不区分大小写,也就是不管大小写都可以识别的。但是对于查询的内容来讲,如果是大写 就是 大写,如果是小写的话,就是小写了。

 

Orcl里面 and的 优先级 要高于 or

所以 下面两句话的执行结果 是不相同的。

 

 

全角符号查出来的 竟然是这么一个 结果。

没有报错 倒是。

应该能看出来 长度不一样。

 

 

 

 

 

 

 

-- 按照年薪排序:

select empno,ename,(sal+nvl(comm,0))*12 as 年薪 from emp

order by 年薪;

-- 可以这样执行的原因: 

-- 先执行 from 

-- 然后是 where 

-- 再然后是: select 

-- 最后是 : order by 

-- 所以 在 select 起完别名以后,再次调用 order by 是可以执行的。

 

 

 

 

 

 

-- 虽然知道这是笛卡尔积现象,但是好像还蛮难处理的。最起码单独执行下面这条语句的时候,没有效果。

-- 需要 点击 fetch next page 来展示下14个【并不知道这里为什么是14.】的结果,需要 点击fetch last page 来展示所有的笛卡尔积结果。

-- 应该展示60条结果。

 

 

-- 关联查询

-- 内连接

select * from emp e,dept d where e.deptno = d.deptno;

select * from emp e inner join dept d on d.deptno = e.deptno;

select * from emp e join dept d on d.deptno = e.deptno;

-- 左外链接  (不行完全解释不了 就是 每页只显示14条记录。然后利用 sql命令行 那个 对这个 并不起作用。)

-- 好想知道 是哪里的问题了,就是 下面这个 框框的缘故,够显示多少条 就显示多少条,不够的话,就需要点击 fetch next page 或者 fetch end page。来显示所有。

-- 我的 这个屏幕下方就是 14条数据的地方,所以帮我制定了 每次 加载 下一页 也只是 14条。的缘故

select * from emp e left join dept d on e.deptno = d.deptno;

-- 右外链接 

select * from emp e right join dept d on e.deptno = d.deptno;

-- 全连接

select * from emp e full join dept d on e.deptno = d.deptno;

-- sql server 里面可能是 这样两种方式 表达左外链接和右外链接:

-- select * from emp e left join dept d on e.deptno * = d.deptno; 左

-- select * from emp e left join dept d on e.deptno = * d.deptno; 右

 

-- 自连接查询,这个 本科用于我的 二期项目。 一直 没想到这回事。 

-- 实质是: 把一张表 当做两张来用

-- 看到表发现 有mgr这一列 表示 当前员工的上级。

-- 所以 显示 所有员工以及他的上级:

 

-- 不加as也可以

select e1.empno as 编号,e1.ename 员工,e2.empno 编号,e2.ename as 领导   from emp e1,emp e2 

 

 

 

区别:

在 orcl 里面 对于 字符串的 内容的表达 要使用 '' 单引号 引起来。 与次一脉相承的是 模糊查询都要使用  ' '  单引号。

起别名的时候 要使用 "" 双引号 引起来。

 

在 mysql中, 单引号 双引号   不写 貌似 都可以 用来表示 别名,对于字符串的表达 单引号 和 双引号 都是可以的

 

--  分页查询

select * from emp;

-- orcl系统 里面提供了一个 rownum的一项

-- 如果 带上系统的这个数值的话,就一定要注意,给表格起别名。

select e.*,rownum from emp e;

-- 为了看到显示效果,在按 工资一下排序

select e.*,rownum from emp e order by sal desc;

-- 这里 这个 排序 好像跟 主键有关系 跟 这个 工资 没有关系,或者说 按照 工资排序的话 这个东西是乱的。所以先按照 empno 排一下序

select e.*,rownum from emp e order by empno desc;

-- 显示 头 5条:

-- 未知的 列 rn,我们在回顾一遍orcl语法的执行顺序: 先from 再 where 在 select ,那这样的话 bug在于,在起别名之前 rn 还不存在,所以 会爆出一个 位置的 rn这样一个错误

select e.*,rownum rn from emp e where rn<5 order by sal desc ;

 

-- 这样 就显示了前面四条

select a.* from (select e.*,rownum rn from emp e order by empno desc) a where a.rn<5;

 

select a.* from (select e.*,rownum rn from emp e order by empno desc) a where a.rn<6;

-- 流传最广

select a.* from (select e.*,rownum rn from emp e order by empno desc) a where a.rn>5 and a.rn <=10;

 

-- 比较烧脑:

select a.* from (select e.*,rownum rn from emp e where rownum<=10 order by empno desc) a where a.rn>5;

-- 这个顺序不能颠倒 这个 rownum并不真实存在。所以 当我们制定 rownum>5的话,这个数 会无限制的走下去,所以 无论怎样都不可写rownum> 这个 条件。否则就没有结果。

select a.* from (select e.*,rownum rn from emp e where rownum>5 order by empno desc) a where a.rn<11;

-- 写的时候 只能是 先写小于

select a.* from (select e.*,rownum rn from emp e where rownum>3 order by empno desc) a where a.rn<6;

索引的概念

 

索引 提高查询的效率。

对于主键来讲,默认带有索引。

通常情况下 数据记录条数大于10w条时,索引的效果是明显的。

单列索引:

 

序列自增完整版 :

-- 尝试一个 完整过程:

create table testsequence_tb(

       testsequence_id number,

       testsequence_name varchar2(20)

)

 

-- 这几个单词 之间貌似 是可以无序的。

create sequence testsequence_sequence

start with 1

increment by 1

minvalue 1

nomaxvalue

cache 10

order;

 

create or replace trigger testsequence_tb_insert

before insert on testsequence_tb

for each row

  begin 

    select testsequence_sequence.nextval into:New.testsequence_id from dual;

  end;

 

insert into testsequence_tb (testsequence_name) values ('测试');

 

select * from testsequence_tb;

 

复习:

1、索引 提高查询效率   数据库很大的时候 : 字段多  元组多

2、每一张表都至少有一个主键   一个主键索引

3、单列索引 符合索引(多列索引) 经常会被用作条件的字段。Where条件,链接字段

4、索引层次不宜过多,4层为限

5、删除索引 drop index 索引名称【大小写均可】

6、视图: 减少代码量(提高对代码的复用),可以对权限进行一定的管理

7、Create view  viewName as 查询语句   好像(这个 权限还比较特殊)

8、Drop view 删除视图

9、序列 计数器 select emp_sequence.nextval from dual;

Create sequence testsequence_sequence

Start with 1

Increment by 1

Minvalue 1

Nomaxvalue

Cache 10

Order 

 

Create sequence testsequence_tb_insert

Before insert to testsequence_tb

Begin

Select testsequence_sequence.nextval fromdual;

End;

 

Insert into testsequence_tb values ('wangwang');

 

10、触发器 我们就可以 对一张表进行逐渐自增

11、Pl/sql编程

程序块

 

显示 hello world

 

 存储过程通道,这个是原来jdbc里面的一个事儿,原来在jdbc里面说过有三种通道,状态通道,预状态通道,存储过程通道。在oracle里面有这三种通道,而在mysql里面就只有 前两种,因为 这就相当于一部分逻辑写在了数据库里面,对于一些大型公司的重要逻辑都是放在数据库里面的,甚至连java形式的都不是。

 

posted on 2016-02-10 11:32  木鸟飞  阅读(1302)  评论(0编辑  收藏  举报

导航