What does a relational database allow you to do?

Databases have been a staple of business computing from the very beginning of the digital era. In fact, the relational database was born in 1970 when E.F. Codd, a researcher at IBM, wrote a paper outlining the process. Since then, relational databases have grown in popularity to become the standard.

Originally, databases were flat. This means that the information was stored in one long text file, called a tab delimited file. Each entry in the tab delimited file is separated by a special character, such as a vertical bar (|). Each entry contains multiple pieces of information (fields) about a particular object or person grouped together as a record. The text file makes it difficult to search for specific information or to create reports that include only certain fields from each record. Here's an example of the file created by a flat database:

Lname, FName, Age, Salary|Smith, John, 35, $280|Doe, Jane, 28, $325|Brown, Scott, 41, $265|Howard, Shemp, 48, $359|Taylor, Tom, 22, $250

You can see that you have to search sequentially through the entire file to gather related information, such as age or salary. A relational database allows you to easily find specific information. It also allows you to sort based on any field and generate reports that contain only certain fields from each record. Relational databases use tables to store information. The standard fields and records are represented as columns (fields) and rows (records) in a table.

With a relational database, you can quickly compare information because of the arrangement of data in columns. The relational database model takes advantage of this uniformity to build completely new tables out of required information from existing tables. In other words, it uses the relationship of similar data to increase the speed and versatility of the database.

The "relational" part of the name comes into play because of mathmatical relations. A typical relational database has anywhere from 10 to more than 1,000 tables. Each table contains a column or columns that other tables can key on to gather information from that table.

By storing this information in another table, the database can create a single small table with the locations that can then be used for a variety of purposes by other tables in the database. A typical large database, like the one a big Web site, such as Amazon would have, will contain hundreds or thousands of tables like this all used together to quickly find the exact information needed at any given time.

Relational databases are created using a special computer language, structured query language (SQL), that is the standard for database interoperability. SQL is the foundation for all of the popular database applications available today, from Access to Oracle.

Here are some interesting links:

  • How Domain Name Servers Work
  • How Internet Cookies Work
  • What are the different types of computers?
  • Introduction to Relational Database Design
  • WhatIs.com: Relational Database
  • WhatIs.com: Database
  • Database Management Systems

What does a relational database allow you to do?

A relational database is a type of database that stores data in tables made up of rows and columns. Each table represents a specific object in the database like users, products, orders, and so on.

The term "relational" is the main characteristic feature that makes relational databases unique. This is because each entity (table) in a relational database can "relate" to another to create more tables which makes the flow of data flexible.

The columns denote the specific type of data that can be stored under each respective column. An example would be a user table with the following columns: Name, Date of birth, Phone, and Country.

You use rows to populate data in a database table. Using the column names above as examples, we can populate the rows with data like this: John Doe, 02-04-1970, 555-777-999, Earth.

That is:

NameDoBPhoneCountry
John Doe 02-04-1970 555-777-999 Earth
Jane Does 03-05-1980 444-222-444 Nigeria
Jade Do 05-03-1990 565-784-437 England

As you can see above, each row has data that corresponds with its column name.

What are Primary Keys and Foreign Keys in a Relational Database?

The table in the last section lacks one of the most important attributes in a relational database — a unique identifier/ID.

Tables are able to relate with each other using unique identifiers.

A good example of the importance of identifiers is a situation where you have two or more users with the same name performing the same action like buying a product. The only way to tell the difference between these users is to use their unique ID.

These unique identifiers are generally referred to as keys in a relational database.

In a relational database, you can have either a primary key or a foreign key. Let's explain what they mean.

What is a Primary Key in a Relational Database?

A primary key is a column in a database column used to identify specific records/rows. Each row must have its own unique key.

The primary key can be any attribute in the table like the user's phone number or whatever information the user provides.

In the long run, using information provided by the users as a primary key may become detrimental because you may have duplicate values. This would make it difficult to differentiate records.

In most cases, primary keys are automatically generated by the database management system as records are created. No particular primary key can be generated twice – this makes each row unique.

Here's an example of a table with a primary key:

User IDNameDoBPhoneCountry
1 John Doe 02-04-1970 555-777-999 Nigeria
2 John Doe 03-05-1980 444-222-444 Nigeria
3 John Doe 05-03-1990 565-784-437 Nigeria

In the table above, we have three users with same name and country.  

If these users go on and buy products, differentiating them with their names becomes a problem.

Note that a primary key should never have a null value.

Using the primary key (USER ID), we make each user unique. This would also eliminate the option of making a different attribute provided by the users — DOB, Phone – a primary key.

What is a Foreign Key in a Relational Database?

A foreign key is a column/group of columns used to make reference/link to another table's primary key.

In other words, it is the representation of another table's primary key.

Here's an example to show the importance of foreign keys:

Customer IDNameDoBPhoneCountry
1 John Doe 02-04-1970 555-777-999 Nigeria
2 John Doe 03-05-1980 444-222-444 Nigeria
3 John Doe 05-03-1990 565-784-437 Nigeria

In the table above, we have a user table with a primary key called CUSTOMER ID.

The table below shows the different products the customers can order:

Product IDProduct Name
10 Pizza
20 Coffee
30 John Doe

Next, we'll create a table to show orders made by customers.

Order IDCustomer IDProduct Name
1 3 Pizza
2 2 Pizza
3 3 Coffee
2 1 Coffee

In table above, CUSTOMER ID is a foreign key. It serves as a reference to the primary key in the customer table.

The order table is easier to understand because we can identify each customer using their unique identifier instead of their similar looking names.

A table can have more than one foreign key.

Examples of Relational Databases

Here are examples of some popular relational databases:

  • MySQL.
  • PostgreSQL.
  • Oracle Database.
  • Microsoft SQL Server.
  • IBM Db2.
  • MariaDB.

There are also cloud based relational databases like:

  • Oracle Cloud.
  • Google Cloud SQL.
  • IBM Db2 on Cloud.
  • AWS Databases.

Benefits of a Relational Database

In this section, we'll list and explain some of the features of a relational database.

Ease to use

Interacting with the data in a relational database is easy and straightforward. You do this using SQL (Structured Query Language).

Flexibility

You can easily scale up a relational database as the data grows. It's also easy to add, update, and delete data stored in the database.

When specific data is stored across many tables, changing it in one table updates its value for every other table sharing the data.

Security

You can easily restrict access to certain tables in a relational database, making it inaccessible to unauthorized users. You do this using a relational database management system (RDBMS).

RDBMSs are basically used to create, read, update and delete data on a relational database.

Accuracy

With the use of keys (primary and foreign), each piece of data stored on a relational database can be accessed when required without confusing it for another.

Simplicity

The structure of relational databases makes it easy to understand the type and flow of data stored in it.  You can as well represent the data stored in a database graphically. This helps you understand the relationship between tables.

RDBMS is an acronym for Relational Database Management System. This is a program used to managed a relational database.

It allows you create, read, update, and delete (CRUD operations) data on a database.

Most RDBMSs use SQL to interact with the database. SQL is a programming language used in managing and performing operations in a relational database. It is somewhat easy to learn because its syntax is similar to the English language.

SELECT * FROM Users;

The SQL code above selects all the columns in a table called Users.

Here are some of the popular relational database management systems:

  • MySQL.
  • Microsoft SQL Server.
  • SQLite.
  • PostgreSQL.

Summary

In this article, we talked about relational databases.

We saw some of the features of a relational database like the representation of data in tables with rows and columns, and the use of keys to make each piece of data unique from the rest.

We also talked about primary and foreign keys with examples to demonstrate how to use each.

Lastly, we talked about some of the benefits of using a relational database.

To learn more about relational databases, check out freeCodeCamp's Relational Database certification.

You'll start by learning basic Bash commands and how to use the terminal to navigate and manipulate a file system.

You'll also learn how to create and use a relational database, and the use of Git for version control.

As you progress through the course, you'll be building awesome projects to help you deepen your knowledge about relational databases.

Happy coding!



Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

What can relational databases be used for?

Relational databases are ideal for complex data analysis and operations. In a non-relational database, tables can share the same data but they can't 'relate' to each other. With a relational database, they can. One use of a relational database is connecting tables for customer data and transactions.

What are the 3 main operations of a relational database?

In a relational database, three operations are used to develop sets of data: Select: Creates a subset of data of records that meet stated criteria. Join: Combines relational data from different tables. Project: Creates a subset of a table using only specified columns.