SQL

What is SQL and Databses? | Introduction to SQL

What is SQL and Databses? | Introduction to SQL

Introduction to SQL

SQL (Structured Query Language) is a language that is used to communicate with a relational database management system.

Before we know about SQL, let's know about the database first.

What is a Database

The database is an organized collection of information. It is organized so that it can be easily accessed, managed, updated. It makes data management very easy. For example, Facebook needs a database to store the information of the members, their friends, activities, messages, and a lot more.

Database Management System

A DBMS(Database Management System) is a software that is used to manage different databases. It helps to create and manage the database such as-

  1. Data Export & Import
  2. Data Security
  3. Managing a huge amount of data
  4. Data backup
  5. It assists requests from multiple concurrent databases.

There are two types of databases-

  • Non-Relational Database
  • Relational Database

Non-Relational Database

In this type of database, data is not organized in the form of tables instead data is stored in the form of key and value pairs. Examples of Non-Relational Database are HBase, Cassandra, MongoDB, etc.

SQL doesn't help in interacting with this type of database.

Relational Database

In this type of database, data is organized in the form of tables. A table consisting of rows and columns of data. The table contains a unique key that uniquely identifies each row of the table.

SQL is used to interact with the relational database.

What is SQL?

Structured Query Language (SQL) which is used to communicate with the Relational Database Management System(RDBMS). Examples of RDBMS are MySQL, Oracle, etc.

SQL is used to perform CRUD (Create, Retrieve, Update, Delete) operations on RDBMS. It also helps in performing administrative tasks such as database security, data backup, etc.

Types of Structured Query Language - 

  1. Data Definition Language (DDL)
  2. Data Query Language (DQL)
  3. Data Manipulation Language (DML)
  4. Data Control Language (DCL)

Data Definition Language

DDL is used to define the structure of the table like creating a table, deleting a table, etc.

Here are some examples of DDL commands-

  • CREATE
  • DROP
  • ALTER
  • TRUNCATE

CREATE - It is used to create a table

Syntax:

CREATE TABLE table_name(Column_Name Datatypes[,....]);  

Example:

CREATE TABLE EMP(Name VARCHAR(30), Email VARCHAR(100), UID VARCHAR(50));  

DROP - It is used to delete the structure as well as the record stored in the table

Syntax:

DROP TABLE ; 

Example:

DROP TABLE EMP;

ALTER- It is used to alter the structure of the database such as modifying the column definition or adding a new column.

Syntax:

To add a new column in the table

ALTER TABLE table_name ADD column_name COLUMN-DEFINITION; 

To Modify an existing table

ALTER TABLE MODIFY(COLUMN DEFINITION....); 

Example:

ALTER TABLE EMP ADD(ADDRESS VARCHAR(40));  
ALTER TABLE EMP MODIFY (NAME VARCHAR(50));  

TRUNCATE- It is used to delete all the records from the table.

Syntax:

TRUNCATE TABLE table_name;

Example:

TRUNCATE TABLE EMP;  

Data Query Language

DQL is used to fetch the data from the table. It has only one command SELECT.

SELECT- It is used to fetch the data from the table 

Syntax:

SELECT expressions FROM TABLE;

Example:

SELECT Name FROM EMP;

Data Manipulation Language

DML command is used to modify the database.

Here are some examples of DDL commands-

  • INSERT
  • UPDATE
  • DELETE

INSERT- It is used to insert the data into the table 

Syntax:

INSERT INTO TABLE_NAME    
(col1, col2, col3,.... col N)  
VALUES (value1, value2, value3, .... valueN);  

Or

INSERT INTO TABLE_NAME    
VALUES (value1, value2, value3, .... valueN);    

Example:

INSERT INTO EMP(Name, Email, UID) VALUES ("Amit", "[email protected]","1344XYZ");  

UPDATE- It is used to update the value of a column in the table 

Syntax:

UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION];

Example:

UPDATE EMP
SET Name = 'Abhi'    
WHERE UID = '1234XYZ';

DELETE- It is used to remove the row from the table 

Syntax:

DELETE FROM table_name [WHERE condition]; 

Example:

DELETE FROM EMP
WHERE Name="Abhi";

Data Control Language

DCL commands are used to grant and revoke authority from any database user.

Here are some examples of DDL commands-

  • GRANT
  • REVOKE

GRANT- It is used to give access rights to a database

Example:

GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER; 

REVOKE- It is used to remove rights from a user

Example:

REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
Rajib Kumar Jha

Rajib Kumar Jha is a certified Big Data Architect, is a Computer Science student at Chandigarh University.