Cheat Sheet
Cheat Sheet
Candidate Key: A minimal superkey (K) such that no proper subset of a superkey within the relation.
Primary Key
One of candidate keys selected to identify tuples uniquely within relation.
The primary key attributes are underlined in the schema
Alternate Keys
Candidate keys that are not selected to be primary key.
Foreign Key
Attribute, or set of attributes, within one relation that matches candidate key of some (possibly same) relation
Cardinality
One to one (1:1) relationship,One to many (1:m) relationship,Many to one (m:1) relationship,Many to many (m:n) relationship
Strong entities: the instances of the entity class can exist on their own, without participating in any relation.
Weak entities: each instance of the entity class has to participate in a relationship in order to exist. Do not have key attributes
of their own. Keys are imported from dependent entity.
Fan trap: a model represents a relationship type between entity types, but pathway between certain entity occurrences is
ambiguous.
Here one division has many department and each division has many employee.
So we can not find which employee belongs to which department. That is Fan Trap because it is ambiguous.
Chasm Trap: a model suggests the existence of a relationship type between entity types, but pathway does not exist between
certain entity occurrences.
It occurs where there is a relationship with partial participation, which forms part of the pathway between entities that are
related.
Disjoint:emp total disjoint ->(hourly paid or monthly paid)|| Patial disjoint (sales or developer or manager)
Overlapping : person overlaps(employee and student)
Constructing an ER model
1.Identify entities 2.Remove duplicate entities 3.List the attributes of each entity (all properties to describe the entity which
are relevant to the application). 4. Mark the primary keys. 5.Define the relationships 6. Describe the cardinality and optionality
of the relationships 7.Remove redundant relationships
(min,max): (0, n) is the (min, max) constraint for BANK_BRANCH because a branch can have zero or many accounts.
Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project.
Use relational operators :
Answer: EMP_W_X <-- ( s PNAME='ProductX' (PROJECT)) J (PNUMBER),(PNO)(WORKS_ON) EMP_WORK_10 <-- (EMPLOYEE) J
(SSN),(ESSN) ( s HOURS>10 (EMP_W_X)) RESULT <-- P LNAME,FNAME ( s DNO=5 (EMP_WORK_10))
Tuple relational Calculus:
{ e.LNAME, e.FNAME | EMPLOYEE(e) AND e.DNO=5 AND (EXISTS p) (EXISTS w) (WORKS_ON(w) AND PROJECT(p) AND
e.SSN=w.ESSN AND
w.PNO=p.PNUMBER AND p.PNAME='ProductX' AND w.HOURS>10 ) }
Domain relational Calculus:{qs|EMPLOYEE(qrstuvwxyz) AND z=5 AND (EXISTS a) (EXISTS b) (EXISTS e) (EXISTS f) (EXISTS g) (
WORKS_ON(efg) AND PROJECT(abcd) AND t=e AND f=b AND a='ProductX'AND g>10 ) }
Retrieve the names of employees who make at least $10,000 more than the employee who is paid the least in the
company.
RESULT <-- P LNAME ( S SALARY >= 10000 + P SALARY ( S SALARY = MIN(SALARY) EMPLOYEE ) EMPLOYEE )
SELECT LNAMEFROM EMPLOYEEWHERE SALARY >= 10000 + ( SELECT MIN(SALARY) FROM EMPLOYEE)
For each department whose average employee salary is more than $30,000, retrieve the department name and the number
of employees working for that department. MALE.
SELECT DNAME, DNUMBER, COUNT (*)FROM DEPARTMENT, EMPLOYEEWHERE DNUMBER=DNO AND SEX='M' AND DNO IN
GROUP BY DNAME(SELECT DNOFROM EMPLOYEEGROUP BY DNOHAVING AVG (SALARY) > 30000)
Oredering: Default is ascending order, but descending order can be specified by the DESC keyword.
SELECT B-name, Assets FROM branch WHERE Assets > 2500000 ORDER BY Assets
List the name and cities of all customers who have an account with balance greater than $2,000.
SELECT C.C-name, City FROM customer C, deposit D WHERE Balance > 2000 AND C.C-name = D.C-name
Find the name and assets of all branches which have customers living in New York.
SELECT B-name, Assets FROM branch, customer, deposit WHERE customer.City = New York AND customer.C-name =
deposit.C-name AND deposit.B-name = branch.B-name
Find all cities where there is either a customer or a branch.
(SELECT City FROM customer) UNION (SELECT City FROM branch)
Find all cities that have a branch but no customers.
(SELECT City FROM branch) MINUS (SELECT City FROM customer)
Find all the cities that have both customers and bank branches.
(SELECT City FROM customer) INTERSECT (SELECT City FROM branch)
Find all the customers who have a deposit at some branch at which John has a deposit.
SELECTC-name FROM deposit WHERE B-name IN (SELECT B-name FROM deposit WHERE C-name = John)
Find all the customers who have either a deposit or a loan and their cities.
SELECT C-name, City FROM customer WHERE C-name IN (SELECT C-name FROM deposit ) OR C-name IN (SELECT C-name
FROM borrow)
Find all the branches that have assets greater than all branches in New York.
SELECT B-name FROM branch WHERE Assets > ALL (SELECT Assets FROM branch WHERE City = New York)
Find the names of customers who live in a city which has a bank branch.
SELECT C-name FROM customer WHERE EXISTS (SELECT * FROM branch WHERE customer.City=branch.City)
Find the names and addresses of all the customers who have no deposit in the bank.
SELECT C-name, Street, City FROM customer WHERE NOT EXISTS (SELECT * FROM deposit WHERE customer.C
name=deposit.C-name)
Find all the customers who have deposits in every branch of the bank.
SELECT * FROM customer WHERE C-name NOT IN (SELECT C.C-name FROM branch B, Customer C WHERE C.C-name NOT IN
(SELECT D.C-name FROM deposit D WHERE B.B-name=D.B-name));
Find all the customers who have a deposit at every branch at which John Doe has a deposit.
SELECT * FROM customer WHERE NOT EXISTS (SELECT * FROM branch B WHERE (B.B-name IN (SELECT B-name FROM
deposit D WHERE D.C-name=John Doe )) and NOT EXISTS (SELECT * FROM deposit E WHERE B.B-name=E.B-name AND
customer.C-name = E.C-name));
Find all the customers who have a loan from every branch in New York.
SELECT * FROM customer WHERE ((SELECT B-name FROM borrow WHERE customer.C-name=borrow.C-name) CONTAINS
(SELECT B-name FROM branch WHERE City = New York))
Find all the customers who have a loan from every branch in New York.
SELECT * FROM customer C WHERE NOT EXISTS (SELECT * FROM branch B WHERE B.City= New York AND NOT EXISTS
(SELECT * FROM Borrow D WHERE B.B-name=D.B-name AND C.C-name = D.C-name))
Find all the customers who have a loan from every branch in New York.
SELECT * FROM customer WHERE C-name NOT IN (SELECT C.C-name FROM branch B, Customer C WHERE B.city=New York
AND C.C-name NOT IN (SELECT D.C-name FROM Borrow D WHERE B.B-name=D.B-name)) Find the total assets of branches in
New York.
SELECT SUM(Assets) FROM branch WHERE City = New York
Find the number of cities where John Doe has an account with any bank branch.
SELECT COUNT(DISTINCT City) FROM branch, deposit WHERE branch.B-name = deposit.B-name AND deposit.C-name = John
Doe
Find the names of branches which have assets greater than the average assets of all bank branches.
SELECT B-name FROM branch WHERE Assets > (SELECT AVG(Assets) FROM branch)
For each city, find the number of branches.
SELECT City, COUNT(*) FROM branch GROUP BY City
For each branch at each city, list the numbers of the customers loans over $100,000.
SELECT City, B-Name, C-name, Count(*) FROM customer, borrow WHERE customer.C-name = borrow.C-name AND Amount >
100000 GROUP BY City, B-name, C-name
For each branch, list the names and number of loans of customers who have more than 2 loans over $100,000.
SELECT B-name, C-name, COUNT(Acc-number) FROM borrow WHERE Amount > 100000 GROUP BY B-name, C-name HAVING
COUNT(*) > 2
For each city where there are more than two branches, find the names of branches.
SELECT City, B-name FROM branch WHERE City in (SELECT City FROM branch GROUP BY City HAVING COUNT(*) > 2)
INSERT INTO customer VALUES (John Smith,145 Broadway ,New York)
DELETE FROM customer WHERE C-name IN (SELECT C-name FROM deposit WHERE Balance < 1000) UPDATE deposit SET
Balance = Balance*1.05 WHERE Balance > 5000 AND C-name IN (SELECT C-name FROM customer WHERE City = New York)
Create a view NewYork-Customer of customers who live in NewYork.
CREATE VIEW NewYork-Customer AS SELECT * FROM customer WHERE City = New York
Create a view total-balance that gives, for each customer at each city, the number of accounts owned by the customer and the
total balance.
CREATE VIEW total-balance(Name, City, Number, Total) AS SELECT C.C-name, C.City, COUNT(Acc-number), SUM(Balance)
FROM deposit D, customer C WHERE D.C-name=C.C-name GROUP BY C.C-name, City