Database Management Systems
What to remember
- A DBMS stores data once, keeps it consistent, and lets many users share it safely. It reduces redundancy and gives security, integrity and backup.
- The relational model keeps data in tables (relations) of rows (tuples) and columns (attributes). Keys link the tables.
- SQL is the standard language. DDL defines structure, DML changes data, DCL gives permissions, TCL controls transactions. Normalization removes redundancy.
1. Basic ideas
Data is raw facts. A database is an organised collection of related data. A DBMS (Database Management System) is the software that creates, stores, retrieves and protects the database. Examples are MySQL, Oracle, PostgreSQL and MS Access.
A file system stores data in separate files. It has problems that a DBMS solves.
| Point | File system | DBMS |
|---|---|---|
| Redundancy | High, same data repeated | Low, controlled |
| Consistency | Hard to keep | Easy to keep |
| Sharing | Poor | Many users together |
| Security | Weak | Strong, with permissions |
| Backup and recovery | Manual | Built in |
| Query | Needs a program | Query language such as SQL |
Advantages of a DBMS: less redundancy, data integrity, data independence, concurrent access, security, backup and recovery.
Users of a database: the Database Administrator (DBA) controls the whole system and security; application programmers write programs; end users use the data. The data dictionary (catalog) stores data about data, called metadata.
2. Three-level architecture and data independence
The ANSI-SPARC architecture has three levels:
- 1. Internal (physical) level: how data is stored on disk.
- 2. Conceptual (logical) level: the whole database structure for the community of users, tables and relationships.
- 3. External (view) level: what each user or group sees.
Data independence means a change at one level does not force a change at the next higher level. Physical data independence is a change in storage without changing the conceptual schema. Logical data independence is a change in the conceptual schema without changing the views or programs. Logical independence is harder to achieve.
A schema is the design (structure) of the database. An instance is the data present at a given moment.
3. Data models and the ER model
A data model describes how data is organised. The main types are hierarchical (tree), network (graph), relational (tables) and object-oriented. The relational model, proposed by E. F. Codd in 1970, is the most widely used.
The Entity-Relationship (ER) model is a design tool, introduced by Peter Chen.
- Entity: a real-world thing, such as Student. Entity set: all entities of one type.
- Attribute: a property of an entity. Types are simple, composite, single-valued, multi-valued and derived (for example, age derived from date of birth).
- Relationship: an association between entities. Mapping cardinality can be one-to-one, one-to-many, many-to-one or many-to-many.
- Weak entity: it has no key of its own and depends on a strong (owner) entity.
ER diagram symbols: rectangle for entity, ellipse for attribute, diamond for relationship, double rectangle for weak entity, double ellipse for multi-valued attribute, dashed ellipse for derived attribute.
4. Relational model and keys
- Relation: a table. Tuple: a row (record). Attribute: a column (field). Domain: the set of allowed values for an attribute.
- Degree: number of columns. Cardinality: number of rows.
- Rules: each cell holds one value, row order and column order do not matter, and duplicate rows are not allowed.
| Key | Meaning |
|---|---|
| Super key | Any set of attributes that identifies a row uniquely |
| Candidate key | A minimal super key (no extra attribute) |
| Primary key | The candidate key chosen to identify rows; cannot be NULL or duplicate |
| Alternate key | Candidate keys not chosen as primary |
| Composite key | A key made of two or more attributes |
| Foreign key | An attribute that refers to the primary key of another table |
Integrity rules: *Entity integrity* says a primary key cannot be NULL. *Referential integrity* says a foreign key value must match an existing primary key value in the parent table, or be NULL. A table can have only one primary key but many candidate keys and many foreign keys.
5. Relational algebra
Relational algebra is a procedural query language. Main operations: Select (σ) picks rows; Project (π) picks columns; Union (∪), Intersection (∩), Difference (−); Cartesian product (×); Join (⋈); Rename (ρ). Union, intersection and difference need union-compatible relations (same number and type of columns). Relational calculus is the non-procedural form.
6. SQL (Structured Query Language)
SQL is the standard language of relational databases. It is not case-sensitive for keywords.
| Group | Full form | Commands |
|---|---|---|
| DDL | Data Definition Language | CREATE, ALTER, DROP, TRUNCATE, RENAME |
| DML | Data Manipulation Language | SELECT, INSERT, UPDATE, DELETE |
| DCL | Data Control Language | GRANT, REVOKE |
| TCL | Transaction Control Language | COMMIT, ROLLBACK, SAVEPOINT |
Some books place SELECT in a separate group called DQL.
Common syntax:
- `CREATE TABLE student (roll INT PRIMARY KEY, name VARCHAR(30), marks INT);`
- `INSERT INTO student VALUES (1, 'Ravi', 80);`
- `SELECT name FROM student WHERE marks > 50 ORDER BY name;`
- `UPDATE student SET marks = 90 WHERE roll = 1;`
- `DELETE FROM student WHERE roll = 1;`
DELETE vs TRUNCATE vs DROP: DELETE removes chosen rows and can use WHERE. TRUNCATE removes all rows but keeps the table structure. DROP removes the table itself.
Clauses: WHERE filters rows; GROUP BY groups rows; HAVING filters groups; ORDER BY sorts (ASC is the default). Aggregate functions: COUNT, SUM, AVG, MIN, MAX. LIKE uses `%` for any number of characters and `_` for one character. Use `IS NULL` to test for NULL, never `= NULL`. DISTINCT removes duplicates.
Joins: INNER JOIN returns matching rows only; LEFT, RIGHT and FULL OUTER JOIN also keep unmatched rows of one or both tables; CROSS JOIN gives the Cartesian product.
Constraints: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT.
7. Normalization
Normalization is the process of organising tables to remove redundancy and avoid insertion, deletion and update anomalies. Functional dependency X → Y means each value of X determines exactly one value of Y.
| Normal form | Condition |
|---|---|
| 1NF | All values atomic, no repeating groups |
| 2NF | In 1NF and no partial dependency on part of a composite key |
| 3NF | In 2NF and no transitive dependency |
| BCNF | For every dependency X → Y, X is a super key |
Worked example: Table (RollNo, Subject, Marks, StudentName) with key (RollNo, Subject). StudentName depends only on RollNo, which is a partial dependency. Splitting into Student(RollNo, StudentName) and Marks(RollNo, Subject, Marks) gives 2NF. Denormalization adds redundancy on purpose for speed.
8. Transactions
A transaction is a logical unit of work. The ACID properties are:
- Atomicity: all or nothing.
- Consistency: the database moves from one valid state to another.
- Isolation: transactions do not disturb each other.
- Durability: committed changes survive failure.
Example: moving money from account A to B must debit and credit together, or neither. COMMIT saves changes; ROLLBACK undoes uncommitted changes. A deadlock happens when two transactions wait for each other forever. Locks and serializability give concurrency control. An index speeds up searching, like the index of a book. A view is a virtual table made from a query. A stored procedure is saved SQL code that can be run again.
Worked examples
- 1. Keys: In Student(RollNo, Aadhaar, Name, Phone), both RollNo and Aadhaar identify a row uniquely and neither has an extra attribute, so both are candidate keys. If RollNo is chosen as primary key, Aadhaar becomes an alternate key.
- 2. Degree and cardinality: A table with 5 columns and 40 rows has degree 5 and cardinality 40.
- 3. Aggregate query: `SELECT dept, COUNT(*) FROM emp GROUP BY dept HAVING COUNT(*) > 2;` lists each department with more than two employees.
- 4. Anomalies: If a table repeats a customer's address in every order row, changing the address needs many updates (update anomaly); deleting the only order may lose the customer (deletion anomaly); a new customer cannot be added without an order (insertion anomaly). Splitting into Customer and Order tables removes these.
- 5. Cartesian product: if relation R has 4 rows and S has 3 rows, R × S has 12 rows and the degree is the sum of both degrees.
Exam traps
- Primary key vs candidate key: the primary key is only the chosen one of the candidate keys.
- Degree is the number of columns; cardinality is the number of rows.
- DELETE vs TRUNCATE vs DROP: rows chosen, all rows, whole table.
- WHERE filters rows before grouping; HAVING filters groups after grouping.
- DDL vs DML: CREATE, ALTER, DROP are DDL; SELECT, INSERT, UPDATE, DELETE are DML.
- Foreign key can be NULL and duplicate; primary key cannot be NULL.
- Physical vs logical data independence: logical is harder.
- The select operation (σ) picks rows; project (π) picks columns.
One-liners
- 1. A DBMS is software that manages databases.
- 2. Metadata means data about data.
- 3. The relational model was proposed by E. F. Codd.
- 4. The ER model was proposed by Peter Chen.
- 5. A row of a table is a tuple.
- 6. A primary key cannot be NULL.
- 7. A foreign key refers to a primary key of another table.
- 8. GRANT and REVOKE are DCL commands.
- 9. COMMIT and ROLLBACK are TCL commands.
- 10. 1NF means atomic values.
- 11. ACID stands for Atomicity, Consistency, Isolation, Durability.
- 12. A view is a virtual table.
Practice questions
Who proposed the relational model of data?
- Charles Babbage
- Peter Chen
- E. F. Codd
- Dennis Ritchie
Answer
C. E. F. Codd
The relational model was proposed by E. F. Codd in 1970.
In a relational table, a row is also called a
- schema
- tuple
- attribute
- domain
Answer
B. tuple
A row is a tuple; a column is an attribute.
The number of columns in a relation is called its
- cardinality
- degree
- instance
- domain
Answer
B. degree
Degree = number of columns; cardinality = number of rows.
Data about data is known as
- big data
- microdata
- metadata
- raw data
Answer
C. metadata
The data dictionary stores metadata.
Which key cannot hold a NULL value?
- Foreign key
- Alternate key only
- Super key only
- Primary key
Answer
D. Primary key
Entity integrity forbids NULL in a primary key.
Which SQL command removes the table structure itself along with its data?
- DROP
- DELETE
- UPDATE
- TRUNCATE
Answer
A. DROP
DROP deletes the whole table; DELETE and TRUNCATE remove rows only.
GRANT and REVOKE belong to
- DML
- DCL
- DDL
- TCL
Answer
B. DCL
Data Control Language gives or takes back permissions.
Which command saves the changes of a transaction permanently?
- SAVEPOINT
- ROLLBACK
- COMMIT
- REVOKE
Answer
C. COMMIT
COMMIT makes changes permanent; ROLLBACK undoes them.
The symbol used for a relationship in an ER diagram is a
- triangle
- diamond
- ellipse
- rectangle
Answer
B. diamond
Rectangle = entity, ellipse = attribute, diamond = relationship.
The ACID property that means 'all or nothing' is
- Consistency
- Isolation
- Atomicity
- Durability
Answer
C. Atomicity
Atomicity means a transaction is completed fully or not at all.
Which SQL clause is used to filter groups formed by GROUP BY?
- HAVING
- WHERE
- DISTINCT
- ORDER BY
Answer
A. HAVING
HAVING filters groups; WHERE filters rows before grouping.
A foreign key in a table refers to
- a NULL value only
- any non-key column of the same table
- the primary key of another table
- the DBA of the system
Answer
C. the primary key of another table
A foreign key links to a primary key of the parent table.
In relational algebra, the operation that picks specific columns is
- Union
- Cartesian product
- Select
- Project
Answer
D. Project
Project (π) picks columns; Select (σ) picks rows.
The person who controls the whole database system and its security is the
- compiler
- application programmer
- end user
- DBA
Answer
D. DBA
The Database Administrator manages and secures the database.
Which is the first normal form that removes partial dependencies?
- 1NF
- 2NF
- BCNF
- 3NF
Answer
B. 2NF
2NF removes partial dependency on part of a composite key.
Which is the first normal form that removes transitive dependencies?
- 3NF
- 1NF
- 2NF
- BCNF
Answer
A. 3NF
3NF = 2NF with no transitive dependency.
A table has 6 columns and 25 rows. Its degree and cardinality are
- 25 and 6
- 6 and 25
- 6 and 150
- 31 and 6
Answer
B. 6 and 25
Degree is column count (6); cardinality is row count (25).
Which statement correctly finds names that start with the letter A?
- SELECT name FROM t WHERE name = 'A%';
- SELECT name FROM t WHERE name LIKE '%A';
- SELECT name FROM t HAVING name = 'A';
- SELECT name FROM t WHERE name LIKE 'A%';
Answer
D. SELECT name FROM t WHERE name LIKE 'A%';
LIKE with % matches any number of characters after A.
To test for missing values in SQL one should write
- WHERE marks == 0
- WHERE marks = NULL
- WHERE marks IS NULL
- WHERE marks LIKE NULL
Answer
C. WHERE marks IS NULL
NULL is tested with IS NULL; = NULL never matches.
A relation R has 4 rows and relation S has 5 rows. The Cartesian product R × S has how many rows?
- 9
- 1
- 20
- 45
Answer
C. 20
Cartesian product gives 4 × 5 = 20 rows.
A bank transfer must debit one account and credit the other together or not at all. This is ensured by
- atomicity
- normalization
- redundancy
- data independence
Answer
A. atomicity
Atomicity makes the two steps one unit.
Changing how files are stored on disk without changing the conceptual schema shows
- physical data independence
- logical data independence
- entity integrity
- referential integrity
Answer
A. physical data independence
Physical independence hides storage changes from the conceptual level.
Which of the following SQL statements removes only some rows of a table?
- ALTER TABLE student;
- TRUNCATE TABLE student;
- DROP TABLE student;
- DELETE FROM student WHERE marks < 35;
Answer
D. DELETE FROM student WHERE marks < 35;
DELETE with WHERE removes selected rows.
A candidate key that is not chosen as the primary key is called an
- foreign key
- alternate key
- weak key
- composite key
Answer
B. alternate key
Unchosen candidate keys are alternate keys.
A table where each cell holds a single atomic value and has no repeating groups is in at least
- BCNF
- 1NF
- 3NF
- 2NF
Answer
B. 1NF
1NF requires atomic values.
A virtual table created from the result of a query is called a
- view
- trigger
- cursor
- index
Answer
A. view
A view stores a query, not separate data.
Which of these is an aggregate function?
- LIKE
- ORDER
- JOIN
- AVG
Answer
D. AVG
AVG, SUM, COUNT, MIN, MAX are aggregate functions.
Which type of join returns only the rows that match in both tables?
- FULL OUTER JOIN
- LEFT OUTER JOIN
- INNER JOIN
- CROSS JOIN
Answer
C. INNER JOIN
INNER JOIN keeps only matching rows.
A weak entity in an ER diagram is shown by a
- single diamond
- double ellipse
- dashed ellipse
- double rectangle
Answer
D. double rectangle
Weak entity = double rectangle; multi-valued attribute = double ellipse.
Which SQL constraint ensures that all values in a column are different?
- UNIQUE
- CHECK
- NOT NULL
- DEFAULT
Answer
A. UNIQUE
UNIQUE blocks duplicate values; NOT NULL only blocks missing values.
The order of clauses in a typical SELECT query is
- SELECT, WHERE, FROM, HAVING, GROUP BY, ORDER BY
- SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY
- FROM, SELECT, GROUP BY, WHERE, ORDER BY, HAVING
- SELECT, FROM, GROUP BY, WHERE, HAVING, ORDER BY
Answer
B. SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY
This is the standard written order of clauses.
Consider these statements about keys. 1. A table can have only one primary key. 2. A foreign key value can never be NULL. Which is/are correct?
- 1 only
- 2 only
- Both 1 and 2
- Neither 1 nor 2
Answer
A. 1 only
A table has one primary key; foreign keys may be NULL, so 2 is wrong.
Consider these statements about SQL. 1. TRUNCATE removes all rows but keeps the table structure. 2. DELETE cannot use a WHERE clause. Which is/are correct?
- 1 only
- 2 only
- Both 1 and 2
- Neither 1 nor 2
Answer
A. 1 only
DELETE can use WHERE, so statement 2 is wrong.
Consider these statements. 1. DROP is a DDL command. 2. INSERT is a DML command. Which is/are correct?
- 1 only
- 2 only
- Both 1 and 2
- Neither 1 nor 2
Answer
C. Both 1 and 2
Both are correct classifications.
Consider these statements about normalization. 1. It reduces data redundancy. 2. It helps avoid insertion, deletion and update anomalies. Which is/are correct?
- 1 only
- 2 only
- Both 1 and 2
- Neither 1 nor 2
Answer
C. Both 1 and 2
Both are goals of normalization.
Consider these statements about ACID. 1. Durability means committed changes survive a system failure. 2. Isolation means transactions run without interfering with each other. Which is/are correct?
- 1 only
- 2 only
- Both 1 and 2
- Neither 1 nor 2
Answer
C. Both 1 and 2
Both describe the properties correctly.
Consider these statements. 1. A view stores a separate copy of the data permanently. 2. An index speeds up searching. Which is/are correct?
- 1 only
- 2 only
- Both 1 and 2
- Neither 1 nor 2
Answer
B. 2 only
A view is virtual and does not store a separate copy; so only 2 is correct.
Consider these statements about data independence. 1. Logical data independence is easier to achieve than physical data independence. 2. Physical independence means storage changes do not affect the conceptual schema. Which is/are correct?
- 1 only
- 2 only
- Both 1 and 2
- Neither 1 nor 2
Answer
B. 2 only
Logical independence is harder; so only 2 is correct.
Match the SQL command with its group. P. SELECT Q. GRANT R. ROLLBACK 1. TCL 2. DCL 3. DML
- P-3, Q-1, R-2
- P-1, Q-2, R-3
- P-2, Q-3, R-1
- P-3, Q-2, R-1
Answer
D. P-3, Q-2, R-1
SELECT is DML (or DQL), GRANT is DCL, ROLLBACK is TCL.
Match the ER symbol with its meaning. P. Rectangle Q. Ellipse R. Diamond 1. Relationship 2. Entity 3. Attribute
- P-2, Q-1, R-3
- P-1, Q-3, R-2
- P-3, Q-2, R-1
- P-2, Q-3, R-1
Answer
D. P-2, Q-3, R-1
Rectangle is an entity, ellipse an attribute, diamond a relationship.
Match the key with its description. P. Super key Q. Candidate key R. Foreign key 1. Minimal unique identifier 2. Refers to another table's key 3. Any attribute set that identifies rows uniquely
- P-3, Q-1, R-2
- P-2, Q-1, R-3
- P-1, Q-3, R-2
- P-3, Q-2, R-1
Answer
A. P-3, Q-1, R-2
A super key may have extra attributes; a candidate key is minimal.
Table Emp(Id, Name, Dept) with Id as primary key. Which query counts the employees in each department?
- SELECT Dept, COUNT(*) FROM Emp ORDER BY Dept;
- SELECT COUNT(Dept) FROM Emp WHERE Dept;
- SELECT Dept FROM Emp HAVING COUNT(*);
- SELECT Dept, COUNT(*) FROM Emp GROUP BY Dept;
Answer
D. SELECT Dept, COUNT(*) FROM Emp GROUP BY Dept;
GROUP BY with COUNT(*) gives a count per department.
A relation has attributes (RollNo, Subject, Marks, StudentName) with key (RollNo, Subject). StudentName depends only on RollNo. This is a
- multi-valued attribute
- partial dependency
- referential constraint
- transitive dependency
Answer
B. partial dependency
Dependence on part of a composite key is partial dependency, removed in 2NF.
Two transactions each hold a lock the other needs and both wait forever. This situation is called
- normalization
- atomicity
- deadlock
- durability
Answer
C. deadlock
Waiting forever for each other's locks is deadlock.
Which one is a relational database management system?
- Photoshop
- Notepad
- MySQL
- Python IDLE
Answer
C. MySQL
MySQL, Oracle and PostgreSQL are RDBMS software.