AL-502 DBMS Unit 2
AL-502 DBMS Unit 2
Syllabus
UNIT II: Relational Data models: Domains, Tuples, Attributes, Relations, Characteristics of
relations, Keys, Key attributes of relation, Relational database, Schemas, Integrity constraints.
Referential integrity, Intension and Extension, Relational Query languages: SQLDDL, DML,
integrity con straints, Complex queries, various joins, indexing, triggers, assertions , Relational
algebra and relational calculus, Relational algebra operations like select, Project ,Join,
Division, outer union. Types of relational calculus i.e. Tuple oriented and domain oriented
relational calculus and its operations.
The Relational Data Model is one of the most widely used data models in Database
Management Systems (DBMS). Introduced by Edgar F. Codd in 1970, it organizes data into
tables (also known as relations) consisting of rows and columns. This model emphasizes data
accessibility and flexibility, making it foundational for modern databases. Relational model
makes the query much easier than in hierarchical or network database systems. In 1970, E.F
Codd has been developed it. A relational database is defined as a group of independent tables
which are linked to each other using some common fields of each related table. This model can
be represented as a table with columns and rows. Each row is known as a tuple. Each table of
the column has a name or attribute. It is well knows in database technology because it is usually
used to represent real-world objects and the relationships between them. Some popular
relational databases are used nowadays like Oracle, Sybase, DB2, MySQL Server etc.
Relation Table
Let's explain each term one by one in detail with the help of example:
Insert operation: It is used to insert a new record in the table. Adding new records to the table
is much easier than other models. Data values will not be found in a relation when the following
condition occurs:
o If we try to insert a duplicate value for the field that is selected as a primary key.
o If we insert a NULL value in the attribute that contains primary key.
o If we try to enter a data value in the foreign key attribute that does not exist in
corresponding primary key attribute.
o If an attribute is assigned a value that does not exist in the corresponding domain.
DELETE operation: This operation is used to delete records from the table but problems arise
when the rows to be deleted have some attributes which are foreign key attributes.
Update operation: It is used to modify or change the data value of a record in a table. Updating
an attribute that is neither a primary key nor a foreign key requires only checking that the new
value is of the correct data type and domain. If we modify a data value of a primary key and
foreign key attribute then need to check:
o The modified value does not contain the value of the corresponding foreign key value.
o The new values must not already exist in the table.
Retrieval operation: It is used to save a record from a relation. This operation is very simple
and homogeneous.
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
1. Domain
Definition: A domain is the set of allowable values for one or more attributes.
Examples:
o Domain for "age" could be integers between 0 to 150.
o Domain for "color" could be {red, blue, green}.
Purpose: Restricts and validates the values entered into a database.
Types of Domains: Numeric, alphanumeric, date, etc.
2. Tuple
Definition: A tuple represents a single row in a table, which contains data for each
attribute.
Example:
o In an Employee table, a tuple might be (1, John, 30, IT).
Components: A tuple consists of multiple attribute values.
Uniqueness: Each tuple in a relation is unique.
3. Attribute
Definition: An attribute is a column in a table that represents a property of the entity.
Example:
o In a "Student" table, attributes could be "Student_ID," "Name," "Age," and
"Department."
Types:
o Primary Key: Uniquely identifies a tuple.
o Foreign Key: Refers to the primary key of another table.
Properties: An attribute has a name, domain, and may have constraints (e.g., NOT
NULL, UNIQUE).
4. Relationship
Definition: A relationship defines how entities are related to each other in a database.
Example:
o "Teacher" and "Class" can have a relationship like "teaches."
Types of Relationships:
o One-to-One: A record in one table is linked to only one record in another table.
o One-to-Many: A record in one table can be related to multiple records in
another table.
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
o Many-to-Many: Records in both tables can be related to multiple records in
each other.
5. Characteristics of Relationships
Cardinality: Specifies the number of occurrences in one entity that are related to the
number of occurrences in another.
o One-to-One (1:1): Each entity in the relationship will have only one related
entity.
o One-to-Many (1
): One entity can be associated with multiple entities.
o Many-to-Many (M
): Entities on both sides of the relationship can have multiple associations.
Participation: Defines whether all entities in a set must participate in a relationship.
o Total Participation: Every entity in the set must be involved in the relationship.
o Partial Participation: Only some entities from the set are involved.
Degree: The number of participating entities in the relationship (Unary, Binary,
Ternary).
Attributes of Relationships: A relationship can have its own attributes (e.g.,
"date_of_joining" in an "employs" relationship between "Company" and "Employee").
6. Properties of Relations
o Each attribute in a relation has only one data value corresponding to it i.e. they do not
contain two or more values.
Keys in DBMS
Types of Keys in Relational Model (Candidate, Super, Primary, Alternate and Foreign)
Keys are one of the basic requirements of a relational database model. It is widely used to
identify the tuples(rows) uniquely in the table. We also use keys to set up relations amongst
various columns and tables of a relational database.
Different Types of Database Keys
Candidate Key
Primary Key
Super Key
Alternate Key
Foreign Key
Composite Key
Keys play an important role in the relational database.
It is used to uniquely identify any record or row of data from the table. It is also used to establish
and identify relationships between tables.
For example, ID is used as a key in the Student table because it is unique for each student. In
the PERSON table, passport_number, license_number, SSN are keys since they are unique for
each person.
Definition:
In Database Management System (DBMS), a key is a unique identifier that distinguishes each
record (tuple) in a relational database table. Keys ensure data integrity, uniqueness, and
efficient data retrieval.
Importance:
1. Uniquely identifies each record.
2. Ensures data integrity and consistency.
3. Supports efficient data retrieval and sorting.
4. Enables relationships between tables.
5. Prevents data duplication.
Why We Need Keys:
1. Data organization and structuring.
2. Efficient querying and indexing.
3. Data integrity and consistency.
4. Scalability and performance.
5. Reduced data redundancy.
Types of Keys:
1. Primary Key (PK):
- Unique identifier for each record.
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.
The candidate key can be simple (having only one attribute) or composite as well.
Example:
{STUD_NO, COURSE_NO} is a composite
candidate key for relation STUDENT_COURSE.
Table STUDENT_COURSE
1 001 C001
2 056 C005
There can be more than one candidate key in relation out of which one can be chosen as the
primary key. For Example, STUD_NO, as well as STUD_PHONE, are candidate keys for
relation STUDENT but STUD_NO can be chosen as the primary key (only one out of many
candidate keys).
It is a unique key.
It can identify only one tuple (a record) at a time.
It has no duplicate values, it has unique values.
It cannot be NULL.
Primary keys are not necessarily to be a single column; more than one column can also
be a primary key for a table.
Example:
STUDENT table -> Student(STUD_NO, SNAME,
ADDRESS, PHONE) , STUD_NO is a primary key
Table STUDENT
Enrollments Table
Products Table
Product_ID Product_Name Price
OrderDetails Table
301 501 2
301 502 1
302 501 1
A Super Key is a fundamental concept in Database Management Systems (DBMS) that plays
a crucial role in ensuring data integrity and facilitating efficient data retrieval. Understanding
super keys is essential for designing robust and reliable databases.
Definition of Super Key
A Super Key is a set of one or more attributes (columns) that can uniquely identify a tuple
(row) in a relation (table). In other words, no two distinct rows in a table can have the same
value for a super key. Super keys are the broadest form of keys in relational databases and
include all possible combinations of attributes that ensure uniqueness.
Key Characteristics of Super Keys
a. Uniqueness
Definition: Each super key must ensure that no two distinct tuples have the same value
for the key attributes.
Purpose: Guarantees that every record can be uniquely identified within a table.
Example: In an Employees table, both Employee_ID alone and the combination of
Employee_ID and Email can serve as super keys because each uniquely identifies an
employee.
b. Minimality (Not Required)
Definition: Unlike candidate keys, super keys do not need to be minimal. They can
contain additional attributes beyond those necessary for uniqueness.
Implication: A super key may include unnecessary attributes, making it a broader
concept compared to candidate keys.
Example: In the Employees table, {Employee_ID, Name} is a super key but not a
candidate key because Name is not required for uniqueness if Employee_ID alone is
sufficient.
c. Inclusivity
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
Definition: Every candidate key is a super key, but not every super key is a candidate
key.
Implication: Super keys encompass all possible unique identifier combinations,
including those that are not minimal.
Super Key vs. Other Keys
a. Super Key vs. Candidate Key
Super Key:
o May contain additional attributes beyond what is necessary for uniqueness.
o Example: {Employee_ID, Email}, {Employee_ID, Email, Phone_No}
Candidate Key:
o Must be minimal; no subset of the attributes can uniquely identify a tuple.
o Example: {Employee_ID}, {Email} (if both uniquely identify records)
Relationship: All candidate keys are super keys, but not all super keys are candidate
keys.
b. Super Key vs. Primary Key
Super Key:
o General concept encompassing all unique identifier combinations.
Primary Key:
o A specific candidate key chosen to uniquely identify records in a table.
o Example: If {Employee_ID} and {Email} are both candidate keys, one (e.g.,
{Employee_ID}) is selected as the primary key.
Relationship: The primary key is one of the candidate keys, which in turn is a super
key.
c. Super Key vs. Foreign Key
Super Key:
o Used within a table to uniquely identify its records.
Foreign Key:
o A set of attributes in one table that references the primary key of another table
to establish a relationship.
Example: Department_ID in an Employees table is a foreign key referencing
Department_ID in a Departments table.
Super Keys:
o {Student_ID}
o {Student_ID, Name}
o {Student_ID, Email}
o {Student_ID, Phone_Number}
o {Student_ID, Name, Email}
o ...and so on.
Explanation: While {Student_ID} alone is sufficient to uniquely identify a student, adding
additional attributes like Name or Email still maintains uniqueness, thereby forming super
keys.
Example 2: Table with Multiple Super Keys
Table: Books
Super Keys:
o {ISBN}
o {ISBN, Title}
o {ISBN, Author}
o {ISBN, Publisher}
o {ISBN, Title, Author}
o {ISBN, Title, Publisher}
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
and so on.
Explanation: Here, ISBN uniquely identifies each book, and any combination that includes
ISBN also ensures uniqueness.
Example 3: Composite Super Key
Table: Enrollments
Super Keys:
o {Student_ID, Course_ID}
o {Student_ID, Course_ID, Enrollment_Date}
Explanation: The combination of Student_ID and Course_ID uniquely identifies each
enrollment record, making it a composite super key. Adding Enrollment_Date still maintains
uniqueness, forming another super key.
Identifying Super Keys in SQL
While SQL does not have a specific syntax to declare a super key directly, super keys are
implicitly defined through primary keys and unique constraints.
When you define a primary key, you are effectively declaring a super key.
Super Keys:
o {Student_ID, Course_ID}
o {Student_ID, Course_ID, Enrollment_Date}
Super Keys:
o {Book_ID}
o {ISBN}
o {Book_ID, ISBN}
o {Book_ID, Title}
o {ISBN, Title}
Explanation: Both Book_ID and ISBN uniquely identify a book, making them individual
super keys. Combinations including these attributes also form super keys.
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
Foreign Key
A Foreign Key is a critical concept in Database Management Systems (DBMS) that establishes
and enforces a link between the data in two tables. Understanding foreign keys is essential for
maintaining referential integrity, designing robust relational databases, and facilitating
complex data relationships.
Definition of Foreign Key
A Foreign Key is an attribute or a set of attributes in one table that uniquely identifies a row
of another table or the same table. In essence, a foreign key is a reference used to link two
tables together, establishing a relationship between them. This relationship ensures that the
value in the foreign key column(s) corresponds to a valid primary key in the related table
Key Points:
Referential Integrity: Foreign keys enforce referential integrity by ensuring that
relationships between tables remain consistent.
Linking Tables: They create a connection between two tables, allowing for relational
data operations such as joins
Example:
Consider two tables, Departments and Employees. The Departments table has a primary
key Department_ID, and the Employees table includes a foreign key Department_ID to
link each employee to their respective department
-- Departments Table
CREATE TABLE Departments (
Department_ID INT PRIMARY KEY,
Department_Name VARCHAR(100) NOT NULL
);
-- Employees Table
CREATE TABLE Employees (
Employee_ID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Department_ID INT,
FOREIGN KEY (Department_ID) REFERENCES Departments(Department_ID)
);
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
Importance of Foreign Keys
Foreign keys play a pivotal role in relational databases by ensuring data consistency and
enabling meaningful data relationships. Their importance can be highlighted through the
following points:
a. Maintaining Referential Integrity
Consistency: Ensures that a foreign key value always points to an existing, valid record
in the related table.
Prevents Orphan Records: Eliminates the possibility of having records in the child
table that do not correspond to any record in the parent table.
b. Enabling Relational Operations
Joins: Facilitates SQL join operations, allowing for the retrieval of related data across
multiple tables.
Data Navigation: Enables navigation through related data, making it easier to query
complex datasets.
c. Supporting Data Normalization
Eliminating Redundancy: Helps in breaking down data into multiple related tables,
reducing data duplication.
Improving Data Organization: Enhances the logical structure of the database by
organizing data into related entities.
d. Enforcing Business Rules
Data Validity: Ensures that only valid data that complies with defined relationships is
entered into the database.
Integrity Constraints: Supports the implementation of business rules through integrity
constraints.
Characteristics of Foreign Keys
Foreign keys have specific characteristics that define their behavior and role within the
database schema:
a. Attribute Matching
Data Types: The foreign key and the referenced primary key must have compatible
data types.
Value Constraints: The values in the foreign key column must either match the
primary key values in the parent table or be NULL (if allowed).
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
b. Nullability
Optional Relationships: Foreign key columns can accept NULL values, indicating that
the relationship is optional.
Mandatory Relationships: When NOT NULL constraints are applied, every foreign
key value must correspond to a valid primary key in the parent table.
c. Single vs. Composite Keys
Single-Attribute Foreign Keys: Involve one column referencing a single primary key.
Composite Foreign Keys: Involve multiple columns referencing a composite primary
key in the parent table.
d. Cascading Actions
ON DELETE and ON UPDATE: Define what happens to the foreign key records
when the referenced primary key is deleted or updated.
o CASCADE: Automatically deletes or updates the child records.
o SET NULL: Sets the foreign key to NULL when the parent record is deleted or
updated.
o RESTRICT: Prevents the deletion or update if related child records exist.
o NO ACTION: Similar to RESTRICT, preventing changes that would violate
referential integrity.
e. Constraint Naming
Naming Conventions: Foreign key constraints often follow naming conventions like
FK_TableName_ReferencedTable
Advantages of Foreign Keys
Implementing foreign keys in a database offers several benefits:
a. Data Integrity
Referential Integrity: Ensures that relationships between tables remain consistent.
Prevents Invalid Data: Stops the entry of foreign key values that do not correspond to
any primary key in the parent table.
b. Improved Data Navigation
Ease of Access: Simplifies querying related data across multiple tables.
Relational Queries: Facilitates complex queries involving multiple tables through
joins.
c. Enhanced Database Design
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
Normalization Support: Aids in organizing data efficiently, reducing redundancy.
Logical Structure: Creates a clear and logical structure, making the database easier to
understand and maintain.
d. Automatic Enforcement of Business Rules
Constraint Enforcement: Automatically enforces business rules related to data
relationships without additional application logic.
Consistency Maintenance: Maintains consistent data states across related tables.
e. Cascading Actions
Automated Updates and Deletions: Simplifies data management by automating the
propagation of changes to related tables
Disadvantages of Foreign Keys
While foreign keys are beneficial, they also come with certain drawbacks:
a. Performance Overhead
Slower Operations: Enforcing foreign key constraints can slow down INSERT,
UPDATE, and DELETE operations due to additional checks.
Indexing Requirements: Efficient foreign key operations often require proper
indexing, which can consume additional storage and maintenance resources.
b. Complexity in Database Design
Design Constraints: Requires careful planning and understanding of data
relationships, which can complicate database design.
Migration Challenges: Modifying table structures with foreign keys can be
challenging, especially in large databases.
c. Potential for Deadlocks
Locking Issues: Improperly managed foreign key constraints can lead to deadlocks,
especially in high-concurrency environments.
d. Limitations with Certain Data Models
Hierarchical and Non-Relational Models: Foreign keys are inherently relational and
may not be suitable for other data models like hierarchical or graph-based databases.
e. Maintenance Overhead
Constraint Management: Requires ongoing management to ensure constraints are
correctly maintained, especially when dealing with data migrations or schema changes
Task:
List all possible super keys for the Books table.
Answer:
Possible super keys include:
1. {ISBN}
2. {ISBN, Edition}
3. {ISBN, Title}
4. {ISBN, Author}
5. {ISBN, Publisher}
6. {ISBN, Edition, Title}
7. {ISBN, Edition, Author}
8. {ISBN, Edition, Publisher}
9. {ISBN, Title, Author}
10. {ISBN, Title, Publisher}
11. {ISBN, Author, Publisher}
12. {ISBN, Edition, Title, Author}
13. {ISBN, Edition, Title, Publisher}
14. {ISBN, Edition, Author, Publisher}
15. {ISBN, Title, Author, Publisher}
16. {ISBN, Edition, Title, Author, Publisher}
Explanation:
{ISBN} alone is sufficient to uniquely identify each book.
Adding more attributes still maintains uniqueness, thus forming additional super keys.
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
Question 2: Differentiating Super Keys and Candidate Keys
Scenario:
Given the Employees table:
Task:
Identify which of the following are super keys and which are candidate keys:
1. {Employee_ID}
2. {Email}
3. {Phone_No}
4. {Employee_ID, Email}
5. {Employee_ID, Phone_No}
6. {Email, Phone_No}
7. {Employee_ID, Email, Phone_No}
Answer:
Super Keys:
1. {Employee_ID}
2. {Email}
3. {Phone_No}
4. {Employee_ID, Email}
5. {Employee_ID, Phone_No}
6. {Email, Phone_No}
7. {Employee_ID, Email, Phone_No}
Candidate Keys:
1. {Employee_ID}
2. {Email}
3. {Phone_No}
Explanation:
All listed sets are super keys because they uniquely identify each record.
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
{Employee_ID}, {Email}, and {Phone_No} are also candidate keys as they are
minimal super keys without any unnecessary attributes.
Question 3: Creating a Table with a Composite Super Key
Scenario:
You are designing a CourseEnrollments table where each enrollment is uniquely identified by
a combination of Student_ID and Course_ID.
Task:
Write the SQL statement to create the CourseEnrollments table with a composite primary key
serving as a composite super key.
Answer:
CREATE TABLE CourseEnrollments (
Student_ID INT,
Course_ID INT,
Enrollment_Date DATE,
PRIMARY KEY (Student_ID, Course_ID),
FOREIGN KEY (Student_ID) REFERENCES Students(Student_ID),
FOREIGN KEY (Course_ID) REFERENCES Courses(Course_ID)
);
Explanation:
The combination of Student_ID and Course_ID serves as a composite primary key,
making it a composite super key.
Foreign keys link to the respective Students and Courses tables
Question 4: Implementing Alternate Super Keys
Scenario:
In the Employees table, besides Employee_ID, both Email and Phone_No can uniquely identify
an employee.
Task:
Modify the SQL statement to include Email and Phone_No as alternate super keys using unique
constraints.
Answer:
sql
Copy code
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
CREATE TABLE Employees (
Employee_ID INT PRIMARY KEY, -- Primary Key (Super Key)
Name VARCHAR(100) NOT NULL,
Email VARCHAR(100) UNIQUE, -- Unique Constraint (Alternate Super Key)
Phone_No VARCHAR(15) UNIQUE -- Unique Constraint (Another Alternate Super
Key)
);
Explanation:
Email and Phone_No are defined with UNIQUE constraints, making them alternate
super keys.
This ensures that each Email and Phone_No is unique across the table.
Question 5: Identifying Non-Candidate Super Keys
Scenario:
Given the Products table:
Task:
Determine which of the following sets are super keys but not candidate keys:
1. {Product_ID, SKU}
2. {Product_ID, Serial_No}
3. {SKU, Serial_No}
4. {Product_ID, SKU, Serial_No}
Answer:
All the listed sets are super keys but not candidate keys because they contain unnecessary
attributes beyond the minimal requirement for uniqueness.
{Product_ID, SKU}
{Product_ID, Serial_No}
{SKU, Serial_No}
{Product_ID, SKU, Serial_No}
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
Explanation:
Each of these sets includes multiple attributes that individually are sufficient to
uniquely identify a product (Product_ID, SKU, or Serial_No).
Since they are not minimal, they are super keys but not candidate keys.
Question 7: Super Key vs. Primary Key in SQL
Scenario:
You have a Users table with User_ID, Username, and Email, all of which are unique.
Task:
Explain how to implement super keys in SQL and select a primary key.
Answer:
Implementation:
CREATE TABLE Users (
User_ID INT PRIMARY KEY, -- Primary Key (Super Key)
Username VARCHAR(50) UNIQUE, -- Unique Constraint (Alternate Super Key)
Email VARCHAR(100) UNIQUE, -- Unique Constraint (Another Alternate Super
Key)
Password VARCHAR(255) NOT NULL
);
Explanation:
User_ID is chosen as the primary key, making it a super key.
Username and Email are defined with UNIQUE constraints, making them alternate
super keys.
Selection of Primary Key:
Criteria: Stability, minimality, and non-meaningful (to prevent changes).
Choice: User_ID is preferred over Username and Email because it is typically auto-
generated, ensuring stability and simplicity.
Common Issues and Solutions with Super Keys
a. Redundant Super Keys
Issue: Including unnecessary attributes in super keys can lead to redundancy.
Solution: Focus on identifying candidate keys by removing superfluous attributes,
ensuring minimality.
b. Composite Super Keys Complexity
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
Issue: Composite super keys (keys with multiple attributes) can complicate queries and
foreign key relationships.
Solution: Where possible, use single-attribute super keys or surrogate keys to simplify
table relationships and queries.
c. Managing Multiple Super Keys
Issue: Handling numerous super keys can complicate database design and maintenance.
Solution: Clearly define and document all super keys, and prioritize candidate keys for
primary key selection.
d. Performance Overheads
Issue: Indexing multiple super keys can consume additional storage and impact
performance.
Solution: Index only those super keys that are frequently used in queries or as foreign
keys, balancing performance and storage needs.
e. Ensuring Consistency
Issue: Maintaining consistency across multiple super keys requires diligent constraint
management.
Solution: Implement automated checks through database constraints and regular
integrity audits.
Practical Examples: Designing a Database with Super Keys
Example 1: Employees and Departments
Scenario: Each employee belongs to one department. Both Employee_ID and Email uniquely
identify an employee.
Tables and Super Keys:
-- Creating Departments Table
CREATE TABLE Departments (
Department_ID INT PRIMARY KEY,
Department_Name VARCHAR(100) NOT NULL
);
Task:
Identify whether {VIN, License_Plate} is a super key and if it is a candidate key.
Answer:
Super Key: {VIN, License_Plate} is a super key because the combination uniquely
identifies each vehicle.
Candidate Key: It is not a candidate key if VIN alone uniquely identifies each vehicle.
Explanation:
Minimality: Since VIN alone is sufficient to uniquely identify a vehicle, adding
License_Plate makes the key non-minimal, hence not a candidate key.
Summary
Super Key: A set of one or more attributes that can uniquely identify a tuple in a table.
It is the most general form of a unique identifier in a database.
Candidate Key: A minimal super key with no unnecessary attributes. Every candidate
key is a super key, but not every super key is a candidate key.
Primary Key: A chosen candidate key used to uniquely identify records within a table.
It is enforced by primary key constraints and typically indexed for performance.
Disadvantages:
1. Complexity: Managing composite keys can be challenging.
2. Performance overhead: Maintaining composite keys can impact database performance.
3. Indexing challenges: Indexing composite keys can be complex.
Composite Key Types:
1. Simple Composite Key: Two or more attributes form the primary key.
Example: OrderID + CustomerID
2. Compound Composite Key: Combination of simple and composite keys.
Example: OrderID + (CustomerID + Address)
3. Nested Composite Key: Composite key with another composite key as an attribute.
Example: (OrderID + CustomerID) + (ProductID + Quantity)
4. Overlapping Composite Key: Two or more composite keys share common attributes.
Example: (OrderID + CustomerID) and (CustomerID + Address)
5. Non-Overlapping Composite Key: Composite keys with no shared attributes.
Example: (OrderID + CustomerID) and (ProductID + Quantity)
Design Considerations:
1. Choose attributes that ensure uniqueness.
2. Minimize the number of attributes.
3. Avoid redundant or null values.
4. Consider indexing strategies.
Key Differences:
- Composite Key vs. Primary Key: A primary key can be simple or composite.
- Composite Key vs. Super Key: A composite key is a type of super key.
- Composite Key vs. Foreign Key: A foreign key references the primary key of another table.
a. Defining Composite Primary Keys During Table Creation
CREATE TABLE CourseEnrollments (
Student_ID INT,
Course_ID INT,
Enrollment_Date DATE,
PRIMARY KEY (Student_ID, Course_ID),
FOREIGN KEY (Student_ID) REFERENCES Students(Student_ID),
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
FOREIGN KEY (Course_ID) REFERENCES Courses(Course_ID)
);
Explanation:
PRIMARY KEY (Student_ID, Course_ID): Defines a composite primary key
comprising Student_ID and Course_ID.
Foreign Keys: Establishes referential integrity by linking to the Students and Courses
tables.
b. Adding Composite Keys to Existing Tables
ALTER TABLE OrderDetails
ADD PRIMARY KEY (Order_ID, Product_ID);
Explanation:
ADD PRIMARY KEY (Order_ID, Product_ID): Establishes a composite primary key
using Order_ID and Product_ID.
Practice Questions on Composite Keys
Question 1: Identifying Composite Keys
Scenario:
Consider the following Enrollments table:
Task:
Determine whether {Student_ID, Course_ID} is a composite key and explain why.
Answer:
Composite Key: {Student_ID, Course_ID} is a composite key because the
combination of Student_ID and Course_ID uniquely identifies each record in the
Enrollments table.
Explanation:
o Uniqueness: Each combination of Student_ID and Course_ID is unique.
In this table:
Candidate Keys: {Employee_ID}, {Email}, {Phone_No}
Primary Key: {Employee_ID}
Alternate Keys: {Email}, {Phone_No}
Importance of Alternate Keys
Alternate keys hold significant importance in relational database design for several reasons:
a. Ensuring Data Uniqueness
Data Integrity: Alternate keys enforce additional layers of uniqueness beyond the
primary key, preventing duplicate records based on different attributes.
Flexible Identification: They provide alternative ways to identify records, enhancing
flexibility in data retrieval and manipulation.
b. Supporting Database Normalization
Normalization Forms: Alternate keys assist in achieving higher normal forms by
ensuring that data dependencies are correctly managed.
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
Eliminating Redundancy: By identifying multiple unique identifiers, alternate keys
help in organizing data efficiently, reducing redundancy.
c. Facilitating Referential Integrity
Foreign Keys: Alternate keys can be used as targets for foreign key relationships,
maintaining consistent and valid references between tables.
Data Consistency: They ensure that relationships between tables remain intact,
preventing orphaned records and maintaining data consistency.
d. Enhancing Query Performance
Indexing: Alternate keys are often indexed, which can significantly improve the
performance of queries that filter or join based on these keys.
Optimized Searches: They provide additional pathways for efficient data retrieval,
especially in large datasets.
Task:
Identify all possible alternate keys for the Books table, assuming ISBN is chosen as the primary
key.
Answer:
Given that ISBN is the primary key, we need to identify other candidate keys that can uniquely
identify records.
Candidate Keys: {ISBN}, {Edition, Title}, {ISBN, Edition, Title} (but these are not
minimal)
Alternate Keys: Since ISBN is the primary key, the alternate keys could be:
o If Edition and Title together are unique, then {Edition, Title} is an alternate key.
o If no other combination of columns uniquely identifies a book, then there may
be no alternate keys beyond ISBN.
Explanation:
In this case, {Edition, Title} can uniquely identify a book if each title can have multiple editions
but no two books share the same title and edition combination.
Question 2: Differentiating Alternate Keys from Primary Keys
Scenario:
Given the Employees table:
Task:
Unique Key
Definition:
A Unique Key is a database constraint that ensures that all the values in a specific column (or
group of columns) are unique across the rows in a table. It enforces the uniqueness of the data
and prevents duplication. A table can have multiple unique keys, but only one primary key.
Importance of Unique Key:
Data Integrity: Ensures that each record in the table is unique and prevents the
insertion of duplicate records.
Efficient Search: Improves the efficiency of searches and queries, especially when
combined with indexing.
Ensures Uniqueness: It provides a mechanism to guarantee that certain fields within a
table contain distinct values, which is essential for identifying records uniquely without
redundancy.
1 Alice 10
2 Bob 20
3 Charlie 30
4 David NULL
Departments
DeptID DeptName
10 HR
20 Engineering
40 Marketing
SQL Example:
SELECT Employees.Name, Departments.DeptName
FROM Employees
INNER JOIN Departments ON Employees.DeptID = Departments.DeptID;
Result:
Name DeptName
Alice HR
Bob Engineering
Explanation:
Only Alice and Bob have matching DeptID values in both tables. Charlie has a DeptID of 30,
which doesn't exist in the Departments table, and David has a NULL DeptID. Thus, only Alice
and Bob are returned.
2. LEFT (OUTER) JOIN
Definition:
A LEFT JOIN returns all records from the left table and the matched records from the right
table. If there is no match, the result is NULL on the right side.
Use Case:
When you need all records from the left table regardless of whether there's a matching record
in the right table, such as listing all employees and their departments, including those without
a department.
SQL Example:
SELECT Employees.Name, Departments.DeptName
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
FROM Employees
LEFT JOIN Departments ON Employees.DeptID = Departments.DeptID;
Result:
Name DeptName
Alice HR
Bob Engineering
Charlie NULL
David NULL
Explanation:
All employees are listed. Alice and Bob have matching departments, while Charlie and David
do not, resulting in NULL for their DeptName.
3. RIGHT (OUTER) JOIN
Definition:
A RIGHT JOIN returns all records from the right table and the matched records from the left
table. If there is no match, the result is NULL on the left side.
Use Case:
When you need all records from the right table regardless of whether there's a matching
record in the left table, such as listing all departments and their employees, including
departments with no employees.
SQL Example:
SELECT Employees.Name, Departments.DeptName
FROM Employees
RIGHT JOIN Departments ON Employees.DeptID = Departments.DeptID;
Result:
Name DeptName
Alice HR
Bob Engineering
NULL Marketing
Name DeptName
Alice HR
Bob Engineering
Charlie NULL
David NULL
NULL Marketing
Explanation:
All employees and all departments are listed. Alice and Bob are matched, Charlie and David
have no departments, and Marketing has no employees.
Alice HR
Alice Engineering
Alice Marketing
Bob HR
Bob Engineering
Bob Marketing
Charlie HR
Charlie Engineering
Charlie Marketing
David HR
David Engineering
David Marketing
Explanation:
Each of the four employees is paired with each of the three departments, resulting in 12
combinations.
1 Alice 3
2 Bob 3
3 Charlie NULL
4 David 2
SQL Example:
SELECT E.Name AS Employee, M.Name AS Manager
FROM Employees E
LEFT JOIN Employees M ON E.ManagerID = M.EmployeeID;
Result:
Employee Manager
Alice Charlie
Bob Charlie
Charlie NULL
David Bob
Explanation:
Each employee is paired with their manager. Charlie has no manager (NULL), while David's
manager is Bob.
1 John 101
2 Emma 102
3 Liam 103
Classes
ClassID ClassName
101 Mathematics
102 Science
104 History
SQL Example:
SELECT Students.Name, Classes.ClassName
FROM Students
NATURAL JOIN Classes;
Result:
Name ClassName
John Mathematics
Emma Science
1 Alice USA
2 Bob Canada
3 Charlie USA
4 David UK
Orders
101 1 2024-01-15
102 2 2024-02-20
103 1 2024-03-05
104 5 2024-04-10
Explanation:
Only customers with matching orders are listed. David has no orders, and OrderID 104 has no
matching customer.
Scenario 2: LEFT JOIN
Objective: List all customers and their orders, including customers without orders.
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
SQL Query:
SELECT Customers.CustomerName, Orders.OrderID, Orders.OrderDate
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Result:
CustomerName OrderID OrderDate
Explanation:
All customers are listed. Charlie and David have no orders, resulting in NULL values.
Scenario 3: RIGHT JOIN
Objective: List all orders and the corresponding customers, including orders without valid
customers.
SQL Query:
SELECT Customers.CustomerName, Orders.OrderID, Orders.OrderDate
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Result:
CustomerName OrderID OrderDate
Explanation:
All orders are listed. OrderID 104 has no matching customer, resulting in NULL for
CustomerName.
Explanation:
All customers and all orders are listed. Unmatched records have NULL values.
Integrity constraints. Referential integrity, Intension and Extension
1. Integrity Constraints
Integrity constraints are rules that ensure the accuracy and consistency of data in a database.
These constraints enforce the correctness of the data being entered and maintained in the
database. There are several types of integrity constraints:
Domain Constraints: Specify that all values in a column must belong to a predefined
domain (e.g., age must be a positive integer).
Entity Integrity: Ensures that the primary key of a table is unique and not null,
meaning each entity (row) is identifiable.
Referential Integrity: Ensures that a foreign key value in one table always refers to an
existing record in another table.
2. Referential Integrity
Referential integrity ensures that relationships between tables remain consistent. Specifically,
it ensures that a foreign key in one table points to a valid primary key in another table.
1 Alice 10 70000
2 Bob 20 60000
3 Charlie 10 80000
4 Diana 30 75000
Operation:
Retrieve all employees who work in department 10.
Relational Algebra Expression:
σ<sub>DeptID = 10</sub>(Employees)
Result:
1 Alice 10 70000
3 Charlie 10 80000
2. Projection (π)
Projection is used to retrieve specific columns from a relation, effectively removing unwanted
attributes. It’s similar to the SELECT clause in SQL.
Symbol: π
Syntax: π<sub>attributes</sub>(Relation)
Example:
Relation:
Employees (as above)
Operation:
Retrieve the names and salaries of all employees.
Relational Algebra Expression:
π<sub>Name, Salary</sub>(Employees)
Result:
Name Salary
Alice 70000
Bob 60000
Charlie 80000
Diana 75000
3. Joins
Joins combine rows from two or more relations based on a related column between them. There
are several types of joins in relational algebra, including Natural Join, Theta Join, and Equi
Join. Here, we'll focus on the Natural Join, which is the most commonly used.
Symbol: ⋈
Syntax: Relation1 ⋈ Relation2
Example:
Relations:
Employees
EmpID Name DeptID Salary
1 Alice 10 70000
2 Bob 20 60000
3 Charlie 10 80000
4 Diana 30 75000
Departments
10 HR New York
30 Marketing Chicago
40 Sales Boston
Operation:
Combine Employees and Departments to list employees along with their department names.
Relational Algebra Expression:
Employees ⋈ Departments
(Assuming a natural join on the common attribute DeptID)
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
Result:
EmpID Name DeptID Salary DeptName Location
1 Alice 10 70000 HR New York
3 Charlie 10 80000 HR New York
2 Bob 20 60000 Engineering San Francisco
4 Diana 30 75000 Marketing Chicago
Explanation:
The natural join automatically matches rows from Employees and Departments where
DeptID is the same.
Only departments that have matching employees are included in the result. If you want
to include all departments or all employees regardless of matches, you would use
different types of joins like Left Outer Join or Right Outer Join (though these are
extensions beyond basic relational algebra).
Combining Selection, Projection, and Joins
Often, you'll need to use multiple operations together to formulate complex queries. Here's an
example that combines all three operations.
Example:
Task:
Retrieve the names and salaries of employees who work in the "HR" department.
Relational Algebra Steps:
1. Join Employees and Departments on DeptID.
Employees ⋈ Departments
2. Select the rows where DeptName = 'HR'.
σ<sub>DeptName = 'HR'</sub>(Employees ⋈ Departments)
3. Project the Name and Salary columns.
π<sub>Name, Salary</sub>(σ<sub>DeptName = 'HR'</sub>(Employees ⋈
Departments))
Result:
Name Salary
Alice 70000
Outer Joins
Outer Joins include rows that do not have matching counterparts in the other relation. They
are typically categorized as:
Left Outer Join: All rows from the left relation and matching rows from the right.
Right Outer Join: All rows from the right relation and matching rows from the left.
Full Outer Join: All rows when there is a match in either left or right relation.
Note: Basic relational algebra does not include outer joins, but they are common extensions.
Fundamental Operators
These are the basic/fundamental operators used in Relational Algebra.
1. Selection(σ)
2. Projection(π)
3. Union(U)
4. Set Difference(-)
5. Set Intersection(∩)
6. Rename(ρ)
7. Cartesian Product(X)
1. Selection(σ): It is used to select required tuples of the relations.
Example:
A B C
1 2 4
2 2 3
3 2 3
4 3 4
For the above relation, σ(c>3)R will select the tuples which have c more than 3.
A B C
1 2 4
4 3 4
B C
2 4
2 3
3 4
Student_Name Roll_Number
Ram 01
Mohan 02
Vivek 13
Geeta 17
GERMAN
Vivek 13
Geeta 17
Shyam 21
Rohan 25
Consider the following table of Students having different optional subjects in their course.
π(Student_Name)FRENCH U π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Vivek
Geeta
Shyam
Rohan
Note: The only constraint in the union of two relations is that both relations must have the
same set of Attributes.
4. Set Difference(-): Set Difference in relational algebra is the same set difference operation
as in set theory.
Example: From the above table of FRENCH and GERMAN, Set Difference is used as
follows
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
π(Student_Name)FRENCH - π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Note: The only constraint in the Set Difference between two relations is that both relations
must have the same set of Attributes.
5. Set Intersection(∩): Set Intersection in relational algebra is the same set intersection
operation in set theory.
Example: From the above table of FRENCH and GERMAN, the Set Intersection is used as
follows
π(Student_Name)FRENCH ∩ π(Student_Name)GERMAN
Student_Name
Vivek
Geeta
Note: The only constraint in the Set Difference between two relations is that both relations
must have the same set of Attributes.
6. Rename(ρ): Rename is a unary operation used for renaming attributes of a relation.
ρ(a/b)R will rename the attribute 'b' of the relation by 'a'.
7. Cross Product(X): Cross-product between two relations. Let’s say A and B, so the cross
product between A X B will result in all the attributes of A followed by each attribute of B.
Each record of A will pair with every record of B.
Example:
A
Ram 14 M
Sona 15 F
Kim 20 M
ID Course
1 DS
2 DBMS
AXB
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS
Note: If A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘ n*m ‘ tuples.
Name ID Dept_Name
A 120 IT
B 125 HR
C 110 Sales
D 111 IT
DEPT
Dept_Name Manager
Sales Y
Production Z
IT A
A 120 IT A
C 110 Sales Y
D 111 IT A
2. Conditional Join: Conditional join works similarly to natural join. In natural join, by
default condition is equal between common attributes while in conditional join we can
specify any condition such as greater than, less than, or not equal.
Example:
R
ID Gender Marks
1 F 45
2 F 55
3 F 60
ID Gender Marks
10 M 20
11 M 22
12 M 59
1 F 45 10 M 20
1 F 45 11 M 22
2 F 55 10 M 20
2 F 55 11 M 22
3 F 60 10 M 20
3 F 60 11 M 22
3 F 60 12 M 59
Name
Ram
Shyam
...
(Assuming Ram and Shyam work in HR)
Domain Relational Calculus (DRC)
Prof. Vineeta Shrivastava
A.P CSE
LNCTE
Database Management Systems
Domain Relational Calculus (DRC) is another form of relational calculus where queries are
expressed by specifying domains (attributes) that satisfy certain conditions. In DRC, variables
represent individual attributes rather than entire tuples.
Syntax of DRC:
The general form of a DRC query is:
{⟨x1,x2,…,xn⟩∣P(x1,x2,…,xn)}
x₁, x₂, …, xn: Domain variables representing attributes.
P(...): A predicate that these variables must satisfy.
Example:
Problem: Retrieve the names of all employees who work in the "HR" department.
Assumptions:
Employees relation with attributes: EmpID, Name, DeptID, Salary.
Departments relation with attributes: DeptID, DeptName, Location.
DRC Query:
{⟨eName⟩∣∃eID∃dID∃salary(Employees(eID,eName,dID,salary)∧Departments(dID,′HR′,loca
tion))}
Explanation:
eName is the domain variable for the employee's name.
The query selects the Name attribute (eName) of all employees where:
o There exists eID, dID, and salary such that:
A tuple exists in Employees with EmpID = eID, Name = eName,
DeptID = dID, and Salary = salary.
A tuple exists in Departments with DeptID = dID and DeptName =
'HR'.
Result:
Name
Ram
Shyam
...
(Assuming Ram and Shyam work in HR)
Usage in SQL Underlies SQL's operational aspects Mirrors SQL's declarative aspects
Key Takeaway: While Relational Algebra provides a set of operations to transform relations
step-by-step, Relational Calculus allows for specifying the properties of the desired result
without detailing the transformation process.