SQL Commands
SQL commands are instructions used to communicate with a database. They allow us to create database objects, retrieve data, insert new records, modify existing records and remove records.
What are SQL Commands?
SQL commands are statements written using SQL syntax. These statements are executed by SQL Server to perform a specific operation on a database or its data.
Simple Definition
SQL commands are instructions used to perform operations on databases and database objects.
Common SQL Commands
| Command | Purpose | Example |
|---|---|---|
| CREATE | Creates database objects. | CREATE TABLE |
| SELECT | Retrieves data. | SELECT * FROM Students |
| INSERT | Adds new records. | INSERT INTO Students |
| UPDATE | Modifies existing records. | UPDATE Students |
| DELETE | Removes records. | DELETE FROM Students |
| ALTER | Changes the structure of an object. | ALTER TABLE |
| DROP | Removes a database object. | DROP TABLE |
CREATE Command
The CREATE command is used to create database objects such as databases and tables.
CREATE TABLE Students
(
StudentId INT,
StudentName VARCHAR(100),
Age INT
);
SELECT Command
The SELECT command is used to retrieve data from one or more tables.
SELECT *
FROM Students;
INSERT Command
The INSERT command is used to add new records to a table.
INSERT INTO Students
(
StudentId,
StudentName,
Age
)
VALUES
(
101,
'Rahul',
22
);
UPDATE Command
The UPDATE command is used to modify existing records in a table.
UPDATE Students
SET Age = 23
WHERE StudentId = 101;
DELETE Command
The DELETE command is used to remove records from a table.
DELETE FROM Students
WHERE StudentId = 101;
ALTER Command
The ALTER command is used to change the structure of an existing database object.
ALTER TABLE Students
ADD Email VARCHAR(100);
DROP Command
The DROP command is used to remove a database object.
DROP TABLE Students;
SQL Command Flow
SQL commands are sent to SQL Server, processed by the database engine and then the required result or operation is returned.
User
↓
SQL Command
↓
SQL Server
↓
Database Engine
↓
Database / Table
↓
Result
SQL Commands Example
CREATE TABLE Students
(
StudentId INT,
StudentName VARCHAR(100),
Age INT
);
INSERT INTO Students
VALUES (101, 'Rahul', 22);
SELECT *
FROM Students;
UPDATE Students
SET Age = 23
WHERE StudentId = 101;
DELETE FROM Students
WHERE StudentId = 101;
Summary
In this section, we learned about common SQL commands including CREATE, SELECT, INSERT, UPDATE, DELETE, ALTER and DROP. These commands are used to create, retrieve, modify and manage data and database objects in SQL Server.