Assignment OF DBMS: Submitted To: Submitted By: Mr. Ramandeep Anish Sethi Btech Hons MBA (CSE A) RA1702C069
Assignment OF DBMS: Submitted To: Submitted By: Mr. Ramandeep Anish Sethi Btech Hons MBA (CSE A) RA1702C069
OF
DBMS
RA1702C069
Q-1 The following tables form part of a database held in a relational DBMS:
(a) Find the name of all employees who have borrowed a book published by
McGraw-Hill.
(b) Find the name of all employees who have borrowed all book published by
McGraw-Hill.
(c) Find the names of employees who have borrowed more than five different
books published by McGraw-Hill.
(d) For each publisher, find the name of employees who have borrowed more
than five books of that publisher
Ans -
Ans- A)
b.publishers = MCGRAWHILL
from employe e
From books
Except
(select.isbn
From Loanl
Where l.empno=e.empno)
from( Select.publisher,name,isbn,cnt)
and l.isbn=b.isbn
publisher MCGRAWHILL
and l.isbn=b.isbn
e.
Πcompany-name (company ÷
(Πcity (σcompany-name=“MCGRAWHILL” (company))))
Projection
Example: The table E (for EMPLOYEE)
nr name salary
1 John 100
5 Sarah 300
7 Tom 100
nr Salary
1 100
select nr, salary PROJECTnr, salary(E)
from E 5 300
7 100
Selection :
The same table E (for EMPLOYEE) as above.
nr name salary
select *
from E 1 John 100 SELECTsalary < 200(E)
where salary < 200
7 Tom 100
Note that the select operation in relational algebra has nothing to do with the SQL keyword select.
Selection in relational algebra returns those tuples in a relation that fulfil a condition, while the SQL
keyword selectmeans "here comes an SQL statement".
Q-4 How join can be expressed in basic relational algebra operators? Justify with
example ?
Ans- Join ( called "inner join")
The cartesian product example above combined each employee with each department. If we only
keep those lines where the dept attribute for the employee is equal to the dnr (the department
number) of the department, we get a nice list of the employees, and the department that each
employee works for:
If we assume that these relational algebra expressions are executed, inside a relational DBMS
which uses relational algebra operations as its lower-level internal operations, different
relational algebra expressions can take very different time (and memory) to execute.
Natural join
A normal inner join, but using the join condition that columns with the same names should be
equal. Duplicate columns are removed. The Natural Join
The natural join is a binary operation that is written as R |×| S where R and S are relations. The result
of the cartesion product is the set of all combinations of tupels in R and S that are equal on their
common attribute names. More formally:
where fun(r) is a predicate that is true for a binary relation r iff r is a functional binary relation. This
operation can be regarded as a generalization of the previously defined cartesian product since it is
the special case where Rand S have no common attributes.
nr name dept
1 Bill A
2 Sarah C
3 John A
nr name
A Marketing
B Sales
C Legal
Several columns in the result will have the same name (nr and name).
How do we express the join condition, when there are two columns called nr?
Solutions:
3 John A A Marketing
You can use another variant of the renaming operator to change the name of a table, for example to
change the name of E to R. This is necessary when joining a table with itself (see below).
RENAMER(E)
A third variant lets you rename both the table and the columns:
Q-4 Write a PL/SQL code for generating the electricity bills of customers. Make use
of functions and cursors. Apply triggers also to automatically update the database to
modify customer’s total bill. ?
Ans
Private Sub getMyRows(inSchema As String, InTable As String)
Dim RS As Object
Dim TableSQL As String
Dim DataType As String
Dim DataLength As String
Dim DataPrecision As String
Dim DataScale As String
Dim ColCount As Integer
Dim WS As Worksheet
create a sheet with the current table as name
Worksheets.Add().Name = InTable
Set RS = CreateObject("ADODB.recordset")
TableSQL = "Select * from " & inSchema & "." & InTable
grab the data
RS.Open TableSQL, conn, adOpenStatic
For ColCount = 0 To RS.Fields.Count - 1
set column headings to match table
ActiveSheet.Cells(1, ColCount + 1).Value = RS.Fields(ColCount).Name
Next
End Sub
Q-5 Identify different scalar and aggregate functions in SQL. Give examples ?
Ans
SQL Scalar Numeric Functions
The Scalar Numeric Function operates on numeric values (i.e. INTEGER, SMALLINT, DECIMAL, FLOAT,
DOUBLE and NUMERIC data types). The PointBase database supports the following standard Numeric
Functions:
Multiplication
Division
Addition
Subtraction
The numeric functions are evaluated in the following order. Numeric Functions within parentheses
are evaluated from the innermost set of parentheses, following the same rules of precedence:
Examples
2 + 3 * 4 / 2 = 8
2 + (3 * 4) / 2 = 8
2 + 3 / 2 = 3.5
SQL Aggregate Functions operate on complete sets of data and return a single result. PointBase
supports five Aggregate Functions: AVG, COUNT, MAX, MIN, and SUM.
AVG
The AVG Function returns the average value for the column when applied to a column containing
numeric data. The following is the syntax for the AVG Function.
AVG (column_name)
Example
SELECT AVG(commission_rate) FROM sales_rep_tbl
COUNT
The COUNT Function returns the number of rows in a specified result set. The following syntax is one
form of the COUNT Function:
COUNT(*)
Example
SELECT COUNT(*) FROM sales_rep_tbl
The second form of the COUNT Function returns the number of rows in a result set where the
specified column has a distinct, non-NULL value. The following syntax is the second form of the
COUNT Function.
COUNT(DISTINCT column_name)
MAX
The MAX Function returns the data item with the highest value for a column when applied to a
column containing numeric data. If you apply the MAX Function to a CHARACTER value, it returns the
last value in the sorted values for that column. The following syntax is for the MAX Function.
MAX(column_name)
Example
SELECT MAX(commission_rate) FROM sales_rep_tbl
MIN
The MIN Function returns the data item with the lowest value for a column when applied to a column
containing numeric data. If you apply the MIN Function to a CHARACTER value, it returns the first
value in the sorted values for that column. The following syntax is for the MIN Function.
MIN(column_name)
Example
SELECT MIN(commission_rate) FROM sales_rep_tbl
SUM
The SUM Function returns the sum of all values in the specified column. The result of the SUM
Function has the same precision as the column on which it is operating. The following syntax is for
the SUM Function.
SUM(column_name)
Example
SELECT SUM(ytd_sales) FROM sales_rep_tbl
Aggregate functions
Example: The table E (for EMPLOYEE)
1 John 100 A
5 Sarah 300 C
7 Tom 100 A
12 Anne null C
sum
select sum(salary) Fsum(salary)(E)
from E 500