Commands:: A Complete Guide

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

SQL

Commands:
A Complete Guide
02 codebasics.io

Structured query language (SQL) is a programming language for storing and


processing information in a relational database.

In general, “Command” is an instruction given to a system or person to perform a


specific task or function.

SQL commands are instructions used to communicate with a database to perform


tasks, queries, and modifications to database data.

Provide the
name of the
person whose Dhaval
roll number
is 15.

SQL Commands
03 codebasics.io

TYPES OF SQL COMMANDS

DDL DML DCL TCL


Create Select Grant Commit
Alter Insert Revoke Rollback
Drop Update Savepoint

Truncate Delete

1 DDL COMMANDS:
DDL stands for Data Definition Language.

DDL Commands are used to define, modify, and delete the structure of
database objects like tables, indexes, views, and schemas.

CREATE ALTER TRUNCATE DROP

CREATE:
This command is used to create new objects in the database.

Syntax:
CREATE TABLE table_name (column1 datatype, column2 datatype, ...);
04 codebasics.io

ALTER:
This command is used to alter the structure of the database.

Adding a Column to a Table

Syntax:
ALTER TABLE table_name
ADD column_name datatype(size);

Modifying the Data Type of a Column

Syntax:
ALTER TABLE table_name
MODIFY column_name datatype(size);

Dropping a Column from a Table

Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

Renaming the existing table’s column

Syntax:
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;

TRUNCATE:
Removes all records from a table without deleting the table structure.

Syntax:
TRUNCATE TABLE table_name;

DROP:
Deletes an entire table and its data.

Syntax:
DROP TABLE table_name;
05 codebasics.io

2 DML COMMANDS:
DML stands for Data Manipulation Language.

DML commands are used for managing data within tables.

They facilitate CRUD operations: creating, reading, updating, and deleting


data, using INSERT, SELECT, UPDATE, and DELETE commands, respectively.

INSERT SELECT UPDATE DELETE

INSERT:
Inserts new data into a table.

Syntax:
INSERT INTO table_name (column1, column2) VALUES (value1, value2);

SELECT:
Retrieves data from one or more tables.

Syntax:
SELECT column1, column2 FROM table_name;
06 codebasics.io

UPDATE:
Modifies existing data in a table.

Syntax:
UPDATE table_name SET column1 = value1 WHERE condition;

DELETE:
Removes data from a table based on a condition.

Syntax:
DELETE FROM table_name WHERE condition;

3 DCL COMMANDS:
DCL stands for Data Control Language.

DCL Commands are used to grant and revoke user access to database
resources.

GRANT REVOKE
07 codebasics.io

GRANT:
It is used to give user access privileges to a database.

Syntax:
GRANT <privilege_name> [ON <object_name>] TO <grantee> [WITH
GRANT OPTION];

REVOKE:
It is used to give take-back access privileges from the user.

Syntax:
REVOKE <privilege_name> [ON <object_name>] FROM <grantee>
[CASCADE | RESTRICT];

4 TCL COMMANDS:
TCL stands for Transaction Control Language.

TCL Commands are used to manage changes made by DML operations.

Final
Output
COMMIT ROLLBACK
08 codebasics.io

COMMIT:
It is used to save all changes made during the current transaction.

Syntax: COMMIT;

ROLLBACK:
It is used to revert changes made during the current transaction.

Syntax: ROLLBACK;

ROLLBACK without SAVEPOINT Final


Output

ROLLBACK

ROLLBACK with SAVEPOINT

ROLLBACK

SAVEPOINT

SAVAPOINT:
It is used to save the different parts of the same transaction using different names.

Syntax: SAVEPOINT savepoint_name;


E n a b l i n g C a r e e r s

Found this interesting?


Dive deeper!
Check this Free SQL Tutorial Series

Join our YT community of 1M learners

codebasics.io

You might also like