0% found this document useful (0 votes)
32 views9 pages

Assignment - 1 Solution

The document provides solutions for a DBMS assignment covering integrity constraints, querying relational data, database normalization, views, and relational algebra and calculus. It explains various integrity constraints such as domain, key, entity, and referential integrity, along with examples and their significance. Additionally, it discusses SQL queries, normalization processes, the creation and implications of views, and the differences between relational algebra and relational calculus.

Uploaded by

neeraj.sahu1202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views9 pages

Assignment - 1 Solution

The document provides solutions for a DBMS assignment covering integrity constraints, querying relational data, database normalization, views, and relational algebra and calculus. It explains various integrity constraints such as domain, key, entity, and referential integrity, along with examples and their significance. Additionally, it discusses SQL queries, normalization processes, the creation and implications of views, and the differences between relational algebra and relational calculus.

Uploaded by

neeraj.sahu1202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Here are the solutions for the problems listed in your DBMS assignment:

UNIT I: Relational Model


1. Integrity Constraints Over Relations

a. Define and explain the following integrity constraints:

 Domain Constraints: These ensure that every attribute in a relation follows a specific
domain (set of allowed values). For example, an age field must only contain integers
between 0 and 150.
 Key Constraints: These ensure uniqueness in a relation. The primary key is an
example, where no two rows can have the same value for the primary key column.
 Entity Integrity Constraints: This ensures that the primary key of any entity cannot
have NULL values. Every entity must have a unique identifier.
 Referential Integrity Constraints: These ensure that a foreign key value in a child
table always refers to a valid primary key in the parent table. If a foreign key
references a primary key in another table, that referenced row must exist.

b. Examples of Integrity Constraints

 Domain Constraint Example:

sql
Copy code
CREATE TABLE Employees (
Employee_ID INT,
Name VARCHAR(50),
Age INT CHECK (Age > 0 AND Age <= 150)
);

 Key Constraint Example:

sql
Copy code
CREATE TABLE Employees (
Employee_ID INT PRIMARY KEY,
Name VARCHAR(50)
);

 Entity Integrity Example:

sql
Copy code
CREATE TABLE Employees (
Employee_ID INT PRIMARY KEY NOT NULL,
Name VARCHAR(50)
);

 Referential Integrity Example:

sql
Copy code
CREATE TABLE Departments (
Department_ID INT PRIMARY KEY,
Department_Name VARCHAR(50)
);

CREATE TABLE Employees (


Employee_ID INT PRIMARY KEY,
Name VARCHAR(50),
Department_ID INT,
FOREIGN KEY (Department_ID) REFERENCES Departments(Department_ID)
);

c. Significance of Referential Integrity

Referential integrity ensures that relationships between tables are consistent. For example, if
an employee is assigned a department, that department must exist in the department table. If
an invalid department ID is assigned, it could lead to data inconsistency.

2. Enforcing Integrity Constraints

a. Enforcing Integrity in DBMS

In most DBMSs, integrity constraints are automatically enforced whenever data is inserted,
updated, or deleted. The DBMS checks whether the data adheres to the defined constraints,
and if not, it throws an error.

b. Comparison of Integrity Enforcement in MySQL and PostgreSQL

 MySQL: Constraints like foreign keys and check constraints are enforced using SQL
syntax. However, older versions of MySQL did not support the CHECK constraint,
though it is available in recent versions.
 PostgreSQL: PostgreSQL supports a wide variety of constraints (foreign keys, check
constraints, unique constraints) and enforces them rigorously.

c. Consequences of Not Enforcing Integrity Constraints

Without integrity constraints, data anomalies such as duplicate rows, incorrect foreign key
references, or invalid data types could occur. For example, without referential integrity, an
employee could reference a non-existent department, leading to confusion when retrieving
data.

3. Querying Relational Data


a. SQL Queries:

 Query 1: Select all records where age is greater than 30:

sql
Copy code
SELECT * FROM Employees WHERE Age > 30;

 Query 2: Retrieve all distinct job titles:

sql
Copy code
SELECT DISTINCT Job_Title FROM Employees;

 Query 3: Inner join between employees and departments:

sql
Copy code
SELECT Employees.Name, Departments.Department_Name
FROM Employees
INNER JOIN Departments
ON Employees.Department_ID = Departments.Department_ID;

b. Explanation of JOIN:

A JOIN combines rows from two or more tables based on a related column. Different types
of joins:

 INNER JOIN: Only returns rows where there is a match in both tables.
 LEFT JOIN: Returns all rows from the left table, with matching rows from the right
table. If there’s no match, NULLs are returned from the right table.
 RIGHT JOIN: Returns all rows from the right table, with matching rows from the
left.
 FULL OUTER JOIN: Returns all rows when there is a match in one of the tables.

4. Logical Database Design

a. Database Normalization

Normalization is the process of organizing a database to reduce redundancy and dependency.


It involves dividing large tables into smaller tables and defining relationships between them.

b. Normalization of Given Relation

Original Relation:

Copy code
Employee_ID | Employee_Name | Department | Project_ID | Project_Name |
Hours_Worked
 1NF: Eliminate repeating groups, ensuring that each column contains atomic values.
 2NF: Remove partial dependencies, ensuring all non-key attributes depend on the
whole primary key.
 3NF: Remove transitive dependencies, ensuring no non-key attribute depends on
another non-key attribute.

Final normalized tables:

1. Employees:

Copy code
Employee_ID | Employee_Name | Department

2. Projects:

Copy code
Project_ID | Project_Name

3. Work_Hours:

Copy code
Employee_ID | Project_ID | Hours_Worked

c. Trade-offs between Normalization and Denormalization

 Normalization reduces redundancy and improves data integrity but can result in
slower queries due to the need for multiple table joins.
 Denormalization improves performance by reducing the need for joins but can lead
to data redundancy and inconsistency.

5. Introduction to Views

a. Definition of View

A view is a virtual table that provides a simplified representation of data from one or more
tables. It does not store data but queries the underlying tables whenever accessed.

b. SQL to Create a View:

sql
Copy code
CREATE VIEW Employee_Department_View AS
SELECT Employees.Name, Departments.Department_Name
FROM Employees
INNER JOIN Departments
ON Employees.Department_ID = Departments.Department_ID;

c. Advantages and Disadvantages of Views:

 Advantages:
o Provides data abstraction.
o Simplifies complex queries.
o Enhances security by restricting access to specific columns.
 Disadvantages:
o Performance overhead as views are not stored.
o Cannot always modify data through views.

6. Destroying/Altering Tables and Views

a. SQL Queries:

 Add a new column:

sql
Copy code
ALTER TABLE Employees ADD Email VARCHAR(50);

 Drop a column:

sql
Copy code
ALTER TABLE Employees DROP COLUMN Email;

 Drop a view:

sql
Copy code
DROP VIEW Employee_Department_View;

b. Implications of Dropping Tables or Views:

Dropping a table permanently deletes all its data. Dropping a view only deletes the view
definition without affecting underlying tables. It should be done carefully to avoid accidental
data loss.

UNIT II: Relational Algebra and Relational Calculus


1. Relational Algebra

a. Relational Algebra Definition

Relational Algebra is a procedural query language that works on relational databases using
operations like selection, projection, and joins.

b. Relational Algebra Queries

Given the relations:


 Students: (Student_ID, Name, Age)
 Courses: (Course_ID, Course_Name)
 Enrollments: (Student_ID, Course_ID)

1. Select all students older than 20: σ Age > 20 (Students)


2. Project student names and course names: π Name, Course_Name (Students ⨝
Enrollments ⨝ Courses)

'Mathematics' (Students ⨝ Enrollments ⨝ Courses))


3. Find names of students enrolled in "Mathematics": π Name (σ Course_Name =

c. Explanation of Relational Algebra Operations:

 Selection (σ): Filters rows based on a condition.


 Projection (π): Selects specific columns.
 Set Operations: Combines two relations (e.g., Union, Intersection).
 Join: Combines two relations based on a related attribute.
 Division: Used to find records in one relation that are associated with all records in
another relation.

2. Relational Calculus

Tuple Relational Calculus (TRC) Expressions

Given the Employees relation:

Employee_ID Name Salary Department

Query 1: Retrieve all employees earning more than $50,000

The TRC expression for this query is:

TRC

{ <e.Name, e.Salary, e.Department> | e ∈ Employees ∧ e.Salary > 50000 }


Copy code

This expression retrieves the Name, Salary, and Department of all employees in the
Employees relation where the Salary is greater than 50,000.

Query 2: Retrieve the names of all employees working in the "IT" department

The TRC expression for this query is:

TRC

{ <e.Name> | e ∈ Employees ∧ e.Department = 'IT' }


Copy code

This expression retrieves only the Name of employees who work in the "IT" department.
Domain Relational Calculus (DRC) Expressions

Domain Relational Calculus works on individual fields (or domains) rather than tuples. Here,
we'll define variables for each column in the Employees relation.

Query 1: Retrieve all employees earning more than $50,000

The DRC expression for this query is:

DRC

{ <n, s, d> | ∃ e1, e2, e3 (e1 = n ∧ e2 = s ∧ e3 = d ∧ Employees(e1, e2,


Copy code

e3, d) ∧ s > 50000) }

Here, we define n for Name, s for Salary, and d for Department, and we retrieve records
where Salary is greater than 50,000.

Query 2: Retrieve the names of all employees working in the "IT" department

The DRC expression for this query is:

DRC

{ <n> | ∃ e1, e2, e3 (e1 = n ∧ Employees(e1, e2, e3, 'IT')) }


Copy code

This expression retrieves only the Name of employees working in the "IT" department.

Comparison of TRC and DRC

1. Usability:

 TRC: Easier to read and write because it works with tuples directly, making it more
intuitive for complex queries. You query on a set of tuples rather than individual
domain values.
 DRC: Operates on individual fields, requiring more variables to express the same
query. For simple queries, this can be easy, but for complex queries, DRC becomes
more cumbersome due to the need to define domain variables.

2. Expressiveness:

 TRC and DRC are equivalent in terms of expressive power. Any query expressed in
TRC can also be expressed in DRC and vice versa. Both are declarative and allow
querying based on the properties of data rather than describing how to retrieve it.

3. Efficiency:
 TRC: Since it's more intuitive and operates on tuples, it's often preferred when
working with relations directly.
 DRC: While also expressive, it can require more effort to maintain, especially for
complex queries with multiple conditions across different domains. However, it can
be more suitable in scenarios where precise control over individual attribute values is
needed.

a. Difference between Relational Algebra and Relational Calculus

 Relational Algebra: Procedural; describes how to retrieve data.


 Relational Calculus: Non-procedural; specifies what data to retrieve without
describing how.

b. Tuple Relational Calculus (TRC)

Given the Employees relation:

1. Employees earning more than $50,000: { e | e ∈ Employees ∧ e.Salary >

2. Names of employees in the IT department: { e.Name | e ∈ Employees ∧


50000 }

e.Department = 'IT' }

c. Domain Relational Calculus (DRC)

1. Employees earning more than $50,000: {<Name, Salary, Department> | ∃e (e


∈ Employees ∧ Salary > 50000)}
2. Names of employees in the IT department: {<Name> | ∃e (e ∈ Employees ∧
Department = 'IT')}

3. Expressive Power of Relational Algebra and Relational Calculus

a. Expressive Power

Both relational algebra and relational calculus are equivalent in expressive power, meaning
any query expressible in one can also be expressed in the other.

b. Example of Expressive Power

A simple query like retrieving all employees older than 30 can be written in both:

Relational Algebra: σ Age > 30 (Employees)


Relational Calculus: { e | e ∈ Employees ∧ e.Age > 30 }

c. Strengths and Limitations

 Relational Algebra: Easier for optimization but more procedural.


 Relational Calculus: More declarative, making it easier to express complex queries,
but less intuitive for query optimization.

You might also like