天天看點

《資料庫系統概念》學習筆記2

                 第二章    

  關系模型

a.關系代數基本運算有:選擇、投影、并、集合差、笛卡爾積、和更名

b.附加的關系代數運算:集合交、自然連接配接、除運算、指派運算、

c.擴充的關系代數運算:廣義投影,聚集函數,外連接配接

d.資料庫的修改:删除,插入,更新

小結

a.關系資料模型建立在表的集合的基礎之上。資料庫系統的使用者可以對這些表進行查詢,可以插入新元組、删除元組以及更新(修改)元組。

b.關系代數定義了一套在表上運算,且輸出結果也是表的代數運算。這些運算可以混合使用以得到表達所希望查詢的表達式。關系代數定義了關系查詢語言中使用的基本運算

c.關系代數運算可分為:基本運算、附加運算、擴充運算

d.資料庫可以通過插入、删除或更新元組來修改。我們用包含指派運算符的關系代數來表達這些修改

e.關系代數是簡潔的、形式化的語言,不适合那些偶爾使用資料庫系統的使用者。是以,商用資料庫采用有更多“文法修飾”的語言------sql

                   第三章  

sql

a.sql語言有以 下幾個部分:資料定義語言,互動式資料操縱語言,完整性,視圖定義,事務控制        

 嵌入式sql和動态sql,授權

b.sql表達式基本結構包括三個字句

   select子句:對應關系代數中的投影運算,用來列出查詢結果中所要的屬性

   from子句:  對應關系代數中的笛卡爾積,它列出表達式求值中需掃描的關系

   where子句:對應關系代數中的選擇謂詞,它包括一個作用在from子句中關系的屬性上的謂詞

c. select        ”找出loan關系中所有支行的名字“

                select

branch_name

                   from

loan

    *若要删除重複,則select後加入關鍵詞distinct       

d.  where      

 ”找出所有再perryridge支行貸款并且貸款額超過1200美元的貸款的貸款号“

loan_number

                 where

 branch_name = ‘perryridge‘ and amount>1200

    *還提供between比較運算符來說明一個值是小于或等于某個值、同時大于或等于另一個值

e.   from          

”找出從銀行貸款的所有使用者的名字、貸款号、貸款數目“       

 select customer_name,borrower.loan_number,amount

 from borrower,loan

 where borrower.loan_number=loan.loan_number

f.    as更名運算  

 ”我們想用名字loan_id代替屬性名loan_number,我們可以像下面這樣重寫上面的查詢

        select customer_name,borrower.loan_number as

loan_id,amount

        from borrower,loan 

        where borrower.loan_number=loan.loan_number

g. like字元串運算    “找出街道位址中包含子串‘main‘的所有客戶名“

        select customer_name

        from customer

        where customer_street like ‘%main%‘

   *其中有轉義字元‘\’類似java

h. order by 排列元組的顯示次序    

 ”按字母順序列出perryridge支行中有貸款的客戶“

      select distinct customer_name

      from borrower,loan

      where borrower.loan_number=loan.loan_number and

                branch_name =

‘perryridge‘

       order by customer_name

   *order by預設為升序,可加desc表示降序,asc表示升序,如”order by amount

desc,loan_number asc"

i.集合運算:union、intersect、except分别對應關系代數中的交、并、差

 union      “找出在銀行有賬戶、有貸款或兩者都有的所有客戶

           (select customer_name

depositor)

              union

             (select customer_name

               from borrower)

*  與select字句不同,union運算自動去除重複,若要保留重複,可用union all代替union

intersect     ”找出在銀行同時有賬戶和貸款的客戶“

                  (select

distinct customer_name

   from depositor)

 intersect

(select distinct customer_name

 from borrower)

*同union一樣,也是自動去除重複

except     ”找出在銀行中有賬戶但無貸款的客戶“

              (select distinct

customer_name

                  from

                except

                (select

                 from

borrower)

*自動去除重複

j.聚集函數:平均值:avg,最小值:min,最大值:max,總和:sum,計數:count。

 *sum和avg的輸入必須是數字集,而其他運算符還可以作用在非數字資料類型

        ”找出perryridge支行賬戶餘額平均值“

             select avg(balance)

             from account

             where

branch_name=‘perryridge‘

        “找出每個支行儲戶數”

branch_name,count(distinct customer_name)

                from

depositor,account

                where

depositor.account_number = account.account_number

      * group

by  語句用于結合合計函數,根據一個或多個列對結果集進行分組。

         此處解釋很精辟

k.having子句中的的謂詞在形成分組後才起作用(即group by起作用後才起作用),是以可以使用聚集函數

          select branch_name,avg(balance)

          from account

          group by branch_name

          having avg(balance)>1200

l.可以用count計算一個關系中的元組數。sql中該函數的寫法是count(*)。是以,要找出customer關系中的元組數,可寫成:  

 select count(*)

         from customer

*sql不允許在用count(*)時使用distinct

為了說明having子句和where子句出現在同一個查詢時的用法,我們考慮查詢“找出住在harrison且在銀行中至少有三個賬戶的客戶的平均餘額”

 select depositor.customer_name,avg(balance)

 from depositor,account,customer

 where depositor.account_number = account.account_number and

           depositor,customer_name =

customer.customer_name and

           customer_city = ‘harrison‘

 group by depositor.customer_name

 having count(distinct depositor.account_number)>=3

m.null 空值     “找出loan關系中amount為空值的貸款号”

     select loan_number

     from loan 

     where amount is null

*謂詞is not

null用來檢測非空值。如果算數運算的輸入有一個是空,則該算數表達式的結果是空。如果有空值參與比較運算,sql将比較運算的結果看成unknown(既不是is

null,也不是is not null)

   sql還允許我們用is unknown子句或is not

unknown子句來判斷比較結果是不是unknown,而不是判斷true or false

n.嵌套子查詢,子查詢是嵌套在另一個查詢中的select-from-where表達式。一般子查詢的使用時為了對集合的成員資格、集合的比較以及集合的基數進行檢查

連接配接詞in測試元組是否是集合中成員,not in 則相反

“找出在銀行中同時有賬戶和貸款的客戶”

    select distinct customer_name

    from borrower

    where customer_name in (select customer_name

 from depositor)

*in和not in運算符也能用于枚舉集合  

“找出在銀行中有貸款的客戶,并且它的名字既不是”smith",也不是“jones”

              select distinct

customer_name 

              from borrower

              where customer_name not in

(‘smith‘,‘jones‘)

o.短語“至少比某一個大”在sql中用>some表示

          select branch_name

          from branch

          where assets>some (select

assets 

                    from

branch 

                    where

branch_city = ‘brooklyn‘)

p.exists 測試是否為空關系,非空時傳回true

            “找出在銀行既有賬戶又有貸款的客戶”

   select customer_name

   from borrower

   where exists (select* 

  from depositor

  where depositor.customer_name=borrower.customer_name)

q. unique 檢視子查詢中有沒重複的元組。沒有則傳回true

 “找出所有在perryridge支行中隻有一個賬戶的客戶”

                  select

t.customer_name

depositor as t

                  where unique

(select r.customer_name

account,depositor as r

t.customer_name = r.customer_name and

        r.account_number = account.account_number and

        account.branch_name = ‘perryridge‘)

r.派生關系,as

    “查詢産生的關系包含各支行的名字和相應的平均賬戶餘額”

           (select branch_name

,avg(balance)

            from account 

            group by branch_name)

            as

branch_avg(branch_name,avg_balance)

s. with 子句

with子句提供定義臨時視圖的方法,這個定義隻對with子句出現在的那條查詢有效

t.視圖:任何不是邏輯模型的一部分,但作為虛關系對使用者可見的關系稱為視圖。

           create view all_customer as

              (select

branch_name,customer_name

depositor,accunt

depositor.account_number=account.account_number)

                union

borrower,loan

borrower.loan_number=loan.loan_number)

一旦我們定義了一個視圖,我們就可以用視圖名指代該視圖生成的虛關系

                 select

all_customer

branch_name = ‘perryridge‘

*隻要沒更新操作在視圖上的執行,視圖名可以出現在關系名可以出現的任何地方

u.資料庫的修改

删除:   格式    delete from r

   where p

         删除perryridge支行的所有賬戶

                   delete

from account

                   where

       删除所有數額在1300美元到1500美元之間的貸款

                 delete from

                 where amount

between 1300 and 1500

      删除所有位于brooklyn的支行的所有賬戶

account

branch_name in(select branch_name

          where branch_city = ‘brooklyn‘

插入:insert into account 

                   select

loan_number,branch_name,200

    "向depositor關系中添加元組“

                  insert into

depositor

       select customer_name ,loan_number

       from borrower,loan

      where borrower.loan_number = loan.loan_number and

更新:     ”對餘額超過10000美元的賬戶付6%的利息,其餘賬戶付5%”

                   update

account 

    set  balance = balance * 1.06

    where balance>10000

                  update

 set balance = balance * 1.05

balance<=10000

*這兩條update語句的順序非常重要。假如改變這兩條語句的順序,略少于1w美元的存款将獲得11.3%的利息。

小結:

a.商業資料庫系統并不使用第二章所介紹的簡潔的、形式化的關系代數。本章我們學習廣泛應用的sql語言

,是建立在關系代數基礎上并提供了許多便利文法的語言

b.sql的資料定義語言用于建立具有特定模式的關系。sql dll支援若幹資料類型,包括date和time等類型。

c.sql包括各種用于查詢資料庫的語言結構。所有的關系代數運算,包括擴充的關系代數運算都可以用sql表達。sql還允許對查詢結果按某些特定屬性進行排序

d.sql通過一般的真值取值(即true和false)外增加真值”unknown“來處理關系中含有空值的查詢

e.sql允許在where子句中嵌套子查詢。外層查詢可以在子查詢的結果上執行多種操作

f.視圖關系可以定義為包含查詢結果的關系

g.臨時視圖用with來定義