Most developers encounter working with databases in their daily work, but not all of them take time to get to know the theory. However, this causes a lot of questions to arise at the worst possible time. What is a relational database? Why do relational databases use primary keys and foreign keys? What exactly is supposed to happen here or there?
To be able to work with data professionally, you must understand the different types of databases and their unique features. In this comprehensive guide, we will introduce you to the most popular type called the relational database.
Databases and their types
Without using a database, all the data you create or collect would resemble a random pile without any order. You couldn’t possibly access and use it for your needs. Now, a database is a data storage solution that provides you with not only space to keep your data in, but also powerful methods to manipulate it. Databases have been prominent in computer technology ever since the 1960s. To manage them, we use a special kind of software – a database management system (DBMS for short).
According to the specific needs you or your company has, you can choose from multiple database types. It can be operational, personal, distributed, end-user, etc. However, relational databases are so popular that some developers even simplify database typing down to two groups only: relational or non-relational. As SQL (Structured Query Language) is the standard method of working with the former, the latter is sometimes also referred to as NoSQL. A few simple non-relational database examples would be key-value stores, document stores, or graph databases. While we must admit their popularity is growing, the relational databases still take up the lion’s share of the market.
What is a relational database, exactly?
The first to mention the term relational database was Edgar F. Codd in 1962. Working at IBM, he saw major disadvantages in the navigational databases that were used at the time. According to him, not only they were over-complicated to use, but there was no solid theory to back the principles up. Trying to solve these issues, he wrote a paper called A Relational Model of Data for Large Shared Data Banks. IBM was reluctant to put his ideas into practice. However, because of this groundbreaking work to redefine database models, Edgar F. Codd received the prestigious Turing Award in 1981.
To fully grasp what is a relational database, you have to understand that before they came around, developers used flat databases. Instead of tables, the files contained long texts, in which the entries were separated with a vertical bar:
Employee, Team, Experience|Anna, Developers, 7 years|Melissa, Developers, 3 years|Andrew, Developers, 4 years|Stanley, Designers, 4 years|Andy, Designers, 5 years|Christina, Designers, 2 years
You may have an inkling this made accessing and using the data a hassle. You couldn’t sort, search, or filter it easily. In a relational database, however, the data is stored using a clear structure: a table that has columns and rows.
Each column (also called an attribute or a field) has a distinct name and a specific data type assigned to it. Each piece of data fills up a row, also called a tuple or a record. The example we saw previously would now have three columns (Employee, Team, and Experience) and six records (one for each individual). Each of these columns would store string data types, or we could make the Experience column to only hold integers and enter the number of years only.
Relational database design: relationships explained
Getting to know what is a relational database, you will notice the term itself is very revealing: a relational database is one that allows database relationships.
It’s rather common for a database of this type to have up to thousands of tables. A relationship in database design is established when two or more of them hold some related data and therefore are linked together. Not only this simplifies data maintenance, but it also increases its integrity and security. Relational databases are easier to scale and expand too.
There are three types of relationships in database design. The most common ones are one-to-many: this means a row in one table can match a few rows in the other, but not vice versa. In the example we saw before, one employee could only belong to one team – however, one team had multiple employees. If we had another table called Projects, it could be a perfect example of a many-to-many relationship: each person could work on multiple projects, and each project could employ multiple people. One-to-one database relationships are probably the least common.
Identification and relation: using the right keys
Now, why do relational databases use primary keys and foreign keys? It’s got to do with database relationships as well. Each and every table in a database has one or multiple columns, which act as the primary key. Its purpose is to identify the records in the table, therefore, the value of each record in this column should be unique. For example, instead of the name of the customer, you should use, let’s say, an order number. When you set the column as the primary key, the system will make sure you’re unable to enter the same value twice.
Using a foreign key, you can create a relationship between records in two separate tables. It is a column (or a few) that holds values corresponding to the primary key of another table. You cannot add a record that doesn’t exist in the referenced table to the table with a foreign key. Imagine you have two tables: Customers and Orders. You can easily link the first one to the second by creating a foreign key that references its primary key column (most likely the Customer ID).
Relational database management systems and SQL
Now that you understand what is a relational database, you might start looking into software to manage it. As we're dealing with the most popular type of databases in the world, you can choose from a bunch of already established and well-known names, such as MySQL, PostgreSQL, Oracle, or SQL Server. Most beginners prefer the first two, as they are open-source and completely free to use. Both Oracle and SQL Server have free versions available, but there are certain limitations to the functionalities you can use.
According to DB-Engines ranking, Oracle is currently the most popular relational database management system in the world. No wonder – according to their website, it's been ‘self-driving, self-securing, and self-repairing’ since 2018. With machine learning leaving a lot less manual work to the actual human, the system is able to achieve higher security and lower the risk of mistakes. Naturally, it's also a lot easier to use, as Oracle itself takes care of a lot of tasks. We should mention that since 2010 MySQL belongs to the Oracle Corporation as well, and the support of a huge company does heaps for the system's reliability. In the ranking mentioned above, MySQL comes a close second.
The choice of a relational database management system depends solely on the needs of your projects. Now that you're familiar with what is a relational database, it will be easier to clarify your expectations for it as well. Of course, to be able to work with any of them, you will need to learn SQL. We'll tell you a secret: BitDegree's online courses are a great way to start! Check out an interactive tutorial or a more traditional course today.