oracle开发学习小结(待补充)

 

1.Oracle  PRELUDE(前奏)

    windows+RàcmdSTART LISTENERlsnrctl  start  [listener  name]

 

                        START SERVICEoradim  –starup  –sid  orcl

                        SYS LOGIN sqlplus  /  as  sysdba;

         控制面板à管理工具à服务àOracleOraDb11g_home2TNListenerà启动

                                 OracleServiceORCLà启动    

2.sys  LOGIN

window+Ràsqlplus: 

          FIRST  LOGIN : sqlplus(with  out  ‘conn’  or  ‘;’)

     Super  usersys/ysjian  as  sysdba (  / as  sysdba )

                Nomal  userscott/tigger

                Switch user  to  loginconn  sys/ysjian  as  sysdba;

3.The  Operations  following  of  super  user

       CRARTE USERcreate  user  ysjian  indetified  by  ysjian //创建用户ysjian/ysjian

       ALTER PASSWORDalter  user  ysjian  identified  by  000 //更改秘密 

       ALTER STATE OF USERalter  user  scott  account  unlock //scott用户解锁

                            alter  user  scott  account  lock //scott用户加锁

                            show  user; //显示当前用户,任何用户都可以用

                            select * from  tab; //查询当前用户的所有的表

                            desc  tableName;//查询表结构

                            select * from  v$nls_parameters; //查询系统参数

                            drop user ysjian cascade; //强制删除,有数据也可以删除

                            disconn //断开连接

         GRANT  RIGHT(授权):

         System right(系统权限)

         EXPRESSION:  grant  权限名称  to  用户名

                      revoke  权限名称  from  用户名

grant create session,create table,create sequence  to ysjian

           grant  connect  to  ysjian  //用户ysjian可以登录

           grant  resource  to  ysjian//用户ysjian可以使用数据库相关资源

           grant  unlimited  tablespace  to  ysjian //用户ysjian具有不受限表空间                   grant  create  any  table  to public  // 所有用户有创建表的权限

            //用户ysjian  //可以传递此权限

           grant  alter  any  table  to  ysjian  with  admin  optio

            //用户ysjian可以查询scott用户emp   

            grant  select  on  scott.emp  to  ysjian

          Object  right(对象权限)

           conn  scott/tigger; //用户scott登录

           grant select on emp to ysjian; //将对emp的查询权限给了ysjian

grant insert on emp to ysjian;

grant update on emp to ysjian;

grant delete on emp to ysjian;

grant all on emp to ysjian; //将对emp的所有权限给了ysjian

grant update(ename) on emp to ysjian; //将对empname字段更新 的权限授予ysjian

grant insert(id) on emp to ysjian;

revoke all on mytable from wangwu; //ysjianemp的所有权限撤销

//用户ysjian可以讲对scotte  mp表的查询功能传递下去

grant select on emp to ysjian with grant option;

4.DICTIONARY OF DATA

    Select * from  user_sys_privs; //查询当前用户拥有的系统权限

    Select * from  user_tab_privs;// 查询当前用户拥有的对象权限

    Select * from  user_col_privs;//查询当前用户控制到列的权限

5.BASIC  QUERY (基本查询)

    conn  scott/tigger;  the  following  operations  is  done  by  scott

    //1.查询scott所有的表

Select * from tab;

//2.查询emp表所有的记录

Select * from emp;

    //3.查询emp表的empnoenameljobsal

Select empno,ename,job,sal from emp;

//4. 查询emp表的empnoenameljobsal,并起别名

Select empno as 编号,ename as 姓名,job as 工作,sal as 薪水 from emp;

//5.查询emp中的job,并去除重复项,用关键字distinct

Select distinct job from emp;

//6.查询enpmopenamejob,显示效果:编号:7369,姓名:Smith

Select ‘编号:’||empno,’姓名:’||ename,’工作:’||job from emp;

//7.查询员工的编号,姓名,工作和年薪,空值判断nvl(comm,0)

Select empno,ename,job,(sal+nvl(comm,0))*12 from emp;;

//8.查询工资大于1500的雇员

Select * from emp where sal>1500;

//9.查询能得到奖金的所有雇员

Select * from emp where comm is not null and comm>0;

//10.查询工资大于1500或可以得到奖金的所有雇员

Select * from emp where sal>1500 or (comm is not null and comm>0);

//11.查询工资大于1500并且可以得到奖金的雇员

Select * from emp where sal>1500 and comm is not null and comm>0;

//12.查询工资大于1500或者不可以领取奖金的雇员

Select * from emp where sal >1500 or comm is null or comm=0;

//13.查询工资在15003000只见所有的雇员

Select * from emp where sal between 1500 and 3000;

//14.查询在1981年雇佣的员工

Select * from emp where hiredate lile ‘%81’;

Select * from emp where hiredate between ’01-1-81 and ’01-1-82’;

//15.查询员工姓名中第二个字母为’M’的雇员

Select * from emp where ename like ‘_M%’;

//16.查询工资带8字的员工

Select * from emp where sal like ‘%8%’;

//17.查询编号是7396,7399,7521,7799的雇员信息

Select * from emp where empno in(7396,7399,7521,7799);

//18. 查询编号不7396,7399,7521,7799的雇员信息

Select * from emp where empno not in 7396,7399,7521,7799);

//19.查询员工编号不为7396的雇员

Select * from emp where empno <> 7396;

//20.查询员工,并按工资的升序排列(asc,默认)

Select * from emp order by sal asc;

//21.查询员工,并按工资的降序排列(desc)

Select * from emp order by sal desc;

//union:将两表记录合并,去点重复项

Select distinct deptno from emp union select deptno from dept;

//union all:将两表记录合并,不去除重复项

Select distinct deptno from emp union all select deptno form dept;

//intersect:取两个集合的交集

Select * from deptno from emp intersect select deptno from dept;

//minus:去掉交集

Select * from deptno from emp minus select deptno from dept;

 

6.ADVANCED  QUERY(高级查询)

    //1.聚合函数 count,sum,mix,max,avg

    Select count(*) from emp;

    //2.内连接(inner join)(两表数据的交集):查询员工的姓名,工资和隶属的部门名称

    Select ename ,sal,dname from emp,dept where emp.deptno = dept.deptno;

    Select ename,sal,dname from emp inner join dept on emp.deptno=dept.deptno;

//3.左连接(left join)(左表为主,右表没有填充null)

Select ename,sal,dname from emp left join dept on emp.deptno=dept.deptno;

Select ename,sal,dname from emp,dept where emp.deptno=dept.deptno(+);

    //4.右连接(right join)(右表为主,左表没有填充null)

    Select ename,sal,dname from emp right join dept on emp.deptno=dept.deptno;

    Select ename,sal.dname form emp,dept where emp.deptno(+)=dept.deptno;

    //5.全连接(full join)(左右表所有数据均出现,对方没有的用null填充)

    Select ename,sal,dname from emp full join dept on emp.deptno=dept.deptno;

    //6.交叉连接(cross join)(笛卡尔乘积)

    Select ename,sal,dname from emp cross join dept on emp.deptno=dept.deptno;

    //7.自然连接(natural join)(笛卡尔乘积)

    Select ename,sal,danme from emp natural join dept on emp.deptno=dept.deptno;

    //8.子查询,查询工资最高的员工的姓名,工资以及隶属的部门名称

Select emp.ename,emp.sal,dept.dname from emp,dept where

emp.deptno=dept.deptno and emp.sal= (select max(sal) from emp);

    //9分组函数(group by),查询平均工资大于2000的部门编号,having 出现在group by 后面

    Select deptno from emp group by deptno having avg(sal)>2000;

    //10.分组函数,查询平均工资大于2000的部门编号和部门名称

    Select deptno,dename from dept where deptno in(select deptno from emp group by

                                                 deptno having avg(sal)>2000)

select current_date from dual //日期函数,dualOracle中一张虚拟的表

select to_date(‘2013-1-24’,’yyyy-mm-dd’) from dual;//日期转换函数:24-1 -13

select to_char(hiredate,’yyyy-mm-dd’) from emp;//将日期转换成字符串

select to_number(‘10000’) from dual;//将字符串转换为数字

 

PAGE DIVIDSIOIN QUERY(分页查询,分页单位为3)

         第一页:rownum(1,2,3)

           Select * from emp where rownum <4;

         第二页:rownum(4,5,6)

            Select * from emp where rownum>3 and rownum < 7 //结果为空,没有记录

            Select * from (select rownum rn,emp.* from emp where rownum<7 ) where rn>3;

         第三页:rownum(7,8,9)

            Select * from(select rownum rn,emp.* from emp where rownum<10) where rn >6;

         n页:

            Select * from(select rownum rn,emp.* from emp where rownum<3*n+1) where

           rn>(n-1)*3

JDBC IN ORACLE(Oracle与数据库的连接)

  class JdbcInOracle{

     //实际开发中,下面发放是封装的,结果也是单独封装的

public void jdbc (){

           try{

               Class.forName(“oracle.jdbc.driver.OracleDriver”);

               String url = “jdbc:oracle:thin:@localhost:1521:orcl”;

               String username = “scott”;

               String password = “ysjian”;

               Connection conn = DriverManager.getConnection(url,username,password);

               String sql = “select * from emp where empno=?”;

               PreparedStatement pstm = conn.prepareStatement(sql);

               pstm.setObject(7396);

               ResultSet rs = pstm.executeQuery();

               if(rs.next()){

                   int deptno = rs.getInt(“deptno”);

                   String ename = rs.getString(“ename”);

}

}catch(ClassNotFoundException e){

         e.printStackTrace();

}catch(SQLException e){

         e.printStackTrace();

}

}

}

你可能感兴趣的:(oracle,开发,学习)