以简体中文显示日期结果:
alter session set nls_date_language=’SIMPLIFIED CHINESE’;
eg: select ename,hiredate from emp
ename hiredate
Smith 17-12月-08
以美国英语显示日期结果:
alert seesion set nls_date_language=’AMERICAN’;
以特定格式显示日期结果:
alter session set nls_date_formate=’YYYY”年”MM”月”DD”日” ’;
select ename,to_char(hiredate,’YYYY-MM-DD’) from emp;
当算术表达式中包含NULL时,其显示的结果也为空(NULL);
解决的方法:
1.NVL( COL ,0);
SELECT ename,sal, conn,sal+nvl(conn,0) from emp;
2.NVL(COL,表达式,)
SELECT ename,sal,conn,nvl(conn,sal+conn,sal) from emp;
nvl(conn,sal+conn,sal): 当conn为NULL时,则sal+conn的值为 sal
3,字符串的连接:
select ename || ‘’’’||’s job is ‘||job from emp;
eg: smith’s job is clerk;
4.CONCAT连接字符串:
select concat(concat(ename,’’’s salary is ‘),sal) from emp;
eg: smith’s salary is 12385,
5.大小写转换函数:
LOWER(), UPPER();
6.X IN(Y1,Y2,Y3) ó X=Y1 OR X=Y2 OR X=Y3
7.mgr is NULL
8. x Between a and b ó a<=x<=b
9.关于排序问题:
select ename,sal from emp order by sal ASC; ASC:升序
DESC:降序
可以按: 列别名,列位置,非选择列进行排序
Select ename,sal 工资 FROM emp order by 工资 DESC;
Select ename,sal from emp order by 2 desc;
Select ename,sal from emp order by job desc;
多列排序:
Select ename,deptno,sal from emp order by deptno asc,sal desc;
10.s数字函数:
round() 取得数字值的四舍五入结果
Select sum(sal), round(avg(sal)) from emp where deptno=20
Trunc():截断数字值到定位数
11.to_char函数转换数字数据:
select ename, to_char(sal,’L99999.99’) from salary
eg: ename salary
smith ¥2000.00
12.常用的数据函数:
AVG():取得列或表达式的平均值;
Sum():取得列或表达式的总和;
Count():取得总计行数;
Variance():取得列或表达式的方差
Stddev():取得列或表达式的标准差;
13.Select * from table
where….
Group by…
Having ….
Order by ….
>>如果选择列表同时包含有列,表达式和分组函数,那么列和表达式必需出现在GROUP BY子句中
14.关于子查询:
Select ename,sal,deptno from emp where deptno=(select deptno from emp where ename=’scott’);
Select ename,job,sal,deptno from emp where job in(select distinct job from emp where deptno=10);
Select ename,sal,deptno from emp where sal>all (select sal from emp where deptno=30);
Select ename,sal,deptno from emp where sal>any( select sal from emp where deptno=30);
多列表子查询:
Select ename,job,sal,deptno from emp where (deptno,job)=(select deptno,job from emp where ename=’SMITH’);
SELECT 和UPDATE中的子查询:
Select ename,sal,deptno from emp where sal>(select avg(sal) from emp where deptno=outer.deptno);
Update emp e set dept_name=(select dname from dept p where d.dept=e.deptno);
Drop table A;删除表的所有数据,而且也删除表的结构;
恢复被删除的表:
FLASHBACK TABLE A TO BEFORE DROP;
删除约束:
alter table emp drop primary key cascade;
禁止约束:
Alter table A disable constraint constraint_name;
视图:
执行DML操作的具体原则:
DELETE操作原则:
如果视图包含GROUP BY子句 ,分组函数,distinct 关键字和rownum,那么不能在该视图上执行delete操作;
INSERT: 如果视图包含GROUP BY子句 ,分组函数,distinct 关键字和rownum伪列及使用表达式所定义的列,或者在视图上包含视图基表的NOT NULL列,那么不能在该视图上执行INSERT 操作;
视图的建立:
create view emp_vu as select empno,ename,sal,job,deptno from emp;
建立只读视图:
create view emp_vu as select * from emp
with read only;
视图的维护:
修改视图:
create or replace view emp_vu(name,salary,title) as select ename,sal,job from emp;
重新编译视图:
ALTER VIEW emp_vu compile;
删除视图:
Drop view emp_vu;
游标的使用:
显式游标的属性:
%ISOPEN: 检测游标是否打开,
EG: IF NOT EMP_CURSOR%ISOPEN THEN
OPEN EMP_CURSOR;
END IF;
%ISFOUND:检测游标结果集是否存在数据;
%NOTFOUND:检测游标是否不存在数据;
EG: LOOP
FETCH emp_cursor INTO v_name,v_sal;
EXIT WHEN emp_cursor%NOTFOUND;
%ROWCOUNT:返回已提取的实际行数;
declare cursor emp_cursor is
select ename,sal from emp order by desc
emp_record emp_cursor%rowtype;
begin
open emp_cursor;
loop
fetch emp_cursor into emp_record;
exit when emp_cursor%notfound or
emp_cursor%rowcount>&n;
dbms_output.put_line(‘姓名:’||emp_record.name||’,工资:‘||emp_record.sal);
end loop;
close emp_cursor;
end;