In the world of data management, relational databases are a fundamental technology. They provide a structured way to store and manage data, making it easier to retrieve, update, and maintain. This tutorial will introduce you to the basics of relational databases and SQL (Structured Query Language), which is used to interact with these databases.
A relational database is a collection of related tables that are linked together using common fields. Each table consists of rows and columns, where each row represents a record, and each column represents a field or attribute of the record. The relationship between tables is defined by keys: primary keys and foreign keys.
Tables are the basic unit of data storage in a relational database. Each table has a specific structure defined by its columns, and each column has a name and a data type.
SQL is the standard language used to manage and manipulate relational databases. It allows you to perform various operations such as creating tables, inserting data, querying data, updating records, and deleting records.
Let's walk through some practical examples to understand how relational databases and SQL work.
First, let's create a simple table named students with columns for id, name, and age.
1CREATE TABLE students (2id INT PRIMARY KEY,3name VARCHAR(100),4age INT5);
Next, let's insert some data into the students table.
1INSERT INTO students (id, name, age) VALUES2(1, 'Alice', 20),3(2, 'Bob', 22),4(3, 'Charlie', 21);
To retrieve data from the students table, you can use the SELECT statement.
1SELECT * FROM students;
The output will be:
id | name | age ---|---------|----- 1 | Alice | 20 2 | Bob | 22 3 | Charlie | 21
To update a record in the students table, use the UPDATE statement.
1UPDATE students SET age = 23 WHERE id = 1;
To delete a record from the students table, use the DELETE statement.
1DELETE FROM students WHERE id = 2;
Now that you have a basic understanding of relational databases and SQL, you might want to explore more advanced topics such as: