LightMySky

Relational Databases: Tables, Keys and Relationships

A relational database stores records in tables. Primary keys identify rows, and foreign keys refer to unique keys in the same table or another table. Relationships describe how records connect.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Choose a primary key for a table and justify why it identifies a row
  • Link two tables with a foreign key and name the relationship as one-to-many or many-to-many
  • Say what goes wrong when the same fact is stored in two places

1 · Read

Tables have rows and columns. A row records one entity, such as a student, or one relationship, such as a membership. Columns hold its properties. A primary key is one column or a combination that identifies each row uniquely. No key column may be null (missing). A different ID given to every student and kept unchanged is a suitable student key. Names and favourite colours may be shared.

Try it together

Two member records hold club C8's name as Drama. A rename to Theatre changes one record but misses the other, leaving different current names for the same club. Instead, store C8's name once in Clubs and let member records refer to C8. The ID may repeat in links; the current name has one place to update. This reduces copy-update mistakes, but other errors can still occur.

A and B: DramaRename to TheatreUpdate A onlyA: Theatre; B: Drama
A partial update can leave two current names for the same club.

A foreign key is one column or a set of columns that refers to a unique key in the same table or another table. Here, Memberships.StudentID refers to the primary key Students.StudentID. With the constraint enforced, a non-null membership StudentID must match an existing student. This column may allow null if the student link is optional.

Relationship types describe what the design allows, not just today's rows. If each book has one author and an author may have several books, Authors to Books is one to many. Books holds the author key as a foreign key. If a student may join several clubs and a club may have several students, Students to Clubs is many to many.

Students has StudentID, Clubs has ClubID, and Memberships has one row per student-club pair. Memberships.StudentID and Memberships.ClubID refer to the two tables' keys. Either ID alone can repeat. If each pair may occur only once and both IDs are non-null, the two columns together can be the membership's primary key. Keep student names in Students and club names in Clubs.

Key and link: E1Update key and referenceKey and link: E2
A database that supports automatic key updates can change matching foreign-key references with the key.

Primary keys must be unique and non-null. Stability is a design preference; a primary key can change. If an email used as a key changes, foreign keys referencing it must stay consistent. A database may update those references automatically or reject the change until it is handled. A uniquely allocated ID kept unchanged avoids key changes when an email changes. Inventing an ID alone does not make it unique.

Use unique, non-null primary keys. Keep changing details in one place and link rows with foreign keys. Check how many matches each side allows.

Lesson, questions and revised flashcards written for this dataset. External records were consulted for factual checks and design study; no source prose is reproduced.

Readings and revised flashcards written for this dataset. Video reference: Welcome to SQL by Khan Academy, hosted on YouTube.

More to read

Identifying records without relying on names

A school tracks its projectors. Each row in Equipment describes one projector. Columns hold details such as EquipmentID, Room and Condition. Two projectors may be in the same room or have the same condition, so those details cannot identify a projector on their own.

The school assigns a different EquipmentID to every projector. If EquipmentID is the primary key, duplicate IDs and null IDs are not allowed. The key identifies a record; it does not prove that the recorded room or condition is correct.

A primary key can use several columns. Their combination must be unique, and none of the key columns may be null. Test the proposed combination against all the rows the design permits, not just the records entered so far.

An invented ID still needs a process that assigns it uniquely. Keeping that ID unchanged lets other records continue to refer to the projector when its room or condition changes.

What database rules can check

A database management system is software for storing, retrieving and changing database records. It can also manage access, coordinate users and support recovery. These features need suitable settings and procedures; their presence does not make every stored claim true.

A table definition can specify column types and constraints. A primary-key constraint rejects duplicate key values and null key columns. A foreign-key constraint checks references to an eligible unique key, which may be in the same table or a different table.

Checks have limits. A valid equipment ID may refer to the wrong projector. A room number may have an accepted type but still name the wrong room. Rules catch violations of the rules that were declared, not every mismatch with the world.

Reducing repeated copies of changing details also reduces update work. It does not replace careful data entry, access controls or recovery planning. Different database systems handle types differently, so check the behaviour of the system being used.

Designing records for repeated loans

A study centre lends tablets. Members stores one row per member, Tablets stores one row per tablet, and Loans stores one row per loan event. Each loan includes a MemberID and a TabletID that refer to the matching records.

A member may borrow several tablets over time. A tablet may also be borrowed by several members over time. Loans records those connections without placing every loan inside a member or tablet row.

The same member can borrow the same tablet again. The MemberID and TabletID pair is therefore not unique across loan events. This design can give each event a unique, non-null LoanID instead. Key choices depend on what one row represents and which repetitions the rules allow.

Keep a member's current contact details in Members rather than copying them into every loan. A contact change then has one record to update. A detail about the loan itself, such as its return date, belongs in Loans. Separating records reduces repeated changing details, but it does not prevent every recording mistake.

Checking a proposed key

Start by deciding what one row represents. Then ask whether two permitted rows could share the proposed key. A surname fails this test in a people table because different people may share it. A generated number passes only when the assignment process gives each record a different number.

Next, check for missing values. No column in a primary key may be null. For a key made from several columns, check the whole combination for uniqueness and each column for a non-null value.

Finally, consider changes. Stability is useful, but a primary key is not automatically immutable. If a referenced key changes, its foreign-key references must stay consistent. A database may support automatic updates or reject a change until the references are handled.

For a table with one row per participant per workshop, the participant and workshop IDs can form a key if both are non-null and each pair is allowed only once. If repeated attendance events are separate rows, the pair alone no longer identifies an event. The rule about permitted rows decides the key.

Explore more

2 · Watch

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Learn first

Nothing; this is a starting point.

Optional connections

This opens up

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Relational Databases: Tables, Keys and Relationships · Computing, ages 17 to 18 · LightMySky