MySQL和Oracle的区别
-
MySQL一般采用自动增长类型,MySQL每张表只能有一个自增字段,这个自增字段既可以是主键也可以是非主键,要注意的是把自增字段作为非主键的时候必须为其添加唯一索引,一般都把自增字段作为主键。Oracle 中序列采用sequence, CREATE SEQUENCE 序列号的名称。
-
引号的处理
MySQL中单引号,双引号都可以使用,Oracle中只能用单引号。 -
dual虚拟表的使用
MySQL中没有dual表,直接select 1即可。Oracle中要满足select from结构,就引入了虚拟表dual。 -
分页的处理
MySQL用limit进行分页,limit接收两个参数,第一个参数表示偏移量,第二个参数表示返回的条数,初始记录行的偏移量是 0 。
select * from table limit 5,10; 返回第6-15条数据
select * from table limit 5,-1; 返回第5条到最后
select * from table limit 5; 返回前5条记录行
Oracle利用rownum进行分页。
select e.*,rownum from emp e where rownum<=10 and rownum>5; 会报错
因为sql先运行where语句,rownum从1开始,所以>5的条件永远为假。
查询6-10条数据
第一种方式:
select * from
(
select a.*, rownum as rn from css_bl_view a
where capture_phone_num = '(1) 925-4604800'
) b
where b.rn between 6 and 10;
第二种实现方式:
select * from css_bl_view a
where capture_phone_num = '(1) 925-4604800'
and rownum <=10
minus
select * from css_bl_view a
where capture_phone_num = '(1) 925-4604800'
and rownum <= 5
第三种实现方式:
select * from
(
select a.*, rownum as rn from css_bl_view a
where capture_phone_num = '(1) 925-4604800'
and rownum <= 10
) b
where b.rn > 5;
- 不能对rownum与大于1的数值进行>,>=,=操作,要想进行rownum>1之类的操作,必须通过子查询
select count(*) from css_bl_view a
where rownum > 1; 错误
正确示例:
select object_id,object_name
from (select object_id,object_name, rownum as rn from t_test1)
where rn > 1;
- rownum和order by
在使用rownum是,只有order by的字段是主键时,查询结果才会先排序再计算rownum,但如果排序的字段是非主键时,就会先计算rownum再进行排序。 - rownum只是一个伪列
select rownum from emp; 不报错
select e.rownum from emp e; 报错
select e.*,rownum from emp e; 不报错
select *,rownum from emp; 报错