Database Concepts
Database Management Systems
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
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
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.
- 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
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
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.
| Term | Meaning | In the example |
|---|---|---|
| Degree | Number of attributes | 5 |
| Cardinality | Number of tuples | 40 |
| Candidate keys | Minimal sets identifying a tuple | RollNo, AadhaarNo |
| Alternate key | Candidate key not chosen as primary | AadhaarNo |
| Foreign key | Attribute referring to another relation's key | MARKS.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.
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?
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.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?
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.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?
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
Which of the following best defines a foreign 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.ATTENDANCE(RollNo, AttDate, Status) records whether each student was present on each day. Which choice of primary key is correct?
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.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?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.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.
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.The overall design of a database — its tables, their attributes and data types, and the constraints on them — is called the database:
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.In a relational database, the number of attributes (columns) in a relation is called its:
Show answer
Answer: A — Degree
Degree counts the columns. Cardinality counts the rows, which is the usual mix-up.The number of tuples (rows) in a relation is called its:
Show answer
Answer: A — Cardinality
Cardinality is the count of rows. Degree is the count of columns.Which of the following can NOT be NULL in a table?
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.MySQL is an example of:
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.A relation has 5 attributes and 20 tuples. Its degree and cardinality are:
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.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:
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.A foreign key is an attribute of one table that:
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.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: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.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: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.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 rowShow 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.Table A has primary key RollNo, and table B has a foreign key RollNo referring to A. Which operation violates referential integrity?
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.Table X has degree 3 and cardinality 4, and table Y has degree 2 and cardinality 5. The Cartesian product X × Y has:
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.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: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.In an ENROLMENT table that records which student took which course, the best choice of primary key is:
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.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?
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.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: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.