SQL commands are used in SQL to perform various functions. These functions include building database objects, manipulating objects, populating database tables with data, updating existing data in tables, deleting data, performing database queries, controlling database access, and overall database administration.
The main categories of SQL Commands are,
- Data Definition Language (DDL)
- Data Manipulation Language (DML)
- Data Query Language (DQL)
- Data Control Language (DCL)
- Transaction Control Language (TCL)
Data Definition Language (DDL)
Data Definition Language (DDL) is a set of SQL commands used to define a database's structure, including tables, indexes, and constraints.DDL commands are used to create, modify, and delete database objects. Here are some common DDL commands:
CREATE
This command creates new database objects, such as tables, views, indexes, and constraints.
Example
ALTER
This command is used to modify the structure of existing database objects, such as adding or removing columns from a table or changing the data type of a column.
Examples
DROP
This command deletes an existing database object, such as a table, view, or index.
TRUNCATE
This command deletes all data from a table but keeps the table structure intact.
RENAME
This command renames an existing database object, such as a table or column.
Note: SQL Server doesn't support the RENAME keyword in the ALTER TABLE statement. Instead, you can use the sp_rename system stored procedure to rename a table and table column.
COMMENT
This command adds comments to a database object, such as a table or column, to provide additional information about the object.
In short, DDL commands are used for creating and modifying the structure of a database.
Data Manipulation Language (DML)
Data Manipulation Language (DML) is a set of SQL commands used to manipulate data stored in a database. DML commands retrieve, insert, update, and delete data in tables. Here are some common DML commands:
SELECT
This command retrieves data from one or more tables in a database.
INSERT
This command is used to insert new data into a table.
UPDATE
This command is used to modify existing data in a table.
DELETE
This command is used to delete data from a table.
MERGE
This command performs insert, update, or delete operations on a target table based on the data in a source table.
DML commands are essential for managing the data stored in a database. By using DML commands, users can add, update, or delete data in a table, which is crucial for maintaining the data's accuracy and integrity.
Data Query Language (DQL)
Data Query Language (DQL) is a subset of SQL commands used to retrieve data from one or more tables in a database. DQL commands are also known as data retrieval commands.
Here are some common DQL commands,
SELECT
This command retrieves data from one or more tables in a database.
DISTINCT
This command is used to retrieve unique values from a column in a table.
WHERE
This command is used to filter data based on specific criteria.
ORDER BY
This command is used to sort data in ascending or descending order.
GROUP BY
This command is used to group data based on one or more columns.
JOIN
This command combines data from two or more tables into a single result set.
DQL commands are essential for retrieving data from a database. Using DQL commands, users can filter, sort, and group data based on specific criteria, which is crucial for analyzing and interpreting the data stored in the database.
Data Control Language (DCL)
Data Control Language (DCL) is a set of SQL commands used to control access to a database. DCL commands are used to grant or revoke permissions to users and roles.
Here are some common DCL commands:
GRANT
This command is used to grant permissions to a user or a role.
REVOKE
This command is used to revoke permissions from a user or a role.
DENY
This command is used to deny permissions to a user or a role.
DCL commands are essential for managing access to a database. Using DCL commands, database administrators can control who has access to the database and what actions they can perform on the data stored in the database.
This is critical for maintaining the security and integrity of the data stored in the database.
Transaction Control Language (TCL)
Transaction Control Language (TCL) is a set of SQL commands used to manage transactions in a database. A transaction is a sequence of one or more SQL statements treated as a single unit of work. TCL commands are used to commit or rollback transactions. Here are some common TCL commands:
COMMIT
This command permanently saves the changes made by a transaction to the database.
ROLLBACK
This command is used to undo the changes made by a transaction and restore the database to its previous state.
SAVEPOINT
This command is used to set a savepoint within a transaction, which allows you to roll back to a specific point in the transaction.
RELEASE SAVEPOINT
This command is used to remove a savepoint within a transaction.
TCL commands are essential for managing transactions in a database. Using TCL commands, users can ensure that changes made to the database are consistent and reliable, even if there are errors or failures during the transaction. This is critical for maintaining the integrity of the data stored in the database.
Note
it's generally a good practice to use transactions in stored procedures to ensure data integrity and prevent data corruption. Using transactions and committing or rolling back changes as needed can help ensure that your database remains consistent and reliable.
Conclusion
I hope the blog has helped you understand SQL commands.