Databases and DBMS: Keys, Normal Forms, SQL Commands and MCQs
A database is an organised collection of related data, and a database management system (DBMS) is the software that stores, retrieves and protects it. Banks, NADRA, railway reservation systems and university result portals all run on databases, so computer operator, data entry and IT officer papers ask about keys, table terms, normal forms and SQL commands. The vocabulary is precise, so learn each term with a small example table in mind.
Asked in: PPSC, FPSC, NTS, ETEA, KPPSC, SSC, IBPS and ICS/ECAT computer
Data, databases and the DBMS
Before databases, organisations kept data in separate files for each department. The same facts were stored many times (data redundancy), copies went out of step (inconsistency) and sharing data was hard. A DBMS keeps data in one controlled place and offers reduced redundancy, consistency, sharing among many users, security through user rights, integrity rules, and backup and recovery.
Popular DBMS products include Oracle, MySQL, Microsoft SQL Server, PostgreSQL and Microsoft Access. The person responsible for designing, securing and maintaining a database is the database administrator (DBA). Data about data, such as table names, column types and constraints, is called metadata and is kept in the data dictionary.
Databases follow a data model. The hierarchical model arranges records in a tree (one parent, many children); the network model allows a record to have many parents (a graph); the relational model, proposed by E. F. Codd in 1970, stores data in tables; and the object-oriented model stores objects. Relational databases (RDBMS) are by far the most common today.
- Database = organised collection of related data
- DBMS = software to create, manage and control access to a database
- Metadata = data about data (kept in the data dictionary)
- Relational model: tables, proposed by E. F. Codd (1970)
- Hierarchical = tree; Network = graph; Relational = tables
Tables, rows, columns and their formal names
In the relational model a table is called a relation. Each row is a tuple (also called a record), and each column is an attribute (also called a field). The set of values a column is allowed to take, for example whole numbers from 0 to 100 for marks, is its domain.
The degree of a relation is its number of attributes (columns); the cardinality is its number of tuples (rows). A table with 6 columns and 40 rows therefore has degree 6 and cardinality 40. Adding a student changes the cardinality, not the degree.
The schema is the design or structure of the database (table names, columns and types) and changes rarely. The instance is the actual data stored at a particular moment and changes whenever rows are added, edited or removed. A view is a virtual table built from a query; it shows data from one or more tables without storing a separate copy.
- Relation = table; Tuple = row/record; Attribute = column/field
- Domain = set of allowed values of an attribute
- Degree = number of columns; Cardinality = number of rows
- Schema = structure; Instance = data at a given moment
Keys
A super key is any set of one or more columns whose values identify each row uniquely. A candidate key is a minimal super key: remove any column from it and it stops being unique. A table can have several candidate keys; for students these might be RollNo and CNIC.
The primary key is the one candidate key chosen by the designer to identify rows. Its values must be unique and can never be NULL (empty), and a table has only one primary key. The remaining candidate keys are called alternate keys. A primary key made of two or more columns together, such as (RollNo, SubjectCode), is a composite key.
A foreign key is a column in one table that refers to the primary key of another (or the same) table, creating a link between them. Unlike a primary key, foreign key values may repeat, because many enrolments can belong to one student. Referential integrity is the rule that every foreign key value must match an existing primary key value (or be NULL if allowed), so an enrolment cannot point to a student who does not exist.
- Super key: identifies rows uniquely (may have extra columns)
- Candidate key: minimal super key
- Primary key: chosen candidate key; unique, never NULL, one per table
- Alternate key: candidate key not chosen as primary
- Composite key: key made of two or more columns
- Foreign key: refers to another table's primary key; may repeat
The entity-relationship (ER) model
Database design usually starts with an ER diagram, a model introduced by Peter Chen in 1976. An entity is a real-world thing about which data is stored, such as Student or Course; an attribute is a property of an entity, such as Name; a relationship is an association between entities, such as Student enrols in Course.
In Chen notation an entity is drawn as a rectangle, an attribute as an ellipse (oval) and a relationship as a diamond. A key attribute is underlined. A multivalued attribute, such as several phone numbers, uses a double ellipse; a derived attribute, such as age worked out from date of birth, uses a dashed ellipse; a weak entity, which cannot be identified without another entity, uses a double rectangle.
Cardinality ratios describe how many instances take part in a relationship: one-to-one (a country has one capital), one-to-many (one department has many employees) and many-to-many (many students take many courses). A many-to-many relationship becomes a separate linking table when converted into a relational design.
- Entity: rectangle; Attribute: ellipse; Relationship: diamond
- Key attribute: underlined; Multivalued: double ellipse
- Derived attribute: dashed ellipse; Weak entity: double rectangle
- Cardinality: 1:1, 1:N, M:N
Normalisation: 1NF, 2NF and 3NF
Normalisation is the process of organising tables to reduce redundancy and avoid anomalies. An update anomaly happens when the same fact stored in many rows is changed in some but not all; an insertion anomaly happens when a fact cannot be stored without unrelated data; a deletion anomaly happens when removing one fact accidentally removes another.
First normal form (1NF): every cell holds a single, atomic value and there are no repeating groups. A cell containing two phone numbers breaks 1NF. Second normal form (2NF): the table is in 1NF and no non-key column depends on only part of a composite key (no partial dependency). Third normal form (3NF): the table is in 2NF and no non-key column depends on another non-key column (no transitive dependency).
Boyce-Codd normal form (BCNF) is a stricter version of 3NF in which every determinant is a candidate key. Each higher normal form includes all the lower ones. Sometimes designers deliberately combine tables again for faster reading; this is called denormalisation.
- 1NF: atomic values, no repeating groups
- 2NF: 1NF + no partial dependency on part of a composite key
- 3NF: 2NF + no transitive dependency (non-key → non-key)
- BCNF: every determinant is a candidate key
- Anomalies: insertion, update, deletion
SQL: DDL, DML, DCL and TCL
SQL (Structured Query Language) is the standard language for relational databases. Data Definition Language (DDL) commands define or change structure: CREATE, ALTER, DROP, TRUNCATE and RENAME. Data Manipulation Language (DML) commands work on the rows: INSERT adds rows, UPDATE changes them and DELETE removes them; SELECT reads data and is grouped with DML in most textbooks (some list it separately as DQL).
Data Control Language (DCL) manages permissions: GRANT gives a privilege and REVOKE takes it back. Transaction Control Language (TCL) manages transactions: COMMIT saves changes permanently, ROLLBACK undoes changes since the last commit and SAVEPOINT marks a point to roll back to.
A typical query is SELECT name, city FROM Students WHERE city = 'Lahore' ORDER BY name. WHERE filters individual rows; GROUP BY forms groups; HAVING filters groups after grouping; ORDER BY sorts (ascending by default, DESC for descending); DISTINCT removes duplicate rows from the result. Aggregate functions are COUNT, SUM, AVG, MIN and MAX. DELETE removes chosen rows and can be rolled back, TRUNCATE quickly removes all rows but keeps the table, and DROP removes the table itself.
A transaction should satisfy the ACID properties: Atomicity (all or nothing), Consistency (the database moves from one valid state to another), Isolation (simultaneous transactions do not interfere) and Durability (committed changes survive a crash).
- DDL: CREATE, ALTER, DROP, TRUNCATE, RENAME
- DML: INSERT, UPDATE, DELETE (and SELECT)
- DCL: GRANT, REVOKE; TCL: COMMIT, ROLLBACK, SAVEPOINT
- WHERE filters rows; HAVING filters groups
- ORDER BY default = ascending
- ACID: Atomicity, Consistency, Isolation, Durability
Solved examples
- A table Employees has the columns EmpID, Name, Designation, City and Salary, and currently stores 85 employees. What are its degree and cardinality?
- Degree is the number of attributes, that is, columns. There are 5 columns, so the degree is 5.
- Cardinality is the number of tuples, that is, rows. Each employee is one row, so the cardinality is 85.
- If 10 more employees join, the cardinality becomes 95 while the degree stays 5.
Answer: Degree 5, cardinality 85 - In the table Students(RollNo, CNIC, Name, City), both RollNo and CNIC are different for every student. The designer chooses RollNo to identify rows. Name the key type of RollNo, CNIC and the pair (RollNo, Name).
- RollNo and CNIC each identify a student on their own and contain no unnecessary column, so both are candidate keys.
- RollNo was chosen, so it is the primary key; CNIC, the candidate key not chosen, is an alternate key.
- (RollNo, Name) is still unique, but Name is unnecessary, so the pair is a super key, not a candidate key.
Answer: RollNo: primary key; CNIC: alternate key; (RollNo, Name): super key - Table Enrolments(EnrolID, RollNo, CourseCode) records which student takes which course. RollNo is the primary key of Students. Ayesha's roll number appears in four rows of Enrolments. Which key is RollNo in Enrolments, and why may it repeat?
- RollNo in Enrolments points to the primary key of another table, Students, so it is a foreign key.
- Ayesha takes four courses, so four enrolment rows carry her roll number; a foreign key is not required to be unique.
- Referential integrity means each RollNo in Enrolments must already exist in Students.
Answer: A foreign key; repeats are allowed because one student can have many enrolments - Classify these SQL commands: CREATE, UPDATE, GRANT, ROLLBACK, TRUNCATE, INSERT.
- Commands that define or change structure are DDL: CREATE and TRUNCATE.
- Commands that change rows are DML: UPDATE and INSERT.
- GRANT controls permissions, so it is DCL; ROLLBACK undoes a transaction, so it is TCL.
Answer: DDL: CREATE, TRUNCATE; DML: UPDATE, INSERT; DCL: GRANT; TCL: ROLLBACK - A table stores Customer(CustID, Name, Phones), and for Ravi the Phones cell holds two numbers separated by a comma. Which normal form is broken and how is it fixed?
- 1NF requires every cell to hold one atomic value. A cell with two phone numbers is a repeating group, so 1NF is broken.
- Fix it by moving phone numbers to a separate table CustomerPhone(CustID, Phone), with one row per number.
- CustID in the new table is a foreign key to Customer, and (CustID, Phone) together form its composite key.
Answer: 1NF is broken; store each phone number in its own row of a separate table - Employee(EmpID, Name, DeptID, DeptName) has primary key EmpID. DeptName is decided by DeptID. Is the table in 3NF? If not, normalise it.
- The key is a single column, so there can be no partial dependency; the table is in 2NF.
- EmpID determines DeptID, and DeptID determines DeptName. DeptName depends on the key only through another non-key column: a transitive dependency, so the table is not in 3NF.
- Split it into Employee(EmpID, Name, DeptID) and Department(DeptID, DeptName). Now renaming a department changes one row instead of every employee row.
Answer: Not in 3NF (transitive dependency); split into Employee and Department tables
Exam shortcuts
- Degree counts columns and cardinality counts rows; new students change cardinality, a new column changes degree.
- Primary key: unique and never NULL. Foreign key: may repeat and links to another table's primary key.
- Definition words mean DDL (CREATE, ALTER, DROP, TRUNCATE); data words mean DML (INSERT, UPDATE, DELETE); permissions mean DCL (GRANT, REVOKE); COMMIT and ROLLBACK are TCL.
- Normal forms in one line: 1NF atomic cells, 2NF whole key, 3NF nothing but the key.
- ER shapes: rectangle entity, ellipse attribute, diamond relationship; double means multivalued (ellipse) or weak (rectangle).
Common mistakes
- Calling TRUNCATE a DML command because it removes rows. It works on the table as a whole and is DDL; DELETE is the DML command.
- Thinking a table can have several primary keys. It can have several candidate keys but only one primary key, which may be composite.
- Using WHERE to filter on aggregate results such as COUNT. Conditions on groups belong in HAVING.
- Confusing degree with cardinality. Adding rows changes cardinality; adding a column changes degree.
- Assuming a foreign key must be unique. Many rows of the child table can point to the same parent row.
Practice MCQs
- In a relational database, a single row of a table is formally called a:
A) Attribute
B) Tuple
C) Domain
D) Schema
Answer: B) Tuple — A row of a relation is a tuple, also called a record. An attribute is a column, a domain is the set of values a column may take, and a schema is the overall design of the database, so none of them names a single row. - Which of these SQL commands is a DDL command?
A) INSERT
B) UPDATE
C) CREATE
D) GRANT
Answer: C) CREATE — CREATE defines a new database object such as a table, so it belongs to the Data Definition Language. INSERT and UPDATE change the rows inside a table and are DML commands, while GRANT gives permissions and is a DCL command. - In a Chen-style ER diagram, a relationship between two entities is drawn as a:
A) Diamond
B) Rectangle
C) Ellipse
D) Double ellipse
Answer: A) Diamond — Relationships such as enrols in or works for are drawn as diamonds. Rectangles stand for entities, ellipses for attributes and double ellipses for multivalued attributes. - A column in one table whose values refer to the primary key of another table is called a:
A) Candidate key
B) Super key
C) Composite key
D) Foreign key
Answer: D) Foreign key — A foreign key links a child table to the primary key of a parent table and enforces referential integrity. A candidate key and a super key identify rows within their own table, and a composite key is simply a key made of more than one column. - Which SQL command takes back a privilege that was earlier given to a user?
A) COMMIT
B) GRANT
C) REVOKE
D) ROLLBACK
Answer: C) REVOKE — REVOKE removes a privilege from a user and, with GRANT, makes up DCL. GRANT does the opposite by giving the privilege. COMMIT and ROLLBACK are transaction commands that save or undo data changes, not permissions. - A table Books has 8 columns and 250 rows. What is its degree?
A) 8
B) 250
C) 258
D) 2,000
Answer: A) 8 — Degree is the number of attributes (columns), which is 8. The number of rows, 250, is the cardinality. Adding the two or multiplying them gives no standard measure of a table. - The relational model of data, which stores data in tables, was proposed by:
A) Peter Chen
B) Charles Babbage
C) Dennis Ritchie
D) E. F. Codd
Answer: D) E. F. Codd — Edgar F. Codd of IBM proposed the relational model in 1970. Peter Chen introduced the entity-relationship model, Charles Babbage designed the Analytical Engine, and Dennis Ritchie created the C programming language. - Which normal form requires that every cell of a table contain a single, indivisible value?
A) Third normal form
B) First normal form
C) Second normal form
D) Boyce-Codd normal form
Answer: B) First normal form — First normal form demands atomic values and no repeating groups. Second normal form removes partial dependencies, third normal form removes transitive dependencies, and BCNF requires every determinant to be a candidate key; these build on 1NF but are not the atomic-value rule itself. - Which ACID property ensures that a transaction is carried out completely or not at all?
A) Atomicity
B) Consistency
C) Isolation
D) Durability
Answer: A) Atomicity — Atomicity treats a transaction as one indivisible unit, so a money transfer either debits and credits both accounts or does neither. Consistency keeps the data valid, isolation stops simultaneous transactions interfering, and durability keeps committed changes after a failure. - In SQL, which clause is used to filter groups after GROUP BY has formed them?
A) WHERE
B) ORDER BY
C) HAVING
D) DISTINCT
Answer: C) HAVING — HAVING applies conditions to groups, for example keeping only cities with more than ten students. WHERE filters individual rows before grouping, ORDER BY only sorts the result, and DISTINCT removes duplicate rows. - In Students(RollNo, CNIC, Name), both RollNo and CNIC are unique for every student, and RollNo is chosen as the primary key. CNIC is then called a:
A) Foreign key
B) Composite key
C) Derived attribute
D) Alternate key
Answer: D) Alternate key — A candidate key that is not chosen as the primary key is an alternate key, and CNIC fits this exactly. It does not refer to another table, so it is not a foreign key; it is a single column, so it is not composite; and it is stored directly rather than calculated, so it is not derived. - Which command removes all rows from a table while keeping its structure, and is classed as DDL?
A) DROP
B) TRUNCATE
C) DELETE
D) ALTER
Answer: B) TRUNCATE — TRUNCATE empties a table but leaves its structure in place, and it is a DDL command. DROP deletes the table structure as well, DELETE removes rows but is a DML command, and ALTER changes the structure without removing rows. - Marks(RollNo, SubjectCode, StudentName, Score) has the composite key (RollNo, SubjectCode). StudentName depends on RollNo alone. What is the lowest normal form this table fails?
A) Second normal form
B) First normal form
C) Third normal form
D) Boyce-Codd normal form
Answer: A) Second normal form — StudentName depends on only part of the composite key, which is a partial dependency, so the table is not in second normal form. Its cells are atomic, so it does satisfy first normal form. Third normal form and BCNF are higher forms that also fail, but they are not the lowest one broken. - Employee(EmpID, Name, DeptID, DeptHead) has primary key EmpID, and DeptHead is decided by DeptID. The dependency of DeptHead on EmpID through DeptID is a:
A) Partial dependency
B) Multivalued dependency
C) Trivial dependency
D) Transitive dependency
Answer: D) Transitive dependency — EmpID determines DeptID and DeptID determines DeptHead, so DeptHead depends on the key only through a non-key column: a transitive dependency, which breaks 3NF. A partial dependency needs a composite key, a multivalued dependency involves independent sets of values, and a trivial dependency is one where the right side is part of the left side. - The City column of a Students table holds Lahore, Delhi, Lahore, Karachi and Delhi in its five rows. How many rows does SELECT DISTINCT City FROM Students return?
A) 5
B) 2
C) 3
D) 1
Answer: C) 3 — DISTINCT keeps one copy of each different value: Lahore, Delhi and Karachi, which is 3 rows. Five would be the result without DISTINCT, two counts only the repeated cities, and one would mean all rows had the same city.
Questions
What is the difference between a primary key and a foreign key?
A primary key uniquely identifies each row of its own table and can never be NULL. A foreign key is a column that refers to the primary key of another table; its values may repeat and it is used to link related tables.
Is SELECT a DDL or DML command?
SELECT reads data rather than defining structure, so it is never DDL. Most textbooks and exam keys group it with DML, while some list it separately as DQL (Data Query Language).
What is the difference between DELETE, TRUNCATE and DROP?
DELETE is DML and removes selected rows (or all rows) and can be rolled back. TRUNCATE is DDL and quickly removes all rows while keeping the table. DROP is DDL and removes the whole table, structure included.
Why is normalisation needed?
It reduces repeated data and prevents insertion, update and deletion anomalies, so each fact is stored in one place and stays consistent.