Unit - II Dbms
Unit - II Dbms
You need to study the files, forms, reports, data currently maintained by the organization
to identify attributes. You can also conduct interviews with various stakeholders to
identify entities. Initially, it's important to identify the attributes without mapping them to a
particular entity.
Once, you have a list of Attributes, you need to map them to the identified entities.
Ensure an attribute is to be paired with exactly one entity. If you think an attribute should
belong to more than one entity, use a modifier to make it unique.
Once the mapping is done, identify the primary Keys. If a unique key is not readily
available, create one.
For Course Entity, attributes could be
Duration, Credits, Assignments, etc.
For the sake of ease we have
considered just one attribute.
All of these are just an employee of a company, however their role is completely different
and they have few different attributes. Just for the example, I have shown that
Technician handles service requests, Engineer works on a project and Accountant
handles the credit & debit details. All of these three employee types have few attributes
common such as name & salary which we had left associated with the parent entity
“Employee” as shown in the diagram.
Aggregation
Aggregation is a process in which a single entity alone is not
able to make sense in a relationship so the relationship
of two entities acts as one entity.
A manager not only manages the employee
working under them but he has to manage the project as well.
In such scenario if entity “Manager” makes a “manages”
relationship with either “Employee” or “Project”
entity alone then it will not make any sense because
he has to manage both. In these cases the relationship
of two entities acts as one entity. In this, the relationship
“Works-On” between “Employee” & “Project” acts as one entity
that has a relationship “Manages” with the entity “Manager”.
Strong and Weak Entities
Strong entity
● Strong Entity is independent of any other entity in the schema
Example – A student entity can exist without needing any other entity in the schema or a course entity can exist without needing any other entity in the
schema
● A Strong entity is nothing but an entity set having a primary key attribute or a table that consists of a primary key column
Representation
○ Consider the ER diagram which consists of two entities student and course
○ Student entity is a strong entity because it consists of a primary key called student id which is enough for accessing each record
uniquely
○ In the same way, the course entity contains of course ID attribute which is capable of uniquely accessing each row.
Weak entity
● A weak entity is an entity set that does not have sufficient attributes for Unique Identification of its records.
Example 1 – A loan entity can not be created for a customer if the customer doesn’t exist
Example 2 – A dependents list entity can not be created if the employee doesn’t exist
● Simply a weak entity is nothing but an entity that does not have a primary key attribute
● It contains a partial key called a discriminator which helps in identifying a group of entities from the entity set
Representation
○ The double diamond symbol is used for representing the relationship between a strong entity and a weak entity which is known as
identifying relationship
Example for weak entity
○ In the ER diagram, we have two entities Employee and Dependents.
○ Employee is a strong entity because it has a primary key attribute called Employee number (Employee_No) which is capable of
Strong entity always has a primary key. It will not have a primary key but it has partial discriminator
key.
It is not dependent on any other entity. Weak entity is dependent on the strong entity.
Relationship between two strong entities is Relationship between a strong entity and the weak entity is
represented by a single diamond. represented by double Diamond.
A strong entity may or may not have total It has always total participation.
participation.
What is an EER diagram?
An EER diagram provides you with all the elements of an ER diagram while adding:
Overall, an EER diagram builds off of an ER diagram by including elements that allow for aggregation,
generalization, and specialization.
Keys - A key is a way to categorize attributes in an E-R diagram
EER Diagram
An EER diagram provides you with all the elements of an ER diagram while adding:
Generalization and specialization act as opposites of one another. Generalization combines lower-level
entities into one of a higher level. Meanwhile, specialization divides high-level entities into lower levels.
With aggregation, two entities are treated as a single one.
Types of Keys
1. Candidate Key
2. Primary Key
3. Foreign Key
4. Super Key
5. Alternate Key
6. Composite Key
7. Unique Key
1. Primary Key
The primary key refers to a column or a set of columns of a table that helps us
identify all the records uniquely present in that table. A table can consist of just one
primary key. Also, this primary key cannot consist of the same values
reappearing/repeating for any of its rows. All the values of a primary key have to
be different, and there should be no repetitions.
A super key refers to the set of all those keys that help us uniquely identify
all the rows present in a table. It means that all of these columns present in
a table that can identify the columns of that table uniquely act as the super
keys.
4. Alternate Key
As we have stated above, any table can consist of multiple choices for the primary
key. But, it can only choose one. Thus, all those keys that did not become a primary
key are known as alternate keys.
5. Foreign Key
We use a foreign key to establish relationships between two available tables. The foreign key
would require every value present in a column/set of columns to match the referential table’s
primary key. A foreign key helps us to maintain data as well as referential integrity.
6. Composite Key
The composite key refers to a set of multiple attributes that help us uniquely identify every
tuple present in a table. The attributes present in a set may not be unique whenever we
consider them separately. Thus, when we take them all together, it will ensure total
uniqueness.
7. Unique Key
A unique key refers to a column/a set of columns that identify every record uniquely
in a table. All the values in this key would have to be unique. Remember that a
unique key is different from a primary key. It is because it is only capable of having
one null value. A primary key, on the other hand, cannot have a null value.
Key Type Definition Example
Candidate Key A minimal super key that can act Employee ID, Email Address
as a primary key.
Alternate Key A candidate key is not chosen as Email Address (if Employee ID is
the primary key. primary)
● The Primary Key field shouldn’t be left NULL; the Primary Key column must contain
a value.
● In that column, no two rows in the table may contain identical values.
● If a foreign key in a DBMS refers to the primary Key, no value may be altered or
modified in this primary key column
A primary key is a column that allows each entry in a A column that can uniquely identify each record in a
database to be identified only once. It is selected as database is sometimes referred to as a candidate key.
the primary key for the table from the list of potential It might be used as the primary key.
keys.
A primary key is a unique identifier for a record in a A candidate key is also a unique identifier for a record
database table. in a database table.
There can be only one primary key in a table. There can be multiple candidate keys in a table.
The primary key enforces entity integrity, Candidate keys are potential candidates
The primary key is chosen by the database designer or A candidate key can be chosen from the set of
administrator. candidate keys for a table.
Importance of candidate keys in DBMS
These keys in DBMS are essential for preserving a database’s consistency and integrity. They are used to enforce connections
between tables and guarantee that each row in a database is distinct and can be recognized independently.
3. Super Key
The collection of all keys enabling us to recognize every row in the table is a super key.
This type of key in DBMS specifies that all the table columns that may identify the
columns uniquely function as the super keys.
Properties of a super key
● A super key must ensure that each record in a table is unique.
● Nevertheless, the minimal collection of characteristics that can guarantee
uniqueness should be a super key.
● Multiple subsets that are likewise regarded as super keys can exist for a super key.
Patients Table
03 Feb2022 6700
04 Apr2022 8900
03 Feb2022 6700
04 Apr2022 8900
Super Key
Super Key is a set of attributes that can uniquely identify a table. A single table can have multiple super keys. A
candidate key, primary key and a unique key can be a super key, but the reverse does not hold true.
Example:
In our above example we have chosen the PatientID, MedInsuranceNo and Aadhar No to uniquely identify tuples. So
the super key set will be as follows:
{PatientID}
{MedInsuranceNo}
{AadharNo}
{PatientID, MedInsuranceNo}
{PatientID, AadharNo}
{MedInsuranceNo, AadharNo}
The Social Security number is actually the primary key of the table, but we also issue each person an employee
number that is unique. Under these circumstances, ORDERS.EmployeeNo can be a foreign key pointing to
EMPLOYEES.EmployeeNo, even though the latter column is not a primary key. So, the actual rule is slightly more
5. Alternate key
An alternate key is essentially all the keys in a data table that are not the primary key.
Remember that only one key can be set as the primary key. Once the primary key is
established, all others are now considered alternate keys. In the example above, the
Social Security number is the identified primary key and all other keys are alternate.
6. Surrogate key
A surrogate key is an artificial key that is created strictly for the purposes of data analysis. It's sometimes also called a
synthetic key or a pseudokey because it isn't derived from any application data. Instead, it is used to identify each
7. Composite key
A composite key is a key that has more than one attribute. Any super key, primary key or candidate key can be a
composite key as long as it meets the requirement of having more than one attribute. By combining two or more
columns in a table into a key, it guarantees that the key refers to a specific row of data. This is the primary use of
8. Compound key
Construct an E-R diagram for a hospital with a set of patients and a set of medical
doctors. Associate with each patient a log of the various tests and examinations
conducted.
Construct an ER Diagram for Car Insurance Company whose customers own one
or more cars each. Each car has associated with it zero to any number of
recorded accidents.
Design an ER Diagram for keeping track of the exploits of your favorite sports
team. You should store the matches played, the scores in each match, the players
in each match and individual player statistics for each match. Summary statistics
should be modeled as derived attributes.
ER Diagram Assignment
College offers course in different stream. In each stream, various students are
enrolled. Each stream is divided into 8 semesters and each semester is divided
into two sections. In each semester various subjects are taught. One subject is
taught by at most one lecturer and one lecturer can teach more than one subjects.
ER Diagram Assignment
A publishing company produces books on various subjects. The books are written
by authors who specialize in one particular subject. The company employs editors
who, not necessarily being specialists in a particular area, each take sole
responsibility for editing one or more book publications. Every book require some
items for publication. These items supplied by suppliers. One supplier can supply
many items. Shop owner buys books from the publisher. Shop owner can buy
many books but one book can be bought by one shop owner only. Books are
uniquely identified by Bookid.
ER Diagram to Tables
Following rules are used for converting an ER diagram into the tables-
Rule-01: For Strong Entity Set With Only Simple Attributes-
A strong entity set with only simple attributes will require only one table in relational
model.
● Attributes of the table will be the attributes of the entity set.
● The primary key of the table will be the key attribute of the entity set.
Schema : Student ( Roll_no , Name , Sex )
Roll_no Name Sex
Rule-02: For Strong Entity Set With Composite Attributes-
● A strong entity set with any number of composite attributes will require only
one table in relational model.
● While conversion, simple attributes of the composite attributes are taken into
account and not the composite attribute itself.
Roll_no First_name Last_name House_no Street City
A strong entity set with any number of multi valued attributes will require two tables
in relational model.
● One table will contain all the simple attributes with the primary key.
● Other table will contain the primary key and all the multi valued attributes.
Roll_no City
Roll_no Mobile_no
Rule-04: Translating Relationship Set into a Table-
A relationship set will require one table in the relational model.
Attributes of the table are-
● Primary key attributes of the participating entity sets
● Its own descriptive attributes if any.
Set of non-descriptive attributes will be the primary key.
Emp_no Dept_id since Schema : Works in ( Emp_no , Dept_id , since )
NOTE-
If we consider the overall ER diagram, three tables will be required in relational model-
Way-02: A ( a1 , a2 ) 2. BR ( a1 , b1 , b2 )
Rule-06: Convert Binary Relationship With Both Cardinality Constraints and
Participation Constraints into the table.
The total participation constraint acquires NOT NULL constraint foreign key.
Case-01: In Binary Relationship (Cardinality Constraint and Total Participation Constraint) From One Side (cardinality ratio
= 1 : n)
FInd the minimum number of tables required for the
following ER diagram in relational model.
Codd’s Rules
Codd's 12 rules are used to determine whether a relational database management
system is a true relational database management system or not.
Codd's 12 rules form the foundation for evaluating a true relational database
management system.
These rules ensure consistency, accessibility, and integrity of data within the database.
Compliance with these rules guarantees the reliability and effectiveness of the database
management system.
Understanding and implementing Codd's rules are crucial for database designers and
administrators to build robust and efficient relational databases.
Rule 0: The Foundation Rule
For a system to be qualified as a relational database management system, it must be able to manage databases
entirely through its relational capabilities.
This constraint prevents any row from having duplicate or null values in the primary key column,
which is essential for data accuracy and integrity.
In the Employee_ID column, an entity integrity constraint ensures that each employee has a unique
identifier. This prevents two employees from sharing the same Employee_ID, which could lead to data
confusion.
Entity integrity is crucial in scenarios like payroll processing, where each employee must be easily
distinguishable by their unique ID.
Referential Integrity Constraint
Referential integrity constraints manage relationships between tables using foreign keys. These
constraints ensure that a foreign key value in one table matches a primary key value in another
table. This maintains consistent relationships between tables and prevents DBMS anomalies.
Practical Application:
Roll_No Name Course The Roll_No column is a primary key, ensuring that every
student has a unique registration number. This prevents two
1001 Aarav Sharma Math students from being assigned the same roll number, which
could create confusion during exams or grades tracking.
1002 Priya Patel Science
Key constraints prevent duplication, keeping your student
1003 Rohan Verma English database accurate and reliable.
Enterprise Constraints
View:
A virtual table that does not physically store data but rather generates data on-demand based on a SQL query.
Allows users to see a customized subset of data from the base tables, potentially hiding sensitive information
or simplifying complex data access.
Can be used to restrict access to specific data based on user permissions.
A schema can be defined as the design of a
database. The overall description of the database
is called the database schema. It can be
categorized into three parts. These are:
Physical Schema
Logical Schema
View Schema
Example 1: Online Shopping System
Description: This diagram models an online shopping system. Customers place orders,
each of which contains one or more products. Products belong to categories for easy
classification and search.
Example 2: University Database
Entities: Student, Course, Professor, Department, Enrollment.
Relationships: Student 'enrolls in' Course, Course 'taught by'
Professor, Professor 'works in' Department.
In this schema:
The Student table represents the students who enroll in courses.
The Department table represents the departments in which
professors work.
The Professor table represents the professors who teach courses.
Each professor works in a department, which is represented by
the foreign key DepartmentID.
The Course table represents the courses in which students enroll.
Each course is taught by a professor, who is represented by the
foreign key ProfessorID.
The Enrollment table represents the many-to-many relationship
between students and courses. Each row in this table represents a
student enrolled in a course and the grade they received in that
course.
Description: This diagram models a university database.
Students enroll in courses, which are taught by professors.
Professors work in departments, indicating their specialties.
Example 3: E-Learning Platform
Entities: Student, Course, Instructor, Assignment, Enrollment.
Relationships: Student 'takes' Course, Course 'taught by' Instructor,
Course 'has' Assignment, Enrollment "has" Course and Student.
In this schema:
The Student table represents the students who take courses.
The Instructor table represents the instructors who teach courses.
The Course table represents the courses that students can take. Each
course is taught by an instructor, who is represented by the foreign
key InstructorID.
The Enrollment table represents the many-to-many relationship
between students and courses. Each row in this table represents a
student taking a course.
The Assignment table represents the assignments for each course.
Each assignment is associated with a course, which is represented by
the foreign key CourseID. The DueDate field represents the due date
for the assignment.
Description: This diagram models an e-learning platform. Students
take courses, which are taught by instructors. Each course may have
one or more assignments.
Problem Statement:
University Database Management System creates, manages and
performs all the activities related to the database of a given
university. The database consists of information about the
university, colleges, students faculties, academic and research
Programs. The main aim of this project is to manage the
database in such a way that information about academic and
research activities can be retrieved easily, efficiently and
accurately