oracle的一些基本语法和一些优化问题
2010-01-22 19:11
337 查看
查询:
select distinct(empno) , name , age from t_person t where (t.age between 21 and 25 or name like '%王%') and
empno like '32%';
更新:
update t_person set name = 'ww' where empno = '321654';
update t_person set(empno,name,age) = (select empno,name,age from t_person1 where ...)
插入:
insert into t_person values(seq_person.nextval,'ww',21); -- seq_person.currval 当前序列中的值
insert into t_person(empno,name) values('321654','ww');
删除:
delete t_person <==> delete from t_person
创建表:
create table t_person (
empno varchar2(6),
name varchar2(20),
age int ,
constraint pk_person primary key(empno)
);
comment on table t_person is '员工表';
comment on column empno is '员工编号';
comment on column name is '员工姓名';
comment on column age is '员工年龄';
create table t_person as select * from t_person1 where 1<>1; 只复制表结构
create table t_person as select * from t_person1 where 1=1; 复制表结构和数据(拷贝),但除了非空约束能被复制,其它的约束都不能拷贝。
创建视图:
create view v_person as select * from t_person t left join t_person1 m on t.empno = m.empno where ...;
截取字符串:
select substr('wangwei',2,3) from dual; --从第二位开始截取取三个字符(注意:数据库中的是从1开始的,而不是0)
dbms_output.put_line :ang
计算长度的函数:
select length('wangwei') from dual; --dbms_output.put_line:7
计算总量的函数:
select count(1) from t_person; -- 此语句的执行效率比非索引字段的效率低
select count(*) from t_person; -- 此语句的执行效率比非索引字段的效率低,比第一个的效率低一些。
select count(name) from t_person; -- 此语句的执行效率是最低的,前提条件是name字段不是索引。
select count(empno) from t_person; --此语句的执行效率最高,前提是empno字段是索引。
在拼接sql语句的时候,一定要注意字符串和空格的问题,比如:
String sql = "select * from t_person t where t.name = '"+name + "' order by empno";
转换函数:
select decode(xb,1,'男',2'女',0,'不详') from t_person ;
select decode(xb,1,'男','女') from t_person;
select (case when xb = '1' then '男' when xb = '2' then '女' else '不详' end) as xb from t_person;
select (case xb when '1' then '男' when '2' then '女' else '不详' end) xb from t_person;
优化问题:
from 后面的where前面的执行顺序是从右往左的,所以选择表中数据量比较小的作为基表放在最后面,依此类推;
where 后面的过滤条件是从左往右开始的,所以一般将过滤范围小的放在前面,依此类推。
group by (分组) 一般select后from前为组函数,或者在group by 之后出现的字段,可以出现在select后;group by 在where之前执行,即先分组在过滤。
having 一般与group by 联用,用于过滤分组中的数据。
order by 在执行完所有的过滤之后得到的结果排序。(desc 降序,asc 升序)
like 匹配函数: 如果试用下面的例子就完全用不到索引了
select * from t_person where name like '%王%'
但如果使用下面的写法效率就会高些
select * from t_person where empno like '32____'
select * from t_person where empno like '32%'
exist , not exist , in , not in
select distinct(empno) , name , age from t_person t where (t.age between 21 and 25 or name like '%王%') and
empno like '32%';
更新:
update t_person set name = 'ww' where empno = '321654';
update t_person set(empno,name,age) = (select empno,name,age from t_person1 where ...)
插入:
insert into t_person values(seq_person.nextval,'ww',21); -- seq_person.currval 当前序列中的值
insert into t_person(empno,name) values('321654','ww');
删除:
delete t_person <==> delete from t_person
创建表:
create table t_person (
empno varchar2(6),
name varchar2(20),
age int ,
constraint pk_person primary key(empno)
);
comment on table t_person is '员工表';
comment on column empno is '员工编号';
comment on column name is '员工姓名';
comment on column age is '员工年龄';
create table t_person as select * from t_person1 where 1<>1; 只复制表结构
create table t_person as select * from t_person1 where 1=1; 复制表结构和数据(拷贝),但除了非空约束能被复制,其它的约束都不能拷贝。
创建视图:
create view v_person as select * from t_person t left join t_person1 m on t.empno = m.empno where ...;
截取字符串:
select substr('wangwei',2,3) from dual; --从第二位开始截取取三个字符(注意:数据库中的是从1开始的,而不是0)
dbms_output.put_line :ang
计算长度的函数:
select length('wangwei') from dual; --dbms_output.put_line:7
计算总量的函数:
select count(1) from t_person; -- 此语句的执行效率比非索引字段的效率低
select count(*) from t_person; -- 此语句的执行效率比非索引字段的效率低,比第一个的效率低一些。
select count(name) from t_person; -- 此语句的执行效率是最低的,前提条件是name字段不是索引。
select count(empno) from t_person; --此语句的执行效率最高,前提是empno字段是索引。
在拼接sql语句的时候,一定要注意字符串和空格的问题,比如:
String sql = "select * from t_person t where t.name = '"+name + "' order by empno";
转换函数:
select decode(xb,1,'男',2'女',0,'不详') from t_person ;
select decode(xb,1,'男','女') from t_person;
select (case when xb = '1' then '男' when xb = '2' then '女' else '不详' end) as xb from t_person;
select (case xb when '1' then '男' when '2' then '女' else '不详' end) xb from t_person;
优化问题:
from 后面的where前面的执行顺序是从右往左的,所以选择表中数据量比较小的作为基表放在最后面,依此类推;
where 后面的过滤条件是从左往右开始的,所以一般将过滤范围小的放在前面,依此类推。
group by (分组) 一般select后from前为组函数,或者在group by 之后出现的字段,可以出现在select后;group by 在where之前执行,即先分组在过滤。
having 一般与group by 联用,用于过滤分组中的数据。
order by 在执行完所有的过滤之后得到的结果排序。(desc 降序,asc 升序)
like 匹配函数: 如果试用下面的例子就完全用不到索引了
select * from t_person where name like '%王%'
但如果使用下面的写法效率就会高些
select * from t_person where empno like '32____'
select * from t_person where empno like '32%'
exist , not exist , in , not in
相关文章推荐
- Oracle中的一些基本建议优化原则和NULL值
- oracle操作的一些基本语法
- oracle的一些基本语法
- 非原创,看到以后发现自己在很多时候都忽略了这个问题一些基本的SQL优化
- MySQL 和 Oracle SQL的一些基本语法和函数
- Oracle里的一些基本语法
- 非原创,看到以后发现自己在很多时候都忽略了这个问题一些基本的SQL优化
- Oracle存储过程基本语法
- shell进阶之路的基本用法知识和问题解决(基本语法、正则、日期转化、&符号)
- Oracle基本语法
- 图像处理中的一些基本问题解释
- Java基本类型的一些问题探讨
- Oracle学习(1):基本语法
- Oracle 11g 开始使用时遇到的一些问题及解决。
- Oracle 基本知识及问题解决
- oracle 存储过程的基本语法
- 最近oracle开发中遇到的一些问题及解决办法(二)待续
- Oracle存储过程基本语法
- oracle 存储过程的基本语法和注意
- SQL Server 连接时的一些基本问题