Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views17 pages

Unit-2 (AutoRecovered)

This document provides an overview of the Relational Model, including key concepts such as domain, attributes, tuples, relations, and the significance of null values and constraints in database management. It details various types of keys (primary, candidate, super, foreign, and composite) and their definitions, along with examples of how to implement these concepts in SQL. Additionally, it covers relational algebra operations for querying and manipulating data, emphasizing the importance of constraints in maintaining data integrity.

Uploaded by

019236m005
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)
6 views17 pages

Unit-2 (AutoRecovered)

This document provides an overview of the Relational Model, including key concepts such as domain, attributes, tuples, relations, and the significance of null values and constraints in database management. It details various types of keys (primary, candidate, super, foreign, and composite) and their definitions, along with examples of how to implement these concepts in SQL. Additionally, it covers relational algebra operations for querying and manipulating data, emphasizing the importance of constraints in maintaining data integrity.

Uploaded by

019236m005
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/ 17

Unit II

Syllabus:

Relational Model: Introduction to relational model, concepts of domain, attribute, tuple,


relation, importance of null values, constraints (Domain, Key constraints, integrity
constraints) and their importance, Relational Algebra, Relational Calculus.

Relational Model
The Relational Model represents data and their relationships through a collection of tables. Each
table also known as a relation consists of rows and columns.

It was introduced by Edgar F. Codd in 1970

Key Terms

 Degree: The number of attributes in the relation is known as the degree of the relation. The
STUDENT relation defined above has degree 5.

 Cardinality: The number of tuples in a relation is known as cardinality. The STUDENT relation
defined above has cardinality 4.

 NULL Values: The value which is not known or unavailable


is called a NULL value. It is represented by NULL. e.g.
PHONE of STUDENT having ROLL_NO 4 is NULL.
 Relation Key: These are basically the keys that are used
to identify the rows uniquely or also help in identifying
tables. These are of the following types:

o Primary Key

o Candidate Key

o Super Key

o Foreign Key

o Alternate Key

o Composite Key

o Primary Key: The primary key is an attribute or a set of attributes that help to uniquely
identify the tuples(records) in the relational table.

Rules For Defining a Primary Key


o Minimal: The primary key is composed of a minimum
number of attributes that satisfy the unique occurrence of
the tuples or records.
o NON NULL Value: The primary key which refers to a
nonnull value that is required for the identification of the
tuple (record) means that any attribute can not contain the
non-null value in DBMS.
o

o Candidate Key

It refers to a set of one or more columns (attributes) in a table that can uniquely identify each row in
that table.

2. Candidate Key

 A Candidate Key is a column or a set of columns that can uniquely identify each row but is
not yet chosen as the primary key.

 A table can have multiple candidate keys, but only one can be chosen as the Primary Key.

Example:

 StudentID and Email both are unique and can act as a Candidate Key.

 If StudentID is chosen as the Primary Key, then Email becomes an Alternate Key.
 StudentID and Email both are unique and can act as a Candidate Key.

 If StudentID is chosen as the Primary Key, then Email becomes an Alternate Key.

3. Super Key

 A Super Key is a set of one or more columns that can uniquely identify a row.

 A Super Key may have extra attributes that are not necessary for uniqueness.

Example:

For the Students table:

 {StudentID}

 {Email}

 {StudentID, Name}

 {StudentID, Email, Age}

✅ Note:
Every Candidate Key and Primary Key is a Super Key, but a Super Key may contain extra unnecessary
attributes.

4. Composite Key

 A Composite Key is a key that consists of two or more columns to uniquely identify a record.

 Used when a single column is not enough to ensure uniqueness.

CREATE TABLE Enrollments (

StudentID INT,

CourseID INT,

EnrollmentDate DATE,

PRIMARY KEY (StudentID, CourseID)

);

Here, (StudentID, CourseID) together form a Composite Key, ensuring that a student cannot enroll in
the same course twice.

5. Foreign Key

 A Foreign Key is a column in one table that references the Primary Key of another table.

 It helps maintain referential integrity.

CREATE TABLE Orders (

OrderID INT PRIMARY KEY,

StudentID INT,
OrderDate DATE,

FOREIGN KEY (StudentID) REFERENCES Students(StudentID)

);

Here, StudentID in Orders is a Foreign Key referencing StudentID in Students.

6. Alternate Key

 An Alternate Key is a Candidate Key that is not chosen as the Primary Key.

Example:

In the Students table:

 Candidate Keys: StudentID, Email

 If StudentID is chosen as the Primary Key, then Email becomes an Alternate Key.

Key Type Definition Example

Uniquely identifies rows; no NULLs or


Primary Key duplicates StudentID in Students table
Links two tables by referencing a StudentID in Courses references Students(StudentI
Foreign Key primary key D)
Any set of attributes that
Super Key uniquely identifies rows {StudentID}, {StudentID, FirstName}
Candidate Minimal subset of attributes that
Key uniquely identifies rows {StudentID}, {SocialSecurityNumber}
Candidate keys not chosen as If StudentID is primary,
Alternate Key the primary key then SocialSecurityNumber is alternate
Composite Combination of columns used to
Key uniquely identify rows (StudentID, CourseID) in an Enrollments table

Concepts of domain:
In an RDBMS (Relational Database Management System), a domain refers to
the set of permissible values that a column (attribute) in a table can hold. It
defines the data type, format, and constraints for the values in a specific
column, ensuring data integrity and consistency.

 For example, a column representing "Age" may have a domain that


restricts values to integers between 0 and 150.
 Domain Constraints:
 These are rules applied to ensure that only valid data is
entered into a column. Examples include:
 Data type (e.g., integer, string, date).
 Range restrictions (e.g., numbers between 1 and 100).
 Format restrictions (e.g., email addresses).

CREATE TABLE Employee (


EmployeeID INT NOT NULL, -- Domain: Positive integers
Name VARCHAR(50) NOT NULL, -- Domain: Strings up to 50 characters
Age INT CHECK (Age >= 18 AND Age <= 65), -- Domain: Integers between 18
and 65
Email VARCHAR(100) UNIQUE -- Domain: Valid email format
);

 Here, each column has a specific domain:


 EmployeeID: Must be a positive integer.
 Name: Can only store strings up to 50 characters.
 Age: Restricted to values between 18 and 65.
 Email: Must be unique and follow an email format.

importance of null values


1. Representing Missing or Unknown Data
 Null values allow a database to handle situations where data is
not available or not yet known. For example, if a customer's
middle name is not provided, a null value can be used instead
of leaving the field blank or using a placeholder (e.g., "N/A" or
"Unknown"), which could lead to ambiguity.
2. Handling Inapplicable Data
 In some cases, certain fields may not be applicable to a specific
record. For example, in a database storing employee
information, the "termination date" field would not apply to
current employees. Using a null value in such cases is more
appropriate than inserting a default or dummy value.
3. Flexibility in Data Modeling

 Null values provide flexibility in designing database schemas. They


allow optional fields in tables, which is particularly useful when not
all attributes are mandatory for every record.

 Example 1: Representing Missing Data


 Scenario: A company maintains a database of employees. Some
employees have not provided their middle names.

Employee FirstNa MiddleNa LastNa


ID me me me
1 John NULL Doe
2 Jane Ann Smith
3 Alice NULL Johnson
Example 2: Handling Inapplicable Data
Scenario: A university database tracks student enrollment.
Some students are still enrolled, so their graduation date is
not applicable.
StudentI
Name EnrollmentDate GraduationDate
D
101 John Smith 2020-09-01 NULL
102 Jane Doe 2019-09-01 2023-06-15
Example 3: Optional Fields in a Form
Scenario: An e-commerce website collects customer
information. The "Phone Number" field is optional.
CustomerID Name Email PhoneNumber
1 Alice Brown [email protected] NULL
2 Bob Smith [email protected] 123-456-7890
Example 4: Aggregating Data with Null Values
Scenario: A sales database tracks the commission earned by
sales representatives. Some representatives have not earned
any commission yet.
SalesRepI
Name Commission
D
1 John Doe 500.00
2 Jane Smith NULL
3 Alice Brown300.00
constraints (Domain, Key constraints, integrity constraints)
and their importance
Constraints in a database management system (DBMS) are rules that ensure
the data in a database is accurate, consistent, and reliable. They prevent errors
and anomalies from entering the data. Here's an explanation of Domain
Constraints, Key Constraints, and Integrity Constraints, along with examples:
Domain Constraints
 Definition: Domain constraints define the valid range of values that a
particular attribute can have. They restrict the type and range of data
that can be stored in a column, ensuring data integrity and consistency.
 Example: In a "Students" table, the "Age" column might have a domain
constraint that only allows positive integers, ensuring that no student's
age is recorded as negative or non-numeric.
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT CHECK (Age > 0)
);

Key Constraints
 Definition: Key constraints are used to uniquely identify records in a
table. They include primary keys and unique keys.
 Primary Key: A primary key is a column or set of columns that
uniquely identifies each row in a table. It cannot contain null
values.
 Unique Key: A unique key ensures that all values in a column are
different. Unlike a primary key, it can contain one null value.
 Examples:
 Primary Key: In a "Customers" table, the "CustomerID" could be
the primary key, ensuring each customer has a unique identifier.
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100)
);
Unique Key: In a "Users" table, the "Email" column might have a unique
constraint to prevent duplicate email addresses.
CREATE TABLE Users (
UserID INT PRIMARY KEY,
Name VARCHAR(10),
Email VARCHAR(20) UNIQUE
);
Integrity Constraints
Integrity constraints ensure that the data in a database remains consistent and
accurate. They include:
 Entity Integrity Constraints: The entity integrity constraint states that
no primary key value can be NULL.
 Referential Integrity Constraints: Ensure that relationships between
tables are maintained by requiring that foreign keys in one table match
primary keys in another table.
 Check Constraints: Specify conditions that data must meet before it can
be entered into a table.
Examples of Integrity Constraints
 Entity Integrity Constraint: In an "Employees" table, the "EmployeeID"
must be unique and non-null to ensure each employee can be identified
uniquely.
 CREATE TABLE Employees (
 EmployeeID INT PRIMARY KEY,
 Name VARCHAR(100),
 Salary DECIMAL(10,2)
 );
 Referential Integrity Constraint: In a university database, if a student's
course ID is a foreign key referencing the course ID in a "Courses" table,
referential integrity ensures that only valid course IDs are entered for
students.
 CREATE TABLE Courses (
 CourseID INT PRIMARY KEY,
 CourseName VARCHAR(100)
 );

 CREATE TABLE Students (


 StudentID INT PRIMARY KEY,
 Name VARCHAR(100),
 CourseID INT,
 FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
 );
 Check Constraint: In a "Products" table, a check constraint might ensure
that all product prices are greater than zero.
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(10,2) CHECK (Price > 0)
);
Importance of Constraints
Constraints are crucial for maintaining data integrity and preventing
inconsistencies:
 Prevent Data Anomalies: Constraints ensure that data is consistent and
accurate, preventing errors such as duplicate records or invalid data
types.
 Maintain Relationships: Referential integrity constraints ensure that
relationships between tables are consistent, preventing orphaned
records.
 Improve Data Security: By enforcing specific rules, constraints can help
protect sensitive data by limiting the types of data that can be entered
into certain fields.
 Enhance Data Retrieval: Constraints like primary keys improve data
retrieval efficiency by providing unique identifiers for each record.
Relational Algebra
Relational Algebra consists of a set of operations that take one or more
relations (tables) as input and produce a new relation as output. These
operations are used to query and manipulate data in a relational database.
Basic Operations in Relational Algebra
1. Selection (σ)
o Selects tuples (rows) from a relation that satisfy a given condition.
o Syntax: σ<sub>condition</sub>(R)
o Example: σ<sub>age > 30</sub>(Employee)
 This selects all rows from the Employee table where the age
is greater than 30.
2. Projection (π)
o Selects specific attributes (columns) from a relation.
o Syntax: π<sub>attribute_list</sub>(R)
o Example: π<sub>name, age</sub>(Employee)
 This selects only the name and age columns from the
Employee table.
3. Union (∪)
o Combines tuples from two relations, removing duplicates.

o Syntax: R ∪ S
o Both relations must have the same set of attributes.

o Example: Employee ∪ Manager


 This combines rows from the Employee and Manager
tables, removing any duplicates.
4. Set Difference (-)
o Finds tuples that are in one relation but not in another.
o Both relations must have the same set of attributes.
o Syntax: R - S
o Example: Employee - Manager
 This finds rows that are in the Employee table but not in
the Manager table.
5. Cartesian Product (×)
o Combines each tuple from one relation with each tuple from
another relation.
o Syntax: R × S
o Example: Employee × Department
 This combines each row from the Employee table with
each row from the Department table, resulting in a large
table with all possible combinations.
6. Rename (ρ)
o Renames a relation or its attributes.
o Syntax: ρ<sub>new_name</sub>(R) or
ρ<sub>new_name(attribute_list)</sub>(R)
o Example: ρ<sub>Emp(emp_id, name)</sub>(Employee)
 This renames the Employee table to Emp and its
attributes to emp_id and name.
Derived Operations
1. Intersection (∩)
o Finds tuples that are common to both relations.
o Both relations must have the same set of attributes.
o Syntax: R ∩ S
o Example: Employee ∩ Manager
 This finds rows that are present in both the Employee and
Manager tables.
2. Join (⨝)
o Combines tuples from two relations based on a related column
between them.
o Syntax: R ⨝<sub>condition</sub> S
o Example: Employee ⨝<sub>Employee.dept_id =
Department.dept_id</sub> Department
 This combines rows from the Employee and Department
tables where the dept_id matches.
3. Division (÷)
o Finds tuples in one relation that match all tuples in another
relation.
o Syntax: R ÷ S
o Example: Employee ÷ Department
 This finds employees who work in all departments listed in
the Department table.

Example
Consider the following tables:
Employee
emp_id name age dept_id

1 Alice 25 101

2 Bob 30 102

3 Charlie 35 101

Department
dept_id dept_name

101 HR

102 IT

1. Selection (σ)
o σ<sub>age > 30</sub>(Employee)
 Result:
emp_id name age dept_id

Charli
3 35 101
e
2. Projection (π)
o π<sub>name, age</sub>(Employee)
 Result:
name age

Alice 25

Bob 30

Charlie 35

3. Join (⨝):
Student
student_i ag
name dept_id
d e

1 Alice 20 101

2 Bob 22 102

3 Charlie 21 103

Department

dept_id dept_name

101 Computer

102 Mathematics

104 Physics

 Student⨝<sub>Student.dept_id=Department.dept_id</sub> Department

o Result:
nam dept_i
student_id age dept_name
e d

1 Alice 20 101 Computer

2 Bob 22 102 Mathematics

4. Union (∪)
The Union operation combines rows from two relations and removes
duplicates. Both relations must have the same schema (same number of
attributes with matching domains).
Let
Manager
emp_i dept_i
name age
d d

2 Bob 30 102

4 David 40 103

Employee ∪ Manager
Result:
ag
emp_id name dept_id
e

1 Alice 25 101

2 Bob 30 102

Charli
3 35 101
e

4 David 40 103

5. Intersection (∩)
The Intersection operation returns only the rows that are common to both
relations. Both relations must have the same schema.
Example
Employee ∩ Manager
Result:
emp_i dept_i
name age
d d

2 Bob 30 102

6. Cartesian Product (×)


The Cartesian Product combines each row from the first relation with every
row from the second relation. The result is a new relation with all possible
combinations of rows from the two input relations.
Employee × Department
Result:
emp_i dept_i dept_i
name age dept_name
d d d

1 Alice 25 101 101 HR

1 Alice 25 101 102 IT

1 Alice 25 101 103 Finance

2 Bob 30 102 101 HR

2 Bob 30 102 102 IT

2 Bob 30 102 103 Finance

Charli
3 35 101 101 HR
e

Charli
3 35 101 102 IT
e

Charli
3 35 101 103 Finance
e

Division in Relational Algebra


Division in relational algebra is a binary operation used to find tuples in one
relation that are associated with all tuples in another relation. It is particularly
useful for queries that involve the concept of "for all" or "all possible."
Notation:
 R÷S
 R÷S

You might also like