A database is a collection of data stored in an organized format. It can be accessed through database management systems that can view, edit, and utilize the dataset for the task. These databases can be stored locally on our machines, on-premise clusters, or in the cloud.
The figure below shows an example of a table called "Books" from a "School" database. Each row in the table is called a tuple, while every column is called an attribute. Moreover, an attribute uniquely identifies each row in every table, called the primary key.
A relational database is a database, i.e., a collection of tables that have a relationship between them. Primary and foreign keys create this relationship. A foreign key is an attribute that uniquely identifies another table's tuples. A primary key in its table is a foreign key in another table.
A relational database management system or RDMS can contain multiple types of relationships. These relationships "relate" or link different tables to each other. The types of relationships that can be created are explained below:
One-to-one (1-1): In this relationship, one table is linked to only one table. For example, one person can have only one passport, and one passport can only be for one person.
One-to-many (1-n): In this relationship, one table is linked to multiple instances of another table. An example is that a car brand creates multiple models of cars, whereas one car is only produced by one car brand.
Many-to-one (n-1): In this relationship, multiple table instances are linked to one table. For example, many students can work on one project.
Many-to-many (n-n): In this relationship, multiple instances of a table are linked to many instances of another table. An example is that multiple customers can purchase multiple products, and multiple customers can purchase multiple products.
To use a relational database, we need to use a query language, i.e., a structure query language (SQL). This allows us to query data from one or many tables to get our desired result. An example of SQL is MySQL which supports relational databases.
-- Create the Teacher tableCREATE TABLE Teacher (Teacher_ID INT PRIMARY KEY,Name VARCHAR(255),Office VARCHAR(255),Phone_Number VARCHAR(255));-- Create the Class tableCREATE TABLE Class (Class_ID INT PRIMARY KEY,Location VARCHAR(255),Number_of_Students INT,Teacher_ID INT,FOREIGN KEY (Teacher_ID) REFERENCES Teacher(Teacher_ID));-- Insert a teacher recordINSERT INTO Teacher (Teacher_ID, Name, Office, Phone_Number)VALUES (1, 'John Doe', 'Office A', '1234567890');-- Insert a class record with a teacherINSERT INTO Class (Class_ID, Location, Number_of_Students, Teacher_ID)VALUES (1, 'Classroom A', 25, 1);-- Retrieve and display data from the teacher tableSELECT * FROM Teacher;-- Retrieve and display data from the class tableSELECT * FROM Class;
Now that we know what relational databases are, let us look at their benefits.
RDMS provides a consistent database. They accomplish this by allowing rollbacks. If any process has been canceled, all processes in that task are reverted. This creates a consistent database state and acts as fault tolerance.
In a scenario where multiple processes update one table simultaneously, RDMS uses locking mechanisms to prevent mismanagement and inconsistencies. This prevents multiple processes from modifying one table, which can lead to data loss and incorrect data manipulation.
Another functionality that RDMS provide is that they can create stored procedures. This means that we can set a predefined piece of code as a keyword for ease of use which can help in efficiency and keep the length of the queries short.
RDMS adheres to
Relational databases provide a structured and organized approach to storing data. Data is organized into one or many tables with different attributes tied to a certain data type and relationships between tables. Moreover, it offers a scalable experience where data can be stored while being secure and easy to access. Overall, relational databases provide solid data storage and access structure that can be utilized for any project.
What is a relational database?
A database that stores data in a hierarchical structure.
A database that stores data in tables and establishes relationships between the tables.
A database that stores data in a network-like structure.
A database that stores data in a flat file format.