2 SQL Basic Operatons
2 SQL Basic Operatons
Contents
Database at a Glance
SQL Introduction
Commands of SQL
DML With Examples
DDL With Examples
DCL With Examples
TCL With Examples
Conclusion
2
12/11/2023
What Is a Database?
A database is a collection of related data.
A database management system (DBMS) is a collection of
programs that enables users to create and maintain a database.
To control redundancy.
To restrict unauthorized access.
To provide persistent storage
To provide multiple user interfaces.
To representing complex relationships among data.
To provide backup and recovery.
12/11/2023 3
SQL is a Standard
SQL is an ANSI (American National Standards Institute) standard
computer language for accessing and manipulating database
systems.
12/11/2023 4
Categorization of Commands
12/11/2023 5
DML – Data Manipulation Language
Select
Insert
Update
Delete
Grant
Revoke
• Commit
• Rollback
12/11/2023 7
DML Commands with Syntax & Ex
Select
Ei Ename Sal
d
Syntax(diff Formats) : 1 Paul 12000
• Select column_name(s) 2 Anshuman 10000
FROM table_name
3 Ambrish 12000
• SELECT *
4 Neethika 8000
FROM table_name Emp
Ex(diff Formats):
• Select eid, ename, sal from emp
• Select * from emp
12/11/2023 8
DML Commands with Syntax & Ex
Insert
Ei Ename Sal
d
Syntax : 1 Paul 12000
Insert into table_name 2 Anshuman 10000
values(val1,val2,val3…,valn) 3 Ambrish 12000
Insert into table_name(col1,col2,col3…coln) 4 Neethika 8000
values(val1,val2,val3…,valn) 5 Swamy 7000
Emp
Ex:
Insert into emp values(5,’Swamy’,7000)
Insert into emp(eid,ename,sal) values(5,’Swamy’,7000)
12/11/2023 9
DML Commands with Syntax & Ex
Ei Ename Sal
Update d
Syntax :
1 Paul 12000
update table_name
2 Anshuman 10000
set col1=val1, col2=val2,…coln=valn
where col_name=value 3 Ambrish 12000
Ex: 4 Neethika 8000
Updating single column(only sal) 5 Swaminat 7500
h Emp
Update emp set sal=7500 where eid=5
Update emp
set sal=7500,ename=‘Swaminath’ where eid=5
12/11/2023 10
DML Commands with Syntax & Ex
Ei Ename Sal
Delete d
Syntax :
1 Paul 12000
Delete table_name where col_name=value
Delete from table_name where col_name=value 2 Anshuman 10000
3 Ambrish 12000
4 Neethika 8000
Ex:
5 Swami 7500
Deleting a selected row(by its eid)
The below query allows you to delete the row with eid=5.
Delete from emp where eid=5
OR Emp
Delete emp where eid=5
12/11/2023 11
Operators, Conditional Clauses, Keywords,
Predefined Functions….
12/11/2023 12
Syntax & Ex : Operators, Conditional
Clauses….
Syntax Examples
12/11/2023 13
Syntax & Ex : Predefined Functions, Keywords,
Clauses…..
Syntax Examples
Retrieving min value for a column from Retrieving min sal from emp
table • SELECT min(sal)
SELECT min(column_name) FROM emp
FROM <table_name>
Retrieving max sal from emp
Retrieving max value for a column from
• SELECT max(sal)
table
FROM emp
SELECT max(column_name)
FROM <table_name>
Retrieving count of rows/records from table
12/11/2023 14
DDL Commands with Syntax & Ex
The below format helps us to create structure of table emp with 3 columns
eid,ename,sal with eid set to primary key and identity(1,1)
12/11/2023 15
DDL Commands with Syntax & Ex
Drop
Syntax :
DROP table table_name
Ex:
The below query drop the table emp( records and structure of
table).
12/11/2023 16
DDL Commands with Syntax & Ex
Truncate
Syntax :
Truncate table table_name
Ex(2 Formats):
The below query helps us to delete the table emp(the records
inside the table-if any )
12/11/2023 17
DDL Commands with Syntax & Ex
Ex(Add Column):
12/11/2023 18
DCL Commands with Syntax & Ex
Ex:
GRANT SELECT ON Emp TO user1
12/11/2023 19
DCL Commands with Syntax & Ex
Ex:
REVOKE SELECT ON Emp FROM user1
12/11/2023 20
TCL Commands with Syntax & Ex
Commit
Syntax
COMMIT
12/11/2023 21
TCL Commands with Syntax & Ex
ROOLBACK
Syntax
ROLLBACK
Example
BEGIN TRAN
DELETE FROM EMP
ROLLBACK
SELECT * FROM EMP
END TRAN
12/11/2023 22