天天看點

Oracle入門學習第一天

Oracle第一天

一學習目标

  1. Oracle介紹
  2. Oracle安裝
  3. Oracle體系結構
  4. 基本查詢
  5. 條件查詢
  6. 單行函數
  7. 多行函數

二、Oracle介紹

mysql關系型的資料庫 , 表于表的關系:外鍵 , 端口号:3306

redis:非關系型資料庫 ,key - value , 端口号:6379

Oracle :關系型的資料庫 , 端口号:1521

收費:19萬左右, 每年交服務費

      效率高, 安全
           

Oracle的版本:8I 9I 10G 11G 12C

職位:DBA(資料庫管理者) , javaEE開發工程師

三、Oracle安裝

1) 安裝虛拟機: 解壓到沒有中文的路徑之下

解壓到沒有中文的路徑下  -- xxx.vmx 檔案,輕按兩下打開
           

2) 在虛拟中安裝oracle資料庫

資料庫的密碼: orcl, 此密碼是為 sys ,system
           

3)配置虛拟網卡:必須配置靜态的ip位址

選擇是僅主機模式, 如果改變了ip位址:修改配置檔案,重新開機服務()
           

4)安裝plsql工具

安裝路徑絕對不能有:小括号,空格,特殊符号
           

5) 解決中文亂碼的問題

-- 環境變量的名稱: NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
-- 查詢服務端的密碼

select userenv('language') from dual;
           

四、Oracle體系結構

資料庫:隻有一個資料庫

執行個體:背景運作的一個程序

表空間:邏輯存儲機關

資料檔案:實體存儲機關

使用者:面向使用者管理,都會對應一個表空間,向表空間中添加資料,都是儲存到資料檔案中

五、介紹scott使用者和scott使用者下的表

Oracle公司(甲骨文):埃裡森, 軟體開發實驗室

scott的第一位程式員, pointbase

scott使用者,密碼:tiger
           

– 解鎖使用者

– alter user 使用者名 account unlock;

alter user scott account unlock

— 重置密碼

– alter user 使用者名 identified by 密碼;

alter user scott identified by tiger;

六、基本查詢

-- dual :是一張虛表, 為了完善查詢的文法結構,隻有一行一列
   -- 别名:如果是數字開頭或者純數字必須加雙引号
   -- 字元串使用單引号,别名使用雙引号
   select 
   length('abc') "長度",
   length('abc') as "長度",
   length('abc') as 長度, 
   length('abc') as '123'   from dual;
   -- 消除重複的記錄
   select distinct job from emp;
   -- 四則運算:+ - *  /      -- 連接配接符号:||
   select concat('a' , 'b') from dual;
   select concat(concat('a' , 'b'),'c') from dual;
   select 'a' || 'b'|| 'c' from dual;
   select '1' + 1 from dual;
   -- 查詢員工的年薪: nvl(comm,0)
   select sal * 12 + nvl(comm,0),nvl(comm,0) from emp;
           

七、條件查詢

-- 查詢有獎金的員工
   select * from emp where comm > 0;

   select * from emp where comm is not null and comm != 0;
   -- 查詢沒有獎金的員工
   select * from emp where comm is null or comm = 0;
   -- not 取反
   select * from emp where not(comm is not null and comm != 0);

   -- 查詢1981年入職的員工
   select * from emp where to_char(hiredate,'yyyy') = '1981';

   select * from emp where hiredate >= to_date('1981-01-01','yyyy-mm-dd')
                       and hiredate <= to_date('1981-12-31','yyyy-mm-dd');

   select * from emp where hiredate between  to_date('1981-01-01','yyyy-mm-dd')  
                                         and to_date('1981-12-31','yyyy-mm-dd') ; 
                       --- 函數:to_char  to_date
    -- to_char (p1,p2):将日期轉換為字元串
                 -- p1:要轉換的日期
                 -- p2:轉換的格式
      select sysdate,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss day') from dual;

    -- to_date(p1,p2):将字元串轉換為日期
                 -- p1:要轉換的字元串
                 -- p2:轉換的格式        
     select '2018-07-06 11:11:11' , 
     to_date('2018-07-06 11:11:11' ,'yyyy-mm-dd hh24:mi:ss') from dual;
     -- upper: 轉換為大寫 lower :轉換為小寫
     select * from emp where upper(ename) like upper('%M%');
     -- 排序
   -- 查詢所有員工按照工資升序
   select * from emp order by sal asc;
   -- 查詢所有員工按照工資降序
   select * from emp order by sal desc;
   -- 查詢所有員工按照獎金升序排序(null值放到前面)
   select * from emp order by nvl(comm,0) asc;

   select * from emp order by comm asc nulls first; 
   -- 查詢所有員工按照獎金降序排序(null值放到後面) 
   select * from emp order by comm desc nulls last; 
           

八、單行函數

-- 單行函數
   select length(ename) from emp;
   --字元串函數
      -- concat
      -- length
      -- substr(str, p1 ,p2): str:截取的字元串 ,p1:開始的索引 ,p2:截取的長度
         -- 起始索引用0和1是一樣的
      select substr('abcjavadef' , 4, 4 ) from dual;
      select substr('abcjavadef' , 1, 3 ) from dual;
      select substr('abcjavadef' , 0, 3 ) from dual;
      -- replace(str ,p1,p2) : 替換  str:要替換的字元串 p1:被替換的 p2:替換成的
      select replace('abcdefa' , 'a' ,'z') from dual;
      -- trim() 去除兩側的空白
      select trim('   abc    '),ltrim('   abc    '),rtrim('   abc    ') from dual;
      -- upper lower

   --日期函數
      -- 兩個日期相減 == 天數
      select sysdate - hiredate from emp;
      -- 周數
      select (sysdate - hiredate) / 7 from emp;
      -- 月數:months_between
      select months_between(sysdate , hiredate) from emp;
      -- 修改月份: add_months
      select add_months(sysdate ,-12) from dual;
   --數值函數
      -- round () 四舍五入
      select round(2.666) from dual;
      -- trunc() 截取
      select trunc(2.666,1) from dual;
      -- mod() 模運算符(求餘)
      select mod(3,10) from dual;
   --轉換函數
      -- to_char to_date
      -- to_char to_number
      select 1 ,to_char(1),'1',to_number('1') from dual;
      select 1 + '1' from dual;
   --通用函數
      -- nvl
           

九、多行函數

-- 多行函數(聚合函數,分組函數)(count, avg, max ,min ,sum)
   -- 分組後能顯示的列:分組函數和在group by語句中出現的列
   select 
      count(1),deptno, avg(sal),max(sal) ,min(sal) ,sum(sal)
   from emp group by deptno;

   -- 查詢大于4個人的部門
   -- 在where中不能加分組函數,where 必須放到group by前面
   -- having 在分組的基礎上進一步的篩選
   select count(1),deptno from emp  group by deptno having count(1) > 4;
           

練習

1.  查詢和smith相同部門的員工姓名和雇用日期
    select ename,hiredate from emp where deptno=(select deptno from emp where ename=upper('smith'))
2.  查詢工資比公司平均工資高的員工的員工号,姓名和工資。
     select empno,ename,sal from emp where sal> (select avg(sal) from emp)

3.  查詢各部門中工資比本部門平均工資高的員工的員工号, 姓名和工資
  select empno,ename,sal,deptno from emp where sal>(select avg(sal) from    emp where deptno=20)and deptno!=20

4.  查詢和姓名中包含字母U的員工在相同部門的員工的員工号和姓名
  select empno,ename from emp where deptno=(select deptno from emp where    ename like '%U%')

5. 查詢上司是King的員工姓名和工資
select ename,sal from emp where mgr= (select empno from emp where ename='KING')