DBMS Module 2
DBMS Module 2
For each regular (strong) entity type E in the ER schema, create a relation
R that includes all the simple attributes of E.
Step 2:
For each weak entity type W in the ER schema with owner entity type E,
create a relation R, and include all simple attributes (or simple
components of composite attributes) of W as attributes.
In addition, include as foreign key attributes of R the primary key
attribute(s) of the relation(s) that correspond to the owner entity type(s).
Step 3:
For each binary 1:1 relationship type R in the ER schema, identify the
relations S and T that correspond to the entity types participating in R.
Choose one of the relations, say S, and include the primary key of T as a
foreign key in S. Include all the simple attributes of R as attributes of S.
Step 4:
For each regular binary 1: N relationship type R identify the relation (N)
relation S. Include the primary key of T as a foreign key of S.
Simple attributes of R map to attributes of S.
Step 5:
Step 6:
For each multi-valued attribute A, create a new relation R.
This relation will include an attribute corresponding to A, plus the primary
key K of the parent relation (entity type or relationship type) as a foreign
key in R.
The primary key of R is the combination of A and K.
Step 7:
CREATE TABLE
SELECT STATEMENT
DELETE STATEMENT
DEFINE:
1. Constraints/integrity constraints:
Constraints are the set of rules that ensures that when an authorized user
modifies the database, they do not disturb the data consistency
2. Domain constraints:
Domain integrity constraint contains a certain set of rules or conditions to
restrict the kind of attributes or values a column can hold in the database
table. The data type of a domain can be string, integer, character, Date,
Time, currency, etc.
3. Key constraints:
Key constraints in DBMS are a restriction on duplicate values. A key is
composed of one or more columns whose values uniquely identify each
row in the table.
4. Entity integrity constraints:
An entity integrity constraint is a restriction on null values. Null values
are values that are unknown or not applicable,
5. Referential integrity constraints:
A referential integrity constraint is a restriction on how foreign keys can
be used. A foreign key is a column in one table that references a primary
key in another table
6. Super key:
A super key is a group of single or multiple keys that identifies rows in a
table
7. Candidate key:
The minimal set of attributes that can uniquely identify a tuple is known
as a candidate key
8. Foreign key:
A foreign key is a column or group of columns in a relational database
table that provides a link between data in two tables. It is a column (or
columns) that references a column (most often the primary key) of
another table.
9. Primary key:
A primary key is used to ensure that data in the specific column is unique.
10.Unique key:
A unique key in SQL is the set of fields or columns of a table that helps us
uniquely identify records. The unique key guarantees the uniqueness of
the columns in the database. It is similar to the primary key but can accept
a null value
QUERIES
1.
a) Select the detail of the employee whose name start with P.
2.
a. Write an SQL query to fetch the EmpId and FullName of all the employees
working under the Manager with id – ‘986’.
b. Write an SQL query to fetch the different projects available from the
EmployeeSalary table.
c. Write an SQL query to find the employee id whose salary lies in the range
of 9000 and 15000.
d. Write an SQL query to fetch the employees whose name begins with any
two characters, followed by a text “hn” and ends with any sequence of
characters.
e. Write an SQL query to fetch employee names having a salary greater than
or equal to 5000 and less than or equal to 10000.
ii) For each project retrieve the project number, project name and
number of employees who worked on that project.
R1 ←PROJECT pnumber=pnoWORKS_ON
R2←π pnumber, pname, count(*) (R1)
iii) Retrieve the names of employees who work on all the project
controlled by department 5
DEPT5_PROJS ← ρ(Pno)(πPnumber(σDnum=5(PROJECT)))
EMP_PROJ ← ρ (Ssn, Pno) (πEssn, Pno (WORKS_ON))
RESULT_EMP_SSNS ← EMP_PROJ ÷ DEPT5_PROJS
RESULT ← πLname, Fname (RESULT_EMP_SSNS * EMPLOYEE)
vi) Retrieve the name and address of all employees who work for
'sports' department.
R1← ρ DNAME=’sports’(DEPT)
ii) Find the age of the youngest sailor who is eligible to vote (i.e. is
atleast 18 years old) for each rating level with atleast two such
sailors.
Select s.rating,MIN(s.age)
from sailors s
where s.age>18
group by s,rating
having 1 < (SELECT COUNT (*)
FROM Sailors s2
WHERE s.rating=s2.rating);
iii) Find the names of sailors who have not reserved a red boat. (use
nested query).
SELECT S.sname
FROM Sailors S
WHERE S.sid NOT IN ( SELECT R.sid
FROM Reserves R
WHERE R.bid IN ( SELECT B.bid
FROM Boats B
WHERE B.color=`red’ ));
iv) Compute increments for the rating of persons who have sailed two
different boats on the same day.
SELECT r.sid, r.day, COUNT(*), s.rating
FROM reserves r
JOIN sailors s
ON r.sid=s.sid
GROUP BY day
HAVING COUNT(r.day)=2;
3. Find the passenger names for those who do not have any bookings in
any flights
Π pname ((Π pid (passenger) - Π pid (booking)) ⨝ passenger)
4. Get the details of flights that are scheduled on both dates
01/12/2020 and 02/12/2020 at 16:00 hours
(σ fdate = 01/12/2020 ^ time = 16:00 (flight)) ∩ (σ fdate = 02/12/2020 ^ time = 16:00 (flight))
5. Find the details of all male passengers who are associated with jet
agency.
Π passengers.pid, pname, pcity (σ pgender = “Male” ^ aname =
2. Find the names of all employees in the database who do not work for
First Bank Corporation'. Assume that all people work for exactly one
company
select employee_name from works where company_name != ’First Bank
Corporation’;
3. Find the names of all employees in the database who earn more that
every employee of Small Bank Corporation'. Assume that all people
work for at most one company
select employee_name from works where salary > all (select salary from
works where company_name = ’Small Bank Corporation’);
4. Find the name of the company that has the smallest payroll
select company-name from works group by company-name having sum
(salary) <= all (select sum (salary) from works group by company-name);
5. Find the names of all employees in the database who live in the same
cities and on the same streets as do their managers.
select P.employee-name from employee P, employee R, manages M
where P.employee-name = M.employee-name and M.manager-name =
R.employee-name and P.street = R.street and P.city = R.city;
8.
ANSWERS:
9.
ANSWERS
10.
ANSWERS
11.
ANSWERS
RELATIONAL ALGEBRA OPERATION EXAMPLES:(refer it)