Concept:
SQL (Structured Query Language) is used to manage and manipulate data in relational databases. SQL commands are mainly classified into two categories:
- DDL (Data Definition Language): Used to define and modify the structure of database objects such as tables.
- DML (Data Manipulation Language): Used to insert, modify, and delete data stored in database tables.
1. DDL Commands
DDL commands are used to create and modify the structure of database objects.
(a) CREATE Command
The
CREATE command is used to create a new table or database object.
Syntax:
\[
\texttt{CREATE TABLE table_name (}
\]
\[
\texttt{column1 datatype,}
\]
\[
\texttt{column2 datatype, ... );}
\]
Example:
\[
\texttt{CREATE TABLE Student (}
\]
\[
\texttt{ID INT,}
\]
\[
\texttt{Name VARCHAR(50),}
\]
\[
\texttt{Age INT);}
\]
(b) ALTER Command
The
ALTER command is used to modify the structure of an existing table, such as adding or deleting columns.
Syntax:
\[
\texttt{ALTER TABLE table_name ADD column_name datatype;}
\]
Example:
\[
\texttt{ALTER TABLE Student ADD Address VARCHAR(100);}
\]
(c) DROP Command
The
DROP command is used to delete an entire table or database object permanently.
Syntax:
\[
\texttt{DROP TABLE table_name;}
\]
Example:
\[
\texttt{DROP TABLE Student;}
\]
2. DML Commands
DML commands are used to manipulate the data stored in database tables.
(a) INSERT Command
The
INSERT command is used to add new records into a table.
Syntax:
\[
\texttt{INSERT INTO table_name (column1, column2, ...)}
\]
\[
\texttt{VALUES (value1, value2, ...);}
\]
Example:
\[
\texttt{INSERT INTO Student (ID, Name, Age)}
\]
\[
\texttt{VALUES (1, 'Rahul', 20);}
\]
(b) UPDATE Command
The
UPDATE command is used to modify existing records in a table.
Syntax:
\[
\texttt{UPDATE table_name}
\]
\[
\texttt{SET column_name = value}
\]
\[
\texttt{WHERE condition;}
\]
Example:
\[
\texttt{UPDATE Student}
\]
\[
\texttt{SET Age = 21}
\]
\[
\texttt{WHERE ID = 1;}
\]
(c) DELETE Command
The
DELETE command is used to remove records from a table.
Syntax:
\[
\texttt{DELETE FROM table_name}
\]
\[
\texttt{WHERE condition;}
\]
Example:
\[
\texttt{DELETE FROM Student}
\]
\[
\texttt{WHERE ID = 1;}
\]