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();

}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章