0% found this document useful (0 votes)
8 views

Database Systems - Structured Query Language (SQL)

Structured Query Language

Uploaded by

Muneer Umar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Database Systems - Structured Query Language (SQL)

Structured Query Language

Uploaded by

Muneer Umar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

“Database Management Systems”

CS-222
Lecture 9
“Structured Query Language (SQL)”

By: Dr. Muhammad Muneer Umar


Lecturer in Computer Science
Institute of Computing
KUST, Pakistan
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
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
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)


Attribute names

RELATIONS IN SQL
Product

Table name
PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Tuples or rows
What can SQL do with databases?
Execute
Create stored Delete
queries Insert records Create views
procedures in records from
against a in a database in a database
a database a database
database

Retrieve data Update Set permissions


Create new
from a records in a
on tables, Create new
procedures, and tables in a
database database databases
views database
SQL QUERY
BASIC FORM:
SELECT
SELECT <attributes>
<attributes>
FROM
FROM <one<oneorormore
morerelations>
relations>
WHERE
WHERE <conditions>
<conditions>
SAMPLES:
SELECT
SELECT reg,
reg,sname
sname
FROM
FROM student_table
student_table
WHERE
WHERE city
city==‘Kohat’
‘Kohat’; ;
INSERT
INSERTINTO
INTOstudent_table
student_table(reg,
(reg,sname,
sname,city)
city)
VALUES
VALUES(‘CS123456’
(‘CS123456’, ,‘Farhan’
‘Farhan’, ,‘Kohat’
‘Kohat’););
DROP
DROPTABLE
TABLEstudent_table
student_table; ;
SHOW
SHOWTABLES
TABLES; ;
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
3) STATEMENT "SELECT std_id, std_name, std_age“
A complete query may refer to as a "statement" and we and
can say that set of two or more clause are known as a “ORDER BY std_id”
"statement" in SQL.
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.
RULES FOR TYPING SQL STATEMENTS
• SAME: DIFFERENT:
• SELECT  Select  select ‘Ali’  ‘ali’
• Product  product

• CONSTANTS/VALUES:
• Correct  ‘abc’
• Wrong  “abc”
• Use single quote (‘) for strings/character values
• ‘Ahmed Khan’ , ‘Peshawar’ , ‘+92-3331234567
• Use grave accent (`) for object names
• `table_name` , `std_info_view` , `procedure1`
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
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
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
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
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.
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
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
THANKS
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

You might also like