0% found this document useful (0 votes)
40 views31 pages

UNIT 2 Dbms Notes

Uploaded by

Mridul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views31 pages

UNIT 2 Dbms Notes

Uploaded by

Mridul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

UNIT 2 notes

Data modelling - data modelling is the process of creating a data model for the
data to be stored in a database. This data model is a conceptual representation of
Data objects, the associations between different data objects, and the rules. Data
modelling helps in the visual representation of data and enforces business rules,
regulatory compliances, and government policies on the data. Data Models
ensure consistency in naming conventions, default values, semantics, security
while ensuring quality of the data.

Data Model

The Data Model is defined as an abstract model that organizes data description,
data semantics, and consistency constraints of data. The data model emphasizes
on what data is needed and how it should be organized instead of what
operations will be performed on data. Data Model is like an architect's building
plan, which helps to build conceptual models and set a relationship between
data items.

Types of Data Models


Types of Data Models: There are mainly three different types of data models:
conceptual data models, logical data models, and physical data models, and
each one has a specific purpose. The data models are used to represent the data
and how it is stored in the database and to set the relationship between data
items.

1. Conceptual Data Model: This Data Model defines WHAT the system
contains. This model is typically created by Business stakeholders and
Data Architects. The purpose is to organize, scope and define business
concepts and rules.
2. Logical Data Model: Defines HOW the system should be implemented
regardless of the DBMS. This model is typically created by Data
Architects and Business Analysts. The purpose is to developed technical
map of rules and data structures.
3. Physical Data Model: This Data Model describes HOW the system will
be implemented using a specific DBMS system. This model is typically
created by DBA and developers. The purpose is actual implementation of
the database.

Conceptual Data Model- ER Model/Diagram

ER Diagram stands for Entity Relationship Diagram, also known as ERD is a


diagram that displays the relationship of entity sets stored in a database. In other
words, ER diagrams help to explain the logical structure of databases. ER
diagrams are created based on three basic concepts: entities, attributes and
relationships.

ER Diagrams contain different symbols that use rectangles to represent entities,


ovals to define attributes and diamond shapes to represent relationships.

ER model helps to systematically analyze data requirements to produce a well-


designed database. The ER Model represents real-world entities and the
relationships between them. Creating an ER Model in DBMS is considered as a
best practice before implementing your database.

ER Modeling helps you to analyze data requirements systematically to produce


a well-designed database. So, it is considered a best practice to complete ER
modeling before implementing your database.

ER Diagrams Symbols & Notations


Components of the ER Diagram

This model is based on three basic concepts:

 Entities
 Attributes
 Relationships

ENTITY

A real-world thing either living or non-living that is easily recognizable and


nonrecognizable. It is anything in the enterprise that is to be represented in our
database. It may be a physical thing or simply a fact about the enterprise or an
event that happens in the real world.

An entity can be place, person, object, event or a concept, which stores data in
the database. The characteristics of entities are must have an attribute, and a
unique key. Every entity is made up of some 'attributes' which represent that
entity.

Examples of entities:

 Person: Employee, Student, Patient


 Place: Store, Building
 Object: Machine, product, and Car
 Event: Sale, Registration, Renewal
 Concept: Account, Course

Entity set:

An entity set is a group of similar kind of entities. It may contain entities with
attribute sharing similar values. Entities are represented by their properties,
which also called attributes. All attributes have their separate values. For
example, a student entity may have a name, age, class, as attributes.

Two types of Entity sets –

 Strong Entity
 Weak entity

Strong Entity Set Weak Entity Set


Strong entity set always has a primary It does not have enough attributes to
key. build a primary key.
It is represented by a rectangle symbol. It is represented by a double rectangle
symbol.
It contains a Primary key represented It contains a Partial Key which is
by the underline symbol. represented by a dashed underline
symbol.
The member of a strong entity set is The member of a weak entity set called
called as dominant entity set. as a subordinate entity set.

Primary Key is one of its attributes In a weak entity set, it is a combination


which helps to identify its member. of primary key and partial key of the
strong entity set.

In the ER diagram the relationship The relationship between one strong


between two strong entity set shown by and a weak entity set shown by using
using a diamond symbol. the double diamond symbol.

The connecting line of the strong entity The line connecting the weak entity set
set with the relationship is single. for identifying relationship is double.

Attributes

A real-world property of an entity type is called an attribute. This is the


characteristics of an entity. It is represented by an oval or ellipse in E-R
diagram. Each attribute can take only a set of permitted values. This is
called the domain of that attribute. For example, we define the roll_no of
the ‘Student’ by a numeric value. So, the permitted values are only
integers and hence, ‘integer’ is the domain of attribute ‘roll_no’. Each
attribute is represented by a separate column in a relational table.
It is a single-valued property of either an entity-type or a relationship-type.

For example, a lecture might have attributes: time, date, duration, place, etc.

An attribute in ER Diagram examples, is represented by an Ellipse

For example, The entity ‘Student’ has properties like Name, Address,
Roll_no, Mobile_no, Age, DOB, Class, Section, etc. So, when we make an E-
R diagram then Name, Address, Roll_no, Mobile_no, Age, DOB, Section and
Class are represented as the attributes of the entity type ‘Student’.
In the above example, we see that we have used various modified oval
symbols to represent the E-R diagram. Various symbols represent various
types of the attribute. The text of one attribute(Roll_no) is underlined, the
other attribute(Email) is written in double oval, etc. We will understand all this
as we go through this blog and learn about various types of attributes and how
they are represented in the E-R diagram.

There are are many types of attributes which are as follows:

 Simple Attribute & Composite Attribute


 Single Valued Attribute & Multi-valued Attribute
 Stored Attribute & Derived Attribute
 Key Attribute & Non-key Attribute

Simple Attribute & Composite Attribute


This division is made on the basis that if the attribute can be further divided or
not into more attributes. If it cannot be further divided it is a simple attribute
and if can be further divided it is a composite attribute.

Simple Attribute

A simple attribute contains an atomic value which cannot be further divided. It


is simply represented by an oval. A simple attribute is directly connected to
the entity type. While making the E-R diagram, we directly connect the oval
with the rectangle.

For example, Roll_no of Student, Age of Student. It is represented in the E-R


diagram as:

Composite Attribute

A Composite attribute can be further divided into other attributes. We


represent this in E-R diagram by connecting an oval with oval i.e the
composite attribute is also represented by oval and the further divided attribute
all also represented by ovals. When we convert this E-R diagram to the
relational table then we don't have any column of the composite attribute. We
have column only for the attribute which came after we further divided the
composite attribute.

For example, Name of a student can be further divided into first name, middle
name and last name. The composite attribute name is also represented by oval
as well as the other attributes are also represented by oval and we connect the
all the further divided attributes with the composite attribute. In the table, we
will have three columns i.e. First_name, Middle_name, and Last_name. There
is no such column called "Name".

Single Valued Attribute & Multi-valued Attribute


This division is made on the basis of how many values can be taken by the
attribute. If the attribute can take more than one value, it is a multi-valued
attribute. If the attribute takes only one value it is a single-valued attribute.

Single-valued Attribute

This attribute has only one value. It is represented by a simple oval. Some
simple attribute can also be a single-valued attribute. For example, the Section
of ‘Student’ is a simple attribute as it can’t be further divided. Also, it is a
single-valued attribute because a student can't study in more than one section.
For example, Section of a Student.

Multivalued Attribute

This attribute has more than one value. It is represented by a double oval.
Some Composite keys can also be a multivalued attribute. For example, the
address attribute of a student can further be divided into ‘Locality’, ‘Town’
and ‘PIN’. So, we call it a composite attribute. Also, the address of a student
can have more than one value. A Student can have more than one Address i.e
Permanent Address and Temporary Address. So, it can also be called a
multivalued attribute.

For example, A student can have more than one e-mail address.

Derived Attribute
This classification is made on the basis that if the attribute is just stored in the
database or can be derived from some other attribute.

The value of this attribute should be provided by the user. This attribute is
stored and can be used for finding the value of other attributes. It cannot be
derived from any other attribute. It is also represented by an oval. For
Example, The Date of Birth of ‘Student’ has to be provided by the student.

Example: Date of Birth(DOB) of Student.


Derived Attribute

The value of this attribute is derived from some other attributes. We know the
value of some other attribute(stored attribute)and from stored attribute, we are
deriving the value of this attribute(derived attribute). This is done mainly
because the value for such attribute keeps on changing. It is represented by a
dashed oval.

For example, The value of age attribute is derived from the DOB(date of
birth) attribute.

Key Attribute & Non-key Attribute


This classification is made on the basis that if the attribute can uniquely
identify the entities or not. As the name suggests key attribute will uniquely
identify the entities whereas the non-key attributes would not be able to
uniquely identify the entities.

Key Attribute

A key attribute is used to uniquely identify the entities of an entity type. In a


relational table, it represents the primary key of the table. It is represented by
oval and the text in it is underlined. Even if all the other attributes of an entity
are the same but the key attribute will always be different.

Example: We have Roll_no as the key attribute of the ‘Student’ because two
students can never has same roll number.
Non-Key Attribute

All the other attributes other than the key attribute are the non-key attributes.
Two or more entities can have the same value for this attribute. For example,
the Class attribute would have the same value for all those students who are
studying in the same class.

Example: Class, Section, Age, Name etc, are the non-key attributes.

Relationship

Relationship is nothing but an association among two or more entities. E.g.,


Tom works in the Chemistry department.

Entities take part in relationships. We can often identify relationships with verbs
or verb phrases.

For example:

 You are attending this lecture


 I am giving the lecture
 Just loke entities, we can classify relationships according to relationship-
types:
 A student attends a lecture
 A lecturer is giving a lecture.
Cardinality

Defines the numerical attributes of the relationship between two entities or


entity sets.

Different types of cardinal relationships are:

 One-to-One Relationships
 One-to-Many Relationships
 May to One Relationships
 Many-to-Many Relationships

1. One to One Relationship

When a single instance of an entity is associated with a single instance of


another entity then it is called one to one relationship. For example, a person has
only one passport and a passport is given to one person.

2. One to Many Relationship

When a single instance of an entity is associated with more than one instances
of another entity then it is called one to many relationship. For example – a
customer can place many orders but a order cannot be placed by many
customers.

3. Many to One Relationship

When more than one instances of an entity is associated with a single instance
of another entity then it is called many to one relationship. For example – many
students can study in a single college but a student cannot study in many
colleges at the same time.

4. Many to Many Relationship

When more than one instances of an entity is associated with more than one
instances of another entity then it is called many to many relationship. For
example, a can be assigned to many projects and a project can be assigned to
many students.

Participation Constraints
 Total Participation − Each entity is involved in the relationship. Total
participation is represented by double lines.
 Partial participation − Not all entities are involved in the relationship.
Partial participation is represented by single lines.

PERSON HAS LICENSE

Partial participation Total participation


Degree of Relationship
The degree of a relationship is the number of entity types that
participate(associate) in a relationship. By seeing an E-R diagram, we can
simply tell the degree of a relationship i.e the number of an entity type that
is connected to a relationship is the degree of that relationship.

For example, If we have two entity type ‘Customer’ and ‘Account’ and they
are linked using the primary key and foreign key. We can say that the degree
of relationship is 2 because here two entities are taking part in the relationship.

Based on the number of entity types that are connected we have the following
degree of relationships:

 Unary
 Binary
 Ternary
 N-ary

Unary (degree 1)
A unary relationship exists when both the participating entity type are the
same. When such a relationship is present we say that the degree of
relationship is 1.

For example, Suppose in a classroom, we have many students who belong to a


particular club-like dance club, basketball club etc. and some of them are club
leads. So, a particular group of student is managed by their respective club
lead. Here, the group is formed from students and also, the club leads are
chosen from students. So, the ‘Student’ is the only entity participating here.
We can represent this relationship using the E-R diagram as follows:

So, here we get the answer to our first question which was asked in the
introduction section. Yes, there can be only one entity type in a relationship
and the minimum degree of a relationship can be one.

Binary (degree 2)
A binary relationship exists when exactly two entity type participates. When
such a relationship is present we say that the degree is 2. This is the most
common degree of relationship. It is easy to deal with such relationship as
these can be easily converted into relational tables.

For example, We have two entity type ‘Customer’ and ‘Account’ where each
‘Customer’ has an ‘Account’ which stores the account details of the
‘Customer’. Since we have two entity types participating we call it a binary
relationship. Also, one ‘Customer’ can have many ‘Account’ but each
‘Account’ should belong to only one ‘Customer’. We can say that it is a one-
to-many binary relationship
Ternary(degree 3)
A ternary relationship exists when exactly three entity type participates. When
such a relationship is present we say that the degree is 3. As the number of
entity increases in the relationship, it becomes complex to convert them into
relational tables.

For example, We have three entity type ‘Employee’, ‘Department’ and


‘Location’. The relationship between these entities are defined as an employee
works in a department, an employee works at a particular location. So, we can
see we have three entities participating in a relationship so it is a ternary
relationship. The degree of this relation is 3.

N-ary (n degree)
An N-ary relationship exists when ‘n’ number of entities are participating. So,
any number of entities can participate in a relationship. There is no limitation
to the maximum number of entities that can participate. But, relations with a
higher degree are not common. This is because the conversion of higher
degree relations to relational tables gets complex. We are making an E-R
model because it can be easily be converted into any other model for
implementing the database. But, this benefit is not available if we use higher
degree relations. So, binary relations are more popular and widely used.
Though we can make a relationship with any number of entity types but we
don't do that.

We represent an N-ary relationship as follows:


In the above example, E1 denotes the first entity type, E2 denotes the second
entity type and so on. R represents the relationship. So, here we have a total of
5 entity type which participates in the relationship. Therefore, the degree of
the above n-ary relationship is 5.

Logical data modelling

3 types of models

 Hierarchical Model
 Network Model
 Relational Model

(Refer PPT for this topic)

Diagrams of these models:

Herarchical model
In hierarchical model, data is organised into tree-like structure with one one-
to-many relationship between two different types of data, for example, one
department can have many courses, many professors and of-course many
students.
Relational Model
Keys in DBMS

KEYS in DBMS is an attribute or set of attributes which helps you to identify a


row(tuple) in a relation(table). They allow you to find the relation between two
tables. Keys help you uniquely identify a row in a table by a combination of one
or more columns in that table. Key is also helpful for finding unique record or
row from the table. Database key is also helpful for finding unique record or
row from the table.

Why we need a Key?

Here are some reasons for using sql key in the DBMS system.

 Keys help you to identify any row of data in a table. In a real-world


application, a table could contain thousands of records. Moreover, the
records could be duplicated. Keys ensure that you can uniquely identify a
table record despite these challenges.
 Allows you to establish a relationship between and identify the relation
between tables
 Help you to enforce identity and integrity in the relationship.

Types of Keys in Database Management System

There are mainly seven different types of Keys in DBMS and each key has it’s
different functionality:

 Super Key - A super key is a group of single or multiple keys which

identifies rows in a table.

 Primary Key - is a column or group of columns in a table that uniquely

identify every row in that table.

 Candidate Key - is a set of attributes that uniquely identify tuples in a

table. Candidate Key is a super key with no repeated attributes.

 Alternate Key - is a column or group of columns in a table that uniquely

identify every row in that table.

 Foreign Key - is a column that creates a relationship between two tables.

The purpose of Foreign keys is to maintain data integrity and allow

navigation between two different instances of an entity.


 Compound Key - has two or more attributes that allow you to uniquely

recognize a specific record. It is possible that each column may not be

unique by itself within the database.

 Composite Key - An artificial key which aims to uniquely identify each

record is called a surrogate key. These kind of key are unique because

they are created when you don't have any natural primary key.

 Surrogate Key - An artificial key which aims to uniquely identify each

record is called a surrogate key. These kind of key are unique because

they are created when you don't have any natural primary key.

What is the Super key?

 A superkey is a group of single or multiple keys which identifies rows in


a table. A Super key may have additional attributes that are not needed
for unique identification.
 Example:

EmpSSN EmpNum Empname

9812345098 AB05 Shown

9876512345 AB06 Roslyn

199937890 AB07 James

 In the above-given example, EmpSSN and EmpNum , Empname are


superkeys.

What is a Primary Key?

PRIMARY KEY is a column or group of columns in a table that uniquely


identify every row in that table. The Primary Key can't be a duplicate meaning
the same value can't appear more than once in the table. A table cannot have
more than one primary key.

Rules for defining Primary key:

 Two rows can't have the same primary key value


 It must for every row to have a primary key value.
 The primary key field cannot be null.
 The value in a primary key column can never be modified or updated if
any foreign key refers to that primary key.

Example:

In the following example, <code>StudID</code> is a Primary Key.

StudID Roll No First Name LastName Email

1 11 Tom Price [email protected]

2 12 Nick Wright [email protected]

3 13 Dana Natan [email protected]

What is the Alternate key?

ALTERNATE KEYS is a column or group of columns in a table that uniquely


identify every row in that table. A table can have multiple choices for a primary
key but only one can be set as the primary key. All the keys which are not
primary key are called an Alternate Key.

Example:

In this table, StudID, Roll No, Email are qualified to become a primary key. But
since StudID is the primary key, Roll No, Email becomes the alternative key.

StudID Roll No First Name LastName Email

1 11 Tom Price [email protected]

2 12 Nick Wright [email protected]

3 13 Dana Natan [email protected]


What is a Candidate Key?

CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table.


Candidate Key is a super key with no repeated attributes. The Primary key
should be selected from the candidate keys. Every table must have at least a
single candidate key. A table can have multiple candidate keys but only a single
primary key.

Properties of Candidate key:

 It must contain unique values


 Candidate key may have multiple attributes
 Must not contain null values
 It should contain minimum fields to ensure uniqueness
 Uniquely identify each record in a table

Example: In the given table Stud ID, Roll No, and email are candidate keys
which help us to uniquely identify the student record in the table.

StudID Roll No First Name LastName Email

1 11 Tom Price [email protected]

2 12 Nick Wright [email protected]

3 13 Dana Natan [email protected]

What is the Foreign key?

FOREIGN KEY is a column that creates a relationship between two tables.


The purpose of Foreign keys is to maintain data integrity and allow navigation
between two different instances of an entity. It acts as a cross-reference between
Teacher ID Fname Lname

B002 David Warner

B017 Sara Joseph

B009 Mike Brunton

two tables as it references the primary key of another table.

Example:

DeptCode DeptName

001 Science

002 English

005 Computer

In this key in dbms example, we have two table, teach and department in a
school. However, there is no way to see which search work in which
department.

In this table, adding the foreign key in Deptcode to the Teacher name, we can
create a relationship between the two tables.

Teacher ID DeptCode Fname Lname

B002 002 David Warner

B017 002 Sara Joseph

B009 001 Mike Brunton

This concept is also known as Referential Integrity.

What is the Compound key?

COMPOUND KEY has two or more attributes that allow you to uniquely
recognize a specific record. It is possible that each column may not be unique
by itself within the database. However, when combined with the other column
or columns the combination of composite keys become unique. The purpose of
the compound key in database is to uniquely identify each record in the table.

Example:

OrderNo PorductID Product Name Quantity

B005 JAP102459 Mouse 5

B005 DKT321573 USB 10

B005 OMG446789 LCD Monitor 20

B004 DKT321573 USB 15

B002 OMG446789 Laser Printer 3

In this example, OrderNo and ProductID can't be a primary key as it does not
uniquely identify a record. However, a compound key of Order ID and Product
ID could be used as it uniquely identified each record.

What is the Composite key?

COMPOSITE KEY is a combination of two or more columns that uniquely


identify rows in a table. The combination of columns guarantees uniqueness,
though individually uniqueness is not guaranteed. Hence, they are combined to
uniquely identify records in a table.

The difference between compound and the composite key is that any part of the
compound key can be a foreign key, but the composite key may or maybe not a
part of the foreign key.

What is a Surrogate key?

SURROGATE KEYS is An artificial key which aims to uniquely identify each


record is called a surrogate key. This kind of partial key in dbms is unique
because it is created when you don't have any natural primary key. They do not
lend any meaning to the data in the table. Surrogate key is usually an integer. A
surrogate key is a value generated right before the record is inserted into a table.
Fname Lastname Start Time End Time

Anne Smith 09:00 18:00

Jack Francis 08:00 17:00

Anna McLean 11:00 20:00

Shown Willam 14:00 23:00

Above, given example, shown shift timings of the different employee. In this
example, a surrogate key is needed to uniquely identify each employee.

Surrogate keys in sql are allowed when

 No property has the parameter of the primary key.


 In the table when the primary key is too big or complicated.

Keys in DBMS (Example discussed in class)

Erpid (pk) Prn no. name


1 222 Karan
2 789 Karan
3 564 Hanisha
4 341 Ansh

1. Super key – i)set of attributes which can uniquely identify a tuple. ii) A
super key may have additional attributes that are not needed for unique
identification.
Erpid
Prn no.
Erpid + prn no
Erpid + prnno. + name - trivial sk
Erpid + name
Prn no. + name
2. Candidate key – i) is a minimal super key. Ii) no additional attribute is
required
Erpid
Prn no.

3. Primary key - i)The candidate key chosen to uniquely identify each


tuple in a relation. Ii) Pk value can’t be null.
Erpid

4. Foreign key – is an attribute in a table which is used to define


relationship with another table.
Deptid (pk) deptname Erpid (fk) Hod name
1 CS 1 Abc
2 IT 1 Def

5. Composite key – any key with more than one attribute.

Erpid + prn no

Erpid + prnno. + name - trivial sk

Erpid + name

Prn no. + name

6. Compound key – if a composite key has at least one attribute which is a


foreign key.

Erpid + prn no

Erpid + prnno. + name - trivial sk

Erpid + name

7. Surrogate key – If a relationship has no attribute which can be used to


identify the data stored in it,then we create an attribute for this pupose.

It adds no meaning to the data but serves the sole purpose of identifying
rows uniquely in a table.
For eg: In this below table noth student name and city are not uniquely
defines the the row so now we have to add Sno. To uniquely identify the
row. Therefore, Sno.is a surrogate key.

Student Name City


Neha Delhi
Rohan Delhi
Khushi Mumbai
Khushi Delhi

Now adding S.no attribute

Sno. Student Name City


1 Neha Delhi
2 Rohan Delhi
3 Khushi Mumbai
4 Khushi Delhi

You might also like