An Introduction to SQL
What is SQL?
SQL stands for Structured Query Language
SQL is used for storing, manipulating, and retrieving data from the database.
DATABASE: A database is a well-ordered collection of data. A database is an electronic system that permits data to be easily manipulated, accessed, and updated.
DBMS: Modern databases are handled using a database management system (DBMS).
RDBMS: A Relational Database contains tables that store the data that is related in some way. SQL is the query language that allows retrieval and manipulation of table data in the relational database
USES OF SQL:
SQL is used to communicate with a database
It is usually used to fetch data, update the contents of the table, or operate on the structure of the database or tables, using any type of database tool, which will have a user interface to apply the operations on the database.
In Data Science: SQL plays an important role in data science.
Data Science is the study and analysis of data. In order to analyze the data, we need to extract it from the database. This is where SQL comes into the picture. Relational Database Management is an integral part of Data Science.
SQL Commands:
SQL commands are instructions. It is used to communicate with the database. It is also used to perform specific tasks, functions, and queries of data.
Types of SQL Commands:
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
- Data Definition Language (DDL)
DDL changes the structure of the table by creating a table, deleting a table, altering a table, etc.
All the command of DDL is auto-committed which means it permanently save all the changes in the database.
Here are some commands that come under DDL:
CREATE
ALTER
DROP
TRUNCATE
a. CREATE: It is used to create a new table in the database.
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,….]);
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax:
DROP TABLE table_name;
c. ALTER: It is used to alter the structure of the database. This change could be either to modify the characteristics of an existing attribute or probably to add a new attribute.
Syntax:
To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-definition;
d. TRUNCATE: It is used to delete all the rows from the table and free the space containing the table.
Syntax:
TRUNCATE TABLE table_name;
2. Data Manipulation Language
DML commands are used to modify the database. It is responsible for all forms of changes in the database.
The command of DML is not auto-committed which means it can’t permanently save all the changes in the database. They can be rollback.
Here are some commands that come under DML:
INSERT
UPDATE
DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a table.
Syntax:
INSERT INTO TABLE_NAME
VALUES (value1, value2, value3, …. valueN);
b. UPDATE: This command is used to update or modify the value of a column in the table.
Syntax:
UPDATE table_name SET [column_name1= value1,…column_nameN = valueN] [WHERE CONDITION]
c. DELETE: It is used to remove one or more rows from a table.
Syntax:
DELETE FROM table_name [WHERE condition];
3. Data Control Language
DCL commands are used to grant and take back authority from any database user.
Here are some commands that come under DCL:
Grant
Revoke
a.Grant: It is used to give user access privileges to a database.
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
b. Revoke: It is used to take back permissions from the user.
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
4. Data Query Language
DQL is used to fetch the data from the database.
It uses only one command:
SELECT
SELECT: This is the same as the projection operation of relational algebra. It is used to select the attribute based on the condition described by the WHERE clause.
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;