Learning SQL - Understand the Basics


\


1.Relational Databases

These databases have been made specifically to manage and store structured data. They consist of tables with rows and columns. Each table represents an entity, and rows represent individual records while columns represent attributes or fields.
EXAMPLE
Sure, let's go through an example of how a relational database is structured. Imagine you're building a simple database to store information about books in a library. In this database, you can have two tables: one for books and another for authors. These tables are related because each book is written by one or more authors.

Table 1: Authors
- Columns:
  - `AuthorID` (Primary Key): A unique identifier for each author.
  - `AuthorName`: The author's name.

Here is an illustration of how this table might appear:





Table 2: Books
- Columns:
  - `BookID` (Primary Key): A unique identifier for each book.
  - `Title`: The title of the book.
  - `PublicationYear`: The year the book was published.
  - `AuthorID` (Foreign Key): A reference to the author who wrote the book.

Here is an illustration of how this table might appear:





In this example:
- The "Authors" table stores information about authors, including their unique identifier (`AuthorID`) and their names (`AuthorName`).
- The "Books" table stores information about books, including their unique identifier (`BookID`), title, publication year, and a reference to the author who wrote the book (`AuthorID`).

The relationship between these two tables is established through the `AuthorID` column in the "Books" table, which is a foreign key referencing the `AuthorID` column in the "Authors" table. This relationship allows you to associate each book with its respective author(s).

For instance, you can use SQL to query the database and retrieve information like "List all books written by J.K. Rowling" by using SQL joins to connect the two tables based on the `AuthorID` column.

This is a simple example, but it illustrates the fundamental concept of a relational database, where data is organized into tables, and relationships between tables allow you to query and manipulate the data effectively.


2.SQL Operations

   - Querying Data:The primary purpose of SQL is to retrieve data from a database using the `SELECT` statement. You can specify the columns you want to retrieve and use various conditions to filter the data.

   - Inserting Data SQL provides the `INSERT` statement to add new records to a table.

   - Updating Data The `UPDATE` statement allows you to modify existing records in a table.

   - Deleting Data The `DELETE` statement is used to remove records from a table.

   - Creating and Modifying Structures SQL also lets you create and manage the structure of your database. You can use statements like `CREATE TABLE` to define the structure of a table, `ALTER TABLE` to modify it, and `DROP TABLE` to delete a table.

Certainly, let's explore some examples of SQL operations. I'll provide examples for each of the primary SQL operations: SELECT (querying data), INSERT (adding data), UPDATE (modifying data), and DELETE (removing data).


SELECT (Querying Data):


Suppose you have a table named "Employees" with the following structure:

Example SQL query:

This SQL query will return:



INSERT (Adding Data):

Suppose you want to add a new employee to the "Employees" table:

Example SQL query:


This SQL query will add a new employee to the table.

UPDATE (Modifying Data):

Suppose you want to give John Doe a salary raise:

Example SQL query:


This SQL query will update John Doe's salary to 55000.

DELETE (Removing Data):

Suppose you want to remove Bob Johnson from the "Employees" table:

Example SQL query:



This SQL query will delete Bob Johnson's record from the table.

These examples demonstrate the basic SQL operations for querying, inserting, updating, and deleting data in a relational database. SQL is a versatile language that can be used to perform a wide range of data manipulation tasks in a database system.


SQL is the foundation for working with data in a relational database, and mastering its various operations and syntax will empower you to effectively manage and manipulate data in this type of database system.

Previous Post Next Post