Advanced Databases, Past Papers, UOS Main Campus

Advanced Databases Past Paper 2016 – BSCS 7th Term UOS (CS-371)

ProgramBSCS
Term7th
Year2016
Course codeCS-371
AnswersSolved ✓

University of Sargodha BS Computer Science 7th term Advanced Databases (CS-371) paper, 2016 exam (80 marks, 2:30 hours). The paper has been retyped, and solved answers are given below for all 16 short questions and all six long questions, including the six SQL clauses, immediate-update recovery, multilevel indexing, partitioned hashing, disk access time, nested queries, outer joins and triggers.

Solved answers

Q1 – Short answers (16 × 2)

I. Reading/writing data to disk: Data is transferred in whole blocks between disk and main-memory buffers. The disk controller moves the read/write head to the right cylinder (seek), waits for the sector to rotate under it (rotational delay) and transfers the block to a buffer in memory (or from the buffer to the disk).

II. Components of a disk block address: Cylinder (track) number, surface (head) number and block/sector number – or a single logical block address (LBA) that the controller maps to them.

III. Hashing to construct an index: A hash function is applied to the search-key value to compute a bucket address; the bucket stores <key, record pointer> entries, so a record is found with about one or two block accesses (hash index; static, extendible or linear hashing).

IV. Intention locks: Locks used in multiple-granularity locking (IS, IX, SIX). A transaction sets an intention lock on a higher-level node (e.g. table) to show that it will lock lower-level items (pages/rows) in shared or exclusive mode, so conflicts can be detected at the higher level.

V. When are latches used? Latches are short-duration locks that protect internal data structures in memory (e.g. a buffer page or index page) while it is being read or written; they are held only during the physical operation, not until the end of the transaction, and do not follow 2PL.

VI. Primary, secondary and clustering indexes: Primary index – on the ordering key field of an ordered file, one entry per block (sparse). Clustering index – on an ordering non-key field, one entry per distinct value. Secondary index – on any non-ordering field, one entry per record (dense); a file can have many secondary indexes but only one primary or clustering index.

VII. Locks for insert and delete: An exclusive (write) lock on the record being inserted or deleted; to avoid phantoms, locks on index entries/ranges (index or predicate locking) are also needed.

VIII. Risks of SQL injection: Attackers can bypass authentication, read confidential data, modify or delete data, run administrative operations, call stored procedures or OS commands and even take over the database server (privilege escalation, denial of service).

IX. Preventive measures: Use parameterised (prepared) statements or bind variables, stored procedures, input validation and escaping, filtering of special characters, least-privilege database accounts, and avoiding dynamic SQL built from user input.

X. Statistical database: A database used to produce statistical information (counts, averages, totals) about populations, where users may query aggregate values but not individual records (e.g. census data); it must be protected against inference of individual data.

XI. Symmetric-key algorithm: An encryption algorithm in which the same secret key is used to encrypt and decrypt data, e.g. DES, 3DES, AES. It is fast but the key must be shared securely.

XII. Public key infrastructure (PKI): A framework of certificate authorities, registration authorities, policies and software that issues, manages and revokes digital certificates binding public keys to identities, enabling secure encryption and digital signatures.

XIII. Information in a digital certificate: Owner’s name/identity, owner’s public key, issuer (CA) name, serial number, validity period (from–to dates), signature algorithm and the CA’s digital signature (plus extensions such as key usage).

XIV. Allocating file blocks on disk: Contiguous allocation, linked allocation, clusters (contiguous groups linked together) and indexed allocation (index blocks pointing to data blocks); combinations are common.

XV. File organisation vs access method: File organisation is how the records are physically arranged on disk (heap, sorted, hashed, B+-tree). An access method is the group of operations/programs used to access the records (find, read next, insert, delete) – one organisation can support several access methods.

XVI. UNDO and REDO: UNDO restores the before-image of items written by a transaction that did not commit (rollback); REDO re-applies the after-image of committed transactions whose changes may not be on disk. Deferred update uses NO-UNDO/REDO; immediate update uses UNDO/REDO (or UNDO/NO-REDO if all writes are forced before commit); shadow paging needs NO-UNDO/NO-REDO.

Q2 – Six clauses of an SQL retrieval query

SELECT    <attribute and function list>
FROM      <table list>
[WHERE    <condition>]
[GROUP BY <grouping attribute(s)>]
[HAVING   <group condition>]
[ORDER BY <attribute list> [ASC | DESC]]
  • SELECT (required): the attributes, expressions, aggregate functions (COUNT, SUM, AVG, MAX, MIN), DISTINCT, aliases to be retrieved.
  • FROM (required): the tables, views, joined tables (JOIN … ON, NATURAL JOIN, OUTER JOIN) and table aliases needed by the query.
  • WHERE (optional): selection and join conditions on individual tuples – comparison operators, AND/OR/NOT, BETWEEN, LIKE, IN, IS NULL, EXISTS, nested queries.
  • GROUP BY (optional): attributes used to partition tuples into groups for aggregate functions.
  • HAVING (optional): a condition on whole groups (usually with aggregates), e.g. HAVING COUNT(*) > 2.
  • ORDER BY (optional): the order of the result rows, ascending or descending.

Only SELECT and FROM are required; the other four are optional. The clauses are evaluated in the order FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.

SELECT Dno, COUNT(*) AS Emps, AVG(Salary)
FROM EMPLOYEE
WHERE Sex = 'F'
GROUP BY Dno
HAVING COUNT(*) > 2
ORDER BY Dno;

Q3 – Immediate update recovery

In immediate update, the database on disk may be updated by a transaction before it commits, but every update is first recorded in the log (write-ahead logging: the log record with the before- and after-image is forced to disk before the data item).

  • Single-user environment: after a crash there is at most one active transaction. Recovery (UNDO/REDO): use the log to UNDO all write operations of the uncommitted transaction in reverse order (restore before-images), and REDO the writes of committed transactions in the order they appear in the log (from the last checkpoint). If all updates are forced to disk before commit, only UNDO is needed (UNDO/NO-REDO).
  • Multiuser environment: concurrency control (usually strict two-phase locking) is assumed, so transactions do not read uncommitted data and cascading rollback is avoided. Using checkpoints, the system builds a list of committed transactions since the last checkpoint and a list of active ones; it UNDOes all writes of active (uncommitted) transactions in reverse log order and REDOes the writes of committed transactions in forward order.

Advantages: updates are written as soon as possible, so the buffer does not have to hold all changes until commit (less memory); commit is faster because pages may already be on disk; allows long transactions. Disadvantages: requires UNDO as well as REDO, so recovery is more complex and slower; more log information (before-images) must be kept; possible cascading rollback if not using strict locking; frequent disk writes.

Q4 – Multilevel indexing

A single-level ordered index on a file with bi index blocks is searched by binary search, needing about ⌈log2 bi⌉ block accesses. A multilevel index treats the index itself as an ordered file and builds an index on it (second level), then on that (third level), until the top level fits in one block. With fan-out fo (index entries per block), each level reduces the search space by a factor of fo, so a search needs only about ⌈logfo bi⌉ + 1 block accesses.

Example: a first-level index of 442 blocks with fan-out 68: binary search needs ⌈log2442⌉ = 9 accesses; a multilevel index needs 2nd level ⌈442/68⌉ = 7 blocks, 3rd level 1 block, so 3 levels + 1 data block = 4 accesses. Dynamic multilevel indexes (B-trees and B+-trees) keep the tree balanced and leave free space in nodes for efficient insertion and deletion.

Q5 – Partitioned hashing

Partitioned hashing is a multi-attribute hashing technique (an extension of static external hashing) for files that are searched on several fields. For a search key of n attributes, a separate hash function is applied to each attribute, producing a few bits each; the bit strings are concatenated to form the bucket address.

How it works – example: key (Dno, Age): Dno hashed to 3 bits and Age to 5 bits gives an 8-bit bucket address. For Dno = 4 → 100 and Age = 59 → 10101, the record goes to bucket 100 10101. A query with both values goes to one bucket. A query with only Dno = 4 searches all 32 buckets whose address begins with 100, and a query on Age alone searches the 8 buckets ending in 10101 – so it supports partial-match queries on any subset of the key attributes.

Limitations: it cannot handle range queries (hashing destroys order); many buckets must be searched when only some attributes are given; skewed data can cause overflow and uneven buckets; the number of bits per attribute is fixed in advance, so the file cannot grow well (static hashing).

Q6 – Why accessing a disk block is expensive

Disk access is mechanical and is thousands of times slower than main memory (milliseconds vs nanoseconds). The time to access a block consists of:

  • Seek time (s): moving the read/write arm to the correct track/cylinder – typically 4–10 ms; the largest component.
  • Rotational delay/latency (rd): waiting for the required block to rotate under the head – on average half a revolution; at 7,200 rpm one revolution takes 8.33 ms, so the average is about 4.17 ms.
  • Block transfer time (btt): actually reading/writing the block’s bytes – small, e.g. block size / transfer rate.
  • Plus controller and queueing overhead.

So a random block access costs about s + rd + btt ≈ 9 + 4 + 0.1 ≈ 13 ms, whereas reading consecutive blocks on the same cylinder avoids repeated seeks (bulk transfer). This is why file organisations, buffering, indexing and clustering aim to minimise the number of block accesses.

Q7 – Nested queries, joined tables and triggers

a) Nested queries: a complete SELECT inside the WHERE (or FROM/HAVING) clause of another query, used with IN, comparison operators with ANY/ALL, and EXISTS. Correlated nested queries refer to attributes of the outer query.

SELECT Fname, Lname FROM EMPLOYEE
WHERE Dno IN (SELECT Dnumber FROM DEPARTMENT WHERE Dname = 'Research');

b) Joined tables and outer joins: a join can be written in the FROM clause; an inner join returns only matching tuples, while LEFT/RIGHT/FULL OUTER JOIN also keep non-matching tuples, filling missing values with NULL.

SELECT E.Lname, S.Lname AS Supervisor
FROM EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E.Super_ssn = S.Ssn;

c) Triggers: active rules (event–condition–action) that the DBMS executes automatically when a specified event (INSERT, UPDATE, DELETE) occurs, used for enforcing constraints, auditing and maintaining derived data.

CREATE TRIGGER salary_violation
BEFORE INSERT OR UPDATE OF Salary ON EMPLOYEE
FOR EACH ROW
WHEN (NEW.Salary > (SELECT Salary FROM EMPLOYEE WHERE Ssn = NEW.Super_ssn))
  INFORM_SUPERVISOR(NEW.Super_ssn, NEW.Ssn);
Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
↓ Download Paper (JPG)

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.