SQL Commands
- SQL commands are the instructions. Which are used to communicate with the database. SQL commands are also used to perform specific tasks, functions, and queries of data.
- SQL commands can perform different tasks like creating a table, adding data to tables, dropping the table, modifying the table set permission for users.
Types of SQL Commands with Example:
There are five types of SQL commands
1) DDL
DDL stands for Data Definition Language these commands are used change the structure of the table like creating a table, deleting a table, altering a table, etc. All DDL commands are auto-committed that means they permanently save all the changes in the database.
Here are some commands that come under DDL:
i) CREATE: The CREATE SQL command is used to create a new table in the database.
Syntax:
Example:
ii) DROP: The DROP command is used to delete structure and the record stored in the table.
Syntax:
Example:
iii) ALTER: The ALTER command is used to change the structure of the database. This may be used to change the characteristics of an existing attribute or to create a new attribute.
Syntax to Add New Column
Syntax to Modify Existing Column
Example
iv) TRUNCATE: The TRUNCATE command is used to delete all the rows from the given table.
Syntax:
Example:
2) DML
DML stands for Data Manipulation Language and these commands are used to modify the database. The DML commands are responsible for all forms of changes in the database. The command of DML is not auto-committed.
Here is the list of DML commands:
i) INSERT: The INSERT statement is used to insert data into the row of a table.
Syntax:
Example:
ii) DELETE: The DELETE statement is used to remove one or more rows from a table based on the given condition.
Syntax
Example:
3) DCL
DCL stands for Data Control Language and these commands are used to grant and revoke authority from any database user.
i) GRANT: The GRAND statement is used to give user access privileges to the database.
Syntax:
ii) REVOKE: The REVOKE command is used to take back permissions from the user.
Syntax:
4) TCL
TCL stands for Transaction Control Languages. The TCL commands can only be used with DML commands like INSERT, DELETE, and UPDATE only.
i) COMMIT: The commit command saves every operation to the database. It enables the user to permanently store each change made to a database or table transaction. Once you execute the COMMIT, the database cannot go back to its previous state in any case.
Syntax:
Example:
ii) ROLLBACK: The rollback command is used to undo transactions that have not already been saved to the database.
Syntax:
Example:
iii) SAVEPOINT: The SAVEPOINT is used to roll the transaction back to a certain point without rolling back the entire transaction.
Syntax:
5) DQL
DQL stands for Data Query Language and it is used to fetch the data from the database.
i) SELECT: The SELECT command in SQL is used to select the attribute based on the condition.
Syntax:
Example: