Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
SQL Commands
● SQL commands are instructions. It is used to communicate with the database. It is also used to
perform specific tasks, functions, and queries of data.
● SQL can perform various tasks like create a table, add data to tables, drop the table, modify the
table, set permission for users.
Types of SQL Commands
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV Dr.Anirudh Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
1. Data Definition Language (DDL)
● DDL or Data Definition Language actually consists of the SQL commands that can be
used to define the database schema.
● It simply deals with descriptions of the database schema and is used to create and
modify the structure of database objects in the database.
● DDL changes the structure of the table like creating a table, deleting a table, altering a table,
etc.
● All the command of DDL are auto-committed that means it permanently save all the changes in
the database.
Here are some commands that come under DDL:
● CREATE
● ALTER
● DROP
● TRUNCATE
CREATE : CREATE is used to create the database or its objects (like table, index, function,
views, store procedure and triggers).
The CREATE DATABASE statement is used to create a new SQL database.
Syntax :
CREATE DATABASE databasename;
Example :
CREATE DATABASE testDB;
The CREATE TABLE It is used to create a new table in the database.
Syntax :
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Example :
CREATE TABLE EMPLOYEE(E_ID INT,Name VARCHAR(20),Email VARCHAR(100));
-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV Dr.Anirudh Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
ALTER:
- It is used to alter the structure of the database.
- This change could be either to modify the characteristics of an existing attribute or probably to
add a new attribute.
- The ALTER command allows to make changes to the structure of a table without deleting and
recreating it.
Syntex
1) To add columns:
Alter table table_name add column_name1 data_type(size);
Alter table EMPLOYEE add Department char(30);
2) To modify columns:
Alter table table_name Alter column (column_name1 data_type(size));
Alter table EMPLOYEE Alter column Department char(50);
3) To remove a column:
Alter table table_name drop column column_name;
Alter table EMPLOYEE drop column Department ;
DROP:
- DROP command allows us to remove entire database objects from our DBMS.
- It is used to delete both the structure and record stored in the table.
- The DROP DATABASE statement is used to drop an existing SQL database.
DROP DATABASE databasename;
DROP DATABASE testDB;
- The DROP TABLE statement is used to drop an existing table in a database.
DROP TABLE table_name;
DROP TABLE Shippers;
TRUNCATE:
- It is used to delete all the rows from the table and free the space containing the table.
- The TRUNCATE TABLE statement is used to delete the data inside a table, but not the
table itself.
TRUNCATE TABLE table_name;
TRUNCATE TABLE EMPLOYEE;
-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV Dr.Anirudh Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
2. Data Manipulation Language
● DML commands are used to modify the database. It is responsible for all form of changes in the
database.
● The command of DML is not auto-committed that means it can't permanently save all the
changes in the database. They can be rollback.
Here are some commands that come under DML:
● INSERT
● UPDATE
● DELETE
INSERT:
The INSERT statement is a SQL query. It is used to insert data into the row of a table.
Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2, value3, .... valueN);
Or
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
For example:
INSERT INTO EMPLOYEE (E_ID,Name ) VALUES (1, "Kiran");
UPDATE:
This command is used to update or modify the value of a column in the table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION]
For example:
UPDATE EMPLOYEE
SET Name= 'Sonoo'
WHERE E_ID= 1
-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV Dr.Anirudh Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
DELETE:
- It is used to remove one or more row from a table.
- The DELETE statement is used to delete existing records in a table.
- Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement. The
WHERE clause specifies which record(s) should be deleted. If you omit the WHERE clause, all records in
the table will be deleted!
Syntax:
HERE condition;
DELETE FROM table_name W
For example:
DELETE FROM EMPLOYEE WHERE E_ID=2;
Delete All Records
- It is possible to delete all rows in a table without deleting the table. This means that the
table structure, attributes, and indexes will be intact:
DELETE FROM table_name;
DELETE FROM EMPLOYEE
Data Query Language
DQL is used to fetch the data from the database.
It uses only one command: SELECT
SELECT: This is the same as the projection operation of relational algebra. It is used to select the
attribute based on the condition described by WHERE clause.
Syntax:
SELECT expressions FROM TABLES
WHERE conditions;
For example:
SELECT emp_name FROM EMPLOYEE
WHERE age > 20;
-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV Dr.Anirudh Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
Data Control Language
DCL commands are used to grant and take back authority from any database user.
Here are some commands that come under DCL:
● Grant
● Revoke
Grant: It is used to give user access privileges to a database.
Example
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
Revoke: It is used to take back permissions from the user.
Example
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
Transaction Control Language
TCL commands can only use with DML commands like INSERT, DELETE and UPDATE only.
These operations are automatically committed in the database that's why they cannot be used while
creating tables or dropping them.
Here are some commands that come under TCL:
● COMMIT
● ROLLBACK
● SAVEPOINT
Commit: Commit command is used to save all the transactions to the database.
Syntax:
COMMIT;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
COMMIT;
-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV Dr.Anirudh Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
Rollback: Rollback command is used to undo transactions that have not already been saved to the
database.
Syntax:
ROLLBACK;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
ROLLBACK;
SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back the entire
transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV Dr.Anirudh Mangore