Set Operations and Relation Algebra

 
Aljabar Relasi
 
 
Operasi Tambahan
 
Operasi tambahan menyederhanakan query
Set intersection
Natural join
Assignment
 
Set-Intersection
 
Notasi : 
r
 
 
s
 
r
 
 
s
 = { 
t 
| 
t
 
 
r
 
and
 
t
 
 
s
 }
r
, 
s
 memiliki
 
kesamaan 
aritas
attribute 
r
 dan 
s
 harus bertipe sama
catatan: 
r
 
 
s
 = 
r
 – (
r
s
)
 
Contoh Set-Intersection
 
Relasi 
r, s
:
 
 
 
 
r
 
 
s
 
A       B
 
 
1
2
1
 
A       B
 
 
2
3
 
r
 
s
 
A       B
 
      2
 
n
    Notasi :  r      s
 
Natural-Join
 
Contoh:
R
 = (
A, B, C, D
)
S
 = (
E, B, D
)
Hasil = (
A, B, C, D, E
)
r
     
s
 didefinisikan
 sbg
:
      
r.A, r.B, r.C, r.D, s.E
 (
r.B = s.B 
 r.D = s.D
 (
r 
 x  
s
))
 
Natural Join
 
Relations r, s:
A
B
1
2
4
1
2
C
D
a
a
b
a
b
B
1
3
1
2
3
D
a
a
a
b
b
E
 
r
A
B
1
1
1
1
2
C
D
a
a
a
a
b
E
 
s
 
Assignment
 
Contoh :  
 
temp1
 
 
R
 (
r 
)
  
temp2
 
 
R
 (
temp1
 x 
s 
)
  
result
 = 
temp1
 temp2
Bank Example Queries
 
n
Find the names of all customers who have a loan and an account at bank.
 
customer_name
 (
borrower
) 
 
customer_name
 (
depositor
)
 
Find the name of all customers who have a loan at
the bank and the loan amount
 
customer_name, loan_number, amount 
(borrower        loan)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower
 
(customer_name, loan_number)
 
l
Query
customer_name 
(
branch_name 
= “Downtown
(
depositor
      
account 
)) 
        
customer_name 
(
branch_name 
= “Uptown
(
depositor
     
account
))
Bank Example Queries
Find all customers who have an account from at
least the “Downtown” and the Uptown” branches.
account (account_number, branch_name, balance)
depositor (customer_name, account_number)
 
Operasi lain
 
Generalized Projection
Aggregate Functions
Outer Join
 
Projection Generalisasi
 
Pengembangan dari operasi projection yg menggunakan
fungsi  arithmetic di dalam 
daftar 
projection.
E
  
: ekspresi
 aljabar
 relasi
F
1
, 
F
2
, …, 
F
n
 
 ekspresi 
arithmetic.
Misl: 
credit_info(customer_name, limit, credit_balance),
find how much more each person can spend:
  
customer_name, limit – credit_balance
 
(credit_info)
 
Operasi & Fungsi Aggregate
 
Fungsi Aggregation
.
  
avg
:  average value
 
min
:  minimum value
 
max
:  maximum value
 
sum
:  sum of values
 
count
:  number of values
Operasi Aggregate 
dalam  aljabar relasi
E
 : ekspresi aljabar relasi
G
1
, 
G
2
 …, 
G
n
 : daftar attribut 
yg akan digrup
 (bisa
 kosong
)
F
i
  
function Agregate
A
i
 
name atribut
 
Aggregate
 
Relation 
r
:
A
B
C
7
7
3
10
 
n
g
 
sum(c
) 
(r)
sum
(
c 
)
27
 
Aggregate
 
Relasi 
account
 di group
 berdasarkan 
branch-name
:
 
branch_name
 
g 
sum
(
balance
)
 (
account
)
branch_name
account_number
balance
Perryridge
Perryridge
Brighton
Brighton
Redwood
A-102
A-201
A-217
A-215
A-222
400
900
750
750
700
branch_name
sum
(
balance
)
Perryridge
Brighton
Redwood
1300
1500
700
 
FungsiAggregate
 
Hasil agregasi tdk memiliki nama
Dapat menggunakan operasi rename utk
memberikan nama
 
branch_name
 
g 
sum
(balance) 
as
 sum_balance 
(
account
)
 
Join
 
Relation 
loan
 
n
Relation 
borrower
Join
Join
loan         borrower
Loan
Borrower
Loan
Borrower
Loan
Borrower
Loan
Borrower
 
Modifikasi Database
 
The content of the database may be modified using the following
operations:
Deletion
Insertion
Updating
All these operations are expressed using the assignment operator.
 
Deletion
 
A delete request is expressed similarly to a query, except instead of
displaying tuples to the user, the selected tuples are removed from
the database.
Can delete only whole tuples; cannot delete values on only particular
attributes
A deletion is expressed in relational algebra by:
  
r
 
 
r
E
 
where 
r
 is a relation and 
E
 is a relational algebra query.
Deletion Examples
Delete all account records in the Perryridge branch.
 
n
   Delete all accounts at branches located in Needham.
 
n
 
  
Delete
 
all loan records with amount in the range of 0 to 50
 
loan 
 
loan
amount 

0
and amount 
 50
 (
loan
)
 
account 
 
account 

branch_name = “Perryridge”
 
(
account 
)
branch (branch_name, branch_city, assets)
account (account_number, branch_name, balance)
depositor (customer_name, account_number)
 
Insertion
 
To insert data into a relation, we either:
specify a tuple to be inserted
write a query whose result is a set of tuples to be inserted
in relational algebra, an insertion is expressed by:
  
r 
 
 r
  
  
E
 
where 
r
 is a relation and 
E
 is a relational algebra expression.
The insertion of a single tuple is expressed by letting 
E
  be a constant relation
containing one tuple.
Insertion Examples
 
Insert information in the database specifying that Smith has $1200 in
account A-973 at the Perryridge branch.
 
n
  Provide as a gift for all loan customers in the Perryridge
     branch, a $200 savings account.  Let the loan number serve
     as the account number for the new savings account.
 
account 
 
 account
  
  {(“A-973”,
 
“Perryridge”, 1200)}
depositor 
 
 depositor
  
  {(“Smith”, “A-973”)}
account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower
 
(customer_name, loan_number)
 
Updating
 
A mechanism to change a value in a tuple without charging 
all
 values in the
tuple
Use the generalized projection operator to do this task
Update Examples
 
Make interest payments by increasing all balances by 5 percent.
 
n
  Pay all accounts with balances over $10,000  with  6 percent interest
     and pay all others 5 percent
 
 account
 
  
 
account_number
, 
branch_name
, 
balance 
* 1.06
 
(
 
BAL 
 10000 
(
account 
))
                    
  
 
account_number
, 
branch_name
, 
balance 
* 
1.05 
(
BAL 
 10000 
(
account
))
 
Contoh Banking
 
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower
 
(customer_name, loan_number)
Slide Note
Embed
Share

Delve into the intriguing world of set operations and relation algebra, exploring concepts such as set intersection, natural join, and generalized projection. Unravel the complexities of database queries and examples from the banking sector to enhance your knowledge in this domain.

  • Set Operations
  • Relation Algebra
  • Database Queries
  • Join Operations
  • Projection

Uploaded on Sep 16, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Aljabar Relasi

  2. Operasi Tambahan Operasi tambahan menyederhanakan query Set intersection Natural join Assignment

  3. Set-Intersection Notasi : r s r s = { t | t randt s } r, s memiliki kesamaan aritas attribute r dan s harus bertipe sama catatan: r s = r (r s)

  4. Contoh Set-Intersection Relasi r, s: A B A B 1 2 1 2 3 r s r s A B 2

  5. Natural-Join Notasi : r s Contoh: R = (A, B, C, D) S = (E, B, D) Hasil = (A, B, C, D, E) rs didefinisikan sbg: r.A, r.B, r.C, r.D, s.E ( r.B = s.B r.D = s.D (r x s))

  6. Natural Join Relations r, s: B D E A B C D 1 3 1 2 3 a a a b b 1 2 4 1 2 a a b a b r s r s A B C D E 1 1 1 1 2 a a a a b

  7. Assignment temp1 R (r ) temp2 R (temp1 x s ) result = temp1 temp2 Contoh :

  8. Bank Example Queries Find the names of all customers who have a loan and an account at bank. customer_name (borrower) customer_name (depositor) Find the name of all customers who have a loan at the bank and the loan amount customer_name, loan_number, amount (borrower loan) loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower(customer_name, loan_number)

  9. Bank Example Queries Find all customers who have an account from at least the Downtown and the Uptown branches. Query customer_name ( branch_name = Downtown (depositoraccount )) customer_name ( branch_name = Uptown (depositoraccount)) account (account_number, branch_name, balance) depositor (customer_name, account_number)

  10. Operasi lain Generalized Projection Aggregate Functions Outer Join

  11. Projection Generalisasi Pengembangan dari operasi projection yg menggunakan fungsi arithmetic di dalam daftar projection. , ,..., ( E ) F F F 1 2 n E : ekspresi aljabar relasi F1, F2, , Fnekspresi arithmetic. Misl: credit_info(customer_name, limit, credit_balance), find how much more each person can spend: customer_name, limit credit_balance(credit_info)

  12. Operasi & Fungsi Aggregate Fungsi Aggregation. Operasi Aggregate dalam aljabar relasi E : ekspresi aljabar relasi G1, G2 , Gn : daftar attribut yg akan digrup (bisa kosong) Fifunction Agregate Ainame atribut avg: average value min: minimum value max: maximum value sum: sum of values count: number of values ( E ) G , G , , G F ( A ), F ( A , , F ( A ) 1 2 n 1 1 2 2 n n

  13. Aggregate Relation r: A B C 7 7 3 10 gsum(c) (r) sum(c ) 27

  14. Aggregate Relasi account di group berdasarkan branch-name: branch_name account_number balance Perryridge Perryridge Brighton Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 750 700 branch_nameg sum(balance) (account) branch_name sum(balance) Perryridge Brighton Redwood 1300 1500 700

  15. FungsiAggregate Hasil agregasi tdk memiliki nama Dapat menggunakan operasi rename utk memberikan nama branch_nameg sum(balance) as sum_balance (account)

  16. Join Relation loan branch_name loan_number amount Downtown Redwood Perryridge L-170 L-230 L-260 3000 4000 1700 Relation borrower customer_name loan_number Jones Smith Hayes L-170 L-230 L-155

  17. Join branch_name loan_number amount customer_name loan_number Downtown Redwood Perryridge L-170 L-230 L-260 3000 4000 1700 Jones Smith Hayes L-170 L-230 L-155 Loan Borrower Join loan borrower loan_number branch_name amount customer_name L-170 L-230 Downtown Redwood 3000 4000 Jones Smith

  18. branch_name loan_number amount customer_name loan_number Downtown Redwood Perryridge L-170 L-230 L-260 3000 4000 1700 Jones Smith Hayes L-170 L-230 L-155 Loan Borrower Left Outer Join loan borrower loan_number branch_name amount customer_name L-170 L-230 L-260 Downtown Redwood Perryridge 3000 4000 1700 Jones Smith null

  19. customer_name loan_number branch_name loan_number amount Jones Smith Hayes L-170 L-230 L-155 Downtown Redwood Perryridge L-170 L-230 L-260 3000 4000 1700 Borrower Loan Right Outer Join loan borrower loan_number branch_name amount customer_name L-170 L-230 L-155 Downtown Redwood null 3000 4000 null Jones Smith Hayes

  20. customer_name loan_number branch_name loan_number amount Jones Smith Hayes L-170 L-230 L-155 Downtown Redwood Perryridge L-170 L-230 L-260 3000 4000 1700 Loan Borrower Full Outer Join loan borrower loan_number branch_name amount customer_name L-170 L-230 L-260 L-155 Downtown Redwood Perryridge null 3000 4000 1700 null Jones Smith null Hayes

  21. Modifikasi Database The content of the database may be modified using the following operations: Deletion Insertion Updating All these operations are expressed using the assignment operator.

  22. Deletion A delete request is expressed similarly to a query, except instead of displaying tuples to the user, the selected tuples are removed from the database. Can delete only whole tuples; cannot delete values on only particular attributes A deletion is expressed in relational algebra by: r r E where r is a relation and E is a relational algebra query.

  23. Deletion Examples Delete all account records in the Perryridge branch. account account branch_name = Perryridge (account ) Deleteall loan records with amount in the range of 0 to 50 loan loan amount 0 and amount 50 (loan) Delete all accounts at branches located in Needham. r1 branch_city = Needham (account branch ) r2 account_number,branch_name, balance (r1) r3 customer_name, account_number(r2 depositor) account account r2 depositor depositor r3 branch (branch_name, branch_city, assets) account (account_number, branch_name, balance) depositor (customer_name, account_number)

  24. Insertion To insert data into a relation, we either: specify a tuple to be inserted write a query whose result is a set of tuples to be inserted in relational algebra, an insertion is expressed by: r r E where r is a relation and E is a relational algebra expression. The insertion of a single tuple is expressed by letting E be a constant relation containing one tuple.

  25. Insertion Examples Insert information in the database specifying that Smith has $1200 in account A-973 at the Perryridge branch. account account {( A-973 , Perryridge , 1200)} depositor depositor {( Smith , A-973 )} Provide as a gift for all loan customers in the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account. r1 ( branch_name = Perryridge (borrower loan)) account account loan_number, branch_name,200(r1) depositor depositor customer_name, loan_number (r1) account (account_number, branch_name, balance) loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower(customer_name, loan_number)

  26. Updating A mechanism to change a value in a tuple without charging all values in the tuple Use the generalized projection operator to do this task r ( r ) F ,2 F , , lF , 1

  27. Update Examples Make interest payments by increasing all balances by 5 percent. account account_number, branch_name, balance * 1.05(account) Pay all accounts with balances over $10,000 with 6 percent interest and pay all others 5 percent account account_number, branch_name, balance * 1.06( BAL 10000 (account )) account_number, branch_name, balance * 1.05 ( BAL 10000 (account))

  28. Contoh Banking branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account (account_number, branch_name, balance) loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower(customer_name, loan_number)

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#