Oracle学习笔记(一)

一、访问方式
      (1)命令行
      (2)sqlplus客户端
      (3)浏览器(http://127.0.0.1:5560/isqlplus/,如果不能访问,则isqlplusctl start)
二、用户解锁:
      登录:sqlplus sys/密码 as sysdba;
      解锁:alert user scott account unlock;
三、sql语言:    
      sql1992 structured query language 结构化查询语言
      sql1999两套不同的语言
      机器(第一代语言),汇编(第二代语言),java c++(第三代语言)  sql语言(第四代语言,无条件,循环,分支)
四、scott中的表
     emp(雇员信息): comm津贴  job工种  mgr经理人 

     dept(部门信息)
     salgrade(薪水等级信息):grade local hisal

五、select语句

      1、基本的sql语句

         

             select *from emp;
             select ename,sal*12 from emp;
              dual表:只有一个字段
         select 2*3 from dual:算出的只有一个结果
         select sysdate from dual:系统日期
         select ename, sal*12 anuual_sal from emp;
             //起别名,别名中有特殊字符,则加双引号,双引号保持住原有的格式
         select enmae,comm from emp;好多人没有津贴,0和空值不一样。0是有值
         select ename,sal*12+comm from emp;//算出有的是空值,任何含有空值的表达式结果都是空值
         select ename||sal from emp; 相当于java中的字符串+
             select ename||'asfasdfasdf' from emp;
             select ename|| 'asfasdfa''asdfasdf' from emp 正确语句
         select distinct deptno from emp;
             select distinct deptno,job from emp; 这两个在一起的组合是重复的,就把它去掉

    2、带where的sql语句

          

      select *from emp where deptno=10;
      select *from emp where ename='CLARK'
      select ename,sal from emp where sal>1500;
      select ename,sal,deptno from emp where deptno<>10;不等于
    select ename,sal from emp where ename>'CBA'
      select ename,sal from emp where sal between 800 and 1500;
      select ename,sal from emp where sal>= 800 and sal<1500;
      select ename,sal,comm from emp where comm is (not) null;
      select ename,sal comm from emp where sal in (800,1500,2000);
      select ename,sal,comm from emp where ename in ('smith','king','abc');
      select ename,sal,hiredate from emp where hiredate>'20-2月-81';
      select ename,sal from emp where deptno=10 and sal>1000;      
      select ename,sal from emp where deptno=10 or sal>1000; 
      select ename,sal from emp where sal not in (800,1500);
      select ename from emp where ename like '%ALL%'; %多个
    select ename from emp where ename like '_A%';  _一个
    select ename from emp where ename like '%\%%'; 转义字符
    select ename from emp where ename like '%$%%' escape '$'; //$是转义字符
    select *from dept;//见这个表的顺序
    select *from dept order by deptno desc;
      select empno,ename from emp order by empno asc;不写默认按升序排列
    select empno,enmae form emp where deptno<>10 order by empno asc;
      select ename,sal,deptno from emp order by deptno asc,ename desc; deptno相同的情况下排列ename
      select ename,sal*12 annal_sal form emp where ename not like '_A%' and sal>800 order by sal desc;

   3、sql函数

          3.1单行函数

           

select lower(ename) from emp;
select ename from emp where lower(emane) like '_a%' 
select ename from emp where ename like '_a%' or ename like '_A%';
select substring(ename,1,3) from emp 
select char(65) from dual;//acsci转换为字符
select asci('A') form dual;
select round(23.652) from dual 24
select round(23.652,2) from dual 23.65
select round(23.652,-1) from dual 20 
select to_char(sal,'$99,999.9999') from emp; 
    //9代表一位数字,没有就不显示
select to_char(sal,'L99,999.9999') from emp;
select to_char(sal,'$00,000.0000') from emp;没有就显示0
select to_char(hiredate,'YYYY-MM-DD HH:MI:SS') from emp;
select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from emp;
select ename,hiredate from emp where hiredate>to_date('1981-2-20 12:34:56','YYYY-MM-DD HH24:MI:SS');
select sal from emp where sal>to_number('$1,250.00','$9,999.99');
select ename,sal*12 +nvl(comm,0) from emp;

       3.2  多行函数

  

select max(sal) from emp; 
select min(sal) from emp; 
select avg(sal) from emp; 
select to_char(avg(sal),'99999999.99')from emp; 
select round(avg(sal),2)from emp;   
select sum(sal) from emp;
select count(*) from emp;
select count(*) from emp where empno=10;
select count(comm) from emp  这个字段如果不是空值,就算一个
select count(distinct deptno) from emp;

 4、group by语句

        

//每个部门的平均薪水 意思是分组之后再平均
select deptno,avg(sal) from emp group by deptno;分组之后再平均
//按照两个字段的组合进行分组,组合一样分为一组 
select deptno ,job, max(sal) from emp group by deptno,job;          
//薪水值最高的那个薪水的那个人的名字
select ename from emp where sal=(select max(sal) from emp);
//每一个部门里头挣钱最多的那个人的名字
 select deptno,max(sal) from emp group by deptno;

 5、having语句

          

//对分组进行限制  where对单条记录的进行过滤, where后进行分组
//平均薪水大于2000的组取出来;
select avg(sal),deptno form emp group by deptno having avg(sal)>2000; 
//完整sql语句的执行顺序
 from--> where-->  group by-->  having-->  order by   
//示例句子         
select deptno avg(sal) from emp where sal>1200 group by deptno having avg(sal)>1500 order by avg(sal) desc;

 6 子查询     

       

//谁挣得钱最多
select ename,sal from emp where sal=(select max(sal) from emp);         
// 求出哪些人的工资位于所有人的平均工资之上
select ename from emp where sal>(select avg(sal) from emp);
//求出按部门分组之后每个部门里挣钱最多的那个人。    
select max(sal),deptno from emp group by deptno;一张表
select ename ,sal from emp join (select max(sax) max_sal,deptno from emp group by deptno) t
on(emp.sal=t.max(say) and emp.deptno=t.deptno);//没问题
//某人的名字和他的经理人的名字求出来
select e1.ename,e2.ename from emp e1,emp e2 where e1.mgr=e2.empno;//自连接

 

你可能感兴趣的:(oracle,sql,浏览器)