隨筆-159  評論-114  文章-7  trackbacks-0

          上回,Select DISTINCT dept_id,title from s_emp,注意DISTINCT是作用于兩列的組合。

          Group Functions

          分組函數(shù)

          SELECT column,group_function
          FROM table
          [WHERE condition]
          [GROUP BY group_by_expression]
          [HAVING group_condition]
          [ORDER BY column];

          AVG(DISTINCT|ALL|n)
          COUNT(DISTINCT|ALL|expr|*)
          MAX(DISTINCT|ALL|expr)
          MIN(DISTINCT|ALL|expr)
          SUM(DISTINCT|ALL|n)


          SQL> select AVG(salary),MAX(salary),
            2  MIN(salary),SUM(salary)
            3  from s_emp
            4  where UPPER(title) LIKE 'SALES%';

          AVG(SALARY) MAX(SALARY) MIN(SALARY) SUM(SALARY)
          ----------- ----------- ----------- -----------
              1463.75        1515        1400        5855


          SQL> select count(*)
            2  from s_emp
            3  where dept_id=31
            4  ;

            COUNT(*)
          ----------
                   1

          SQL> select count(commission_pct)
            2  from s_emp
            3  where dept_id = 31;

          COUNT(COMMISSION_PCT)
          ---------------------
                              1

          注意,count具體列,只統(tǒng)計非空數(shù)目。

            1  select dept_id,Count(*) "Number"
            2  from s_emp
            3* group by dept_id
          SQL> /

             DEPT_ID     Number
          ---------- ----------
                  31          1
                  32          1
                  33          1
                  34          1
                  35          1
                  41          2
                  42          2
                  43          2
                  44          2
                  45          3
                                8

          不寫Group by,只是一組,

          加了Group by,多組。

          出現(xiàn)在Select中,非組函數(shù)的值,必須出現(xiàn)加在Group by中。可以不Select某列,但是group by該列,但結(jié)果當然也沒有這一列,只是按照它來分組顯示其他組函數(shù)數(shù)據(jù)。


          組函數(shù)的判斷不能在Where子句中,必須是在Having中。

          SELECT title,12*AVG(salary) "ANNUAL SALARY",COUNT(*) "NUMBER OF EMPLOYEES"
          FROM s_emp
          GROUP BY title
          HAVING COUNT(*)>2;

          第一步,分組
          第二步,應用組函數(shù)
          第三步,匹配Having子句的組函數(shù)

          select title,sum(salary) PALALL
          FROM s_emp
          WHERE title NOT LIKE "VP%"
          GROUP BY title
          HAVING SUM(salary) > 5000
          ORDER BY SUM(salary);

          Subqueries 子查詢

          select last_name,title
          from s_emp
          where title=
          (select title
          from s_emp
          where last_name='Smith');

          select last_name,title,salary
          from s_emp
          where salary<
          (select avg(salary)
          from s_emp);

          自查詢有多值返回時,使用IN關(guān)鍵字

          select last_name,first_name,title
          from s_emp
          where dept_id IN
          (select id
          from s_dept
          where name = 'Finance' or region_id = 2);

          子查詢也可以出現(xiàn)在Having中

          Select dept_id,AVG(salary)
          from s_emp
          group by dept_id
          having avg(salary)>
          (select avg(salary)
          from s_emp
          where dept_id=32);

          子查詢有效率問題,既能夠用連接又能用子查詢時,請使用連接,一次查詢快!

          設(shè)置變量

          SQL> select id,last_name,salary
            2  from s_emp
            3  where dept_id=&department_number;
          Enter value for department_number:

          where title = '&job_title';

          一般用于插入數(shù)據(jù)時,進行定義。

          可以保存一個.sql文件,在文件的開頭設(shè)定變量,在執(zhí)行insert,但sqlplus中,只保存sql命令,不保存sqlplus命令,DEFINE和ACCEPT是sqlplus命令。

          DEFINE did=105(CHAR) 都是字符型。

          insert into lujl11
          values (&did,'&address',&salary);

          使用ACCEPT命令來進行變量定義,可以區(qū)分類型,和添加提示符

          ACCEPT p_dname PROMPT 'Provide the department name'
          ACCEPT p_salary NUMBER PROMPT 'Salary amoount:'
          ACCEPT pswd CHAR PROMPT 'Password:' HIDE

          UNDEFINE或者退出sqlplus,會清楚變量。



          SET VERIFY ON
          察看新舊值開關(guān)。


          數(shù)據(jù)庫建模

          立項
          ----〉需求分析
          ----------------〉設(shè)計,設(shè)計細化
          ----------------------------------〉建表,測試(邏輯測試,需求滿足否,物理測試,數(shù)據(jù)量承受力),優(yōu)化
          --------------------------------------------〉真實數(shù)據(jù),產(chǎn)品。

          實線---必然的聯(lián)系

          虛線---可能的聯(lián)系

          范式

          第一范式 解決單值問題。
          第二范式 解決依賴性。
          第三范式 解決非傳遞性問題。

          例子:一想就明白了

          經(jīng)過需求分析,整理出下列屬性。

          product#,order#,name,date,price,quantity,customer#,customer_name,customer_address(Province,city,address))

          INF分析后

          (product#,order#,name,date,price,quantity,customer#,customer_name,Province,City,Address)

          2NF分析后

          (product#,name,price)
          (order#,date,customer#,customer_name,Province,City,Address)
          (product#,order#,quantity)

          3NF分析后

          (product#,name,price)
          (order#,date,customer#)
          (product#,order#,quantitiy)
          (customer#,customer_name,Province,City,Address)





          今天安裝FC3,費了些時間,為能夠在自己機器上也跑上Redhat,安裝了Fedora Core 3,http://fedora.linuxsir.org/main/
          不錯的網(wǎng)站,我就是照著裝上的,以后就可以研究一下了。哈哈











           



          posted on 2005-11-22 00:05 北國狼人的BloG 閱讀(400) 評論(0)  編輯  收藏 所屬分類: 達內(nèi)學習總結(jié)
          主站蜘蛛池模板: 广昌县| 大化| 扎赉特旗| 岑巩县| 吴川市| 隆尧县| 永德县| 庆元县| 六盘水市| 南丹县| 永平县| 海城市| 洱源县| 张家川| 溧阳市| 湖州市| 济阳县| 台前县| 安丘市| 保靖县| 崇仁县| 北辰区| 沈阳市| 武乡县| 黄平县| 镇平县| 永康市| 靖远县| 米易县| 大埔县| 铁岭市| 江津市| 盘锦市| 历史| 五华县| 巫山县| 平果县| 天祝| 清远市| 泽库县| 平江县|