Book a Demo Start Instantly

When it comes to designing a robust database system, understanding the underlying structure and components is essential for ensuring data integrity and facilitating efficient data retrieval. Among the various elements that make up the structure of databases, keys play a pivotal role. In this article, we’ll delve into the definitions, importance, and differences between two fundamental types of keys used in databases: the Primary Key and the Foreign Key.

What is a Key?

A key in a database is a field or a set of fields that helps you identify a row (record) uniquely within a table. The key concept is foundational in relational database management systems (RDBMS) as it establishes relationships between tables and ensures each record within a table can be uniquely identified.

There are several types of keys in database management systems, including:

  • Primary keys
  • Foreign keys
  • Unique keys
  • Composite keys
  • Super keys
  • Candidate keys

Each type serves different purposes, but primary and foreign keys are particularly crucial for maintaining data integrity and defining relationships among tables.

Importance of Keys

Keys ensure data integrity by preventing duplicate entries and preserving the uniqueness of each record in a database. This is vital for accurate data retrieval and manipulation.

They also play a crucial role in establishing and enforcing relationships between tables, which is fundamental to the relational database model. By defining how tables relate to each other, keys make it easier to retrieve related data across multiple tables efficiently.

Try TiDB Serverless

Spin up a serverless cluster in seconds. Sign up and enjoy a total of 25 GiB free storage.

Try TiDB Serverless

Spin up a serverless cluster in seconds. Sign up and enjoy a total of 25 GiB free storage.

Understanding Primary Key

Definition of Primary Key

A primary key is a unique identifier for each record in a table. It cannot accept null values, and each table can have only one primary key. This key can consist of a single column or multiple columns (composite key) if needed to ensure uniqueness.

Consider a table Users with fields UserID, Username, Email, where UserID could serve as a primary key since it uniquely identifies each user.

Role of Primary Key

The primary role of a primary key is to enforce the uniqueness of records in a table, ensuring that no two rows have the same primary key value.

It also facilitates quick data retrieval by providing a straightforward way to look up records. When records are indexed by the primary key, searching and sorting operations become significantly more efficient.

Understanding Foreign Key

Definition of Foreign Key

A foreign key is a field (or collection of fields) in one table, that references the primary key of another table. The foreign key effectively establishes a link between the records in two tables, maintaining referential integrity within the database.

In a database tracking orders and customers, an Orders table might include a CustomerID field as a foreign key that points to the CustomerID primary key in a Customers table.

Role of Foreign Key

Foreign keys enable the representation of relationships between data by linking records in different tables. This is vital for relational databases where interconnected data is stored across multiple tables.

They also maintain referential integrity by restricting actions that would leave orphaned records in the database. For instance, a customer record cannot be deleted if there are orders linked to it through a foreign key constraint.

Difference between Primary and Foreign Key

The primary key is a unique identifier within its table, whereas a foreign key is a reference in one table to a primary key in another.

Primary keys enforce uniqueness within their table, ensuring each record is identifiable. Foreign keys, however, are used to establish and navigate relationships between tables.

Practical Examples

In a library database, a Books table may have BookID as its primary key, uniquely identifying each book. A Loans table might contain a BookID foreign key to specify which book has been loaned out, linking Loans to Books.

By using a foreign key, the Loans table can keep track of which books have been loaned out from the library. The foreign key references the primary key in the Books table, ensuring that only valid BookIDs are stored in the Loans table. This prevents the possibility of storing incorrect or non-existent BookIDs, maintaining data integrity.

For example, if a Books table contains the following records:

BookIDTitle
1Introduction to Database Systems
2The Art of SQL
3Database Design for Mere Mortals

And the Loans table contains the following records:

LoanIDCustomerIDBookID
10012
20023
30031

Using the foreign key constraint on the BookID column, the database ensures that only valid BookIDs from the Books table can be inserted into the Loans table. This helps to maintain data integrity and prevents the possibility of storing incorrect or non-existent BookIDs.

How to Use Primary and Foreign Keys with SQL

Now that we understand what primary and foreign keys are and how they work, let’s take a quick look at how we can assign these values when we’re creating a table in our database. We’ll be using TiDB SQL syntax. Different flavors of SQL may approach these tasks slightly differently, but we’ll stick with TiDB since it offers a free cloud database up to 25GiB that’s excellent for any project.

The following example uses a single-column foreign key to associate the parent table and the child table:

CREATE TABLE parent (
id INT KEY
);

CREATE TABLE child (
id INT,
pid INT,
INDEX idx_pid (pid),
FOREIGN KEY (pid) REFERENCES parent(id) ON DELETE CASCADE
);

The following is a more complex example where the product_order table has two foreign keys that reference the other two tables. One foreign key references two indexes on the product table, and the other references a single index on the customer table:

CREATE TABLE product (
    category INT NOT NULL,
    id INT NOT NULL,
    price DECIMAL(20,10),
    PRIMARY KEY(category, id)
);

CREATE TABLE customer (
    id INT KEY
);

CREATE TABLE product_order (
    id INT NOT NULL AUTO_INCREMENT,
    product_category INT NOT NULL,
    product_id INT NOT NULL,
    customer_id INT NOT NULL,

    PRIMARY KEY(id),
    INDEX (product_category, product_id),
    INDEX (customer_id),

    FOREIGN KEY (product_category, product_id)
      REFERENCES product(category, id)
      ON UPDATE CASCADE ON DELETE RESTRICT,

    FOREIGN KEY (customer_id)
      REFERENCES customer(id)
);

To create a foreign key constraint, you can use the following ALTER TABLE statement:

ALTER TABLE table_name ADD [CONSTRAINT [identifier]] FOREIGN KEY [identifier] (col_name, ...) REFERENCES tbl_name (col_name,...) [ON DELETE reference_option] [ON UPDATE reference_option]

The foreign key can be self-referencing, that is, referencing the same table. When you add a foreign key constraint to a table using ALTER TABLE, you need to first create an index on the parent table column that the foreign key references.

Learn more at TiDB Docs on foreign keys.

Want to build a little real-world experience with foreign keys and try working with this database for yourself in the cloud? Don’t worry, it’ll only take a few seconds on TiDB Serverless!

Sign Up for Free

 

In conclusion, primary and foreign keys are cornerstone concepts in database design, facilitating efficient data management and integrity. By understanding and effectively utilizing these keys, developers and database administrators can create structured, robust databases capable of handling complex relationships and ensuring data consistency.


Last updated May 21, 2024

Spin up a Serverless database with 25GiB free resources.

Start Now