日期:2014-05-16  浏览次数:20433 次

oracle常用数据操纵语句

1 group by 中 having 子句与where 子句的区别在于:
where子句搜索条件在进行分组操作之前应用;而having子句搜索条件在进行分组操作之后应用。
having 语法与where 语法类似,但是having可以包含聚合函数。

2  连接查询有三种:内连接、外连接、交叉连接

内连接 INNER JOIN 
   * 在创建内连接时,包含空值的列不与任何值匹配,因此不包含在结果集中。空值不与其他的空值匹配。
 
外连接有三种:左向外连接(左连接)、右向外连接(右连接)、完整外部连接
左连接:left outer join 或left join   **以左边表为主
右连接:right outer join 或 right join  ** 以 右边表为主
完全外部连接 相当于左连接和右连接的并集

交叉连接:两表中的任意两行都可以组合,不需要使用on子句指定表之间的连接关系

3  exists 关键字与子查询
   下面两个语句一样效果
   (1)select emp_name,title,wage from hrman.employees e
      where exists
     (select dept_id form hrman.departments d where e.dept_id=d.dept_id and d.dept_name='人事部');
   (2)select emp_name,title,wage from hrman.employees e
      where e.dept_id in
      (select dept_id from hrman.departments d
       where e.dept_id=d.dept_id and d.dept_name='人事部');

 

4 union 关键字可以组合两个查询的结果集,结合的基本规则如下:
   a.所有查询中的列数和列的顺序必须相同
   b.数据类型必须兼容

 ** 在使用union关键字进行合并查询时,数据库引擎会自动过滤掉结果集中的重复记录。 
 使用union all 关键字不会过滤重复数据,其执行效率会更高一些


5 decode 
select emp_name, decode(sex,'男','先生','女','女士','未知') as sex from employees;

6. case函数可以实现与decode函数相同的功能
select emp_name, case sex when '男' then '先生' when '女' then'女士' else '未知' end as sex from employees;
其他用法:
select emp_name, wage,
case when wage <=3000 then '低'
when wage >3000 and wage<5000 then '中' 
when wage >=5000 then '高' end as grage from employees;

7 保存查询结果
create table table_name as select ... from ... where ...;

8  设置默认值 alter table employees ADD inputDate date DEFAULT(sysdate);


9  设置唯一性 alter table employees ADD CONSTRAINT UK_EMPNAME UNIQUE(emp_name);

10 修改数据时不能违反检查约束
设置约束条件 alter table employees ADD CONSTRAINT CK_EMPWAGE CHECK(WAGE >0);

11 修改数据时不能违反外键约束
alter table employees ADD CONSTRAINT FK_EMP_DEPTID FOREIGN(DEPT_ID) REFERENCES DEPARTMENTS(DEPT_ID);

12 删除数据语句
   delete from table_name where condition
   truncate table table_name;

 
 

1楼a4355791456天前 01:19
有没有更全面一点的?