What is SQL?
- SQL stands for Structured Query Language.
- This database language is mainly designed for managing and manipulating the data in relational database management systems(RDBMS).
- SQL is a standardized programming language for accessing and manipulating databases.
- SQL allows users to retrieve specific data from a database using queries.
Types of SQL Commands
- Data Definition Language(DDL)
- CREATE
- ALTER
- DROP
- TRUNCATE
- Data Manipulation Language (DML)
- INSERT
- UPDATE
- DELETE
- Data Control Language (DCL)
- GRANT
- REVOKE
- Transaction Control Language (TCL)
- COMMIT
- ROLLBACK
- SAVEPOINT
1. DDL Commands
- DDL (Data Definition Language) is used to change the structure of the table like creating a table, modifying the table, and managing the structure of a database and its objects.
- All the commands in the DDL are auto committed that means it permanently save all the changes in the database (DB).
- The main purpose of DDL commands is to CREATE and modify the schema of a database.
a). CREATE: The CREATE command is used to create a new database or table.
Syntax
Example
b). ALTER: The ALTER statement is used to modify an existing database table like adding or deleting columns from an existing table.
Syntax
Example
c). DROP: The DROP statement is used to drop an existing table in a database. This command is used to delete both the structure and the records stored in a table.
Syntax
Example
d). TRUNCATE: A TRUNCATE SQL statement is used to remove all the rows from a table. TRUNCATE is similar to te DELETE statement but with no WHERE clause.
Syntax
Example
2. DML Commands
- DML stands for Data Manipulation Langauge.
- DML commands are used to interact with and manipulate data stored in the database.
- DML focuses on inserting, updating, retrieving, and deleting data within database tables.
a). INSERT: The SQL INSERT statement is used to insert a single or multiple records in a table.
Syntax
Example
b). UPDATE: The UPDATE table statement is used to modify the existing records (rows) in a table.
Syntax
Example
c). DELETE: The DELETE statement is used to delete existing records in a table without altering the table structure.
Syntax
Example
3. DCL Commands
- DCL stands for Data Control Language.
- DCL commands are used to control access to data in a database.
- These commands allow database administrators to grant or revoke permissions for uses to perform specific actions on the database.
a). GRANT: This statement is used to give specific permission to users or access privileges to a database. And allowing them to perform some actions like SELECT, INSERT, UPDATE.
Syntax
Example
b). REVOKE: It removes previously granted permissions from users or roles preventing them from performing specific actions.
Syntax
Example
4. TCL Commands
- TCL stands for Transaction Control Language.
- The TCL commands are used to manage and control transactions within a database.
- A transaction is a sequence of one or more SQL operations that are executed as a single unit of work.
a). COMMIT: The COMMIT command permanently saves all the transactions.
Syntax
Example
b). ROLLBACK: If any error occurs with any of the SQL grouped statements, all changes need to be aborted. The process of reversing changes is called rollback.
Syntax
Example
c). SAVEPOINT: SAVEPOINT sets a savepoint within a transaction, and allows partial rollback to a specific point instead of the entire transaction.
Example