常用sql总结

一、查询常用sql总结(表结构是基于oracle中自带的表)

 1.基本的sql查询语句

select * from emp;
select empno,ename from emp;

 2.模糊查询

1.%表示的是任意多个字符
  select
* from emp where ENAME Like '%A%'; --名字中有A

2._表示的是任一一个字符
select * from emp where ENAME Like '_M%'; --第二个字母是M的

3.带有分组函数的查询

  5个常用的分组函数count()  min()  max()  sum()  avg()

select e.deptno,count(empno)num from emp e  group by e.deptno having e.deptno=30

     注意:1.所有包含在select列表不再组函数中的字段都必须在GROUP BY中
             2.group by可以和where搭配,where只能在group by前面,where子句中不能包含任何组函数。

select job from emp where sal=(select max(sal) from emp) group by job; --此处where子句不能直接用组函数,只能使用子查询

4.多表连接查询(复杂查询)

 a.内连接(等值连接)

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

 b.外连接(不满足条件的数据显示出来)

    右外连接(把join右边表的数据显示出来)

方式一:(oracle中特有的)
select
e.ename,d.deptno from emp e,dept d where e.deptno(+)=d.deptno 方式二:(标准版的)
select e.ename,d.deptno from emp e right join dept d on e.deptno=d.deptno

    左外连接(把join左边表的数据显示出来)
    全外连接

5.子查询

单行子查询
     select 字段 from 表 where 字段 >[单行] (子查询)

多行子查询

     in ,all,any in:表示在一个区间内

     select * from emp where ename in (  select ename from emp where sal>2000 )

     any: > any:从子查询中取一个最小的,比其中最小的要大就可以了 < any:从子查询中取一个最大的,

     all >all :从子查询中取一个最大的 <all:从子查询中取一个最小的

6.行列倒置

select XX as '',
SUM(case when MONTH=1 then o else 0 end ) as '一月',
SUM(case when MONTH=2 then o else 0 end ) as '二月',
SUM(case when MONTH=3 then o else 0 end ) as '三月',
SUM(case when MONTH=4 then o else 0 end ) as '四月',
SUM(case when MONTH=5 then o else 0 end ) as '五月',
SUM(case when MONTH=6 then o else 0 end ) as '六月',
SUM(case when MONTH=7 then o else 0 end ) as '七月',
SUM(case when MONTH=8 then o else 0 end ) as '八月',
SUM(case when MONTH=9 then o else 0 end ) as '九月',
SUM(case when MONTH=10 then o else 0 end ) as '十月',
SUM(case when MONTH=11 then o else 0 end ) as '十一月',
SUM(case when MONTH=12 then o else 0 end ) as '十二月'
from XX
group by year

二、DML语句

插入

insert into EMP(EMPNO,ENAME,JOB,SAL) values(1234,'ZHANGSHAN','MANAGER',8000)

修改

update emp set ename='zhangshan',job='manager' where empno=7369

删除

delete from emp where empno=1234

删除和修改中也可以嵌套子查询

三、oracle数据对象操作(表,视图,序列)

   1.创建表

方式一:                                                                     方式二:
create
table person( create table person1 id number(18),                                    as name varchar2(5), select * from person age number(3), sex varchar2(4) );

 2.删除表

方式一:只会删除表中的内容,不会删除表结构truncate  delete                         方式二:删除表结构
truncate
table person drop table person
delete from person where id=123

 3.修改表

alter table
A.追加列
alter table person add (address varchar2(20) default 'shanghai')

B.修改列
alter table person modify (age number(6))
使用modify可以修改数据类型,长度,默认值
请注意:对默认值的修改,只对后面的数据才有效

C.删除列
alter table person drop column age
原文地址:https://www.cnblogs.com/coderising/p/5766803.html