📘 CodingMarble Learn

Relational Databases: Tables, Rows, Columns and Keys

Keeping data in many separate files leads to repeated data, mismatched copies and hard searching, so we use a database managed by a DBMS. In the relational model data sits in tables called relations. A column is an attribute, a row is a tuple, and the set of allowed values for a column is its domain. The number of columns is the degree; the number of rows is the cardinality. A candidate key is any column (or set) that can identify every row uniquely; the one chosen is the primary key and the others are alternate keys. A foreign key is a column in one table that refers to the primary key of another table, linking them.

🎬 Step-by-step story

  1. Two offices keep the same student in two separate files. One copy changes, the other does not. Now the data disagrees. A database keeps one shared copy.
  2. In the relational model a table is a relation. Columns are attributes, rows are tuples. The domain is the set of values a column may take.
  3. Count them: degree = number of columns, cardinality = number of rows. Add a row and only cardinality grows.
  4. Which column can tell every row apart? RollNo and AadhaarNo both can: they are candidate keys. We pick RollNo as primary key; AadhaarNo becomes the alternate key.
  5. A foreign key links two tables. In MARKS, RollNo points to RollNo in STUDENT. It may only hold values that exist there.
  6. Your turn: try to insert rows. Watch the keys accept or reject them.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why not just use separate files for each office?

The same data gets copied many times and the copies stop matching. A DBMS keeps one shared, controlled copy.

Which is degree and which is cardinality?

Degree counts columns (attributes). Cardinality counts rows (tuples).

Can a table have two primary keys?

No. It has one primary key. Other candidate keys become alternate keys. The primary key may be made of two columns (composite).

Is Name a good primary key?

No. Names can repeat and can be missing. A primary key must be unique and not null.

Must a foreign key be unique?

No. It may repeat in the child table, but each value must exist in the parent's primary key (or be NULL).

Why do we need a database?

In a file system, each office keeps its own files. This causes problems:

A database is an organised collection of related data. A DBMS (Database Management System) is the software that creates, stores, updates and protects it, such as MySQL, Oracle, PostgreSQL or SQLite. A DBMS reduces redundancy, keeps data consistent, lets many users share it safely and answers queries quickly.

The relational model: relation, attribute, tuple, domain

In the relational data model (given by E. F. Codd), data is stored in two-dimensional tables.

Rules of a relation: every attribute has a unique name; the order of rows and columns does not matter; no two tuples are exactly the same; each cell holds one single value (atomic).

RollNoNameClassAadhaarNo
1Asha12A4411…
2Ravi12B5522…
3Zoya12A6633…

Table STUDENT above.

Degree and cardinality

Adding a row changes cardinality; adding a column changes degree.

When two relations are combined by a Cartesian product, degrees add and cardinalities multiply.

Keys: candidate, primary, alternate

A key is an attribute (or a set of attributes) used to identify rows.

Relation: candidate keys = primary key + alternate keys.

Foreign key

A foreign key is an attribute in one relation that refers to the primary key of another relation. It links the two tables.

ExamIDRollNoSubjectMarks
1011CS92
1023CS81

In table MARKS, RollNo is a foreign key that refers to STUDENT(RollNo). STUDENT is the parent (referenced) table; MARKS is the child (referencing) table.

Referential integrity: a foreign key value must either match an existing primary key value in the parent table or be NULL. So you cannot enter marks for RollNo 9 if no student 9 exists, and you cannot delete student 1 while marks for student 1 still exist (unless rules say what to do). A foreign key may repeat (a student can have many marks rows).

Try it: find the keys around you

Draw a table of 5 friends with columns Name, Phone, Email, City and BirthDate. Write its degree and cardinality. Which columns could identify each friend (candidate keys)? Pick one as primary key; the rest are alternate keys. Now draw a second table CALLS(CallID, Phone, Minutes) and mark which column is the foreign key. Then check your ideas in the 3D free-play step by trying rows that break the rules.

Key formulas and definitions

Worked examples

1. A table BOOK has columns BookID, Title, Author, Price, ISBN and 40 rows. Find degree and cardinality.

Degree = 5 (columns). Cardinality = 40 (rows).

2. 10 more books are added and a column Publisher is added to BOOK. New degree and cardinality?

Degree = 5 + 1 = 6. Cardinality = 40 + 10 = 50.

3. In BOOK, BookID and ISBN are both unique for every book. Title may repeat. Name the candidate, primary and alternate keys.

Candidate keys: BookID, ISBN. If BookID is chosen as primary key, ISBN is the alternate key. Title is not a key.

4. Table ISSUE(IssueNo, BookID, MemberID, Date). Which are foreign keys and to which tables do they refer?

BookID refers to BOOK(BookID); MemberID refers to MEMBER(MemberID). IssueNo is ISSUE's own primary key.

5. Why can't Name be the primary key of STUDENT?

Two students can have the same name, so it is not unique, and a primary key must be unique and not null.

6. R1 has degree 3 and cardinality 4; R2 has degree 2 and cardinality 5. What are the degree and cardinality of their Cartesian product?

Degree = 3 + 2 = 5. Cardinality = 4 × 5 = 20.

Common mistakes

Practice quiz

1. A row in a relation is called a:
2. Number of columns in a relation is its:
3. Candidate keys not chosen as primary key are:
4. A foreign key refers to:
5. Storing the same data in many files causes:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is degree and cardinality in DBMS?

Degree is the number of columns (attributes) of a relation. Cardinality is the number of rows (tuples).

What is the difference between primary key and candidate key?

Candidate keys are all attributes that can uniquely identify rows. The primary key is the one candidate key chosen; the rest are alternate keys.

What is a foreign key?

An attribute in one table that refers to the primary key of another table, used to link the tables and keep referential integrity.

Where this is taught

ItalySecondaria di secondo grado – classe 3ªTools and foundations
ItalySecondaria di secondo grado – classe 4ªTools and foundations
NetherlandsHAVO 5 (eindexamenjaar)Elective theme: Databases
NetherlandsVWO 6 (eindexamenjaar)Elective theme: Databases
RomaniaClasa a XII-aModule 1: Databases (compulsory)
RomaniaClasa a XII-aWorking with a database application
Ukraine11 класDatabases
CBSE (India)Class 12Database Management
England (GCSE, A level)Year 134.10 Fundamentals of databases
FranceTerminaleDatabases
Russia11 классInformation technologies

Learn first

Learn next

Related lessons

All Computer Science lessons