Chapter4_SQL
Chapter4_SQL
MANAGEMENT
SYSTEM
Subash Manandhar
1
Chapter 4 – SQL
Subash Manandhar 2
Chapter 4 – SQL
Subash Manandhar 3
Chapter 4 – SQL
Subash Manandhar 4
Chapter 4 – Relational Database Query Language
4.1 SQL
• CREATE TABLE student • CREATE TABLE student
( (
S_ID int PRIMARY KEY, S_ID int ,
name varchar (20) NOT NULL, name varchar (20) NOT NULL,
address varchar(20) address varchar(20),
); CONSTRAINTS pk_sid PRIMARY
• CREATE TABLE student KEY(S_ID, name )
( )
S_ID int ,
name varchar (20) NOT NULL,
address varchar(20),
CONSTRAINTS pk_sid PRIMARY
KEY(S_ID )
)
Subash Manandhar 5
Chapter 4 – SQL
Subash Manandhar 6
Chapter 4 – SQL
Subash Manandhar 7
Chapter 4 – SQL
Subash Manandhar 8
Chapter 4 – SQL
Subash Manandhar 9
Chapter 4 – SQL
Subash Manandhar 10
Chapter 4 – SQL
Subash Manandhar 11
Chapter 4 – SQL
Subash Manandhar 12
Chapter 4 – SQL
Subash Manandhar 13
Chapter 4 – SQL
Subash Manandhar 14
Chapter 4 – SQL
• SQL functions:
• SQL has many built-in functions for performing calculations on data.
• AVG() => returns average value of a numeric column.
• SELECT AVG(columnname) FROM tablename;
• COUNT() => returns the no. of rows.
• SELECT COUNT(columnname) FROM tablename;
• SELECT COUNT(*) FROM tablename;
• MAX() => returns the largest value of selected column.
• SELECT MAX(columnname) FROM tablename;
• MIN() => returns the smallest value of selected column.
• SELECT MIN(columnname) FROM tablename;
• SUM() => returns the total sum of numeric column.
• SELECT SUM(columnname) FROM tablename;
Subash Manandhar 15
Chapter 4 – SQL
• SQL functions:
• UPPER() => converts value of field to uppercase.
• SELECT UPPER(columnname) FROM tablename;
• LOWER() => converts value of field to lowercase.
• SELECT LOWER(columnname) FROM tablename;
• SQRT() => used to find out square root of any number.
• SELECT SQRT(columnname) FROM tablename;
• RAND() => used to produce random numbers between 0 and 1.
• SELECT * FROM tablename ORDER BY RAND();
• CONCAT() => used to concatenate two strings to form single string
• SELECT CONCAT(col1,col2) FROM tablename;
Subash Manandhar 16
Chapter 4 – SQL
Subash Manandhar 17
Chapter 4 – SQL
Subash Manandhar 18
Chapter 4 – SQL
Subash Manandhar 19
Chapter 4 – SQL
Subash Manandhar 20
Chapter 4 – SQL
Subash Manandhar 21
Chapter 4 – SQL
• Subquery:
• Is a query within another SQL query and embedded within where clause.
• Is used to return data that will be used in main query as a condition to further
restrict the data to be retrieved.
• Can be used with select, insert, update and delete statements along with
operators like =,>,<,>=,<=, IN, BETWEEN etc.
• Rules for subquery:
• Must be enclosed with in parenthesis.
• Can have only one column in SELECT clause.
• An ‘ORDER BY’ can not be used, but ‘GROUP BY ’ can be.
• Subquery that return more than one row can only be used with multiple value
operator like IN operator.
• A subquery can’t be immediately enclosed in a set function.
• ‘BETWEN’ operator can’t be used with subquery, however ‘BETWEEN’ operator
can be used within subquery.
• SELECT list cannot include any references to values that evaluate BLOB,ARRAY.
Subash Manandhar 22
Chapter 4 – SQL
Subash Manandhar 23
Chapter 4 – SQL
• Set Operations:
• SQL used set operations to combine same type of data from two or more
tables.
• Set operations are based on principle of mathematical set theory.
• To perform Set operations, Union Compatibility property must be hold i.e.
the relations involved in the operations must have an equal no. of attributes
and they must have the same domain of attributes.
• Different set operations are:
• UNION
• INTERSECT
• EXCEPT
Subash Manandhar 24
Chapter 4 – SQL
• Set Operations:
• UNION
• Combines two or more result sets into a single set, without duplicates.
• Any duplicate records are automatically removed unless UNION ALL is used.
• e.g.
• SELECT name, address FROM student
UNION
SELECT name, address FROM teacher
• INTERSECT
• Takes the data from both result sets which are in common.
• Removes duplicate rows from the final result set.
• INTERSECT ALL operator does not remove duplicate rows from the final result
set.
Subash Manandhar 25
Chapter 4 – SQL
• Set Operations:
• INTERSECT
• e.g.
• SELECT name, address FROM student
INTERSECT
SELECT name, address FROM teacher
• EXCEPT
• Takes the data from first result set, but not the second.
• Automatically eliminates duplicates.
• EXCEPT ALL retains all duplicates.
• e.g.
• SELECT name, address FROM student
EXCEPT
SELECT name, address FROM teacher
Subash Manandhar 26
Chapter 4 – SQL
• Views
• View is a virtual table based on the result set of an SQL statement
• Is the logical representation of data.
• It contains rows and columns just like real table.
• SQL functions, where and join statements can be added to a view and data
can be presented as if data are coming from one single table.
• Views allow users to
• Structure data in a way that users want.
• Restrict access to data such that user can see and sometimes modify exactly what
they need and no more
• Summarize data from various tables which can be used to generate reports.
• Syntax to create view
• CREATE VIEW viewname AS
SELECT columnname(s) FROM table; / WHERE clause
Subash Manandhar 27
Chapter 4 – SQL
• Views
• e.g. creating view
• CREATE VIEW view_customer AS
SELECT name, phone FROM customer WHERE address = ‘kathmandu’
• Syntax to query view
• SELECT * FROM viewname
• e.g. to query view
• SELECT * FROM view_customer
• Syntax to update view
• UPDATE viewname SET column = value WHERE clause
• It should not contain aggregate function or distinct or group by clause
while updating views.
Subash Manandhar 28
Chapter 4 – SQL
• Views
• e.g. to update view
• UPDATE view_customer SET phone = 102050 WHERE name = ‘ram’
• Syntax to drop view
• DROP VIEW viewname
• e.g. to drop view
• DROP VIEW view_customer
Subash Manandhar 29
Chapter 4 – SQL
Stored Procedure
• Stored procedure is a set of SQL • Example:
statements with an assigned
• CREATE PROCEDURE selectemp
name, which are stored in RDBMS
as a group, so it can be reused and AS
shared by multiple programs. SELECT * FROM employee
• We can also pass parameters to a GO
stored procedure, so that stored
procedure can act based on the
parameter values(s) passed. • EXEC selectemp
• Syntax:
• CREATE PROCEDURE procedurename
AS
SQL statements
GO;
• EXEC procedurename
Subash Manandhar 30
Chapter 4 – SQL
Subash Manandhar 31
Chapter 4 – SQL
QBE
• QBE (Query By Example) is developed at IBM in early 1970’s.
• It has two dimensional syntax, and queries looks like table.
• QBE queries are expressed by examples.
• QBE queries are expressed by using skeleton tables, which show
the relational schema of the database.
• Skeleton table looks like
Relation name Column1 Column2 …….. ColumnN
Tuple or row operation
Subash Manandhar 32
Chapter 4 – SQL
QBE
• Use of P to get list of relation.
• P. for print command.
• Data retrieval command is P. <variable name> where variable name
is optional.
• QBE automatically eliminates duplicate values.
• To see the duplicate value, P.ALL
• QBE supported by Microsoft Access is Graphical QBE
Subash Manandhar 33
Chapter 4 – SQL
QBE
• Example
• employee (name, city, salary)
• To find all employee names who live in city ‘kathmandu’
employee name city salary
P. or P.ALL kathmandu
• To find entire employee relation
employee name city salary
P.
• To find employee name whose salary is greater than 15000
employee name city salary
P. > 15000
• To find all employees whose salary between 10000 and 25000
employee name city salary
P. >= 10000 AND <= 25000
Subash Manandhar 34
Chapter 4 – SQL
QBE
• Database Modification in QBE
• To INSERT
employee name city salary
I. Ram Pokhara 25000
• To DELETE
employee name city salary
D. Ram
• To delete city of Ram
employee name city salary
Ram D.
• To UPDATE
employee name city salary
Ram U.35000
Subash Manandhar 35