Structured Query Language(SQL), as we all know, is the database language by which we can perform certain operations on the existing database. Also, we can use this language to create a database. SQL uses specific commands like Create, Drop, Insert, etc., to carry out the required tasks.
- DDL – Data Definition Language
- DQL – Data Query Language
- DML – Data Manipulation Language
- DCL – Data Control Language
Though many resources claim there to be another category of SQL clauses TCL – Transaction Control Language, so we will see in detail about TCL as well.
DDL or Data Definition Language consists of the SQL commands used to define the database schema. It simply deals with descriptions of the database schema and is used to create and modify the structure of database objects in the database. These commands usually are not used by a general user, who should be accessing the database via an application.
CREATE
: This command is used to create the database or its objects (like table, index, function, views, store procedure, and triggers).
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
DROP
: This command is used to delete objects from the database.
DROP TABLE table_name;
ALTER
: This is used to alter the structure of the database.
ALTER TABLE Persons
ADD Age int;
TRUNCATE
: This is used to remove all records from a table, including all spaces allocated for the records.
TRUNCATE TABLE Persons;
COMMENT
: This is used to add comments to the data dictionary.
--SELECT * FROM Customers;
SELECT * FROM Persons;
- RENAME
: This is used to rename an object existing in the database.
ALTER TABLE Persons
RENAME COLUMN Age TO Year;
DQL statements are used for performing queries on the data within schema objects. The purpose of the DQL Command is to get some schema relation based on the query passed to it. We can define DQL as follows. It is a component of the SQL statement that allows getting data from the database and imposing order upon it. It includes the SELECT statement. This command allows getting the data out of the database to perform operations with it. When a SELECT is fired against a table(s), the result is compiled into a different temporary table, which is displayed or perhaps received by the program, i.e. a front-end.
SELECT
: It is used to retrieve data from the database.
SELECT * FROM table_name;
+--------+--------------+------------+--------+---------+
| emp_id | emp_name | hire_date | salary | dept_id |
+--------+--------------+------------+--------+---------+
| 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 |
| 2 | Tony Montana | 2002-07-15 | 6500 | 1 |
| 3 | Sarah Connor | 2005-10-18 | 8000 | 5 |
| 4 | Rick Deckard | 2007-01-03 | 7200 | 3 |
| 5 | Martin Blank | 2008-06-24 | 5600 | NULL |
+--------+--------------+------------+--------+---------+
The SQL commands that deal with the manipulation of data present in the database belong to DML or Data Manipulation Language, including most of the SQL statements. It is the component of the SQL statement that controls access to data and the database. DCL statements are grouped with DML statements.
INSERT
: It is used to insert data into a table.
INSERT INTO Customers
(CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
UPDATE
: It is used to update existing data within a table.
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Frankfurt'
WHERE CustomerID = 1;
DELETE
: It is used to delete records from a database table.
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
LOCK
: Table control concurrency.
LOCK TABLES table_name [READ | WRITE]
--------------------------------------
UNLOCK TABLES;
CALL
: Call a PL/SQL or JAVA subprogram.
CREATE PROCEDURE procedure_name
AS sql_statement
GO;
EXEC procedure_name;
EXPLAIN PLAN
: It describes the access path to data.
DCL includes commands such as GRANT and REVOKE, which mainly deal with the database system's rights, permissions, and other controls.
GRANT
: This command gives users access privileges to the database.REVOKE
: This command withdraws the user’s access privileges given by using the GRANT command.
Though many resources claim there to be another category of SQL clauses TCL – Transaction Control Language, we will see in detail about TCL. TCL commands deal with the transaction within the database.
COMMIT
: Commits a Transaction.ROLLBACK
: Rollbacks a transaction in case of any error occurs.SAVEPOINT
:Sets a savepoint within a transaction.SET TRANSACTION
: Specify characteristics for the transaction.