第二章 關係模型
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美圓的貸款的貸款號「
select loan_number
from loan
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
from 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 depositor)
except
(select customer_name
from borrower)
*自動去除重複
j.彙集函數:平均值:avg,最小值:min,最大值:max,總和:sum,計數:count。
*sum和avg的輸入必須是數字集,而其餘運算符還能夠做用在非數字數據類型
」找出Perryridge支行帳戶餘額平均值「
select avg(balance)
from account
where branch_name='Perryridge'
「找出每一個支行儲戶數」
select 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
from depositor as T
where unique (select R.customer_name
from account,depositor as R
where 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
from depositor,accunt
where depositor.account_number=account.account_number)
union
(select branch_name,customer_name
from borrower,loan
where borrower.loan_number=loan.loan_number)
一旦咱們定義了一個視圖,咱們就能夠用視圖名指代該視圖生成的虛關係
select customer_name
from all_customer
where branch_name = 'Perryridge'
*只要沒更新操做在視圖上的執行,視圖名能夠出如今關係名能夠出現的任何地方
u.數據庫的修改
刪除: 格式 delete from r
where P
刪除Perryridge支行的全部帳戶
delete from account
where branch_name='Perryridge'
刪除全部數額在1300美圓到1500美圓之間的貸款
delete from loan
where amount between 1300 and 1500
刪除全部位於Brooklyn的支行的全部帳戶
delete from account
where branch_name in(select branch_name
from branch
where branch_city = 'Brooklyn'
插入:insert into account
select loan_number,branch_name,200
from loan
where branch_name = 'Perryridge'
"向depositor關係中添加元組「
insert into depositor
select customer_name ,loan_number
from borrower,loan
where borrower.loan_number = loan.loan_number and
branch_name = 'Perryridge'
更新: 」對餘額超過10000美圓的帳戶付6%的利息,其他帳戶付5%」
update account
set balance = balance * 1.06
where balance>10000
update account
set balance = balance * 1.05
where 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來定義