Installment Installment 77Tables With No ColumnTables With No Column
Presented by rexmen 2001
資管所.林彥廷. 690530031
Installment 7 Installment 7 流程大綱流程大綱• Relational Algebra OperatorRelational Algebra Operator• Empty set in RDBEmpty set in RDB• Tables and PredicateTables and Predicate• Tables with No columnTables with No column• TABLE_DUM and TABLE_DEETABLE_DUM and TABLE_DEE• Effect on Relational AlgebraEffect on Relational Algebra
Relational Algebra Relational Algebra OperatorOperator
• Select (Restrict)Select (Restrict)• ProjectProject• Cartesian ProductCartesian Product• UnionUnion• DifferenceDifference• JoinJoin• IntersectionIntersection• DivideDivide
原始運算子
非原始運算子
Select (Restrict)Select (Restrict)
• 從一個 從一個 Table Table 中取出符合條件的資料,也就是取中取出符合條件的資料,也就是取得 得 Row Row 的資料,請注意這裡的 的資料,請注意這裡的 Select Select 是運算子是運算子並不是指 並不是指 SQL SQL 語法中的 語法中的 SelectSelect 。。
C1C1 C2C2 C3C3 C4C4
Select * From T1 Where <expression>
ProjectProject
• 從一個 從一個 Table Table 中取出所需要的欄位,也就是取得 中取出所需要的欄位,也就是取得 Column Column 的資料。 的資料。
C1C1 C2C2 C3C3 C4C4
Select C1, C2 From T1
Cartesian Product(Cartesian Product( 卡氏積卡氏積 ))• 將兩個 將兩個 Table Table 以普通算術中的乘法原理相以普通算術中的乘法原理相
乘後組合成一個 乘後組合成一個 Table Table 。。
C1C111
C1C122
A1A1 A2A2
B1B1 B2B2
C1C1 C2C2
C2C211
C2C222
X1X1 X2X2
Y1Y1 Y2Y2
C11C11 C12C12 C21C21 C22C22
A1A1 A2A2 X1X1 X2X2
A1A1 A2A2 Y1Y1 Y2Y2
B1B1 B2B2 X1X1 X2X2
B1B1 B2B2 Y1Y1 Y2Y2
C1C1 C2C2 X1X1 X2X2
C1C1 C2C2 Y1Y1 Y2Y2
╳ =>
Select * From T1, T2
UnionUnion
• 從兩個 從兩個 Table Table 中取出聯集的資料,重複的資料只中取出聯集的資料,重複的資料只取出一筆。 取出一筆。
C1C1 C2C2
X1X1 X2X2
Y1Y1 Y2Y2
C1C1 C2C2
X1X1 X2X2
Z1Z1 Z2Z2
C1C1 C2C2
X1X1 X2X2
Y1Y1 Y2Y2
Z1Z1 Z2Z2
=>
Select * From T1UnionSelect * From T2
DifferenceDifference
• 從一個 從一個 Table Table 中刪除另一個 中刪除另一個 Table Table 中有的資料 。中有的資料 。
C1C1 C2C2
X1X1 X2X2
Y1Y1 Y2Y2
C1C1 C2C2
X1X1 X2X2
Z1Z1 Z2Z2
C1C1 C2C2
Y1Y1 Y2Y2=>
Select * From T1 Where not exists ( Select * From T2 Where T1.C1 = T2.C1 and T1.C2 = T2.C2 )
Set of Relational DBSet of Relational DB
CC11
CC22
CC33
…… CnCn
Column / Attribute
Row / Tuple
TABLE / RELATION
Empty SetEmpty Set
• 空集合空集合 (empty set)(empty set) 仍然是一個集合,所以仍然是一個集合,所以 RDBRDB 集合定集合定理所構成,自然也要能符合含有空集合的性質。理所構成,自然也要能符合含有空集合的性質。
• Empty set of rowsEmpty set of rows
• Empty set of columnEmpty set of column
C1C1 C2C2 C3C3 …… CnCn
Tables and PredicateTables and Predicate
• PredicatePredicate ::– a a Truth-valued Function(Truth-valued Function( 真值函數真值函數 ))– Return Return TrueTrue or or FalseFalse– Ex. Ex.
• F( e, set ) F( e, set )
• F( F( 33, {1,, {1,33,5,6} ) Ans,5,6} ) Ans : : TrueTrue
True, if e contained by setFalse, otherwise
Proposition( 命題 ) / Instantiation( 實例 )
Predicate of TablesPredicate of Tables
• 可以說可以說 predicatepredicate 是是 tabletable 的所表示的意義的所表示的意義• 每一個每一個 tabletable 有一個確切的有一個確切的 predicatepredicate• Ex. Table Ex. Table EMPEMP with heading with heading {E#, SAL, {E#, SAL,
D#}D#}– ““Employee E# earns salary SAL and workEmployee E# earns salary SAL and work
s in department D#. ”s in department D#. ”– E.g. EMP(E1, 50K, D1)E.g. EMP(E1, 50K, D1)
• 如果存在此列如果存在此列 (E1, 50K, D1)(E1, 50K, D1) ,則此,則此 propositionproposition的結果為的結果為 TrueTrue ,反之為,反之為 FalseFalse 。。
TABLE EMP(E#, SAL, TABLE EMP(E#, SAL, D#)D#)
• ““Employee E# earns salary SAL and Employee E# earns salary SAL and works in department D#. ”works in department D#. ”
E#E# SALSAL D#D#
E1E1 50K50K D1D1
E2E2 35K35K D2D2
E3E3 60K60K D3D3
E4E4 40K40K D2D2
E5E5 50K50K D2D2
E6E6 42K42K D3D3EMP
Project Away D# From Project Away D# From EMPEMP
• Table Table ESES is derived from table EMP(E#, is derived from table EMP(E#, SAL, D#) by “ SAL, D#) by “Projecting awayProjecting away” colu” column mn D#D#– Get table Get table ES ES with headingwith heading {E#, SAL} {E#, SAL}– PredicatePredicate ::
• ““THERE EXISTSTHERE EXISTS a department D# a department D# SUCH THSUCH THATAT employee E# earns salary SAL and works i employee E# earns salary SAL and works in department D# .”n department D# .”
TABLE ES(E#, SAL)TABLE ES(E#, SAL)• ““THERE EXISTSTHERE EXISTS a department D# a department D# SUCH THATSUCH THAT employee employee
E# earns salary SAL and works in department D# .”E# earns salary SAL and works in department D# .”
E#E# SALSAL D#D#
E1E1 50K50K D1D1
E2E2 35K35K D2D2
E3E3 60K60K D3D3
E4E4 40K40K D2D2
E5E5 50K50K D2D2
E6E6 42K42K D3D3
E#E# SALSAL
E2E2 35K35K
E4E4 40K40K
E5E5 50K50K
EMP
ES
Project away D2 From EMP
Project Away E# From ESProject Away E# From ES
• Table Table SS is derived from table ES(E#, SA is derived from table ES(E#, SAL) by “L) by “Projecting awayProjecting away” column ” column E#E#– Get table Get table S S with heading with heading {SAL}{SAL}– PredicatePredicate ::
• ““THERE EXISTSTHERE EXISTS a department D# a department D# SUCH THSUCH THAT THERE EXISTSAT THERE EXISTS an employee E# an employee E# SUCH THSUCH THATAT employee E# earns salary SAL and works i employee E# earns salary SAL and works in department D#” n department D#”
TABLE S(SAL)TABLE S(SAL)• ““THERE EXISTSTHERE EXISTS a department D# a department D# SUCH THAT THERE SUCH THAT THERE
EXISTSEXISTS an employee E# an employee E# SUCH THATSUCH THAT employee E# earns employee E# earns salary SAL and works in department D#” salary SAL and works in department D#”
E#E# SALSAL D#D#
E1E1 50K50K D1D1
E2E2 3535KK
D2D2
E3E3 60K60K D3D3
E4E4 4040KK
D2D2
E5E5 5050KK
D2D2
E6E6 42K42K D3D3
E#E# SALSAL
E2E2 3535KK
E4E4 4040KK
E5E5 5050KKEMP
ES
Project away D2 From EMP
SALSAL
4040KK
Project away E4 From ES
S
Project Away SAL From SProject Away SAL From S
• Table Table ZZ is derived from table S(SAL) by is derived from table S(SAL) by ““Projecting awayProjecting away” column ” column SALSAL– Get table Get table Z Z with headingwith heading { }{ }– PredicatePredicate ::
• ““THERE EXISTSTHERE EXISTS a department D# a department D# SUCH THSUCH THAT THERE EXISTSAT THERE EXISTS an employee E# an employee E# SUCH THSUCH THAT THERE EXISTSAT THERE EXISTS a salary SAL a salary SAL SUCH THATSUCH THAT employee E# earns salary SAL and works in deemployee E# earns salary SAL and works in department D#.” partment D#.”
Table ZTable Z
• ““THERE EXISTSTHERE EXISTS a department D# a department D# SUCH SUCH THAT THERE EXISTSTHAT THERE EXISTS an employee E# an employee E# SUCH SUCH THAT THERE EXISTSTHAT THERE EXISTS a salary SAL a salary SAL SUCH SUCH THATTHAT employee E# earns salary SAL and works employee E# earns salary SAL and works in department D#.”in department D#.”E#E# SALSAL D#D#
E1E1 50K50K D1D1
E2E2 3535KK
D2D2
E3E3 60K60K D3D3
E4E4 4040KK
D2D2
E5E5 5050KK
D2D2
E6E6 42K42K D3D3
E#E# SALSAL
E2E2 3535KK
E4E4 4040KK
E5E5 5050KKEMP
ES
Project away D2 From EMP
SALSAL
4040KK
Project away E4 From ES
S
Project away 40k From S
Table with No ColumnTable with No Column !!• 此種此種 TableTable 是否能存在任何的列?是否能存在任何的列?• 在此種在此種 TableTable 中存在著一種為中存在著一種為 0-tuple0-tuple 的列的列
– 因為一個列也是一種欄位的集合因為一個列也是一種欄位的集合– 0-tuple0-tuple 是一種欄位為空集合的一個欄位集合是一種欄位為空集合的一個欄位集合– 此種此種 TableTable 只有一種列,即為只有一種列,即為 0-tuple(0-tuple( 一般正規的關一般正規的關
聯式資料庫中的列是要消除重複性的,因為聯式資料庫中的列是要消除重複性的,因為 0-tuple0-tuple 與與其他其他 0-tuple0-tuple 表示相同的列,故此種表示相同的列,故此種 tabletable 只能至多有只能至多有一個列。一個列。 ))
– Table With No Column is Table With No Column is usefuluseful ?? Of Course, Of Course, YESYES !!
TABLE_DUM and TABLE_DUM and TABLE_DEETABLE_DEE
• Table with No ColumnTable with No Column 只會有兩種情形:只會有兩種情形:– 只有一個列只有一個列 ((TABLE_DEETABLE_DEE) → ) → True/YesTrue/Yes– 沒有任何列沒有任何列 ((TABLE_DUMTABLE_DUM) →) →False/NoFalse/No
• SQLSQL 難以精確的處理難以精確的處理 Yes/NOYes/NO 問題 ,因為問題 ,因為 SSQLQL 並不支援並不支援 Table with No ColumnTable with No Column 。。– Ex. ”Does employee E1 work in departmeEx. ”Does employee E1 work in departme
nt D1?”nt D1?”• 畫出 畫出 TABLE_DUMTABLE_DUM 與與 TABLE_DEETABLE_DEE 的圖的圖
形是困難的。形是困難的。
Effect on Relational Effect on Relational AlgebraAlgebra
IdentityIdentity– 普通算術中數字普通算術中數字 11 為一個為一個 IdentityIdentity
• a*1 = 1*a = a, for all numbers a a*1 = 1*a = a, for all numbers a – 在關聯式代數中在關聯式代數中 TABLE_DEETABLE_DEE 為一個為一個 IdentitIdentit
yy• T TIMES DEE = DEE TIMES T = T for all tables T TIMES DEE = DEE TIMES T = T for all tables
T T
Product (Cartesian product)Product (Cartesian product)DEEDEE
• T TIMES T TIMES DEEDEE = = DEEDEE TIMES T = T for all tables T TIMES T = T for all tables T
C1C1 C2C2
X1X1 X2X2
Y1Y1 Y2Y2
Z1Z1 Z2Z2
Product TABLE_DEE =
C1C1 C2C2
X1X1 X2X2
Y1Y1 Y2Y2
Z1Z1 Z2Z2
Product (Cartesian product)Product (Cartesian product)DUMDUM
• T TIMES T TIMES DUMDUM = = DUMDUM TIMES T = a table with the s TIMES T = a table with the same heading as T but with no rows at all.ame heading as T but with no rows at all.
C1C1 C2C2
X1X1 X2X2
Y1Y1 Y2Y2
Z1Z1 Z2Z2
Product TABLE_DUM =
C1C1 C2C2
Join Join DEEDEE
• T JOIN T JOIN DEEDEE = = DEEDEE JOIN T = a table with the same h JOIN T = a table with the same heading as T but with no rows at all.eading as T but with no rows at all.
C1C1 C2C2
X1X1 X2X2
Y1Y1 Y2Y2
Z1Z1 Z2Z2
JOIN TABLE_DEE = C1C1 C2C2
Join Join DUMDUM
• T JOIN T JOIN DUMDUM = = DUMDUM JOIN T = a table with the same JOIN T = a table with the same heading as T but with no rows at all.heading as T but with no rows at all.
C1C1 C2C2
X1X1 X2X2
Y1Y1 Y2Y2
Z1Z1 Z2Z2
JOIN TABLE_DEE = C1C1 C2C2
THE ENDTHE ENDrexmen 2001