Database Concepts

A school can keep its records in separate files — one for admissions, one for fees, one for results — and many once did. This chapter starts from the problems that approach causes and shows how a database management system solves them. It then sets out the vocabulary of the relational model, in which data lives in tables: attributes and tuples, domain, degree and cardinality, and the properties every relation must have. Finally it covers keys, the attributes that identify a row and link one table to another. These terms are tested as definitions and as small worked examples, and the SQL chapter that follows uses every one of them.
Overview

Why Not Just Files? Limitations of a File System

1 min read · 3 parts

In a file system, each application stores its own data in its own files. That works for a handful of records, but as data and users grow the book identifies six problems:

Limitations of a file system
ProblemWhat it means
Difficulty in accessevery new question needs a new application program written to read the files
Data redundancythe same data is stored in several files, wasting space
Data inconsistencycopies of the same data disagree, because one was updated and another was not
Data isolationrelated data sits in separate files, often in different formats, with no link between them
Data dependencea change to a file's structure forces changes to every program that uses it
Controlled data sharingit is hard to let different users see only what they are allowed to see
⚠️ Redundancy causes inconsistency
They are different problems with a cause-and-effect link. Redundancy is the duplication itself — a student's address in both the fees file and the admissions file. Inconsistency is what happens when the address is changed in one file and not the other, so the two copies disagree.
How redundancy leads to inconsistency
  1. Address in fees and admissions filesRedundancy: the duplication
  2. Address changed in one file only
  3. The two copies disagreeInconsistency

In a file system, each application stores its own data in its own files. That works for a handful of records, but as data and users grow the book identifies six problems:

Database Management Systems

1 min read · 5 parts

A database is an organised collection of related data, and a database management system (DBMS) is the software used to create, store, update and retrieve it — MySQL, Oracle, PostgreSQL, SQL Server, Microsoft Access and MongoDB are examples. Because the DBMS keeps data in one place and controls access to it, it reduces redundancy and inconsistency, lets any authorised user ask new questions without new programs, and enforces who may see what.

  • Database schema: the design of the database — its tables, their attributes and types, and the constraints on them.
  • Data constraint: a rule restricting the values an attribute may take, such as "marks cannot be negative" or "roll number cannot be empty".
  • Meta-data (data dictionary): data about the data — the schema and constraints, stored by the DBMS.
  • Database instance: the data actually stored in the database at a particular moment; the schema stays the same while the instance changes with every insertion or update.
  • Query and data manipulation: a query is a request to retrieve data; manipulation means inserting, deleting and updating it. The database engine is the component that actually carries these out.

A database is an organised collection of related data, and a database management system (DBMS) is the software used to create, store, update and retrieve it — MySQL, Oracle, PostgreSQL, SQL…

The Relational Data Model

1 min read · 1 part

The relational model, proposed by E. F. Codd in 1970, stores data in relations — tables of rows and columns. Each column is an attribute with a name; each row is a tuple, one record. The set of values an attribute may take is its domain — for an attribute Gender perhaps {M, F, T}. The number of attributes in a relation is its degree, and the number of tuples is its cardinality. A STUDENT relation with the attributes RollNo, Name, Class, Marks and City and eight rows has degree 5 and cardinality 8. Adding rows changes only the cardinality; adding or dropping a column changes only the degree.

  • Each attribute in a relation has a unique name, and the order of attributes does not matter.
  • No two tuples are identical, and the order of tuples does not matter.
  • Every value is atomic — it cannot be divided further; a cell holds one value, not a list.
  • A value that is unknown or does not apply is represented by NULL, which is not zero and not an empty string.

The relational model, proposed by E. F. Codd in 1970, stores data in relations — tables of rows and columns. Each column is an attribute with a name; each row is a tuple, one record. The…

Keys in a Relational Database

1 min read · 2 parts

Since no two tuples may be identical, some attribute or set of attributes must tell them apart. Every attribute (or minimal set of attributes) that can uniquely identify each tuple is a candidate key. The one candidate key chosen by the database designer to identify tuples is the primary key; the candidate keys not chosen are alternate keys. A primary key made of more than one attribute is a composite primary key. The primary key can never be NULL, since a missing value cannot identify anything.

A foreign key links two relations: it is an attribute of one relation whose values are drawn from the primary key of another — the referenced relation. In RESULT(RollNo, Subject, Marks), RollNo is a foreign key referring to STUDENT(RollNo), so a result can be entered only for a student who exists. A foreign key may repeat (one student, many results) and may be NULL when it is not part of its own relation's primary key. In ATTENDANCE(RollNo, AttDate, Status) neither RollNo nor AttDate is unique on its own — each student has many dates and each date many students — but together they are, so (RollNo, AttDate) is a composite primary key.

🧠 Candidate = primary + alternates
In STUDENT(RollNo, AadhaarNo, Name, Class), both RollNo and AadhaarNo are unique, so both are candidate keys. Choose RollNo as the primary key and AadhaarNo becomes the alternate key. Name is not a candidate key: two students can share a name.
Keys in STUDENT(RollNo, AadhaarNo, Name, Class)
Candidate keys: RollNo, AadhaarNoEach uniquely identifies a tuple
  • Primary key: RollNo
    • Chosen by the designer; never NULL
  • Alternate key: AadhaarNo
    • Candidate key not chosen
  • Not a key: Name
    • Two students can share a name
RESULT.RollNo, referring to STUDENT, is a foreign key.

Since no two tuples may be identical, some attribute or set of attributes must tell them apart. Every attribute (or minimal set of attributes) that can uniquely identify each tuple is a…

Terminology and Keys on a Worked Relation

1 min read · 1 part

Use a relation STUDENT(RollNo, AadhaarNo, Name, Class, Section) holding 40 tuples. Its degree is the number of attributes, 5, and its cardinality is the number of tuples, 40. Because roll numbers are unique and so are Aadhaar numbers, both RollNo and AadhaarNo are candidate keys, since each could identify a row. If the designer picks RollNo as the primary key, AadhaarNo becomes an alternate key. A second relation MARKS(RollNo, Subject, Score) can use RollNo as a foreign key referring to STUDENT, so that every mark belongs to a real student. This is referential integrity: the DBMS rejects a MARKS row whose RollNo does not exist in STUDENT, and it blocks deleting a student who still has marks unless the rule says otherwise.

Terms on the STUDENT relation
TermMeaningIn the example
DegreeNumber of attributes5
CardinalityNumber of tuples40
Candidate keysMinimal sets identifying a tupleRollNo, AadhaarNo
Alternate keyCandidate key not chosen as primaryAadhaarNo
Foreign keyAttribute referring to another relation's keyMARKS.RollNo

Use a relation STUDENT(RollNo, AadhaarNo, Name, Class, Section) holding 40 tuples. Its degree is the number of attributes, 5, and its cardinality is the number of tuples, 40. Because roll…

Key takeaways

  • A file system suffers difficulty of access, redundancy, inconsistency, isolation, data dependence and poor control over sharing; a DBMS addresses them.
  • Schema is the design, instance the data at a moment, meta-data the stored description of the schema.
  • In the relational model (Codd), a column is an attribute, a row a tuple; degree counts attributes, cardinality counts tuples.
  • Tuples are distinct and unordered, attributes uniquely named and unordered, values atomic, and NULL marks the unknown.
  • Candidate keys can each identify tuples; the chosen one is the primary key (never NULL), the rest are alternate keys.
  • A composite key uses several attributes together; a foreign key takes its values from another relation's primary key.

Practice questions (24)

Attempt each one before opening the answer. Every explanation names the tempting wrong option as well as the right one, because that is where marks are lost.

  1. A relation has 5 attributes and 8 tuples. Two new tuples are inserted and one attribute is dropped. What are its degree and cardinality now?

    1. Degree 10, cardinality 4
    2. Degree 4, cardinality 10
    3. Degree 6, cardinality 9
    4. Degree 5, cardinality 10
    Show answer

    Answer: B — Degree 4, cardinality 10

    Degree counts attributes: 5 − 1 = 4. Cardinality counts tuples: 8 + 2 = 10. Degree 10 with cardinality 4 has the two terms the wrong way round, the most common slip with this pair. Degree 5 forgets the dropped attribute, and degree 6 with cardinality 9 applies each change to the wrong count.
  2. A student's address is stored in both the fees file and the admissions file. The address is changed in the fees file only, so the two files now show different addresses. Which file-system problem does this illustrate?

    1. Data isolation
    2. Data dependence
    3. Data inconsistency
    4. Difficulty in access
    Show answer

    Answer: C — Data inconsistency

    Two copies of the same fact now disagree, and that disagreement is data inconsistency. Its cause is redundancy — storing the address twice — but the question describes the mismatch, not the duplication. Isolation is related data scattered with no link, dependence is programs breaking when a file's structure changes, and difficulty in access is needing a new program for each new query.
  3. In STUDENT(RollNo, AadhaarNo, Name, Class), both RollNo and AadhaarNo are unique for every student, and RollNo is chosen as the primary key. What is AadhaarNo?

    1. A foreign key
    2. A composite key
    3. An alternate key
    4. Not a key, since only one primary key is allowed
    Show answer

    Answer: C — An alternate key

    AadhaarNo can identify every tuple, so it is a candidate key; a candidate key not chosen as the primary key is an alternate key. It is not a foreign key because it does not refer to another relation, and not composite because it is a single attribute. Only one primary key is allowed, but the other candidate keys remain keys.
Show the other 21 questions
  1. Which of the following best defines a foreign key?

    1. An attribute whose values must be unique and never NULL within its own relation
    2. An attribute of one relation whose values are drawn from the primary key of another relation
    3. A key made by combining two or more attributes of the same relation
    4. Any candidate key that was not chosen as the primary key
    Show answer

    Answer: B — An attribute of one relation whose values are drawn from the primary key of another relation

    A foreign key is how two relations are linked: its values must match the primary key of the referenced relation, as RollNo in RESULT matches RollNo in STUDENT. "Unique and never NULL" describes a primary key, a combination of attributes is a composite key, and an unchosen candidate key is an alternate key. A foreign key may repeat and may be NULL, which is exactly what a primary key may not do.
  2. ATTENDANCE(RollNo, AttDate, Status) records whether each student was present on each day. Which choice of primary key is correct?

    1. RollNo alone
    2. AttDate alone
    3. Status alone
    4. RollNo and AttDate together
    Show answer

    Answer: D — RollNo and AttDate together

    Each student has a row for every day, so RollNo repeats; each day has a row for every student, so AttDate repeats; but one student has only one row per day, so the pair identifies each tuple — a composite primary key. Status takes only a couple of values and repeats constantly, so it identifies nothing.
  3. Consider these statements about a relation.
    I. The order of its tuples does not matter.
    II. Two of its tuples may be exactly identical.
    III. Every value in it is atomic.
    Which are correct?

    1. I and II only
    2. II and III only
    3. I and III only
    4. I, II and III
    Show answer

    Answer: C — I and III only

    A relation is a set of tuples, so their order carries no meaning (I) and each cell holds a single indivisible value (III). II is false: no two tuples may be identical, which is exactly why every relation has a key that tells them apart. Any option containing II accepts duplicate rows that the relational model forbids.
  4. Assertion (A): A foreign key attribute can never contain a NULL value. Reason (R): A foreign key refers to the primary key of another relation.

    1. Both A and R are true, and R is the correct explanation of A
    2. Both A and R are true, but R is not the correct explanation of A
    3. A is true, but R is false
    4. A is false, but R is true
    Show answer

    Answer: D — A is false, but R is true

    R is the definition of a foreign key, so it is true. A is false: the no-NULL rule belongs to the primary key, and a foreign key that is not part of its own relation's primary key may be NULL — an employee not yet assigned to a department, for instance. Treating A as true carries the primary key's rule over to the key that merely refers to it.
  5. The overall design of a database — its tables, their attributes and data types, and the constraints on them — is called the database:

    1. instance
    2. schema
    3. engine
    4. query
    Show answer

    Answer: B — schema

    The schema is the structure of the database, and it stays the same while data comes and goes. The instance is the opposite idea, the actual data stored at a given moment, which changes with every insertion. The engine is the component that carries out operations, and a query is a single request to retrieve data.
  6. In a relational database, the number of attributes (columns) in a relation is called its:

    1. Degree
    2. Cardinality
    3. Domain
    4. Schema
    Show answer

    Answer: A — Degree

    Degree counts the columns. Cardinality counts the rows, which is the usual mix-up.
  7. The number of tuples (rows) in a relation is called its:

    1. Cardinality
    2. Degree
    3. Attribute
    4. Key
    Show answer

    Answer: A — Cardinality

    Cardinality is the count of rows. Degree is the count of columns.
  8. Which of the following can NOT be NULL in a table?

    1. The primary key
    2. A foreign key always
    3. Any non-key attribute always
    4. The table name
    Show answer

    Answer: A — The primary key

    A primary key must uniquely identify every row, so it cannot be NULL. A foreign key may be NULL when no reference exists.
  9. MySQL is an example of:

    1. A relational database management system
    2. A programming language
    3. An operating system
    4. A web browser
    Show answer

    Answer: A — A relational database management system

    MySQL is an RDBMS that stores data in tables. It uses SQL, which is a language, but it is not itself a language.
  10. A relation has 5 attributes and 20 tuples. Its degree and cardinality are:

    1. 5 and 20
    2. 20 and 5
    3. 25 and 100
    4. 5 and 100
    Show answer

    Answer: A — 5 and 20

    Degree is the number of attributes, 5, and cardinality the number of tuples, 20. Option 2 swaps them.
  11. A STUDENT table has columns RollNo, AadhaarNo, Name and Phone, and both RollNo and AadhaarNo are unique. If RollNo is chosen as the primary key, AadhaarNo is called:

    1. An alternate key
    2. A foreign key
    3. A composite key
    4. A super attribute only
    Show answer

    Answer: A — An alternate key

    A candidate key not chosen as primary becomes an alternate key. A foreign key refers to another table's primary key, which is not the case here.
  12. A foreign key is an attribute of one table that:

    1. Refers to the primary key of another table
    2. Is always unique in its own table
    3. Cannot hold duplicate values
    4. Is the same as the degree
    Show answer

    Answer: A — Refers to the primary key of another table

    A foreign key links two tables by referring to a primary key. Within its own table it may repeat values.
  13. Assertion (A): A table can have only one primary key.
    Reason (R): The primary key is the one candidate key chosen to identify each tuple uniquely.
    Choose the correct option:

    1. Both A and R are true and R is the correct explanation of A
    2. Both A and R are true but R is not the correct explanation of A
    3. A is true but R is false
    4. A is false but R is true
    Show answer

    Answer: A — Both A and R are true and R is the correct explanation of A

    One candidate key is chosen as primary, as R states; the others become alternate keys. Option 2 is wrong.
  14. Consider the following statements:
    A. A candidate key can uniquely identify each tuple.
    B. A foreign key value must match a primary key value in the parent table or be NULL.
    C. A table can have several primary keys.
    D. The degree of a relation is the number of its attributes.
    Choose the correct answer:

    1. A, B and D only
    2. A and C only
    3. B, C and D only
    4. A, B, C and D
    Show answer

    Answer: A — A, B and D only

    A, B and D are correct. C is false because a table has exactly one primary key, so option 4 is the trap.
  15. Match List I with List II:
    List I:
    A. Relation;
    B. Tuple;
    C. Attribute;
    D. Domain
    List II:
    1. Set of permitted values of an attribute;
    2. A column;
    3. A table;
    4. A row

    1. A-3, B-4, C-2, D-1
    2. A-4, B-3, C-2, D-1
    3. A-3, B-4, C-1, D-2
    4. A-3, B-2, C-4, D-1
    Show answer

    Answer: A — A-3, B-4, C-2, D-1

    A relation is a table (3), a tuple a row (4), an attribute a column (2) and a domain the set of permitted values (1). Option 2 swaps relation and tuple.
  16. Table A has primary key RollNo, and table B has a foreign key RollNo referring to A. Which operation violates referential integrity?

    1. Inserting a row in B with a RollNo that is not in A
    2. Inserting a new row in A
    3. Reading rows of B
    4. Counting the rows of A
    Show answer

    Answer: A — Inserting a row in B with a RollNo that is not in A

    A foreign key value must exist in the parent table. Adding a new parent row or reading data does not break the link.
  17. Table X has degree 3 and cardinality 4, and table Y has degree 2 and cardinality 5. The Cartesian product X × Y has:

    1. Degree 5 and cardinality 20
    2. Degree 6 and cardinality 9
    3. Degree 5 and cardinality 9
    4. Degree 6 and cardinality 20
    Show answer

    Answer: A — Degree 5 and cardinality 20

    Degrees add (3 + 2 = 5) and cardinalities multiply (4 × 5 = 20). Option 3 adds the cardinalities and option 4 multiplies the degrees.
  18. Assertion (A): A DBMS reduces data redundancy.
    Reason (R): Data is stored once in a central place and shared among applications instead of being duplicated in separate files.
    Choose the correct option:

    1. Both A and R are true and R is the correct explanation of A
    2. Both A and R are true but R is not the correct explanation of A
    3. A is true but R is false
    4. A is false but R is true
    Show answer

    Answer: A — Both A and R are true and R is the correct explanation of A

    Central storage and sharing avoid duplication, which R states. Option 2 is wrong.
  19. In an ENROLMENT table that records which student took which course, the best choice of primary key is:

    1. The combination (StudentID, CourseID)
    2. StudentID alone
    3. CourseID alone
    4. Marks
    Show answer

    Answer: A — The combination (StudentID, CourseID)

    A student can take many courses and a course has many students, so only the pair is unique. Either alone would repeat.
  20. A table has attributes RollNo, Email, Name and City; RollNo and Email are each unique. How many candidate keys and alternate keys does it have if RollNo is the primary key?

    1. 2 candidate keys and 1 alternate key
    2. 1 candidate key and 1 alternate key
    3. 2 candidate keys and 2 alternate keys
    4. 4 candidate keys and 3 alternate keys
    Show answer

    Answer: A — 2 candidate keys and 1 alternate key

    RollNo and Email are the candidate keys; the primary key is one of them, so the other, Email, is the alternate key. Name and City can repeat, so they are not candidates.
  21. Assertion (A): A primary key attribute cannot contain NULL.
    Reason (R): NULL stands for an unknown or missing value, which cannot be used to identify a tuple uniquely.
    Choose the correct option:

    1. Both A and R are true and R is the correct explanation of A
    2. Both A and R are true but R is not the correct explanation of A
    3. A is true but R is false
    4. A is false but R is true
    Show answer

    Answer: A — Both A and R are true and R is the correct explanation of A

    An unknown value cannot identify a row, which R states. Option 2 is wrong.