DBMS Answer Key
DBMS Answer Key
Q1. Explain the three-tier architecture of Data base Management System. (4)
Ans:
The presentation tier is the user interface and communication layer of the application,
where the end user interacts with the application.
Its main purpose is to display information to and collect information from the user.
This top-level tier can run on a web browser, as desktop application, or a graphical user
interface (GUI), for example. Web presentation tiers are usually developed using HTML,
CSS and JavaScript.
Desktop applications can be written in a variety of languages depending on the platform.
Application tier
The application tier, also known as the logic tier or middle tier, is the heart of the
application.
In this tier, information collected in the presentation tier is processed - sometimes against
other information in the data tier - using business logic, a specific set of business rules.
The application tier can also add, delete or modify data in the data tier.
The application tier is typically developed using Python, Java, Perl, PHP or Ruby, and
communicates with the data tier using API calls.
Data tier
The data tier, sometimes called database tier, data access tier or back-end, is where the
information processed by the application is stored and managed.
This can be a relational database management system such as PostgreSQL, MySQL,
MariaDB, Oracle, DB2, Informix or Microsoft SQL Server, or in a NoSQL Database server
such as Cassandra, CouchDB or MongoDB.
In a three-tier application, all communication goes through the application tier. The presentation
tier and the data tier cannot communicate directly with one another.
Q2.A company called M/s ABC Consultants Ltd. Has an entity EMPLOYEE with
a number of employees having attributes such as EMP-ID, EMP-NAME, EMP-
ADD and EMP-BDATE. The company has another entity PROJECT that has
several projects having attributes such as PROJ-ID, PROJ-NAME and START-
DATE. Each employee may be assigned to one or more projects or may not be
assigned to one or more projects. A project must have at least one employee
assigned and may have any number of employees assigned. An employee’s
billing rate may vary by project, and the company wishes to record the
applicable billing rate (BILL-RATE) for each employee when assigned to a
particular project.
By making additional assumptions, if so required, draw an E-R diagram for the
above situation. (6)
Ans:
1.
3.
BILL_DATE(EMP_ID,PROJ_ID,PROJ_COST)
5. Searching for a record is 5. Searching for a record 5. A unique, indexed key field is
very difficult since one can is easy since there are used to search for a data element.
retrieve a child only after multiple access paths to
going through its parent a data element.
record.
EER Model
EER is a high-level data model that incorporates the extensions to the original ER model.
Sub class and Super class relationship leads the concept of Inheritance.
The relationship between sub class and super class is denoted with symbol.
1. Super Class
Super class is an entity type that has a relationship with one or more subtypes.
An entity cannot exist in database merely by being member of any super class.
For example: Shape super class is having sub groups as Square, Circle, Triangle.
2. Sub Class
1. Generalization
In the above example, Tiger, Lion, Elephant can all be generalized as Animals.
2. Specialization
Specialization is a process that defines a group entities which is divided into sub
groups based on their characteristic.
It is a top down approach, in which one higher entity can be broken down into two
lower level entity.
It maximizes the difference between the members of an entity by identifying the
unique characteristic or attributes of each member.
It defines one or more sub class for the super class and also forms the
superclass/subclass relationship.
For example
3. Aggregation
In the above example, the relation between College and Course is acting as an Entity in
Relation with Student.
Q5. Define the following terms:
(a) DBMS
(b) Data Independence
Ans:
a. Database management systems are software systems used to manage and
manipulate data in a database. As most application performance issues originate in
the database, knowing how to monitor and optimize your database is essential to
your operations.
Data Independence
o Data independence can be explained using the three-schema architecture.
o Data independence refers characteristic of being able to modify the schema at one level of the
database system without altering the schema at the next higher level.
Backup and It doesn’t provide backup and recovery of It provides backup and recovery
Recovery data if it is lost. of data even if it is lost.
Query There is no efficient query processing in the Efficient query processing is there
processing file system. in DBMS.
The user has to write procedures for The user not required to write
Meaning managing databases procedures.
Data is distributed in many files. So, not easy Due to centralized nature sharing
Sharing to share data is easy
Data It give details of storage and representation of It hides the internal details of
Abstraction data Database
Referential integrity
Referential integrity requires that a foreign key must have a matching primary key
or it must be null. This constraint is specified between two tables (parent and
child); it maintains the correspondence between rows in these tables. It means the
reference from a row in one table to another table must be valid.
The referential integrity constraint states that the customer ID (CustID) in the
Order table must match a valid CustID in the Customer table. Most relational
databases have declarative referential integrity. In other words, when the tables
are created the referential integrity constraints are set up.
The referential integrity constraint states that CrsCode in the Class table must
match a valid CrsCode in the Course table. In this situation, it’s not enough that the
CrsCode and Section in the Class table make up the PK, we must also enforce
referential integrity.
Q10. What is a relation? What are primary, candidate and foreign keys? (4)
Ans:
A relationship in a DBMS is primarily the way two or more data sets are linked. This is so true
for Relational Database Management Systems. One dataset may be then termed as the
foreign key and the ones linked to it may be termed the Primary Key. There may be multiple
Foreign and Primary keys linked to each other.
Relationships allow the datasets to share and store the data in separate tables. They also help
link disparate data with each other.
1. Primary key
o It is the first key used to identify one and only one instance of an entity uniquely. An entity can contain
multiple keys, as we saw in the PERSON table. The key which is most suitable from those lists becomes
a primary key.
o In the EMPLOYEE table, ID can be the primary key since it is unique for each employee. In the
EMPLOYEE table, we can even select License_Number and Passport_Number as primary keys since
they are also unique.
o For each entity, the primary key selection is based on requirements and developers.
2. Candidate key
o A candidate key is an attribute or set of attributes that can uniquely identify a tuple.
o Except for the primary key, the remaining attributes are considered a candidate key. The candidate
keys are as strong as the primary key.
For example: In the EMPLOYEE table, id is best suited for the primary key. The rest of the attributes,
like SSN, Passport_Number, License_Number, etc., are considered a candidate key.
3.Foreign key
o Foreign keys are the column of the table used to point to the primary key of another table.
o Every employee works in a specific department in a company, and employee and department are two
different entities. So we can't store the department's information in the employee table. That's why
we link these two tables through the primary key of one table.
o We add the primary key of the DEPARTMENT table, Department_Id, as a new attribute in the
EMPLOYEE table.
o In the EMPLOYEE table, Department_Id is the foreign key, and both the tables are related.
Q11. Consider the following relational schema (6)
Account (account-number, branch-name, balance)
Loan (Loan-number, branch-name, balance)
Depositor (Customer-name, Account-number)
Borrower (Customer-name, Loan-number)
Formulate the relational algebra statements for the following (Assume keys
and additional information (as per requirement):
(a) Find all loan numbers for loan made at Bombay branch.
(b) Find all customers who have both a loan and an account at the bank.
(c) Find the average account balance at each branch.
(d) Find the number of depositors at each branch.
Q12. Describe the Join Operation. What does it accomplish? (3)
JOINS in SQL are commands which are used to combine rows from two or more tables, based
on a related column between those tables. There are predominantly used when a user is trying
to extract data from tables which have one-to-many or many-to-many relationships between
them.
Now, that you know what joins mean, let us next learn the different types of joins.
INNER JOIN
FULL JOIN
LEFT JOIN
RIGHT JOIN
Employee Table:
Project Table:
Client Table:
INNER JOIN
This type of join returns those records which have matching values in both tables. So, if you
perform an INNER join operation between the Employee table and the Projects table, all the
tuples which have matching values in both the tables will be given as output.
Syntax:
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
INNER JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;
NOTE: You can either use the keyword INNER JOIN or JOIN to perform this operation.
Example:
1 SELECT Employee.EmpID, Employee.EmpFname, Employee.EmpLname, Projects.ProjectID,
Projects.ProjectName
2 FROM Employee
3 INNER JOIN Projects ON Employee.EmpID=Projects.EmpID;
Output:
EmpID EmpFname EmpLname ProjectID ProjectName
FULL JOIN
Full Join or the Full Outer Join returns all those records which either have a match in the
left(Table1) or the right(Table2) table.
Syntax:
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
FULL JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;
Example:
1 SELECT Employee.EmpFname, Employee.EmpLname, Projects.ProjectID
2 FROM Employee
4 ON Employee.EmpID = Projects.EmpID;
Output:
EmpFname EmpLname ProjectID
LEFT JOIN
The LEFT JOIN or the LEFT OUTER JOIN returns all the records from the left table and also those
records which satisfy a condition from the right table. Also, for the records having no matching
values in the right table, the output or the result-set will contain the NULL values.
Syntax:
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
LEFT JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;
Example:
1 SELECT Employee.EmpFname, Employee.EmpLname, Projects.ProjectID,
Projects.ProjectName
2
FROM Employee
3 LEFT JOIN
4 ON Employee.EmpID = Projects.EmpID ;
Output:
EmpFname EmpLname ProjectID ProjectName
RIGHT JOIN
The RIGHT JOIN or the RIGHT OUTER JOIN returns all the records from the right table and also
those records which satisfy a condition from the left table. Also, for the records having no
matching values in the left table, the output or the result-set will contain the NULL values.
Syntax:
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
RIGHT JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;
Example:
1 SELECT Employee.EmpFname, Employee.EmpLname, Projects.ProjectID,
Projects.ProjectName
2
FROM Employee
3 RIGHT JOIN
4 ON Employee.EmpID = Projects.EmpID;
Output:
EmpFname EmpLname ProjectID ProjectName
Now, let us move forward with our next section in this article i.e. the top questions asked
about SQL Joins in your interviews.
Solution:
A Natural Join is also a Join operation that is used to give you an output based on the columns
in both the tables between which, this join operation must be implemented. To understand the
situations n which natural join is used, you need to understand the difference between Natural
Join and Inner Join.
The main difference the Natural Join and the Inner Join relies on the number of columns
returned. Refer below for example.
Now, if you apply INNER JOIN on these 2 tables, you will see an output as below:
If you apply NATURAL JOIN, on the above two tables, the output will be as below:
From the above example, you can clearly see that the number of columns returned from the
Inner Join is more than that of the number of columns returned from Natural Join. So, if you
wish to get an output, with less number of columns, then you can use Natural Join
Employees
Awards
id employee_id award_date
1 1 2022-04-01
2 3 2022-05-01
Example 1: IN
Output
id name
2 Perice Mundford
4 Garwood Saffen
5 Faydra Beaves
Example 2: NOT IN
Output
id name
1 Augustine Hammond
3 Cassy Delafoy
Example 3: ALL
Select all Developers who earn more than all the Managers
SELECT * FROM employees
WHERE role = 'Developer'
AND salary > ALL (
SELECT salary FROM employees WHERE role = 'Manager'
);
Output
Explanation
The developer with id 5 earns (50000) more than all the managers: 2 (10000) and 4 (40000)
Example 4: ANY
Output
Explanation
The developer with id 3 earns (30000) more than the manager with id 2 (10000)
The developer with id 5 earns (50000) more than the managers with id 2 (10000) and 4 (40000)
Select all employees whose salary is above the average salary of employees in their role.
Output
Explanation
The manager with id 4 earns more than the average salary of all managers (25000), and the developer with
id 5 earns more than the average salary of all developers (30000). The inner query is executed for all rows
fetched by the outer query. The role value (emp1.role) of every outer query's row is used by the inner query
(emp1.role = emp2.role).
We can find the average salary of managers and developers using the below query:
Developer 30000
Manager 25000
ACID stands for Atomicity, Consisency, Isolation, and Durbility property. Multiple users can share
data in a safe and secure way using these properties.
Atomicity:
Atomicity is based on the principle of “either all or nothing” which means that if any updates happens
inside the database then that updates should be available for others beyond user and application.
If any updates happens inside the database then that updates should not be available for others
beyond user and application.
Consistency:
Inside a database before or after any transaction takes place this ensures that consistency is
maintained.
Isolation:
This property states that happens in isolation with other which means that any transactions that has
started but not yet completed should be in isolation with others so that the other transactions are not
affected by this transaction.
Durability:
This property states that the data should always be in a durable state i.e. any data which is in the
committed state should be available in the same state even if any failure or restart occurs in the system
Q18. Define a view in SQL. Show with suitable example that insertion of tuple
through view may not be possible.
Q19. Explain with suitable example the creation and modification of a
relational schema in SQL.
Creating a basic table involves naming the table and defining its columns and each column's data
type.
The SQL CREATE TABLE statement is used to create a new table.
Syntax
The basic syntax of the CREATE TABLE statement is as follows −
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
CREATE TABLE is the keyword telling the database system what you want to do. In this case, you
want to create a new table. The unique name or identifier for the table follows the CREATE TABLE
statement.
Then in brackets comes the list defining each column in the table and what sort of data type it is.
The syntax becomes clearer with the following example.
A copy of an existing table can be created using a combination of the CREATE TABLE statement
and the SELECT statement. You can check the complete details at Create Table Using another
Table.
Modification:
ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used
to add and drop various constraints on the existing table.
ALTER TABLE – ADD
ADD is used to add columns into the existing table. Sometimes we may require to add additional
information, in that case we do not require to create the whole database again, ADD comes to our
rescue.
Syntax:
ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,
…
Columnname_n datatype);
ALTER TABLE-MODIFY
It is used to modify the existing columns in a table. Multiple columns can also be modified at once.
*Syntax may vary slightly in different databases.
Syntax(Oracle,MySQL,MariaDB):
ALTER TABLE table_name
MODIFY column_name column_type;
Syntax(SQL Server):
ALTER TABLE table_name
ALTER COLUMN column_name column_type;
Queries
Sample Table:
Student
ROLL_NONAME
1 Ram
2 Abhi
3 Rahul
4 Tanu
QUERY:
To ADD 2 columns AGE and COURSE to table Student.
ALTER TABLE Student ADD (AGE number(3),COURSE varchar(40));
OUTPUT:
ROLL_NONAME AGECOURSE
1 Ram
2 Abhi
3 Rahul
4 Tanu
2 Abhi
3 Rahul
4 Tanu