Skip to content

Information Technology · Ch 5 — Database Concepts using LibreOffice Base

Primary Keys and Relationships

4

Primary Keys and Relationships

A primary key is a field (or a combination of fields) whose value uniquely identifies each record in a table. No two records may share the same primary-key value, and the value can never be left blank. In a Student table, RollNo makes a natural primary key because every student has a different roll number.

Setting a primary key in LibreOffice Base: in the table's Design View, right-click the small grey button at the left of the field's row and choose Primary Key. A small key symbol then appears beside that field. If you save a table without a primary key, Base warns you and offers to add one automatically — but it is far better to choose a meaningful primary key yourself.

The real power of a relational database appears when tables are linked to one another. A foreign key is a field in one table whose values refer to the primary key of another table, and it is this matching of values that creates a relationship between the two tables.

Example. Suppose we keep two tables:

  • Customer(CustID, Name, City) — here CustID is the primary key.
  • Orders(OrderNo, CustID, Amount) — here OrderNo is the primary key, and CustID is a foreign key that refers back to CustID in the Customer table.

This link ensures that every order belongs to a real customer — an order cannot be recorded for a customer who does not exist. That guarantee is called referential integrity.

Creating a relationship in Base: open Tools → Relationships..., add the two tables, and then drag the primary-key field of one table onto the matching foreign-key field of the other. Base draws a line joining the two fields to show the relationship. Relationships come in three common shapes:

  • One-to-many — the most common: one customer can place many orders, but each order belongs to just one customer. …
Definition 1Primary key

A field (or set of fields) whose value uniquely identifies each record in a table; it must be unique and never left blank. In LibreOffice Base it is set f …

Definition 2Foreign key

A field in one table whose values refer to the primary key of another table, creating a relationship (link) between the two tables and enforc …

Definition 3Relationship

A link between two tables created by matching a foreign key to a primary key; common types are one-to-many, one-to- …