Assignment - 1 Solution
Assignment - 1 Solution
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.
sql
Copy code
CREATE TABLE Employees (
Employee_ID INT,
Name VARCHAR(50),
Age INT CHECK (Age > 0 AND Age <= 150)
);
sql
Copy code
CREATE TABLE Employees (
Employee_ID INT PRIMARY KEY,
Name VARCHAR(50)
);
sql
Copy code
CREATE TABLE Employees (
Employee_ID INT PRIMARY KEY NOT NULL,
Name VARCHAR(50)
);
sql
Copy code
CREATE TABLE Departments (
Department_ID INT PRIMARY KEY,
Department_Name VARCHAR(50)
);
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.
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.
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.
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.
sql
Copy code
SELECT * FROM Employees WHERE Age > 30;
sql
Copy code
SELECT DISTINCT Job_Title FROM Employees;
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.
a. Database Normalization
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.
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
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.
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;
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.
a. SQL Queries:
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;
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.
Relational Algebra is a procedural query language that works on relational databases using
operations like selection, projection, and joins.
2. Relational Calculus
TRC
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
TRC
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.
DRC
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
DRC
This expression retrieves only the Name of employees working in the "IT" department.
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.
e.Department = 'IT' }
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.
A simple query like retrieving all employees older than 30 can be written in both: