23mca005 Dbms File
23mca005 Dbms File
(CSL 214)
Lab Workbook
Roll No.:23MCA008
Semester:1st
Session 2022-23
DBMS Lab Workbook (CSL214) | 2
2022-23
Student Name:
INDEX
Experiment No: 1
Student Name and Roll Number: Harsh Gandhi 23MCA008
Link to Code:
Date:
Faculty Signature:
Marks:
Objective
Program Outcome
The students will be able to draw conceptual database design using ERD Plus.
Problem Statement
1. The COMPANY database keeps track of a company’s employees, departments, and projects.
Suppose that after the requirements collection and analysis phase, the database designers provide
the following description of the mini world—the part of the company that will be represented in
the database.
We store each employee’s name, Social Security number, address, salary, sex
(gender), and birth date. An employee is assigned to one department, but
may work on several projects, which are not necessarily controlled by the
same department. We keep track of the current number of hours per week
that an employee works on each project. We also keep track of the direct
supervisor of each employee (who is another employee).
We want to keep track of the dependents of each employee for insurance
purposes. We keep each dependent’s first name, sex, birth date, and relationship to the
employee.
2. Design an ER/EER diagram for keeping track of the exploits of your favourite sports team.
You should store the matches played, the scores in each match, the players in each match and
individual player statistics for each match. Summary statistics should be modelled as derived
attributes. Further, extend the E-R diagram of the previous question to track the same
information for all teams in a league.
Design the ER/EER model by identifying the following from the above requirements:
Background Study
1) Entity:
o Real-world object distinguishable from other objects.
o An entity is described using a set of attributes.
3) .
o Attributes are properties used to describe an entity.
o Example: EMPLOYEE entity may have a Name, SSN, Address, Sex, BirthDate.
4) Relationship
o A relationship relates two or more distinct entities with a specific meaning.
o Relationships of the same type are grouped or typed into a relationship type.
o 3 types of relationships : Unary, Binary & Ternary.
5) Recursive Relationship
o A relationship with the same participating entity type in distinct roles.
o Example: the SUPERVISION relationship
6) Structural Constraints – Semantics of Relationships
o Cardinality Ratio : The number of instances of an entity from a relation that can
be associated with the relation.
o Participation Constraints
Total Participation − Each entity is involved in the relationship.
DBMS Lab Workbook (CSL214) | 7
2022-23
ER Diagram
Preparatory Questions
DBMS Lab Workbook (CSL214) | 8
2022-23
Q1) Given the basic ER and relational models, which of the following is INCORRECT?
Q2) Consider a directed line(-->) from the relationship set advisor to both entity sets instructor
and student. This indicates _________ cardinality
1) One to many
2) One to one
3) Many to many
4) Many to one
Q3) An entity set that does not have sufficient attributes to form a primary key is termed as :
1) Strong entity set
2) Variant set
3) Weak entity set
4) Variable set
Q4) Which of the following indicates the maximum number of entities can be involved in a
relationship?
1) Minimum cardinality
2) Maximum Cardinality
3) ERD
4) Greater Entity Count (GEC)
Q5) State true or false: Every weak entity must be associated with and identifying entity.
1) True
2) False
DBMS Lab Workbook (CSL214) | 9
2022-23
Experiment No: 2
Student Name and Roll Number: Harsh Gandhi 23MCA008
Link to Code:
Date:
Faculty Signature:
Marks:
Program Outcome
The students will be able to map the conceptual database design to logical (relational)
database design.
Problem Statement
Map the ER/EER diagram of the COMPANY database created in the previous experiment to
Relational Database Schema. Follow the below mentioned steps to successfully map ER/EER
model to relational tables:
Background Study
o For each regular binary 1:N relationship type R, identify the relation S that represent the
participating entity type at the N-side of the relationship type.
o Include as foreign key in S the primary key of the relation T that represents the other
entity type participating in R.
Employee:
Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno
Department:
Dept_Locations:
Dnumber Dlocation
Project:
Works_on:
Dependent:
Preparatory Questions
Q1) In which of the following, a separate schema is created consisting of that attribute and the
primary key of the entity set.
1) A many-to-many relationship set
2) A multivalued attribute of an entity set
3) A one-to-many relationship set
4) All of the mentioned
1) 2
2) 3
3) 4
4) 5
Q3) Consider the data given in above question. Which of the following is a correct attribute set
for one of the tables for the correct answer to the above question?
Q4) Let E1 and E2 be two entities in an E/R diagram with simple single-valued attributes. R1
and R2 are two relationships between E1 and E2, where R1 is one-to-many and R2 is many-to-
many. R1 and R2 do not have any attributes of their own. What is the minimum number of tables
required to represent this situation in the relational model?
1) 2
2) 3
3) 4
4) 5
Q5) What is the min and max number of tables required to convert an ER diagram with 2 entities
and 1 relationship between them with partial participation constraints of both entities?
Experiment No: 3
Student Name and Roll Number: Harsh Gandhi 23MCA008
Link to Code:
Date:
Faculty Signature:
Marks:
Objective
To apply SQL integrity constraints as per the DDL statements given below for COMPANY
database.
Program Outcome
The students will understand how a database is created followed by insertion of relevant data.
DBMS Lab Workbook (CSL214) | 16
2022-23
The students will understand the need of applying various types of integrity constraints such
as primary key, foreign key, unique key, NOT NULL, default and CHECK etc
Problem Statement
DBMS Lab Workbook (CSL214) | 17
2022-23
1) Primary Key
2) Foreign Key
3) Unique
4) Default
5) Auto-increment
6) Check
7) Not Null
Background Study
1) Primary Key Constraint: A column or group of columns in a table which helps us to uniquely
identifies every row in that table is called a primary key. This DBMS can't be a duplicate.
The same value can't appear more than once in the table.
2) Foreign Key (Referential integrity constraint): This constraint identifies any column
referencing the PRIMARY KEY in another table. It establishes a relationship between two
columns in the same table or between different tables. For a column to be defined as a
Foreign Key, it should be a defined as a Primary Key in the table which it is referring. One
or more columns can be defined as Foreign key.
Syntax to define a Foreign key at column level:
[CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name)
3) SQL Not Null Constraint : This constraint ensures all rows in the table contain a definite
value for the column which is specified as not null. Which means a null value is not
allowed.
4) SQL Unique Key: This constraint ensures that a column or a group of columns in each row
have a distinct value. A column(s) can have a null value but the values cannot be
duplicated.
5) SQL Check Constraint : This constraint defines a business rule on a column. All the rows
must satisfy this rule. The constraint can be applied for a single column or a group of
columns.
Syntax to define a Check constraint:
[CONSTRAINT constraint_name] CHECK (condition)
Output: Screenshots
Inserted Values :
Employee :
Department :
DBMS Lab Workbook (CSL214) | 23
2022-23
Dependent :
Dept_locations :
DBMS Lab Workbook (CSL214) | 24
2022-23
Project :
Works_on :
DBMS Lab Workbook (CSL214) | 25
2022-23
Preparatory Questions
1) Suppose (A, B) and (C,D) are two relation schemas. Let r1 and r2 be the corresponding
relation instances. B is a foreign key that refers to C in r2. If data in r1 and r2 satisfy
referential integrity constraints, which of the following is ALWAYS TRUE?
1) A
2) B
3) C
4) D
CREATE TABLE S (
a INTEGER,
DBMS Lab Workbook (CSL214) | 26
2022-23
d INTEGER,
e INTEGER,
PRIMARY KEY (d),
FOREIGN KEY (a) references R)
Q5) The following table has two attributes A and C where A is the primary key and C is the
foreign key referencing A with on-delete cascade.
A C
2 4
3 4
4 3
5 2
7 2
9 5
DBMS Lab Workbook (CSL214) | 27
2022-23
6 4
The set of all tuples that must be additionally deleted to preserve referential integrity when the
tuple (2,4) is deleted is:
Experiment No: 4
Student Name and Roll Number: Harsh Gandhi 23MCA008
Link to Code:
Date:
Faculty Signature:
Marks:
Objective
Program Outcome
The students will be able to retrieve zero or more rows from one or more database tables or
database views.
Problem Statement
From the COMPANY database as mentioned and described in the previous database :
1) Retrieve the birth date and address of the employee(s) whose name
is ‘John B. Smith’.
2) Retrieve the name and address of all employees who work for the ‘Research’ department.
DBMS Lab Workbook (CSL214) | 29
2022-23
3) For every project located in ‘Stafford’, list the project number, the
controlling department number, and the department manager’s last name,
address, and birth date
4) Select all combinations of EMPLOYEE Ssn and DEPARTMENT Dname in the database.
5) Retrieve all the attribute values of any EMPLOYEE who works in DEPARTMENT
number 5.
6) Retrieve all distinct salary values.
7) Make a list of all project numbers for projects that involve an employee whose last name
is ‘Smith’, either as a worker or as a manager of the department that controls the project.
8) Retrieve all employees whose address is in Houston, Texas.
9) Find all employees who were born during the 1950s
10) Show the resulting salaries if every employee working on the ‘ProductX’ project is given
a 10 percent raise.
11) Retrieve a list of employees and the projects they are working on, ordered by department
and, within each department, ordered alphabetically by last name, then first name.
Background Study
1) Structured Query Language SQL contains statements for data definitions, queries, and
updates (both DDL and DML)
2) Domain
o Name used with the attribute specification
o Makes it easier to change the data type for a domain that is used by numerous
attributes
4) Out of the complete Database we create we can easily pick out and filter certain amount
of data by using the SQL queries as :
WHERE <condition>;
o <attribute list> is a list of attribute names whose values are to be retrieved by the
query.
o <table list> is a list of the relation names required to process the query.
o <condition> is a conditional (Boolean) expression that identifies the tuples to be
retrieved by the query.
5) Ambiguous Attribute Names: Same name can be used for two (or more) attributes as long
as the attributes are in different relations, these are made to differ by mentioning their
table names before the attribute names.
6) Aliasing, Renaming Tuple variables: We can rename the tables into some smaller easier
names as per choice when it has to be used multiple times in the query.
7) DISTINCT keyword is used when the query wishes to derive no two duplicate values.
8) SET operations such as UNION and INTERSECT can also be applied on the tables to
filter out the values as per choice.
9) Substring Pattern matching is carried out by the use of the keyword LIKE which helps in
retrieving the column values of the tuple matching our mentioned substring.
Output: Screenshots
1) Retrieve the birth date and address of the employee(s) whose name
is ‘John B. Smith’.
DBMS Lab Workbook (CSL214) | 31
2022-23
2) Retrieve the name and address of all employees who work for the ‘Research’ department.
3) For every project located in ‘Stafford’, list the project number, the
controlling department number, and the department manager’s last name,
address, and birth date
4) Select all combinations of EMPLOYEE Ssn and DEPARTMENT Dname in the database.
DBMS Lab Workbook (CSL214) | 32
2022-23
5) Retrieve all the attribute values of any EMPLOYEE who works in DEPARTMENT
number 5
7) Make a list of all project numbers for projects that involve an employee whose last name
is ‘Smith’, either as a worker or as a manager of the department that controls the
project.
10) Show the resulting salaries if every employee working on the ‘ProductX’ project is given
a 10 percent raise.
11) Retrieve a list of employees and the projects they are working on, ordered by department
and, within each department, ordered alphabetically by last name, then first name.
DBMS Lab Workbook (CSL214) | 35
2022-23
DBMS Lab Workbook (CSL214) | 36
2022-23
Preparatory Questions
1) BETWEEN operator
2) LIKE operator
3) EXISTS operator
4) None of the above
Q2) In SQL which commands are used to change the storage characteristics of the table?
1) ALTER TABLE
2) MODIFY TABLE
3) CHANGE TABLE
4) All of the above
Q3) ___________removes all rows from a table without logging the individual row deletions.
1) DELETE
2) REMOVE
3) DROP
4) TRUNCATE
Q4) If you don’t specify ASC or DESC after a SQL ORDER BY clause, the following is used by
default :
1) ASC
2) DESC
3) There is no default value
4) None of the mentioned
Experiment No: 5
Student Name and Roll Number: Harsh Gandhi 23MCA008
Link to Code:
Date:
Faculty Signature:
Marks:
Objective
Program Outcome
The students will be to establish a connection between two or more database tables based on
matching columns, thereby creating a relationship between the tables.
Problem Statement
Consider the Sample database given below and answer the queries as stated:
Pack_grades
Packages
DBMS Lab Workbook (CSL214) | 38
2022-23
Sectors
Sector_id Sector_name
1) Write a query to display first name, last name, package number and internet speed for all
customers.
2) Write a query to display first name, last name, package number and internet speed for all
customers whose package number equals 22 or 27. Order the query in ascending order by last
name.
3) Display the package number, internet speed, monthly payment and sector name for all
packages (Packages and Sectors tables).
4) Display the customer name, package number, internet speed, monthly payment and sector
name for all customers (Customers, Packages and Sectors tables).
5) Display the customer name, package number, internet speed, monthly payment and sector
name for all customers in the business sector (Customers, Packages and Sectors tables).
6) Display the last name, first name, join date, package number, internet speed and sector name
for all customers in the private sector who joined the company in the year 2006.
7) Display the package number, internet speed, monthly payment and package grade for all
packages (Packages and Pack_Grades tables).
8) Display the first name, last name, internet speed and monthly payment for all customers. Use
INNER JOIN to solve this exercise.
9) Display the last name, first name and package number for all customers who have the same
package number as customer named ‘Amado Taylor’ (Customers table).
10) Display the package number and internet speed for all packages whose internet speed is
equal to the internet speed of package number 10 (Packages table).
Background Study
1) JOINED TABLE: Permits users to specify a table resulting from a join operation in the
FROM clause of a query
o The attributes of such a table are all the attributes of the first table followed by all the
attributes of the second table.
DBMS Lab Workbook (CSL214) | 39
2022-23
o The default type of join in a joined table is called an inner join, where a tuple is included
in the result only if a matching tuple exists in the other relation.
o It is possible to rename the attributes so that they match, if the names of the join attributes
are not the same in the base relations.
Output: Screenshots
1) Write a query to display first name, last name, package number and
internet speed for all customers.
DBMS Lab Workbook (CSL214) | 40
2022-23
2) Write a query to display first name, last name, package number and
internet speed for all customers whose package number equals 22 or 27.
Order the query in ascending order by last name.
3) Display the package number, internet speed, monthly payment and sector
name for all packages (Packages and Sectors tables).
DBMS Lab Workbook (CSL214) | 41
2022-23
5) Display the last name, first name, join date, package number, internet
speed and sector name for all customers in the private sector who joined
the company in the year 2006.
7) Display the first name, last name, internet speed and monthly payment for all
customers. Use INNER JOIN to solve this exercise.
8) Display the last name, first name and package number for all customers who have
the same package number as customer named ‘Amado Taylor’ (Customers table).
DBMS Lab Workbook (CSL214) | 43
2022-23
9) Display the package number and internet speed for all packages whose internet
speed is equal to the internet speed of package number 10 (Packages table).
Preparatory Questions
SELECT Count(*)
FROM ( ( SELECT Borrower, Bank_Manager
FROM Loan_Records) AS S
NATURAL JOIN ( SELECT Bank_Manager, Loan_Amount
DBMS Lab Workbook (CSL214) | 44
2022-23
FROM Loan_Records) AS T );
1) 3
2) 9
3) 5
4) 6
Q3) Which join refers to join records from the write table that have no matching key in the left
table are include in the result set:
1) Left outer join
2) Right outer join
3) Full outer join
4) Half outer join
Q5) Which view that contains more than one table in the top-level FROM clause of the SELECT
statement:
1) Join view
2) Datable join view
3) Updatable join view
4) All of the mentioned
DBMS Lab Workbook (CSL214) | 45
2022-23
Experiment No: 6
Student Name and Roll Number: Harsh Gandhi 23MCA008
Link to Code:
Date:
Faculty Signature:
Marks:
Objective
Program Outcome
DBMS Lab Workbook (CSL214) | 46
2022-23
The students will understand the need of applying an aggregate function to get a single value
from a set of values, thus, expressing the significance of the data it is computed from.
Problem Statement
Consider the COMPANY database in Experiment 1 and execute the following queries:
1) Find the sum of the salaries of all employees, the maximum salary, the minimum salary,
and the average salary.
2) Find the sum of the salaries of all employees of the ‘Research’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department.
3) Retrieve the total number of employees in the company.
4) Retrieve the number of employees in the ‘Research’ department.
5) Count the number of distinct salary values in the database
6) For each department, retrieve the department number, the number of employees in the
department, and their average salary.
7) For each project, retrieve the project number, the project name, and the number of
employees who work on that project.
8) For each project on which more than two employees work, retrieve the project number,
the project name, and the number of employees who work on the project
9) For each project, retrieve the project number, the project name, and the number of
employees from department 5 who work on the project.
10) For each department that has more than five employees, retrieve the department number
and the number of its employees who are making more than $40,000.
Background Study
1) Aggregate Functions: Used to summarize information from multiple tuples into a single-
tuple summary
3) Built-in aggregate functions: COUNT, SUM, MAX, MIN, and AVG (NULL values
discarded when aggregate functions are applied to a particular column)
7) If NULLs exist in grouping attribute, then separate group created for all tuples with a
NULL value in grouping attribute
9) SELECT clause includes only the grouping attribute and the aggregate functions to be
applied on each group of tuples.
10) WHERE clause limit the tuples to which functions are applied, the HAVING clause
serves to choose whole groups
Output: Screenshots
1) Find the sum of the salaries of all employees, the maximum salary, the minimum
salary, and the average salary.
DBMS Lab Workbook (CSL214) | 48
2022-23
2) Find the sum of the salaries of all employees of the ‘Research’ department, as well
as the maximum salary, the minimum salary, and the average salary in this
department.
6) For each department, retrieve the department number, the number of employees in
the department, and their average salary.
1) 7) For each project, retrieve the project number, the project name, and the number
of employees who work on that project.
8) For each project on which more than two employees work, retrieve the project
number, the project name, and the number of employees who work on the project.
DBMS Lab Workbook (CSL214) | 50
2022-23
9. For each project, retrieve the project number, the project name, and the number
of employees from department 5 who work on the project.
10. For each department that has more than five employees, retrieve the
department number and the number of its employees who are making more
than $40,000.
Preparatory Questions
DBMS Lab Workbook (CSL214) | 51
2022-23
Q1) Which of the following statements are TRUE about an SQL query? P: An SQL query can
contain a HAVING clause even if it does not have a GROUP BY clause Q : An SQL query can
contain a HAVING clause only if it has a GROUP BY clause R : All attributes used in the
GROUP BY clause must appear in the SELECT clause S : Not all attributes used in the GROUP
BY clause need to appear in the SELECT clause
1) P and R
2) P and S
3) Q and R
4) Q and S
Q2) Consider a database table T containing two columns X and Y each of type integer. After the
creation of the table, one record (X=1, Y=1) is inserted in the table. Let MX and My denote the
respective maximum values of X and Y among all records in the table at any point in time. Using
MX and MY, new records are inserted in the table 128 times with X and Y values being MX+1,
2*MY+1 respectively. It may be noted that each time after the insertion, values of MX and MY
change. What will be the output of the following SQL query after the steps mentioned above are
carried out?
1) the average salary is more than the average salary in the company
2) the average salary of male employees is more than the average salary of all
male employees in the company
3) the average salary of male employees is more than the average salary of
employees in the same department
4) the average salary of male employees is more than the average salary in the
company
Q5) Observe the given SQL query and choose the correct option.
Experiment No: 7
Student Name and Roll Number: Harsh Gandhi 23MCA008
Link to Code:
Date:
Faculty Signature:
Marks:
Objective
Program Outcome
Types of Triggers –
We can define 6 types of triggers for each table:
1. AFTER INSERT activated after data is inserted into the table.
Background study
ON table_name
WHEN (condition)
BEGIN
END;
Output screen
DBMS Lab Workbook (CSL214) | 56
2022-23
Experiment No: 8
Student Name and Roll Number: Harsh Gandhi 23MCA008
Link to Code:
Date:
DBMS Lab Workbook (CSL214) | 57
2022-23
Faculty Signature:
Marks:
Objective
Program Outcome
The students will understand how to devise independent and correlated nested queries.
Problem Statement
Consider the COMPANY database in Experiment 1 and execute the following queries:
1) Make a list of all project numbers for projects that involve an employee whose last name is
‘Smith’, either as a worker or as a manager of the department that controls the project.
a. Select the Essns of all employees who work on the same project and hours as some
project that employee ‘John Smith’ (whose Ssn = ‘123456789’) works on.
2) Return the names of employees whose salary is greater than the salary of all the employees in
department 5
3) Retrieve the name of each employee who has a dependent with the same first name and is the
same sex as the employee
4) Retrieve the names of employees who have no dependents
5) List the names of managers who have at least one dependent using EXISTS and NOT
EXISTS functions
6) Retrieve the name of each employee who works on all the projects controlled by department
number 5 using EXISTS and NOT EXISTS functions
7) Retrieve the names of all employees who have two or more dependents
8) Retrieves the names of all employees who work on only one project
Background Study
DBMS Lab Workbook (CSL214) | 58
2022-23
1) SQL allows queries that check whether an attribute value is NULL: IS or IS NOT NULL
2) Nested Queries: Complete select-from-where blocks within WHERE clause of another query
o Nested Queries generally return a table (relation)
4) = ANY (or = SOME) Operator: Returns TRUE if the value v is equal to some value in the set
V (equivalent to IN)
5) ALL Operator: (v > ALL V) returns TRUE if the value v is greater than all the values in the
set (or multiset) V.
o Other operators that can be combined with ANY (or SOME) and ALL: >, >=, <, <=,
and <>
6) Possible ambiguity among attribute names if attributes of the same name exist—one in a
relation in the FROM clause of the outer query, and another in a relation in the FROM clause
of the nested query.
o Thumb Rule: reference to an unqualified attribute refers to the relation declared in the
innermost nested query.
7) Whenever a condition in the WHERE clause of a nested query references some attribute of a
relation declared in the outer query, the two queries are said to be correlated.
o In a correlated query, the nested query is evaluated once for each tuple (or
combination of tuples) in the outer query
8) EXISTS function: Check whether the result of a correlated nested query is empty or not.
9) EXISTS and NOT EXISTS are typically used in conjunction with a correlated nested query.
10) EXISTS(Q): returns TRUE if there is at least one tuple in the result of the nested query Q,
and it returns FALSE otherwise.
11) NOT EXISTS(Q): returns TRUE if there are no tuples in the result of nested query Q, and it
returns FALSE otherwise.
Output: Screenshots
DBMS Lab Workbook (CSL214) | 59
2022-23
Preparatory Questions
DBMS Lab Workbook (CSL214) | 60
2022-23
SELECT Count(*)
FROM ( ( SELECT Borrower, Bank_Manager
FROM Loan_Records) AS S
NATURAL JOIN ( SELECT Bank_Manager, Loan_Amount
FROM Loan_Records) AS T );
1) 3
2) 9
3) 5
4) 6
Q2) A relational schema for a train reservation database is given below. Passenger (pid, pname,
age) Reservation (pid, class, tid)
Table: Passenger
pid pname age
-----------------
0 Sachin 65
1 Rahul 66
2 Sourav 67
3 Anil 69
Table : Reservation
pid class tid
---------------
0 AC 8200
1 AC 8201
2 SC 8201
5 AC 8203
DBMS Lab Workbook (CSL214) | 61
2022-23
1 SC 8204
3 AC 8202
What pids are returned by the following SQL query for the above instance of the tables?
SELECT pid
FROM Reservation ,
WHERE class ‘AC’ AND
EXISTS (SELECT *
FROM Passenger
WHERE age > 65 AND
Passenger. pid = Reservation.pid)
1) 1,0
2) 1,2
3) 1,3
4) 1,5
1) Find the names of all suppliers who have supplied a non-blue part.
2) Find the names of all suppliers who have not supplied a non-blue part.
3) Find the names of all suppliers who have supplied only blue parts.
4) Find the names of all suppliers who have not supplied only blue parts.
5) None
Q4) Consider the table employee(empId, name, department, salary) and the two queries Q1 ,Q2
below. Assuming that department 5 has more than one employee, and we want to find the
employees who get higher salary than anyone in the department 5, which one of the statements is
TRUE for any arbitrary employee table?
salesRepId is a foreign key referring to empId of the employee relation. Assume that each
employee makes a sale to at least one customer. What does the following query return?
SELECT empName
FROM employee E
WHERE NOT EXISTS ( SELECT custId
FROM customer C
WHERE C.salesRepId = E.empId
AND C.rating <> `GOOD`);
1) Names of all the employees with at least one of their customers having a ‘GOOD’ rating.
2) Names of all the employees with at most one of their customers having a ‘GOOD’ rating.
3) Names of all the employees with none of their customers having a ‘GOOD’ rating.
4) Names of all the employees with all their customers having a ‘GOOD’ rating.
Experiment 9
Student Name and Roll Number: Harsh Gandhi 23MCA008
DBMS Lab Workbook (CSL214) | 64
2022-23
Link to Code:
Date:
Faculty Signature:
Marks:
Objective
Identifying contrast between Relational Databases and NoSQL, thereby recognizing their
applications.
Program Outcome
The students will install MongoDB shell and familiarize themselves with it.
Problem Statement
Background Study
MongoDB is a Schema less database. A database in MongoDB contains collections and inside
collections we have documents.
Database is a physical container for collections. A single MongoDB server typically has
multiple databases.
Collection is a group of MongoDB documents.
Documents within a collection can have different fields. A document is a set of key-value
pairs and have dynamic schema. Dynamic schema means that documents in the same
collection do not need to have the same set of fields or structure, and common fields in a
collection's documents may hold different types of data.
DBMS Lab Workbook (CSL214) | 65
2022-23
RDBMS MongoDB
Output: Screenshots
DBMS Lab Workbook (CSL214) | 67
2022-23
DBMS Lab Workbook (CSL214) | 68
2022-23
Preparatory Questions
1) tables
2) collections
3) rows
4) all the mentioned
1) JSON
2) XML
3) JScript
4) All of the mentioned
DBMS Lab Workbook (CSL214) | 69
2022-23
1) 2 MB
2) 16 MB
3) 12 MB
4) There is no maximum size. It depends on RAM
Experiment 10
Student Name and Roll Number: Harsh Gandhi 23MCA008
Link to Code:
Date:
Faculty Signature:
Marks:
Objective
Program Outcome
Problem Statement
4) Retrieve the details of all projects that are being run in department number 50.
DBMS Lab Workbook (CSL214) | 71
2022-23
5) Retrieve the details of the first project that is being run in department number 50 in
formatted manner.
6) Return the formatted/structured project details of all projects in department number “5”
and project name as “ProductZ”.
7) Return the project details (including project name, project number and department
number and excluding project location and default primary key) of all projects with
department number “5” or project name as “ProductZ”.
8) Return the formatted/structured project details of all projects with department number
less than “10” and project name as “ProductZ” or project location as “Delhi”.
Background Study
1) use: This command is used to create a database in MongoDB. It will return an existing
database or will create a new database if it dosen’t exist.
Output: Screenshots
1)
2) Employee
DBMS Lab Workbook (CSL214) | 73
2022-23
Department :
Project :
Preparatory Questions
1) find
2) move
3) shell
4) replace
Q2) When you query a collection, MongoDB returns a ________ object that contains the results
of the query.
DBMS Lab Workbook (CSL214) | 74
2022-23
1) row
2) cursor
3) colums
4) none of the mentioned
Q3) Which of the following method is called while accessing documents using the array index
notation ?
1) cur.toArray()
2) cursor.toArray()
3) doc.toArray()
4) all of the mentioned
Q4) The mongo shell and the drivers provide several cursor methods that call on the cursor
returned by the _______ method to modify its behavior.
1) cursor()
2) find()
3) findc()
4) none of the mentioned
1) sort()
2) order()
3) orderby()
4) all of the mentioned
DBMS Lab Workbook (CSL214) | 75
2022-23
Experiment 11
Student Name and Roll Number: Harsh Gandhi 23MCA008
Link to Code:
Date:
Faculty Signature:
Marks:
Objective
Program Outcome
The students will be able to perform advanced CRUD operations in MongoDB.
Problem Statement
Project
Possible Fields:
Project Name (Pname)
Project Number (Pnumber)
Department Number in which project is being run (Dnum)
DBMS Lab Workbook (CSL214) | 76
2022-23
1) Insert multiple documents (2-3) together in the Project collection with atleast 1 project
location as a composite attribute
3) Write a MongoDB query to update the project location of all projects to “Pune” and
department number to “20” with project name “ProjectX”.
4) Write a MongoDB query to delete all the documents of collection “Project” with
department number “4”.
5) Write a MongoDB query to retrieve the project details of all projects with project location
as “Delhi” and “Mumbai” both assuming the following documents exist.
7) Write a MongoDB query to retrieve the project details project name, department
number, department name and department manager of projects with department manager
(for department in which the project is being run) as “ABC”.
Background Study
1) pretty() is used to display the query obtained in a neat manner with all the fields clearly
depicted one below the other.
2) Data for a particular field can also be stored as an array with multiple values for the field.
3) $all is used to match the array items for the particular field.
o db.project.find({Plocation:{$all:["Delhi","Mumbai"]}}).pretty()
4) $elemMatch: This operator matches documents that contain an array field with at least
one element that matches all the specified query criteria.
o Syntax: { <field>: { $elemMatch: { <query1>, <query2>, ... } } }
5) Fields can be embedded in the field too and while using find in such a case, always refer
the field embedded in another field by using a dot notation and in quotes.
6) AND/OR Operations
DBMS Lab Workbook (CSL214) | 78
2022-23
>db.project.find
>db.collection_name.find({$and: ({$and:[{Dnum:5},
AND
[{key1: value1}, {key2:value2}]}) {Pname:"ProductZ“}]}).pretty
();
>db.project.find
>db.collection_name.find({$or: ({$or:[{Dnum:5},
OR
[{key1: value1}, {key2:value2}]}) {Pname:"ProductZ“}]}).pretty
();
7) Documents can either be embedded in one to one relation or one to many relation
o In one to one, the new field contains only a single embedded document.
o In one to many, the new field contains an array of multiple embedded documents.
Output: Screenshots
1. Insert multiple documents (2-3) together in the Project collection with atleast 1
project location as a composite attribute
DBMS Lab Workbook (CSL214) | 79
2022-23
3.Write a MongoDB query to update the project location of all projects to “Pune” and
department number to “20” with project name “ProjectX”.
DBMS Lab Workbook (CSL214) | 80
2022-23
4.Write a MongoDB query to delete all the documents of collection “Project” with
department number “4”.
5.Write a MongoDB query to retrieve the project details of all projects with project
location as “Delhi” and “Mumbai” both assuming the following documents exist.
7. Write a MongoDB query to retrieve the project details project name, department
number, department name and department manager of projects with department manager
(for department in which the project is being run) as “ABC”.
DBMS Lab Workbook (CSL214) | 82
2022-23
Preparatory Questions
Q1) The order of documents returned by a query is not defined unless you specify a ______
1) sortfind()
2) sortelse()
3) sort()
4) none of the mentioned
Q2) In aggregation pipeline, the _______ pipeline stage provides access to MongoDB queries.
1) $catch
2) $match
3) $batch
4) All of the mentioned
Q4) Which of the following query selects documents in the records collection that match the
condition { “user_id”: { $lt: 42 } }?
1) db.records.findOne( { “user_id”: { $lt: 42 } }, { “history”: 0 } )
2) db.records.find( { “user_id”: { $lt: 42 } }, { “history”: 0 } )
3) db.records.findOne( { “user_id”: { $lt: 42 } }, { “history”: 1 } )
4) db.records.select( { “user_id”: { $lt: 42 } }, { “history”: 0 } )
Link to Code:
Date:
Faculty Signature:
Marks:
Objective
Program Outcome
The students will design and implement DBMS concepts on a real-world project.
When you read this article you will find these words.”Entity,
Entity
Weak Entity
Attributes
Multi-valued Attribute
Derived Attribute
Relationships
Recursive Relationship
Cardinality
1. one to one (1 to 1)
2. one to many(1 to N)
3. many to many (M to N)
DBMS Lab Workbook (CSL214) | 88
2022-23
Participation
1. Conceptual design
2. Logical design
3. Physical design
After we found entities from the scenario you should check whether
those entities have sub-entities. If so you have to mark sub-entities in
your diagram.
Total Participation
Partial Participation
If all the members in the superclass participate for only one subclass it
is known as disjoint and denoted by “d”. If all the members in the
superclass participate in more than one subclass it is known
as overlap and denoted by “o”.
DBMS Lab Workbook (CSL214) | 96
2022-23
diagrams.😍
Now let’s move on. Our next topic is map ER and EER diagrams into
relational schemas.
First I’ll tell you how to map the ER diagram into a relational schema.
DBMS Lab Workbook (CSL214) | 97
2022-23
Above it shows an ER diagram with its relationships. You can see there
are two strong entities with relationships and a weak entity with a weak
relationship.
When you going to make a relational schema first you have to identify
all entities with their attributes. You have to write attributes in
DBMS Lab Workbook (CSL214) | 99
2022-23
Then you have to map the primary key to where you took from as
shown below.
There are three occasions where one to one relations take place
according to the participation constraints.
DBMS Lab Workbook (CSL214) | 100
2022-23
When both sides have partial participation you can send any of the
entity’s primary key to others. At the same time, if there are attributes
in the relationship between those two entities, it is also sent to other
entity as shown above.
Here you can see I have written CUSTID and STARTDATE inside
the CARD table. Now you have to map CUSTID from where it comes.
That’s it.🤩
You can see between the relationship and CARD entity it has total
participation.
And there you should write both primary keys of the entities and
attributes in the relationship and map them to the initials as shown
below.
DBMS Lab Workbook (CSL214) | 105
2022-23
If it is unary and one to one or one to many relations you do not need
to make a new relationship you just want to add a new primary key to
the current entity as shown below and map it to the initial. For
example, in the above diagram, the employee is supervised by the
supervisor. Therefore we need to make a new primary key as SID and
map it to EMPID. Because of SID also an EMPID.
Now let us see how to map relations with more than two entities.
DBMS Lab Workbook (CSL214) | 110
2022-23
There are four ways to draw relational schema for an EER. You have to
choose the most suitable one. In the end, I'll give you the guidelines on
how to choose the best and most suitable way.
First method
DBMS Lab Workbook (CSL214) | 112
2022-23
Second method
Here we do not write the superclass entity but in each subclass entity,
we write all attributes that are in superclass entity.
Third method
DBMS Lab Workbook (CSL214) | 113
2022-23
Here we write only the superclass entity and write all the attributes
which belong to subclass entities. Specialty in here is that to identify
that PERSON is an EMPLOYEE or STUDENT we add a column
as PERSONTYPE. After the table creates we can mark as
a STUDENT or EMPLOYEE.
Fourth method