Final Revision 2024
Final Revision 2024
Query
Scanning
Parsing
Validating
Intermediate form of Query
(query Tree)
Query
Optimizer
Catalog
Execution Plan
Query Code
Generator
Compile
d Query Executable Code
Code
Execution in
Runtime
processor
SELECT *
FROM Staff s, Branch b
WHERE s.branchNo = b.branchNo AND s.position = ‘Manager’
AND b.city = ‘london’;
Read Staff relation to determine ‘Managers’ (1000 reads)
Create 50 tuple relation(50 writes)
Read Branch relation to determine ‘London’ branches (50 reads)
Create 5 tuple relation(5 writes)
Join reduced relations and check predicate (50 + 5 reads)
Shrinking phase
Existing locks can be released but none can be acquired
Downgrades must be done during this phase
IS IX S SIX X
IS yes yes yes yes no
IX yes yes no no no
S yes no yes no no
SIX yes no no no no
X no no no no no
T1 wants to write the record r211
LOCK: IX(db), IX(f2), IX(p21), X(r211)
Unlock: unlock(r211), unlock(p21), unlock(f2), unlock(db)
T2 wants to read the record r221
LOCK: IS(db), IS(f2), IS(p21), S(r211)
Unlock: unlock(r211), unlock(p21), unlock(f2), unlock(db)
Shadow Paging: The AFIM does not overwrite its BFIM but
recorded at another place on the disk. Thus, at any time a
data item has AFIM and BFIM (Shadow copy of the data
item) at two different places on the disk.
Checkpointing steps:
Threats to databases
Loss of integrity
Improper modification of information
Loss of availability
Valid user cannot access data objects
Loss of confidentiality
Unauthorized disclosure of confidential information
Control measures
Access control
Handled by creating user accounts and passwords
Inference control
Must ensure information about individuals cannot be
accessed
Flow control
Prevents information from flowing to unauthorized
users
Data encryption
Used to protect sensitive transmitted data
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Compare between DAC,MAC and RBAC
(b) Appearance of
EMPLOYEE after filtering for
classification C users
(c) Appearance of
EMPLOYEE after filtering
for classification U users
Code injection
Add additional SQL statements or commands that
are then processed
SELECT * FROM `table`; DROP TABLE
`table`;
TO D;
TO D;
TO A
FROM EMPLOYEE;
type: "movie"
}
)
type: "movie"
},
{
title: "Tag",
genres: [ "Comedy", "Action" ],
runtime: 105,
rated: "R",
year: 2018,
directors: [ "Jeff Tomsic" ],
cast: [ "Annabelle Wallis", "Jeremy Renner", "Jon Hamm" ],
type: "movie"
}
])
• db.movies.find()
• To return all movies where the title equals Titanic from the movies
collection: db.movies.find( { "title": "Titanic" } )
• To return all movies from the movies collection which are either rated
PG or PG-13:
db.movies.find( { rated: { $in: [ "PG", "PG-13" ] } } )
SELECT * FROM movies WHERE rated in ("PG", "PG-13")
• A compound query can specify conditions for more than one field in
the collection's documents.
• Update Documents:
• Update Documents:
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}
use sample_airbnb
db.listingsAndReviews.updateMany(
{ security_deposit: { $lt: 100 } },
{
$set: { security_deposit: 100, minimum_nights: 1 }
}
)
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Perform CRUD Operations
• Replace a Document:
• To replace the entire content of a document except for the _id field,
pass an entirely new document as the second argument to
db.collection.replaceOne().
• The replacement document can have different fields from the original
document.
• In the replacement document, you can omit the _id field since the _id
field is immutable; however, if you do include the _id field, it must
have the same value as the current value.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Perform CRUD Operations
• Replace a Document:
db.accounts.replaceOne(
{ account_id: 371138 },
{ account_id: 893421, limit: 5000, products: [ "Investment",
"Brokerage" ] }
)
• Delete Documents:
• To delete all documents from the movies collection where the title
equals "Titanic":
• To delete the first document from the movies collection where the
cast array contains "Brad Pitt":
db.sales.insertMany([
{ date: ISODate("2023-01-01"), product: "A", quantity: 10, amount: 100 },
{ date: ISODate("2023-01-01"), product: "B", quantity: 5, amount: 50 },
{ date: ISODate("2023-01-02"), product: "A", quantity: 8, amount: 80 },
{ date: ISODate("2023-01-02"), product: "B", quantity: 7, amount: 70 },
{ date: ISODate("2023-01-03"), product: "A", quantity: 12, amount: 120 },
{ date: ISODate("2023-01-03"), product: "B", quantity: 3, amount: 30 }
]);
db.sales.aggregate([
{
$group: {
_id: "$product",
totalAmount: { $sum: "$amount" }
}
}
]);
• Results:
• Equivalent SQL:
db.sales.aggregate([
{
$group: {
_id: "$product",
avgQuantity: { $avg: "$quantity" }
}
}
]);
• Results:
• Equivalent SQL:
db.sales.aggregate([
{
$group: {
_id: "$date",
totalAmount: { $sum: "$amount" }
}
},
{
$sort: { totalAmount: -1 }
},
{
$limit: 1
}
]); Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Aggregation Operations
• Results:
• Equivalent SQL:
SELECT
date,
SUM(amount) AS totalAmount
FROM sales
GROUP BY date
ORDER BY totalAmount DESC
LIMIT 1;
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Aggregation Operations
db.sales.aggregate([
{
$group: {
_id: "$date",
totalAmount: { $sum: "$amount" }
}
},
{
$sort: { _id: 1 }
}
]);
• Results:
{ "_id" : ISODate("2023-01-01T00:00:00Z"), "totalAmount" : 150 }
{ "_id" : ISODate("2023-01-02T00:00:00Z"), "totalAmount" : 150 }
{ "_id" : ISODate("2023-01-03T00:00:00Z"), "totalAmount" : 150 }
• Equivalent SQL:
SELECT
date,
SUM(amount) AS totalAmount
FROM sales
GROUP BY date
ORDER BY date;
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Aggregation Operations
db.sales.aggregate([
{
$group: {
_id: "$date",
totalAmount: { $sum: "$amount" }
}
},
{
$sort: { totalAmount: 1 }
},
{
$limit: 1
}
]); Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Aggregation Operations
• Results:
• Equivalent SQL:
SELECT
date,
SUM(amount) AS totalAmount
FROM sales
GROUP BY date
ORDER BY totalAmount
LIMIT 1;
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Aggregation Operations
db.sales.aggregate([
{
$group: {
_id: "$product",
totalAmount: { $sum: "$amount" }
}
},
{
$match: {
totalAmount: { $gt: 80 }
}
}
]);
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Aggregation Operations
• Results:
• Equivalent SQL:
• Calculate the Total Amount and Average Quantity Sold for Each
Product on January 1, 2023
• Results:
• Equivalent SQL:
SELECT
product, SUM(amount) AS totalAmount, AVG(quantity) AS avgQuantity
FROM sales
WHERE date = '2023-01-01'
GROUP BY product;
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Aggregation Operations
• Filter Products with Total Sales Amount Greater Than 100
db.sales.aggregate([
{
$group: {
_id: "$product",
totalAmount: { $sum: "$amount" }
}
},
{
$match: {
totalAmount: { $gt: 100 }
}
}
]);
• Results:
• Equivalent SQL:
• Results:
• Equivalent SQL:
SELECT
product,
SUM(quantity) AS totalQuantity
FROM sales
GROUP BY product;