UNIT- II (Relational Data Model & Introduction to SQL)

Similar documents
Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU

Chapter 3: Relational Model

CS34800 Information Systems. The Relational Model Prof. Walid Aref 29 August, 2016

Upon completion of this Unit, students will be introduced to the following

Chapter 2: Relational Model

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Chapter 4: SQL. Basic Structure

Database System Concepts

DATABASE TECHNOLOGY - 1MB025

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

Chapter 6: Formal Relational Query Languages

DATABASE TECHNOLOGY - 1MB025

DATABASTEKNIK - 1DL116

Database System Concepts, 5 th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

2.2.2.Relational Database concept

Database Systems SQL SL03

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

Relational Model, Relational Algebra, and SQL

Database Systems SQL SL03

Chapter 5: Other Relational Languages

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

UNIT 2 RELATIONAL MODEL

QQ Group

Comp 5311 Database Management Systems. 2. Relational Model and Algebra

Relational Algebra. Procedural language Six basic operators

The SQL database language Parts of the SQL language

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 6 Outline. Unary Relational Operations: SELECT and

1. The rst database systems were based on the network and hierarchical models. These are covered briey

CMP-3440 Database Systems

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Relational Model. Prepared by: Ms. Pankti Dharwa ( Asst. Prof, SVBIT)

Chapter 5: Other Relational Languages.! Query-by-Example (QBE)! Datalog

Other Relational Query Languages

Chapter 3: SQL. Chapter 3: SQL

A database can be modeled as: + a collection of entities, + a set of relationships among entities.

DATABASE DESIGN I - 1DL300

Silberschatz, Korth and Sudarshan See for conditions on re-use

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

Chapter 8: Relational Algebra

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

DATABASE TECHNOLOGY. Spring An introduction to database systems

Integrity and Security

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

II. Structured Query Language (SQL)

DBMS: AN INTERACTIVE TUTORIAL

QUIZ 1 REVIEW SESSION DATABASE MANAGEMENT SYSTEMS

DATABASDESIGN FÖR INGENJÖRER - 1DL124

ARTICLE RELATIONAL ALGEBRA

Database Management System 11

CSIE30600/CSIEB0290 Database Systems Relational Algebra and Calculus 2

RELATIONAL DATA MODEL: Relational Algebra

Outline. CSIE30600 Database Systems Relational Algebra and Calculus 2

1. Considering functional dependency, one in which removal from some attributes must affect dependency is called

Other Query Languages II. Winter Lecture 12

Basant Group of Institution

Mahathma Gandhi University

CS 377 Database Systems

Informationslogistik Unit 4: The Relational Algebra

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Chapter 2: Intro to Relational Model

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

II. Structured Query Language (SQL)

3. Relational Data Model 3.5 The Tuple Relational Calculus

Midterm Review. Winter Lecture 13

Chapter 14 Query Optimization

Chapter 14 Query Optimization

Chapter 14 Query Optimization

Contents Contents Introduction Basic Steps in Query Processing Introduction Transformation of Relational Expressions...

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

Simple SQL Queries (2)

RELATIONAL ALGEBRA. CS121: Relational Databases Fall 2017 Lecture 2

Relational Databases

Relational Algebra and SQL

RELATIONAL DATA MODEL

Chapter 6 The Relational Algebra and Calculus

DATABASE TECHNOLOGY - 1DL124

Relational Algebra. Mr. Prasad Sawant. MACS College. Mr.Prasad Sawant MACS College Pune

CS317 File and Database Systems

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Chapter 12: Query Processing

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Relational Query Languages: Relational Algebra. Juliana Freire

Fundamentals of Database Systems

SQL (Structured Query Language)

The Relational Algebra

Database System Concepts

Concepts of Database Management Eighth Edition. Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra

CS 2451 Database Systems: Relational Algebra & Relational Calculus

DATABASE MANAGEMENT SYSTEMS UNIT II- RELATIONAL MODEL


ADVANCED DATABASES ; Spring 2015 Prof. Sang-goo Lee (11:00pm: Mon & Wed: Room ) Advanced DB Copyright by S.-g.

Chapter 2: Intro to Relational Model

Chapter 14: Query Optimization

Chapter 8: The Relational Algebra and The Relational Calculus

DATABASE DESIGN I - 1DL300

Introduction Relational Algebra Operations

Chapter 6 5/2/2008. Chapter Outline. Database State for COMPANY. The Relational Algebra and Calculus

Transcription:

Database Management System 1 (NCS-502) UNIT- II (Relational Data Model & Introduction to SQL) 2.1 INTRODUCTION: 2.1.1 Database Concept: 2.1.2 Database Schema 2.2 INTEGRITY CONSTRAINTS: 2.2.1 Domain Integrity: 2.2.2 Entity Integrity Constraint: 2.2.3 Referential Integrity Constraint: 2.2.4 Foreign Key Integrity Constraint: 2.3 THE RELATIONAL ALGEBRA: 2.3.1 Select Operation: 2.3.2 The Project Operation 2.3.2 The Union Operation 2.3.3 The Set Difference Operation 2.3.4 The Cartesian-Product Operation 2.3.5 The Rename Operation 2.3.6 Additional Operations 2.3.6.1 The Set-Intersection Operation 3.3.6.2 The Natural-Join Operation 2.3.6.3 The Division Operation 2.3.6.4 The Assignment Operation 2.3.7 Extended Relational-Algebra Operations 2.3.7.1 Generalized Projection 2.3.7.2 Aggregate Functions 2.3.7.3 Outer Join 2.3.8 Modification of the Database 2.3.8.1 Deletion 2.3.8.2 Insertion 2.3.8.3 Updating 2.4 TUPLE RELATIONAL CALCULUS: 2.5 THE DOMAIN RELATIONAL CALCULUS:

Database Management System 2 (NCS-502) 2.1 INTRODUCTION: 2.1.1 Database Concept: A relational database consists of a collection of tables, each of which is assigned a unique name. A row in a table represents a relationship among a set of values. Since a table is a collection of such relationships, there is a close correspondence between the concept of table and the mathematical concept of relation, from which the relational data model takes its name. A tuple variable is a variable that stands for a tuple; in other words, a tuple variable is a variable whose domain is the set of all tuples. t[account-number] = A-101, and t[branch-name] = Downtown. Alternatively, we may write t[1] to denote the value of tuple t on the first attribute (account-number), t[2] to denote branch-name, and so on. A domain is atomic if elements of the domain are considered to be indivisible units. For example, the set of integers is an atomic domain, but the set of all sets of integers is a nonatomic domain. 2.1.2 Database Schema Database schema can be defined as: Account-schema = (account-number, branch-name, balance) relation instance Branch-schema = (branch-name, branch-city, assets) Let R be a relation schema. If we say that a subset K of R is a superkey for R, we are restricting consideration to relations r(r) in which no two distinct tuples have the same values on all attributes in K. That is, if t1 and t2 are in r and t1 t2, then t1[k] t2[k]. Strong entity set. The primary key of the entity set becomes the primary key of the relation. Weak entity set. The table, and thus the relation, corresponding to a weak entity set includes The attributes of the weak entity set The primary key of the strong entity set on which the weak entity set depends Relationship set. The union of the primary keys of the related entity sets becomes a superkey of the relation.

Database Management System 3 (NCS-502) 2.2 INTEGRITY CONSTRAINTS: Before one can start to implement the database tables, one must define the integrity constraints. Intergrity means something like 'be right' and consistent. The data in a database must be right and in good condition. 2.2.1 Domain Integrity: Domain integrity means the definition of a valid set of values for an attribute. You define data type, length or size is null value allowed is the value unique or not for an attribute. You may also define the default value, the range (values in between) and/or specific values for the attribute. Some DBMS allow you to define the output format and/or input mask for the attribute. These definitions ensure that a specific attribute will have a right and proper value in the database. 2.2.2 Entity Integrity Constraint: The entity integrity constraint states that primary keys can't be null. There must be a proper value in the primary key field. The entity integrity constraints assure that a specific row in a table can be identified. 2.2.3 Referential Integrity Constraint: The referential integrity constraint is specified between two tables and it is used to maintain the consistency among rows between the two tables. The rules are: 1. You can't delete a record from a primary table if matching records exist in a related table. 2. You can't change a primary key value in the primary table if that record has related records. 3. You can't enter a value in the foreign key field of the related table that doesn't exist in the primary key of the primary table. 4. However, you can enter a Null value in the foreign key, specifying that the records are unrelated. 2.2.4 Foreign Key Integrity Constraint: There are two foreign key integrity constraints: cascade update related fields and cascade delete related rows. These constraints affect the referential integrity constraint.

Database Management System 4 (NCS-502) Cascade Update Related Fields Any time you change the primary key of a row in the primary table, the foreign key values are updated in the matching rows in the related table. This constraint overrules rule 2 in the referential integrity constraints. Cascade Delete Related Rows Any time you delete a row in the primary table, the matching rows are automatically deleted in the related table. This constraint overrules rule 1 in the referential integrity constraints. 2.3 THE RELATIONAL ALGEBRA: The fundamental operations in the relational algebra are select, project, union, set difference, Cartesian product, and rename. In addition to the fundamental operations, there are several other operations namely, set intersection, natural join, division, and assignment. The select, project, and rename operations are called unary operations, because they operate on one relation. The other three operations operate on pairs of relations and are, therefore, called binary operations. 2.3.1 Select Operation: We use the lowercase Greek letter sigma (σ) to denote selection. We can find all tuples in which the amount lent is more than $1200 by writing σamount>1200 (loan)

Database Management System 5 (NCS-502) we allow comparisons using =, _=, <,, >, in the selection predicate. Furthermore, we can combine several predicates into a larger predicate by using the connectives and ( ), or ( ), and not ( ). to find those tuples pertaining to loans of more than $1200 made by the Perryridge branch σbranch-name = Perryridge amount>1200 (loan) 2.3.2 The Project Operation Suppose we want to list all loan numbers and the amount of the loans, but do not care about the branch name. The project operation allows us to produce this relation. The project operation is a unary operation that returns its argument relation, with certain attributes left out. Find those customers who live in Harrison Πcustomer-name (σcustomer-city = Harrison (customer)) 2.3.2 The Union Operation Consider a query to find the names of all bank customers who have either an account or a loan or both. We know how to find the names of all customers with a loan in the bank: Πcustomer-name (borrower ) We also know how to find the names of all customers with an account in the bank: Πcustomer-name (depositor) To answer the query, we need the union of these two sets; Πcustomer-name (borrower ) Πcustomer-name (depositor) 2.3.3 The Set Difference Operation The set-difference operation, denoted by, allows us to find tuples that are in one relation but are not in another. The expression r s produces a relation containing those tuples in r but not in s. Πcustomer-name (depositor) Πcustomer-name (borrower ) 2.3.4 The Cartesian-Product Operation The Cartesian-product operation, denoted by a cross ( ), allows us to combine information from any two relations. We write the Cartesian product of relations r1 and r2 as r1 r2.

Database Management System 6 (NCS-502) Suppose that we want to find the names of all customers who have a loan at the Perryridge branch 2.3.5 The Rename Operation Find the largest account balance in the bank. Step 1: To compute the temporary relation, we need to compare the values of all account balances. We do this comparison by computing the Cartesian product account account and forming a selection to compare the value of any two balances appearing in one tuple. Πaccount.balance (σaccount.balance < d.balance (account ρd (account))). The result contains all balances except the largest one. Step 2: The query to find the largest account balance in the bank can be written as: Πbalance (account) Πaccount.balance (σaccount.balance < d.balance (account ρd (account))) Ex. Find the names of all customers who live on the same street and in the same city as Smith Πcustomer.customer-name (σcustomer.customer-street=smith-addr.street customer.customercity=smith-addr.city (customer ρsmith-addr(street,city) (Πcustomer-street, customer-city (σcustomer-name = Smith (customer)))))

Database Management System 7 (NCS-502) 2.3.6 Additional Operations 2.3.6.1 The Set-Intersection Operation Suppose that we wish to find all customers who have both a loan and an account. Πcustomer-name (borrower ) Πcustomer-name (depositor) Note that we can rewrite any relational algebra expression that uses set intersection by replacing the intersection operation with a pair of set-difference operations as: r s = r (r s) 3.3.6.2 The Natural-Join Operation Find the names of all customers who have a loan at the bank, along with the loan number and the loan amount. Πcustomer-name, loan.loan-number, amount (σborrower.loan-number =loan.loan-number (borrower loan)) The natural join is a binary operation that allows us to combine certain selections and a Cartesian product into one operation. It is denoted by the join symbol Find the names of all customers who have a loan at the bank, and find the amount of the loan Πcustomer-name, loan-number, amount (borrower loan) Find the names of all branches with customers who have an account in the bank and who live in Harrison. Πbranch-name(σcustomer-city = Harrison (customer account depositor)). Find all customers who have both a loan and an account at the bank. Πcustomer-name (borrower depositor ) 2.3.6.3 The Division Operation The division operation, denoted by, is suited to queries that include the phrase for all. Suppose that we wish to find all customers who have an account at all the branches located in Brooklyn. We can obtain all branches in Brooklyn by the expression r1 = Πbranch-name (σbranch-city = Brooklyn (branch))

Database Management System 8 (NCS-502) We can find all (customer-name, branch-name) pairs for which the customer has an account at a branch by writing r2 = Πcustomer-name, branch-name (depositor account) Πcustomer-name, branch-name (depositor = Brooklyn (branch)) account) Πbranch-name (σbranch-city 2.3.6.4 The Assignment Operation It is convenient at times to write a relational-algebra expression by assigning parts of it to temporary relation variables. The assignment operation, denoted by, works like assignment in a programming language. temp1 ΠR S (r) temp2 ΠR S ((temp1 s) ΠR S,S(r)) result = temp1 temp2 2.3.7 Extended Relational-Algebra Operations 2.3.7.1 Generalized Projection The generalized-projection operation extends the projection operation by allowing arithmetic functions to be used in the projection list. The generalized projection operation has the form ΠF1,F2,...,Fn(E) where E is any relational-algebra expression, and each of F1, F2,..., Fn is an arithmetic expression involving constants and attributes in the schema of E. If we want to find how much more each person can spend, we can write the following expression: Πcustomer-name, (limit credit-balance) as credit-available (credit-info)

Database Management System 9 (NCS-502) 2.3.7.2 Aggregate Functions Aggregate functions take a collection of values and return a single value as a result. The general form of the aggregation operation G is as follows: G1,G2,...,GnGF1(A1), F2(A2),..., Fm(Am)(E) where E is any relational-algebra expression; G1,G2,...,Gn constitute a list of attributes on which to group; each Fi is an aggregate function; and each Ai is an at tribute name. we shall use the pt-works relation in above example, for part-time employees. Suppose that we want to find out the total sum of salaries of all the part-time employees in the bank. Gsum(salary)(pt-works) Find the number of branches appearing in the pt-works relation. Gcount-distinct(branch-name)(pt-works) Suppose we want to find the total salary sum of all part-time employees at each branch of the bank separately, rather than the sum for the entire bank. branch-namegsum(salary)(pt-works) if we want to find the maximum salary for part-time employees at each branch branch-namegsum(salary),max(salary)(pt-works)

Database Management System 10 (NCS-502) attributes of an aggregation operation can be renamed as: branch-namegsum(salary) as sum-salary,max(salary) as max-salary (pt-works) 2.3.7.3 Outer Join The outer-join operation is an extension of the join operation to deal with missing information. Suppose that we have the relations with the following schemas, which contain data on full-time employees: employee (employee-name, street, city) ft-works (employee-name, branch-name, salary) The left outer join ( ) takes all tuples in the left relation that did not match with any tuple in the right relation, pads the tuples with null values for all other attributes from the right relation, and adds them to the result of the natural join.

Database Management System 11 (NCS-502) The right outer join ( ) is symmetric with the left outer join: It pads tuples from the right relation that did not match any from the left relation with nulls and adds them to the result of the natural join. The full outer join does both of those operations, padding tuples from the left relation that did not match any from the right relation, as well as tuples from the right relation that did not match any from the left relation, and adding them to the result of the join. 2.3.8 Modification of the Database 2.3.8.1 Deletion we remove the selected tuples from the database. We can delete only whole tuples; we cannot delete values on only particular attributes. In relational algebra a deletion is expressed by r r E where r is a relation and E is a relational-algebra query. Delete all of Smith s account records. depositor depositor σcustomer-name = Smith (depositor) Delete all loans with amount in the range 0 to 50. loan loan σamount 0 and amount 50 (loan) Delete all accounts at branches located in Needham. r1 σbranch-city = Needham (account branch) r2 Πbranch-name, account -number, balance (r1) account account r2

Database Management System 12 (NCS-502) 2.3.8.2 Insertion r r E where r is a relation and E is a relational-algebra expression. Suppose that we wish to insert the fact that Smith has $1200 in account A-973 at the Perryridge branch. account account {(A-973, Perryridge, 1200)} depositor depositor {( Smith, A-973)} 2.3.8.3 Updating If we want to select some tuples from r and to update only them, we can use the following expression; here, P denotes the selection condition that chooses which tuples to update: r ΠF1,F2,...,Fn(σP (r)) (r σp (r)) To illustrate the use of the update operation, suppose that interest payments are being made, and that all balances are to be increased by 5 percent. account Πaccount-number, branch-name, balance * 1.05 (account) 2.4 TUPLE RELATIONAL CALCULUS: The tuple relational calculus, by contrast, is a nonprocedural query language. It describes the desired information without giving a specific procedure for obtaining that information. A query in the tuple relational calculus is expressed as {t P(t)} that is, it is the set of all tuples t such that predicate P is true for t. Following our earlier notation, we use t[a] to denote the value of tuple t on attribute A, andwe use t r to denote that tuple t is in relation r. t r (Q(t)) means there exists a tuple t in relation r such that predicate Q(t) is true. Say that we want to find the branch-name, loan-number, and amount for loans of over $1200: {t t loan t[amount] > 1200} Find the loan number for each loan of an amount greater than $1200 {t s loan (t[loan-number] = s[loan-number] s[amount] > 1200)}

Database Management System 13 (NCS-502) To find all customers who have a loan, an account, or both at the bank (we used the union operation in the relational algebra. In the tuple relational calculus, we shall need two there exists clauses, connected by or ( ): ) {t s borrower (t[customer-name] = s[customer-name]) u depositor (t[customer-name] = u[customer-name])} If we now want only those customers who have both an account and a loan at the bank, all we need to do is to change the or ( ) to and ( ) in the preceding expression. {t s borrower (t[customer-name] = s[customer-name]) u depositor (t[customer-name] = u[customer-name])} Find all customers who have an account at the bank but do not have a loan from the bank. {t u depositor (t[customer-name] = u[customer-name]) s borrower (t[customer-name] = s[customer-name])} The formula P Q means P implies Q ; that is, if P is true, then Q must be true. Note that P Q is logically equivalent to P Q. Find all customers who have an account at all branches located in Brooklyn. {t r customer (r[customer-name] = t[customer-name]) ( u branch (u[branch-city] = Brooklyn s depositor (t[customer-name] = s[customer-name] w account (w[account-number] = s[account-number ] w[branch-name] = u[branch-name]))))} three rules: 1. P1 P2 is equivalent to ( (P1) (P2)). 2. t r (P1(t)) is equivalent to t r ( P1(t)). 3. P1 P2 is equivalent to (P1) P2.

Database Management System 14 (NCS-502) 2.5 THE DOMAIN RELATIONAL CALCULUS: An expression in the domain relational calculus is of the form {< x1, x2,..., xn > P(x1, x2,..., xn)} where x1, x2,..., xn represent domain variables. P represents a formula composed of atoms, as was the case in the tuple relational calculus. An atom in the domain relational calculus has one of the following forms: < x1, x2,..., xn > r, where r is a relation on n attributes and x1, x2,..., xn are domain variables or domain constants. x Θ y, where x and y are domain variables and Θ is a comparison operator (<,, =, _=, >, ). We require that attributes x and y have domains that can be compared by Θ. x Θ c, where x is a domain variable, Θ is a comparison operator, and c is a constant in the domain of the attribute for which x is a domain variable. We build up formulae from atoms by using the following rules: An atom is a formula. If P1 is a formula, then so are P1 and (P1). If P1 and P2 are formulae, then so are P1 P2, P1 P2, and P1 P2. If P1(x) is a formula in x, where x is a domain variable, then x (P1(x)) and x (P1(x)) are also formulae. Find the loan number, branch name, and amount for loans of over $1200: {< l, b, a > < l, b, a > loan a > 1200} Find all loan numbers for loans with an amount greater than $1200: {< l > b, a (< l, b, a > loan a > 1200)} Find the names of all customers who have a loan from the Perryridge branch and find the loan amount: {< c,a > l (< c, l > borrower b (< l, b, a > loan b = Perryridge ))}

Database Management System 15 (NCS-502) Find the names of all customers who have a loan, an account, or both at the Perryridge branch: {< c > l (< c, l > borrower b, a (< l, b, a > loan b = Perryridge )) a (< c,a > depositor b, n (< a, b, n > account b = Perryridge ))} Find the names of all customers who have an account at all the branches located in Brooklyn: {< c > n (< c,n > customer) x, y, z (< x, y, z > branch y = Brooklyn a, b (< a, x, b > account < c,a > depositor))}

Database Management System 16 (NCS-502) PRACTICE QUESTIONS Problem # 1 Consider the schema:- given below, EMPLOYEE (E-NAME, STREET, CITY) WORKS-FOR (E-NAME, COMPANY-NAME, SALARY) COMPANY (COMPANY-NAME, CITY) MANAGES (E-NAME, MANAGER-NAME) Express the following queries in relational algebra and SQL:- (a) Find names of employees working for Infosys. RA: E-NAME (σ COMPANY-NAME = Infosys (works-for)) SQL: SELECT E-NAME WHERE COMPAY-NAME = Infosys ; TRC: { t u works-for ( t[e-name] = u [E-NAME] u[company-name] = Infosys )} DRC: { < en > sal ( < en, Infosys, sal> works-for) } (b) Find the names and the cities of residence of employees working for TCS RA: E-NAME, CITY (σ COMPANY-NAME = TCS (works_for * employee)) SQL: SELECT E.E-NAME, CITY W, employee E WHERE W. E-NAME = E. E-NAME AND COMPAY-NAME = TCS ; TRC: { t u works-for ( t[e-name] = u [E-NAME] u[company-name] = TCS v employee ( v[e-name] = u [E-NAME] t[city] = v [CITY] ))} DRC: { < en, city > sal ( < en, TCS, sal> works-for st ( < en, st, city> employee )) } (c) Find name, street and city of residence of employees working for Infosys and earning more than 20,000

Database Management System 17 (NCS-502) RA: E-NAME, STREET, CITY (σ COMPANY-NAME = Infosys SALARY > 20000 (works_for * employee)) SQL: SELECT E.E-NAME, STREET, CITY W, employee E WHERE W.E-NAME = E. E-NAME AND COMPAY-NAME = Infosys AND SALARY > 20000; TRC: { t u works-for ( t[e-name] = u [E-NAME] u[company-name] = infosys u[salary] > 20000 v employee ( v[e-name] = u [E-NAME] t[street] = v [STREET] t[city] = v [CITY] ))} DRC: { < en, st, city > < en, st, city> employee salary ( < en, Infosys, salary> works-for salary > 20000 ) } (d) Find names of employees working in the same city where they live RA: e-name, city (employee * works-for * company) SQL: SELECT E.E-NAME W, employee E, company C WHERE W. E-NAME = E. E-NAME AND W. COMPANY-NAME = C. COMPAY-NAME AND E. CITY = C. CITY; TRC: { t u employee ( t[e-name] = u [E-NAME] v works-for ( v[e-name] = u [E-NAME] w company ( w[company-name] = v [COMPANY-NAME] w[city] = u [CITY] ))) } DRC: { < en > st, city ( < en, st, city> employee cn, salary ( < en, cn, salary> works-for < cn, city > company ))} (e) Find the names of employees, who are not working for WIPRO RA: e-name (employee) - e-name (σ company-name = Wipro (works-for)) SQL: SELECT E-NAME FROM employee

Database Management System 18 (NCS-502) MINUS SELECT E-NAME WHERE COMPANY-NAME = WIPRO ; TRC: { t u employee ( t[e-name] = u [E-NAME]) v works-for ( t[e-name] = v [E-NAME] v[company-name] = WIPRO )} DRC: { < en > st, city ( < en, st, city> employee ) sal ( <en, WIPRO, sal> works-for )} (f) Find the Total and Average Salary Paid by each Company. RA: Company-Name G MAX (SALARY), AVG (SALARY) (works-for) SQL: SELECT COMPANY-NAME, SUM (SALARY), AVG (SALARY) GROUP BY COMPANY-NAME; (g) Find the names of employees, who earn more than every employee of TCS RA: MAX-TCS G MAX (SALARY) (σ COMPANY-NAME = TCS (works-for)) e-name (σ SALARY > MAX-TCS (works-for)) SQL: SELECT E-NAME WHERE SALARY > ( SELECT MAX (SALARY) WHERE COMPANY-NAME = TCS ); (h) Find names of employees, who live in the same street and city as their managers RA: emp employee * manages emp.e-name (σ emp.manager-name= mgr.e-name emp.city =mgr.city emp.street =mgr.street(emp x ρ mgr (emp))) SQL: SELECT E.E-NAME FROM employee E, manages K, employee M WHERE E. E-NAME = K. E-NAME AND K. MANAGER-NAME = M. E- NAME AND E. STREET = M. STREET AND E. CITY = M. CITY; (i) Find the company with maximum number of employees. RA: r COMPANY-NAME G COUNT (E-NAME) AS NUMB (works-for)

Database Management System 19 (NCS-502) Max-Numb G MAX (NUMB) (r) COMPANY-NAME (σ NUMB = MAX-NUMB (r)) SQL: SELECT COMPANY-NAME GROUP BY COMPANY-NAME HAVING COUNT (E-NAME) = ( SELECT MAX (COUNT (E-NAME)) GROUP BY COMPANY-NAME); (j) Find the Company with the smallest total salary paid to employees. RA: r COMPANY-NAME G SUM (SALARY) AS TOTAL-SAL (works-for) MIN-TOTAL G MIN (TOTAL-SAL) (r) COMPANY-NAME (σ TOTAL-SAL = MIN-TOTAL (r)) SQL: SELECT COMPANY-NAME GROUP BY COMPANY-NAME HAVING SUM (SALARY) = ( SELECT MIN (SUM (SALARY)) GROUP BY COMPANY-NAME); (k) Find the company whose employees earn more salary on the average than the average salary of TCS. RA: r COMPANY-NAME G AVG (SALARY) AS AVG-SAL (works-for) AVG-TCS AVG-SAL (σ COMPANY-NAME = TCS (r)) COMPANY-NAME (σ AVG-SAL > AVG-TCS (r)) SQL: SELECT COMPANY-NAME GROUP BY COMPANY-NAME HAVING AVG ( SALARY) >( SELECT AVG (SALARY) WHERE COMPANY-NAME = TCS ); Problem # 2 Answer the following queries in RA, SQL, TRC and DRC for the schema given below:- CUSTOMER (CUSTOMER-NAME, CUSTOMER-STREET, CUSTOMER-CITY) BRANCH (BRANCH-NAME, BRANCH-CITY, ASSETS) ACCOUNT (ACCOUNT-NUMBER, BRANCH-NAME, BALANCE) LOAN (LOAN-NUMBER, BRANCH-NAME, AMOUNT) DEPOSITOR (CUSTOMER-NAME, ACCOUNT-NUMBER) BORROWER (CUSTOMER-NAME, LOAN-NUMBER)

Database Management System 20 (NCS-502) (a) RA: Find the names & cities of customers having a loan from the bank. CUSTOMER-NAME, CUSTOMER-CITY (customer * borrower) SQL: SELECT C. CUSTOMER-NAME, CUSTMER-CITY FROM customer C, borrower B WHERE C. CUSTOMER-NAME = B. CUSTMER-NAME; TRC: { t u customer ( t[customer-name] = u [CUSTOMER-NAME] t[customer-city] = u [CUSTOMER-CITY] v borrower ( v[customer-name] = u [CUSTMER-NAME] )) } DRC: { < cn, cc > cs ( < cn, cs, cc> customer ln ( < cn, ln > borrower )) } (b) Find names of customers having account in all branches of Noida RA: CUSTOMER-NAME, BRANCH-NAME (depositor * account) BRANCH-NAME (σ BRANCH-CITY= Noida (branch)) SQL: SELECT D. CUSTOMER-NAME FROM depositor D WHERE NOT EXISTS (( SELECT BRANCH-NAME FROM branch WHERE BRANCH-CITY = NOIDA ) MINUS (SELECT A.BRANCH-NAME FROM depositor K, account A WHERE K.CUSTOMER_NAME = D.CUSTOMER_NAME AND K.ACCOUNT_NUMBER = A.ACCOUNT_NUMBER)); (c) Find Average Balance at each branch RA: BRANCH-NAME G AVG (BALANCE) AS AVG-BAL (account) SQL: SELECT BRANCH-NAME, AVG (BALANCE) FROM account GROUP BY BRANCH-NAME; (d) Find the accounts held by more than two customers. RA: r ACCOUNT-NUMBER G COUNT DISTINCT (CUSTOMER-NAME) AS NO-OF-CUST (depositor) ACCOUNT-NUMBER (σ NO-OF-CUST > 2 (r))

Database Management System 21 (NCS-502) SQL: SELECT ACCOUNT-NUMBER FROM depositor GROUP BY ACCOUNT-NUMBER HAVING COUNT(CUSTOMER-NAME) > 2;