SQL Commands
SQL Commands – Definition, Syntax and Examples
SQL (Structured Query Language) is a standard
programming language used to create, manage, manipulate, and retrieve data from
relational databases.
SQL
commands are divided into five main categories:
1. DDL – Data Definition
Language
2. DML – Data Manipulation
Language
3. DQL – Data Query Language
4. DCL – Data Control
Language
5. TCL – Transaction Control
Language
1.
DDL – Data Definition Language
DDL
commands are used to create, modify, and delete database structures such as
tables and databases.
1.
CREATE
Definition: CREATE command is used to create a new
database or table.
Syntax:
CREATE TABLE table_name
(
column1 datatype,
column2 datatype
);
Example:
CREATE TABLE Student
(
RollNo INT,
Name VARCHAR(50),
Age INT,
City VARCHAR(30)
);
2.
ALTER
Definition: ALTER command is used to modify the
structure of an existing table.
Syntax:
ALTER TABLE
table_name
ADD column_name datatype;
Example:
ALTER TABLE
Student
ADD Marks INT;
3.
DROP
Definition: DROP command is used to permanently delete
a table or database.
Syntax:
DROP TABLE
table_name;
Example:
DROP TABLE
Student;
4.
TRUNCATE
Definition: TRUNCATE command is used to delete all
records from a table without deleting its structure.
Syntax:
TRUNCATE TABLE
table_name;
Example:
TRUNCATE TABLE
Student;
5.
RENAME
Definition: RENAME command is used to change the name
of an existing table.
Syntax:
RENAME TABLE
old_name
TO
new_name;
Example:
RENAME TABLE
Student
TO
Students;
Note: RENAME syntax varies among database systems.
2.
DML – Data Manipulation Language
DML
commands are used to insert, update, and delete records in a table.
6.
INSERT
Definition: INSERT command is used to add new records
to a table.
Syntax:
INSERT INTO table_name
VALUES (value1,
value2,
value3);
Example:
INSERT INTO Student
VALUES (1,
'Rahul',
15,
'Delhi');
7.
UPDATE
Definition: UPDATE command is used to modify existing
records in a table.
Syntax:
UPDATE table_name
SET column_name
=
value
WHERE condition;
Example:
UPDATE Student
SET Age
=
16
WHERE RollNo
=
1;
8.
DELETE
Definition: DELETE command is used to remove records
from a table.
Syntax:
DELETE FROM table_name
WHERE condition;
Example:
DELETE FROM Student
WHERE RollNo
=
1;
3.
DQL – Data Query Language
DQL
commands are used to retrieve data from database tables.
9.
SELECT
Definition: SELECT command is used to display or
retrieve records from a table.
Syntax:
SELECT column_name
FROM table_name;
Example:
SELECT * FROM Student;
This
displays all records and columns from the Student table.