Lecture 9 (Week 8) - Structured Query Language (SQL).pptx
1. “Database Management Systems”
CS-222
Lecture 17
“Structured Query Language (SQL)”
Dr. Muhammad Muneer Umar
By:
Lecturer in Computer Science
Institute of Computing
KUST, Pakistan
2. Objective of the today’s topic
• To know the fundamentals about Structure Query Language (SQL)
• To know the importance and usability of SQL in RDBMS
• To understand the basic syntax of SQL code
• To define SQL statements, clauses and keywords
• To learn about the utilization and management of RDBMS through SQL
• To understand the categories of SQL statements
3. CONTENTS
• Introduction
• What can SQL do with databases?
• SQL Query
• SQL statements, clauses and keywords
• Rules for typing SQL statements
• SQL commands
• DDL: Data Definition Language
• DML: Data Manipulation Language
• TCL: Transaction Control Language
• DCL: Data Control Language
• DQL: Data Query Language
4. INTRODUCTION
• “Structure Query Language(SQL) is a database query language used for storing and
managing data in Relational DBMS”.
• First commercial language introduced for E.F Codd's Relational model of database.
• Standardization:
• American National Standards Institute (ANSI) in 1986,
• International Organization for Standardization (ISO) in 1987
• Used by almost all RDBMS(MySql, Oracle, Infomix, Sybase, MS Access)
6. What can SQL do with databases?
Execute
queries
against a
database
Retrieve data
from a
database
Insert records
in a database
Update
records in a
database
Delete
records from
a database
Create new
databases
Create new
tables in a
database
Create stored
procedures in
a database
Create views
in a database
Set permissions
on tables,
procedures, and
views
7. SQL QUERY
BASIC FORM:
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
SAMPLES:
SELECT reg, sname
FROM student_table
WHERE city = ‘Kohat’ ;
INSERT INTO student_table (reg, sname, city)
VALUES (‘CS123456’ , ‘Farhan’ , ‘Kohat’ );
DROP TABLE student_table ;
SHOW TABLES ;
8. SQL STATEMENTS, CLAUSES AND KEYWORDS
SELECT std_id, std_name, std_age FROM student ORDER BY std_id;
1) KEYWORD
The individual tokens (elements) which are predefined,
and we cannot change their meaning are known as SQL
keyword.
In the given example: The keyword are "SELECT" ,
"FROM“ and “ORDER BY” 2) CLAUSE
Clause is a part of an SQL statement.
In the given example: The clause are
"SELECT std_id, std_name, std_age“
and
“ORDER BY std_id”
3) STATEMENT
A complete query may refer to as a "statement" and we
can say that set of two or more clause are known as a
"statement" in SQL.
9. RULES FOR TYPING SQL STATEMENTS
• SQL statement is not case sensitive you may write the statement in any
case (even keywords like SELECT, FROM, DROP etc.) are not case
sensitive.
• You can write a complete SQL statement in a single line (but, you
should separate them in multiple lines, each clause should be written in
separate line).
• You may use to enhance the SQL Query readability.
Note: keywords should be written in UPPERCASE (for better readability)
and other words should be written in lowercase.
11. SQL COMMANDS
• SQL defines following ways to manipulate data stored in an RDBMS.
• DDL: Data Definition Language
• DML: Data Manipulation Language
• TCL: Transaction Control Language
• DCL: Data Control Language
• DQL: Data Query Language
12. DDL: Data Definition Language
• This includes changes to the structure of the table
• like creation of table, altering table, deleting a table etc.
• All DDL commands are auto-committed.
• That means it saves all the changes permanently in the database
Command Description
CREATE to create new table or database
ALTER for alteration
TRUNCATE delete data from table
DROP to drop a table
RENAME to rename a table
13. DML: Data Manipulation Language
• DML commands are used for manipulating the data stored in the table
and not the table itself.
• DML commands are not auto-committed.
• It means changes are not permanent to database, they can be rolled back.
Command Description
INSERT to insert a new row
UPDATE to update existing row
DELETE to delete a row
MERGE merging two rows or two tables
14. TCL: Transaction Control Language
• These commands are to keep a check on other commands and their
affect on the database.
• It can also make any temporary change permanent.
Command Description
COMMIT to permanently save
ROLLBACK to undo change
SAVEPOINT to save temporarily
15. DCL: Data Control Language
• Data control language are the commands to grant and take back
authority from any database user.
Command Description
GRANT grant permission of right
REVOKE take back permission.
16. DQL: Data Query Language
• Data query language is used to fetch data from tables based on
conditions that we can easily apply
Command Description
SELECT retrieve records from one or more table
17. SUMMARY
• SQL: A language used for management of RDBMS
• SQL can be used to perform various operations on RDBMS
• Also we can do managerial task with SQL
• SQL is not case sensitive.
• SQL commands can be classified as:
• DDL, DML, TCL, DCL, DQL
19. Sample Queries (Practice in Workbench):
• Select a database
• Select Data from Database Table
• Insert Data into a table
• Update existing data
• Delete data from a table
• Create a new table