Files
G4G0-1/Semester 2/Database Systems/Trimester 2 Assignment.md

7.0 KiB

User ID CHC119
Roll Number 00677611

Q1

a)

i)

1 tuple = 24 + 5 + 10 + 20 + 6 = 65b 1 block = 1024 - 24 = 1000b 1000 / 65 = 15.38, 15 tuples can be stored in 1 block 800 / 15 = 53.33, 54 blocks are needed to store all tuples in this relation.

ii)

65 - 20 = 45b 1000 / 45 = 22.22, 22 tuples per block 800 / 22 = 36.36, 37 blocks for this projection.

iii)

If the "street" attribute is unneeded, less IO operations are needed to retrieve the relevant data from disk, making the query more efficient. As a result, this also uses less disk space, and optimises memory usage for processing.

b)

i)

ii)

Root Node: \pi ( factory.name ) Leaf Nodes: factory, order

iii)

Line-by-line:

  1. This is the final attribute we want to obtain. Placing this here ensures only the necessary data is output; reduces IO operations and memory usage, as only the required data is given.
  2. Exists to join the 2 tables together based on the condition that the order's factory ID = factory's ID, allowing the projection root node to have the necessary data from the factory table to obtain the factory's name.
  3. These projections are necessary to optimise the dataset by reducing the size of the total data when the tables are joined by removing irrelevant columns we would not need for the output of the query. This would decrease the amount of memory and disk space used by the query, and reduce the CPU cycle cost for the theta join.
  4. There is an argument for this being above or below the theta join, however I have placed this here due to the heuristic principles of query optimisation, moving selection operations down the tree. This exists to "filter" the result on the condition that the order is urgent. By placing this selection after the theta join, we can "filter" more selectively and reduce the dataset size to optimise memory usage, similar to the adjacent selection operation.
  5. This exists to "filter" the dataset on the condition that the quantity is larger than 20 units. By placing this at the start, we ensure filtering is executed earlier, as to not waste compute resources. This reduces the dataset size, allowing the following operations to be more optimised, as there is less data to be processed.

Q2

a)

Atomicity: A transaction must be completed, or aborted. If a transaction fails, a DBMS ensures the rollback of data to allow the database to remain consistent. Consistency: Database should be in a consistent state, prior to a transaction and after. A DBMS would enforce constraints on a transaction, assuring the defined rules / relationships are followed. Isolation: All transactions should by fully independent from concurrently executing transactions. Reduces concurrency issues by restricting access to shared data until a transaction is committed. Durability: Guarantees data is permanent once committed, preventing inconsistency after failures. Should withstand system failures or restarts.

b)

To guarantee consistency, a DBMS handles the enforcement of atomicity, consistency, isolation and durability, along with concurrency control to prevent conflicts.

Ideal SQL Transfer of crates:

UPDATE warehouse
SET crates = crates - 3
WHERE wname = 'warehouse1'

UPDATE warehouse
SET crates = crates + 3
WHERE wname = 'warehouse2';

If the second query is not run, it is not the fault of the DBMS. A DBMS can only enforce ACID rules and ensure consistency, not transaction logic.

c)

By allowing concurrent execution of transactions, system resources are allocated more efficiently due to multiple operations occurring simultaneously. This has an effect of being more performant, since more transactions can be processed per second.

d)

i)

Transaction 1 updates multiple values while Transaction 2 is reading, as can be seen in t4, as 10 units are added to W1, so the sum would be 10 units too low.

ii)

Inconsistent Analysis

e)

i)

ii)

The graph is not serialisable, since there is a directed cycle in the precedence graph - the graph is cyclic.

Q3

a)

An exclusive lock is used when a transaction must update / write to an item to avoid conflicts.

b)

2-phase locking consists of a growing phase and a shrinking phase. A transaction must acquire a read or write lock on an item before operation (growing phase), after which it must release all locks and acquire no more (shrinking phase). Transactions do not have to simultaneously acquire locks, but must adhere to these rules.

c)

i)

ii)

Deadlock does not occur, since there are no cycles in the graph

d)

i)

T1 starts and commits before the checkpoint, the recovery manager would recognise this and take no action, since the data is consistent in the database: the transaction is safe. T2 also starts and commits before the checkpoint; the recovery manager would take the same action as with T1, as the data is consistent: transaction is safe. T3 commits after the checkpoint, so must be active during the checkpoint; for this reason, the recovery manager must redo the transaction to maintain consistency in the database. T4 is active at the point of failure - since the transaction never completed, it must be undone as the database is no longer consistent; values may have been made durable before the transaction had committed.

ii)

Using the immediate update protocol, the recovery manager would first analyse the recovery log, working backwards to identify in-progress transactions at the time of failure, and determine the last record of a checkpoint. Following this, the recovery manager creates 2 lists: undo, and redo. Then by compensating for changes made until <T, start>, recorded in the recovery log, changes are undone for transactions running at the time of failure. Finally, the recovery manager reapplies changes recorded in the log for committed transactions before the time of failure, the recovery manager redoes the necessary transactions ensuring the database is consistent with updates prior to the failure.

Q4

a)

i)

SELECT partName
FROM part
WHERE partPrice > 100;

ii)

SELECT carModel
FROM car
JOIN carPart ON car.carId = carPart.carId
GROUP BY car.carModel
HAVING COUNT(carPart.partID) > 4;

b)

If the DBA revoked auth from U2 -> U1, U4, and U5 would also have auth revoked due to cascading revokes. There is no path from U1, U4 or U5 to the DBA if U2's authorisation is revoked. U3 and U6 still have a path to the DBA, as they do not depend on U2 for authorisation, authorisation is still valid.

c)

i)

GRANT ALL PRIVILEGES ON * TO Isavella

CREATE VIEW employeeView AS
	SELECT *
	FROM employee

GRANT SELECT, UPDATE ON employeeView TO Laura, Naser;

ii)

CREATE VIEW projectsView AS
	SELECT *
	FROM project
	WHERE level = 'advanced'

GRANT SELECT ON projectsView TO Jane;

iii)

REVOKE ALL PRIVILEGES ON * FROM Naser;