Download Database lecture 03 slides ppt format and more Slides Database Management Systems (DBMS) in PDF only on Docsity!
Database Systems
Khwaja Fareed University of Engineering &
IT
Lecture 3 Lecture 3
The Relational Algebra The Relational Algebra
Information is extracted from a database by writing queries
query inputs: relations
query result: relation
queries are read-only operations
The SQL command for queries select
The relational algebra defines mathematical operations on relations
provides a solid theory for dealing with queries
These operations define the meaning of SQL queries
think in terms of the algebra operators
translate the algebra to SQL syntax
Query optimization:
SQL queries are translated to expression trees
containing algebraic operators
expression trees are reorganized
to optimize the order of operations
set theoretic:
union , intersection , difference , cross-product
usual mathematical meaning
relational database unique:
select , project : extraction from a single relation
join : combining two relations
"S002", "I065", 120 >, "S333", "I954", 198 >, "S047", "I099", 267 >, "S047", "I954", 300 > r 2 (R)STOR)ESTOCK) =
StoreId = "S047"
(R)STOR)ESTOCK) "S047", "I099", 267 >, "S047", "I954", 300 >
quantity 200
(R)STOR)ESTOCK) "S002", "I065", 120 >, "S333", "I954", 198 >
PR)OJECT extracts attributes from a relation
result schema attributes are a subset
of the operand schema
PR)OJECT requires a attribute list
Syntax:
attribute list>
(R)R)) Duplicates are not kept in result
result is a relation, which is a set
"Ice Cream", $1.49 >, "Cupcakes", $1.99 >, "Twinkies", $1.98 >
Description, Price
(R)STOR)EITEM) r(R)STOCKITEM) = "I075", "Ice Cream", $1.49, false >, "I345", "Cupcakes", $1.99, false >, "I333", "Twinkies", $1.98, false >
Price
(R)
Description="Ice Cream"
(R)STOR)EITEM)) r(R)STOCKITEM) = "I075", "Ice Cream", $1.49, false >, "I345", "Cupcakes", $1.99, false >, "I333", "Twinkies", $1.98, false > "I075", "Ice Cream", $1.49, false > $1.49 > SELECT result: PR)OJECT result:
STOR)ESTOCK(R) StoreId, Item, Quantity ) STOCKITEM(R) ItemId, Description, Price, Taxable ) STOR)ESTOCK ⋈ Item = ItemId> STOCKITEM "S002", "I075", 120 >, "S047", "I333", 267 > r(R)STOR)ESTOCK) = "I075", "Ice Cream", $1.49, false >, "I345", "Cupcakes", $1.99, false >, "I333", "Twinkies", $1.98, false > r(R)STOCKITEM) =
R)ESULT(R) StoreId, Item, Quantity, ItemId, Description, Price, Taxable )
"S002", "I075", 120, "I075", "Ice Cream", $1.49, false >, "S047", "I333", 267, "I333", "Twinkies", $1.98, false > r(R)R)ESULT) = STOR)ESTOCK ⋈ Item = ItemId> STOCKITEM
StoreId Item Quantity
STOR)ESTOCK
S002 I075 120
S047 I333 267
STOCKITEM
ItemId Description Price Taxable I075 Ice Cream $1.49 FALSE I333 Twinkies $1.98 FALSE I345 Cup Cakes $1.99 FALSE
S002 I333 1200
Query: Get the Description and Price for all Items stocked by Store S We need a join to merge data across relations
StoreId Item Quantity S002 I075 120 S047 I333 267 ItemId Description Price Taxable I075 Ice Cream $1.49 FALSE I333 Twinkies $1.98 FALSE S002 I333 1200 I333 Twinkies $1.98 FALSE Query: Get the Description and Price for all Items stocked by Store S002 Now we can select and project to extract the information we want STOR)ESTOCK ⋈ Item = ItemId> STOCKITEM
R)2 = StoreId = "S002" (R)R)1) StoreId Item Quantity S002 I075 120 ItemId Description Price Taxable I075 Ice Cream $1.49 FALSE S002 I333 1200 I333 Twinkies $1.98 FALSE R)3 = Description, Price> (R)R)2) Description Price Ice Cream $1. Twinkies $1.
R)2 = StoreId = "S002" (R)R)1) R)3 = Description, Price> (R)R)2) R)1 = STOR)ESTOCK ⋈ Item = ItemId> STOCKITEM Description, Price>(R)StoreId = "S002" (R)STOR)ESTOCK ⋈Item = ItemId>
STOCKITEM))
Since the operands and results of every query are relations, we can compose or chain queries.