SQL or Structured Query Language is used for accessing and manipulating a database.

What is a database?

A database is a collection of structured and related information. A database is used to insert, retrieve, and update information in a time-efficient way.

Why a database is required?

Suppose you work in an organization and the organization employs more than 1000 employees. The organization needs a database that will have all the information of employees like Name, Sex, Age, Designation, Date of joining, etc.

The information in a database somewhat looks like the information in a spreadsheet, but the retrieval of this information is much faster as compared to spreadsheets.

Image for post

A spreadsheet containing employee data

A database may contain millions of records similar to records shown in the picture above. It is nearly impossible to update or retrieve millions of records from spreadsheets without causing your computer to crash.

To make your life easier, you can use a database and use SQL to retrieve, add, delete, or manipulate the existing data.

Let us assume that you already have a **Database Management System (DBMS)**installed on your computer. If it is not present then you can install any DBMS from the internet. SQLite, MySQL, Microsoft SQL server are some of the DBMS that can be used to create a database. I will be using Microsoft SQL server in this article.

Creating a database in DBMS.

This can be done by using Create database statement:

SYNTAX: Create database database_name;

Create database employees;

Image for post

employees database created

The next step will be to create tables in our employees database. We will be creating 2 tables, namely employees and salaries, in our employees database.

A database can contain tens or hundreds of tables

We can use the Create Table statement to create employees table having emp_no, first_name, birth_date, last_name, gender, hire_date as its columns (or attributes).

Create Table employees (emp_no INT,
birth_date DATE,
first_name VARCHAR(20),
last_name VARCHAR(20),
gender CHAR,
hire_date DATE,
PRIMARY KEY (emp_no)
);

Image for post

employees table created

In a similar fashion, salaries table with emp_no, salary, from_date, to_date columns can be created.

Create Table salaries (
emp_no INT,
salary INT,
from_date DATE,
to_date DATE
)

Image for post

salaries table created

Once we have created the employees and salaries tables, we need to insert data into those tables. This can be done using INSERT INTO statement

Insert INTO employees (emp_no,birth_date,first_name,last_name,gender,hire_date) VALUES (223,’1965–10–12', ‘John’, ‘Oliviar’, ‘M’,’2018–10–23');
Insert INTO employees (emp_no,birth_date,first_name,last_name,gender,hire_date) VALUES (225,’1968–04–20', ‘Emma’, ‘Masterson’, ‘F’,’2017–03–12');

Microsoft SQL server accepts YYYY-MM-DD format to insert dates

Image for post

Values inserted into employees table

Similarly, data can be loaded into salaries table.

Image for post

Values inserted into salaries table

#sql #database #programming #developer

A Beginner’s Guide to the SQL Language
5.10 GEEK