DBMS Ece Unit 2
DBMS Ece Unit 2
UNIT -2
In DBMS (Database Management System), keys are used to uniquely identify records
in a table or establish relationships between tables.
Here are some basic types of keys:
1. Primary Key: A unique identifier for each record in a table. It cannot be null.
2. Foreign Key: A key used to link two tables. It refers to the primary key in
another table.
3. Candidate Key: A set of attributes that can uniquely identify a record in a
table. One of these is selected as the primary key.
4. Alternate Key: A candidate key that is not chosen as the primary key.
5. Composite Key: A key that consists of two or more columns to uniquely
identify a record.
6. Super Key is a set of one or more attributes (columns) in a table that can
uniquely identify each record (row) in that table. It can include additional
attributes beyond what is strictly necessary for uniqueness.
1. Primary Key
A Primary Key uniquely identifies each record in a table. It cannot have duplicate or
NULL values.
Example: Consider a table called Students:
StudentID Name Age Gender
1 Alice 20 Female
2 Bob 22 Male
3 Charlie 21 Male
Here, StudentID can be the Primary Key because it uniquely identifies each student.
2. Foreign Key
A Foreign Key is a field (or a set of fields) in one table that uniquely identifies a row
of another table.
Example: Consider two tables, Orders and Customers:
Customers Table: Orders Table:
CustomerID Name Email
OrderID CustomerID Product Quantity
1 Alice [email protected]
101 1 Laptop 2
2 Bob [email protected]
102 2 Smartphone 1
Here, CustomerID in the Orders table
is a Foreign Key that refers to the CustomerID in the Customers table.
A Candidate Key is a set of one or more columns that can uniquely identify each
record in a table. The table may have more than one candidate key.
Example: Consider a Users table:
UserID Email Username
1 [email protected] alice_123
2 [email protected] bob_456
Both UserID and Email are candidate keys, as they can each uniquely identify a
record.
4. Alternate Key
An Alternate Key is a candidate key that was not chosen as the primary key.
Example: In the Users table above, if we choose UserID as the Primary Key, then
Email becomes an Alternate Key.
5. Composite Key
A Composite Key is made up of two or more columns that together uniquely identify
a record.
Example: Consider a Course_Enrollments table:
StudentID CourseID EnrollmentDate
1 101 2025-01-10
2 102 2025-01-09
In this case, both StudentID and CourseID together form a Composite Key because
neither field alone is sufficient to uniquely identify a record.
These keys help structure and relate the data efficiently in relational databases.
6.Super Key:
In this table:
In these cases, the Super Keys contain more attributes than necessary for uniqueness,
but they still ensure that each row is unique.
Relational Algebra
Relational Algebra is a mathematical system used to query and manipulate data in a
relational database. It is a collection of operations that work on one or more relations
(tables) to produce a result. The operations in relational algebra are used to retrieve, insert,
update, and delete data from a database.
1. Selection (σ):
o This operation is used to retrieve rows from a relation that satisfy a given
condition.
o Syntax: σ(condition)(Relation)
o Example: Retrieve students who are 20 years old from the Students table:
σ(Age = 20)(Students)
2. Projection (π):
o This operation is used to retrieve specific columns (attributes) from a relation.
o Syntax: π(column1, column2, ...)(Relation)
o Example: Retrieve only the Name and Age of all students:
π(Name, Age)(Students)
3. Union (∪):
o This operation combines the results of two relations and removes duplicates.
Both relations must have the same attributes (columns).
o Syntax: Relation1 ∪ Relation2
o Example: Combine two lists of students from different departments:
Students_CS ∪ Students_Maths
Students_Maths − Students_CS
Students × Courses
6. Rename (ρ):
o This operation is used to rename the attributes (columns) of a relation.
o Syntax: ρ(new_name)(Relation)
o Example: Rename the StudentID column to ID in the Students table:
ρ(ID/StudentID)(Students)
Advanced Operations:
1. Join (⨝):
o This operation combines related tuples from two relations based on a condition
(usually equality on a common attribute).
o Syntax: Relation1 ⨝ Condition Relation2
o Example: Join Students and Enrollments based on StudentID:
2. Intersection (∩):
o This operation returns the common rows between two relations.
o Syntax: Relation1 ∩ Relation2
o Example: Find students who are in both CS and Maths:
Students_CS ∩ Students_Maths
σ(Department = 'CS')(Students)
σ(CourseName = 'AI')(Courses)
Relational algebra provides a theoretical foundation for SQL and helps in understanding
and optimizing database queries.
Solution:
To provide expressions in Tuple Relational Calculus (TRC) for the given queries, let's first define the
schemas:
i) ΠA1(r)
In Tuple Relational Calculus (TRC), the projection of a specific attribute is represented by selecting that
attribute from the tuples of the relation.
TRC Expression:
{ t.A | t ∈ r }
ii) σp=19(r)
TRC Expression:
{ t | t ∈ r ∧ t.A = 19 }
The Cartesian product of relations r and s means pairing each tuple from r with each tuple from s.
TRC Expression:
{ (t, u) | t ∈ r ∧ u ∈ s }
SQL Query: SELECT * FROM r CROSS JOIN s;
This means that for each tuple t from relation r and each tuple u from relation s, we return the pair (t,u).
This refers to projection on attributes A and F, after performing a selection on the Cartesian product of r
and s where C=D.
First, we perform a selection where the attribute C from rrr is equal to attribute D from s. After that, we
project only the attributes A (from r) and F (from s).
TRC Expression:
Domain Relational Calculus: DRC is another formal query language used to describe the
queries on a relational database. Unlike Tuple Relational Calculus (TRC), which works
In DRC, a query specifies the values of the attributes (domains) for which we want to find
tuples. A query in DRC is expressed as a formula that specifies conditions on the values of
attributes, and the result consists of those values that satisfy the conditions.
VIEW:
a view is a virtual table that is based on the result of a query. It does not store data
physically, but instead, it displays data derived from one or more tables. Views are used to
simplify complex queries, present a specific subset of data, or protect data by restricting
access to certain parts of a database.
Examples: We can create a view to display only the Name and Department:
1. Creating View:
Types of Joins:
1. Inner Join
2. Left (Outer) Join
3. Right (Outer) Join
4. Full (Outer) Join
5. Cross Join
6. Self Join
Each of these joins serves a different purpose and has its own behaviour when it comes to
combining rows from the involved tables.
1. Inner Join
Inner Join returns only the rows where there is a match in both tables.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Example:
Consider the following two tables:
Employees: Departments:
To get a list of employees and their department names, we can perform an inner join on
DepartmentID.
Result:
Name DepartmentName
Alice HR
Bob IT
Explanation:
The query returns only those rows where there is a matching DepartmentID in both
the Employees and Departments tables.
Carol is excluded because her DepartmentID (103) does not exist in the
Departments table.
A Left Join (or Left Outer Join) returns all the rows from the left table (the first table),
and the matched rows from the right table. If no match is found, NULL values are returned
for columns from the right table.
Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
Example:
Using the same Employees and Departments tables:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Result:
Name DepartmentName
Alice HR
Bob IT
Carol NULL
The query returns all employees, including Carol, whose DepartmentID does not
have a match in the Departments table.
For Carol, the DepartmentName is NULL because there is no matching row in the
Departments table.
A Right Join (or Right Outer Join) returns all the rows from the right table (the second
table), and the matched rows from the left table. If no match is found, NULL values are
returned for columns from the left table.
Syntax:
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
Example:
Again, using the same tables:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Result:
Name DepartmentName
Alice HR
Bob IT
NULL Marketing
Explanation:
The query returns all departments, including Marketing, which does not have any
matching employees.
For the Marketing department, there is no matching employee, so the Name is
NULL.
A Full Join (or Full Outer Join) returns all rows from both the left and right tables. If
there is no match, NULL values are returned for columns from the table that lacks the
matching row.
Notes Prepared by: L Suresh Asst.Prof.(CSE),CJITS
Syntax:
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
Example:
Result:
Name DepartmentName
Alice HR
Bob IT
Carol NULL
NULL Marketing
Explanation:
5. Cross Join
A Cross Join returns the Cartesian product of both tables, meaning it combines every row
from the first table with every row from the second table. This can result in a very large
number of rows.
Syntax:
SELECT columns
FROM table1
CROSS JOIN table2;
Result:
Name DepartmentName
Alice HR
Alice IT
Alice Marketing
Bob HR
Bob IT
Bob Marketing
Carol HR
Carol IT
Carol Marketing
Explanation:
The query returns every possible combination of rows between the Employees and
Departments tables (i.e., each employee is paired with every department).
6. Self Join
A Self Join is a join where a table is joined with itself. This is useful for finding
relationships within the same table, like finding pairs of employees in the same department.
Syntax:
SELECT columns
FROM table1 t1
Notes Prepared by: L Suresh Asst.Prof.(CSE),CJITS
JOIN table1 t2
ON t1.column = t2.column;
Example:
Assume the Employees table also has a ManagerID column to indicate which employee
manages another employee.
Employees:
2 Bob 101 1
3 Carol 102 2
Result:
Employee Manager
Bob Alice
Carol Bob
Explanation:
The query performs a self join on the Employees table to find each employee and
their manager by matching the ManagerID in one row to the EmployeeID in another
row.