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

W5 DBMS Chapter07

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

W5 DBMS Chapter07

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

CHAPTER 7

More SQL: Complex Queries,


Triggers, Views,
and Schema Modification

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe


Chapter 7 Outline
 More Complex SQL Retrieval Queries
 Specifying Semantic Constraints as Assertions
and Actions as Triggers
 Views (Virtual Tables) in SQL
 Schema Modification in SQL

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 3


Aggregate Functions in SQL
 SQL aggregation functions are used to perform the
calculations on multiple rows of a single column of a table. It
returns a single value.
 Used to summarize information from multiple tuples into a
single-tuple summary
 Built-in aggregate functions: COUNT, SUM, MAX, MIN, and
AVG

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 3


COUNT FUNCTION
 COUNT function is used to Count the number of rows in a database
table. It can work on both numeric and non-numeric data types.
 COUNT function uses the COUNT(*) that returns the count of all the
rows in a specified table. COUNT(*) considers duplicate and Null
PRODUCT_MAST
PRODUCT COMPANY QTY RATE COST
SELECT COUNT(*) Item1 Com1 2 10 20
FROM PRODUCT_MAST; Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40

Output:10 Item6 Cpm1 3 25 75


Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 4
SUM FUNCTION
 Sum function is used to calculate the sum of all selected
columns. It works on numeric fields only.
PRODUCT_MAST
PRODUCT COMPANY QTY RATE COST
Item1 Com1 2 10 20
SELECT SUM(COST) Item2 Com2 3 25 75
FROM PRODUCT_MAST Item3 Com1 2 30 60
WHERE QTY>3; Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75

Output:320 Item7 Com1 5 30 150


Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 5


AVG FUNCTION
 The AVG function is used to calculate the average value of the
numeric type.
 AVG function returns the average of all non-Null

PRODUCT_MAST
PRODUCT COMPANY QTY RATE COST
SELECT AVG(COST) Item1 Com1 2 10 20
FROM PRODUCT_MAST; Item2 Com2 3 25 75

Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40

Output:67.00 Item6 Cpm1 3 25 75


Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 6


MAX FUNCTION
 MAX function is used to find the maximum value of a certain
column.
 This function determines the largest value of all selected
values of a column. l
PRODUCT_MAST
PRODUCT COMPANY QTY RATE COST
SELECT MAX(RATE) Item1 Com1 2 10 20
FROM PRODUCT_MAST; Item2 Com2 3 25 75

Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40

Output:30 Item6 Cpm1 3 25 75


Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 7


MIN FUNCTION
 MIN function is used to find the minimum value of a certain
column. This function determines the smallest value of all
selected values of a column.
PRODUCT_MAST
PRODUCT COMPANY QTY RATE COST
SELECT MIN(RATE) Item1 Com1 2 10 20
FROM PRODUCT_MAST; Item2 Com2 3 25 75

Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40

Output:10 Item6 Cpm1 3 25 75


Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 8


SQL Clauses
 The following are some SQL clauses:

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 9


GROUP BY Clause
 Grouping: Create subgroups of tuples before summarizing.
 The GROUP BY clause is used in SQL queries to organize
data that have the same attribute values.
 The GROUP BY clause is applicable when we want to use
aggregate functions to more than one set of rows.
 We can often use this clause in collaboration with aggregate
functions to produce summary reports from the database.
 NULL values are discarded when aggregate functions are
applied to a particular column
 The GROUP BY clause follows the WHERE clause in a
SELECT statement and precedes the ORDER BY clause.
 To select entire groups, HAVING clause is used.
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 10
Example SELECT COUNT (Doctor_ID), Doctor_Country
From Doctor_info GROUP BY Doctor_Country;
GROUP BY
Doctor_ID Doctor_Name Doctor_Disease Doctor_Gender Doctor_Country

1035 Jones Malaria_Specialist Male United Kingdom


1015 Marry Diabities_Specialis Female United State
t
1003 Harry Fever_Specialist Male United Kingdom
1044 Ella Cancer_Specialist Female United State
1025 Moria Corona_Specialist Female Europe

Output:

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 11


HAVING Clause
 HAVING clause: Provides a condition to
select or reject an entire group.
 HAVING clause is used to specify a search
condition for a group or an aggregate.
 Having is used in a GROUP BY clause. If
you are not using GROUP BY clause then
you can use HAVING function like a
WHERE clause.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 12


Example: HAVING Clause
SELECT COMPANY, COUNT(*) PROD COMP QTY RATE COST
UCT ANY

FROM PRODUCT_MAST Item1 Com1 2 10 20

GROUP BY COMPANY Item2 Com2 3 25 75


HAVING COUNT(*)>2; Item3 Com1 2 30 60
Item4 Com3 5 10 50
Output Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Com1 5 Item8 Com1 3 10 30

Com2 3 Item9 Com2 2 25 50


Item1 Com3 4 30 120
0

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 13


Example: Combining the WHERE and
the HAVING Clause
SELECT COMPANY, COUNT(*) PROD COMP QTY RATE COST
UCT ANY

FROM PRODUCT_MAST Item1 Com1 2 10 20

WHERE COST>30 Item2 Com2 3 25 75


GROUP BY COMPANY Item3 Com1 2 30 60
HAVING COUNT(*)>2; Item4 Com3 5 10 50
Output Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Com1 3 Item8 Com1 3 10 30

Com2 3 Item9 Com2 2 25 50


Item1 Com3 4 30 120
0

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 14


ORDER BY Clause
 The ORDER BY clause in SQL will help us to sort
the records based on the specific column of a
table.
 Using the ORDER BY clause, sort the records in
ascending or descending order as per our
requirement.
 The records will be sorted in ascending order
whenever the ASC keyword is used with
ORDER BY clause. DESC keyword will sort the
records in descending order

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 15


Example: ORDER BY
SELECT *FROM customers
customers
ORDER BY Salary DESC; ID NAME AGE ADDRESS SALARY
I NAME AG ADDRESS SALAR 2 Shiva Tiwari 22 Bhopal 21000
D E Y
3 Ajeet Bhargav 45 Meerut 65000
10 Sahil Sheikh 35 Aurangaba 68800
d 4 Ritesh Yadav 36 Azamgarh 26000
3 Ajeet Bhargav 45 Meerut 65000 5 Balwant Singh 45 Varanasi 36000
9 Aakash Yadav 32 Mumbai 43500 6 Mahesh 26 Mathura 22000
8 Neeru Sharma 29 Pune 40000 Sharma
7 Rohit 19 Ahemdaba 38000 7 Rohit 19 Ahemdabad 38000
Shrivastav d Shrivastav
5 Balwant Singh 45 Varanasi 36000 8 Neeru Sharma 29 Pune 40000
4 Ritesh Yadav 36 Azamgarh 26000 9 Aakash Yadav 32 Mumbai 43500
6 Mahesh 26 Mathura 22000
Sharma
10 Sahil Sheikh 35 Aurangabad 68800

1 Himani Gupta 21 Modinagar 22000


2 Shiva Tiwari 22 Bhopal 21000

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 16


Example
Grouping
Ordering

SELECT D_state, avg(D_salary) AS salary


FROM developers
GROUP BY D_state
ORDER BY D_state DESC;

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 17


ALL & SOME logical operators
 ALL and SOME may be applied as functions on Boolean Values.
 ALL operator is used to select all tuples of SELECT STATEMENT. It
is also used to compare a value to every value in another value set
or result from a subquery.
 The ALL operator returns TRUE if all of the subqueries values meet
the condition. The ALL must be preceded by comparison operators
and evaluates true if all of the subqueries values meet the condition.
 ALL is used with SELECT, WHERE, HAVING statement.
 SOME compare a value to each value in a list or results from a
query and evaluate to true if the result of an inner query contains at
least one row.
 SOME must match at least one row in the subquery and must be
preceded by comparison operators. Suppose using greater than ( >)
with SOME means greater than at least one value.
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 18
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 19
OrderDetails Table
Example
SELECT OrderID FROM
OrderDetails GROUP
BY OrderID HAVING
max(Quantity) > ALL
(SELECT
avg(Quantity) FROM
OrderDetails GROUP
BY OrderID);

OrederID Aver
Output Max
10248 12.3 15
10249 5,5 8
10250 14.3 20
10251 5 8
10252 9 9
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 20
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 21
Use of WITH
 SQL WITH clause allows you to give a sub-
query block a name, which can be referenced in
several places within the main SQL query.
 The WITH clause allows a user to define a table
that will only be used in a particular query.
 Used for convenience to create a temporary
“View” and use that immediately in a query
 WITH clause is not supported by all database
system

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 22


Example of WITH: Find all the employee whose salary
is more than the average salary of all employees.

EmployeeID Name Salary


100011 Smith 50000
Output
100022 Bill 94000 EmployeeID Name Salary
100027 Sam 70550 100022 Bill 94000
100845 Walden 80000 100845 Walden 80000
115585 Erik 60000
temporaryTable
1100070 Kate 69000
averageValue =70592
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 23
Example of
WITH:
Find all the airlines
where the total salary of
all pilots in that airline is
more than the average
of total salary of all
pilots in theairlineAverage
database.
avgSalary
57305

EmployeeID Airline Name Salary Output


70007 Airbus 380 Kim 60000
Airline
70002 Boeing Laura 20000 Airbus 380
10027 Airbus 380 Will 80050
10778 Airbus 380 Warren 80780 TotalSalary
115585 Boeing Smith 25000 Airline Total
Airbus 380 298830
114070 Airbus 380 Katy 78000
Boeing 45000
Slide 7- 24
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe
Use of CASE
 Control statements form the heart of most languages. They control
the execution of other sets of statements. These Control
statements are found in SQL too.
 Case-Switch statement in SQL.Used when a value can be different
based on certain conditions.
 Applicable when querying, inserting or updating tuples.
 The CASE statement is SQL’s way of handling if/then logic.
 There can be two valid ways of going about the case-switch
statements:
 First takes a variable called case_value and matches it with

some statement_list.
 Second considers a search_condition instead of variable

equality and executes the statement_list accordingly.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 25


EXAMPLE of use of CASE
FacultyID Name Department Gender
001 Aakash CS M

002 Sahil EC M

003 John HSS M

004 Shelley CS F

Anann
005 CS F
ya

006 Sia HSS F

FacultyID Name Department Gender


001 Aakash CS Male

002 Sahil EC Male

003 John HSS Male

004 Shelley CS Female

005 Anannya CS Female

006 Sia HSS Female

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 26


EXAMPLE of use
of CASE

Output

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 27


EXAMPLE of Use of CASE

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 28


Specifying Constraints as Assertions
and Actions as Triggers
 Semantic Constraints: These are business rule
that may affect the database and the data in it. A
business rule is a regulation that defines or restricts
actions within an organization's operations.
 CREATE ASSERTION
 Specify additional types of constraints outside

scope of built-in relational model constraints


 CREATE TRIGGER
 Specify automatic actions that database system

will perform when certain events and conditions


occur
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 41
Specifying General Constraints as
Assertions in SQL
 The CHECK constraint is used to limit the value range that
can be placed in a column.
 When a constraint involves two or more tables, the
table constraint mechanism is sometimes hard and
results may not come as expected.
 An assertion statement should ensure a certain
condition will always exist in the database, and DBMS
always checks the assertion whenever modifications
are done in the corresponding table.
 Use Drop Assertion for deletion of an assertion.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 30


Example: Assertions CREATE TABLE Persons (
ID int NOT NULL,
The following SQL creates LastName
a CHECK constraint on the "Age" column varchar(255) NOT NULL,
when the "Persons" table is created. FirstName
The CHECK constraint ensures that the varchar(255),
age of a person must be 18, or older Age int,
CHECK (Age>=18)
);
create assertion AT_MOST_ONE_PRESIDENT as
CHECK
((select count(*)
from EMP e
where e.JOB = 'PRESIDENT') <= 1
)
This SQL statement creates an assertion to demand
that there's no more than a single president among
the employees.
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 31
Example: Assertions
create assertion NO_TRAINERS_IN_BOSTON as
CHECK
(not exists
(select 'trainer in Boston'
from EMP e, DEPT d
where e.DEPTNO = d.DEPTNO
and e.JOB = 'TRAINER'
and d.LOC = 'BOSTON')
)
This SQL statement creates an assertion to demand
that Boston based departments do not employ
trainers,
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 32
Introduction to Triggers in SQL

 A trigger is a database object that is associated


with the table, it will be activated when a
defined action is executed for the table.
 The trigger can be executed when we run the
following statements: INSERT, UPDATE,
DELETE
 And it can be invoked before or after the event.
 Use Drop Triggers for deletion of a trigger.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 33


Triggers in SQL
 CREATE TRIGGER statement used to monitor the database
 Typical trigger has three components which make it a rule for an
active database: Event(s), Condition, Action

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 43


USE OF TRIGGERS

 Here we create a trigger to fill in the full name by


concatenating the first and last names. So while inserting the
values, we will only feed the first name and last name, and
we will expect the trigger to automatically update each row
with an additional column attribute bearing the full name

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 44


Views (Virtual Tables) in SQL
 In SQL, a view is a virtual table based on the result-
set of an SQL statement.
 A view contains rows and columns, just like a real
table. The fields in a view are fields from one or
more real tables in the database.
 You can add SQL statements and functions to a
view and present the data as if the data were
coming from one single table.
 A view is created with the CREATE VIEW
statement.
 Use DROP VIEW command to deletion of a view
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 45
Specification of Views in SQL
CREATE VIEW view_name AS
 CREATE VIEW Syntax
SELECT column1, column2,
...
 Example:
FROM table_name
WHERE condition;
CREATE VIEW DetailsView AS SELECT NAME,
ADDRESS FROM StudentDetails WHERE S_ID <
5;
DetailsView StudentDetails

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 46


Creating View from multiple tables
StudentDetails StudentMarks

CREATE VIEW MarksView AS SELECT StudentDetails.NAME,


StudentDetails.ADDRESS, StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
MarksView

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 38


Stored Procedure
 A stored procedure is a prepared SQL code that
you can save, so the code can be reused over
and over again.
 So if you have an SQL query that you write over
and over again, save it as a stored procedure,
and then just call it to execute it.
 You can also pass parameters to a stored
procedure, so that the stored procedure can act
based on the parameter value(s) that is passed.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 39


Stored Procedure
 Stored Procedure Syntax:
CREATE PROCEDURE procedure_name
AS
sql_statement
GO;

 Execute a Stored Procedure:

EXEC procedure_name;

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 40


Example: Stored Procedure
CREATE PROCEDURE SelectAllCustome
rs
AS EXEC SelectAllCustomer
SELECT * FROM Customers s;
GO;
CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedados y Constitución
helados 2222

3 Antonio Moreno Antonio Mataderos México D.F. 05023 Mexico


Taquería Moreno 2312

4 Around the Thomas Hardy 120 Hanover London WA1 1DP UK


Horn Sq.

5 Berglunds Christina Berguvsvägen Luleå S-958 22 Sweden


snabbköp Berglund 8

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 41


Example: Stored Procedure With
One Parameter

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30)


AS
SELECT * FROM Customers WHERE City = @City
GO;

EXEC SelectAllCustomers @City = 'London';

CustomerID CustomerName ContactName Address City PostalCode Country

4 Around the Thomas Hardy 120 Hanover London WA1 1DP UK


Horn Sq.

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 42


Example: Stored Procedure With
Multiple Parameters

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode nvarchar(10)


AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;

EXEC SelectAllCustomers @City = ‘Berlin', @PostalCode = ’12209';

CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 43


Schema Change Statements in SQL
 Schema evolution commands can be used to
alter a schema by adding or dropping tables,
attributes, constraints, and other schema
elements.
 This can be done while the database is
operational and does not require recompilation
of the database schema.
 Certain checks must be done by the DBMS to
ensure that the changes do not affect the rest of
the database and make it inconsistent.
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 54
The DROP Command
 DROP command can be used to drop named schema
elements, such as tables, domains, or constraints. One can
also drop a schema.
 There are two drop behaviour options CASCADE and
RESTRICT.
 To remove the COMPANY database schema and all its
tables, domains, and other elements, the CASCADE option is
used as follows:
 DROP SCHEMA COMPANY CASCADE;

 To use the RESTRICT option to remove the COMPANY


database schema, the user must first individually drop each
element in the schema, then drop the schema itself as
follows:
 DROP SCHEMA COMPANY RESTRICT;

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 55


The ALTER command
 The definition of a base table or of other named
schema elements can be changed by using
the ALTER command
 For base tables, the possible alter table
actions include adding or dropping a column
(attribute), changing a column definition, and
adding or dropping table constraints.
 Example:
 ALTER TABLE COMPANY.EMPLOYEE ADD
COLUMN Job VARCHAR(12);

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 56


Adding and Dropping Constraints
Default Values
 Change constraints specified on a table
 Add or drop a named constraint

 Default values can be dropped and altered :


ALTER TABLE COMPANY.DEPARTMENT ALTER
COLUMN Mgr_ssn DROP DEFAULT;
ALTER TABLE COMPANY.DEPARTMENT ALTER
COLUMN Mgr_ssn SET DEFAULT ‘333445555’;

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 57


Dropping Columns
 To drop a column, we must choose either
CASCADE or RESTRICT for drop behaviour.
 If CASCADE is chosen, all constraints and views

that reference the column are dropped


automatically from the schema, along with the
column.
 If RESTRICT is chosen, the command is

successful only if no views or constraints (or


other schema elements) reference the column.
ALTER TABLE COMPANY.EMPLOYEE DROP COLUMN Address CASCADE;

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 48


Table 7.2 Summary of SQL
Syntax

continued on next slide

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 59


Table 7.2 (continued)
Summary of SQL Syntax

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 60


Assignment 5-A
Consider tables in a Hotel database, create a database trigger
for the following situations:
Hotel (hotelNo, hotelName, city)
Room (roomNo, hotelNo, type, price)
Booking (hotelNo, guestNo, dateFrom, dateTo,
roomNo)
1)Guest
The(guestNo,
price of guestName, guestAddress)
all double rooms must be
greater than £100.
2) A booking cannot be for a hotel room that is
already booked for any of the specified dates

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 61


Assignment 5-B
Consider patient table in a hospital management
database :
ient_id name age gender address disease doctor_id
1 reema 23 female althan,Surat fever 21

 Write SQL stored procedure to retrieve all patient


records.
 Write SQL store procedure to display female
patient data

Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 52


Assignment 5-C
Consider tables in a COMPANY database, create a view for the following
situations:
EMPLOYEE: Fname Minit Lname Ssn Bdate
Address Sex Salary Super_ssn Dno
DEPARTMENT: Dname Dnumber Mgr_ssn
Mgr_start_date
DEPT_LOCATIONS: Dnumber Dlocation
PROJECT: Pname Pnumber Plocation Dnum
WORKS_ON: Essn Pno Hours
DEPENDENT : Essn Depentdent_name Sex
Bdate
1) Relationship
Identify a view that has the employee name, supervisor name,
and employee salary for each employee who works in the
‘Research’ department in SQL on the COMPANY database
shown below.
2) Specify a view that has the project name, controlling department
name, number of employees, and total hours worked per week
on the project for each project with more than one employee
working on it in SQL on the COMPANY database shown below.
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe Slide 7- 61

You might also like