SQL is a acronym meaning Structured Query Language. The data we use can be segregated into two types; relational and non-relational databases.

Relational databases contain tables. Tables in a relational database can form a relationships with one and another. Non-relational databases have data stored in the form of json files, graph data structures and so on.

For relational databases we use SQL. To write SQL statements we would need help of a special piece of software known as the database management system. There are many such systems such as MySQL, SQL Server, Oracle, PostgreSQL and so on. Each of this system use SQL but there are some nuances between the syntax of each system. Here we will work on MySQL.

Since I am on Arch Linux, I have installed MariaDB, since it has better support.

In order to create a database we use the following statement:

CREATE DATABASE myDB;

In order to work inside that database we use this statement:

USE myDB;

In order to drop a database we do:

DROP DATABASE myDB;

Now we know how to create, use and drop a database. Now let us see how we can alter the database.

If we want to make the database read only we could do the following :

ALTER DATABASE myDB READ ONLY = 1;

We can now not make modifications to the database, but still access the data. So if we run the drop command now it would give a error. To disable read only mode we run the same statement with the attribute equal to 0.

Think of a database as a folder. The folder holds files which are tables in case of the database.

Now we will see how we can create some tables. To create a table we do:

CREATE TABLE employees (
   employee_id INT,
   first_name VARCHAR(50),
   last_name VARCHAR(50),
   hourly_pay DECIMAL(5, 2),
   hire_date DATE
);

This above statement creates a table named employees which will have 5 columns as mentioned above. With each columns name we mention the data type which the columns can store.

Now to select a created table we do:

SELECT*FROM employees;

We can also rename an existing table by doing this:

RENAME TABLE employees TO workers;

To drop a table we can do:

DROP TABLE workers;

We can alter a table using the ALTER statement.

ALTER TABLE employees
ADD phone_number VARCHAR(15);

This adds a new column.

We can also rename a column:

ALTER TABLE employees
RENAME COLUMN phone_number TO email;
SELECT*FROM employees;

This statement only changes the name, not the data type. To change the data type we do this:

ALTER TABLE employees
MODIFY COLUMN email VARCHAR(100);