You are on page 1of 17

DATABASE MANAGEMENT SYSTEM

MODULE 2
Data Modelling
Data modeling (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 modeling 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.

Conceptual Data Model


A Conceptual Data Model is an organized view of database concepts and their relationships. The purpose of
creating a conceptual data model is to establish entities, their attributes, and relationships. In this data
modeling level, there is hardly any detail available on the actual database structure. Business stakeholders
and data architects typically create a conceptual data model.
The 3 basic tenants of Conceptual Data Model are
• Entity: A real-world thing
• Attribute: Characteristics or properties of an entity
• Relationship: Dependency or association between two entities
Data model example:
• Customer and Product are two entities. Customer number and name are attributes of the Customer
entity
• Product name and price are attributes of product entity
• Sale is the relationship between the customer and product

Characteristics of a conceptual data model


• Offers Organisation-wide coverage of the business concepts.
• This type of Data Models are designed and developed for a business audience.
• The conceptual model is developed independently of hardware specifications like data storage
capacity, location or software specifications like DBMS vendor and technology. The focus is to
represent data as a user will see it in the “real world.”
Entities
Entity in DBMS can be a real-world object with an existence, For example, in a College database, the entities
can be Professor, Students, Courses, etc.
Entities has attributes, which can be considered as properties describing it, for example, for Professor entity,
the attributes are Professor_Name, Professor_Address, Professor_Salary, etc. The attribute value gets
stored in the database.

Example of Entity in DBMS


Let us see an example −
<Professor>

Professor_ID Professor_Name Professor_City Professor_Salary

P01 Tom Sydney $7000

P02 David Brisbane $4500

P03 Mark Perth $5000

Here, Professor_Name, Professor _Address and Professor _Salary are attributes.


Professor_ID is the primary key

Types of DBMS Entities


The following are the types of entities in DBMS −

Strong Entity
The strong entity has a primary key. Weak entities are dependent on strong entity. Its existence is not
dependent on any other entity.
Strong Entity is represented by a single rectangle −

In the above example, the "Customer" is the entity type with attributes such as ID, Name, Gender, and Phone
Number. Customer is a strong entity type as it has a unique ID for each customer.
Weak Entity
The weak entity in DBMS do not have a primary key and are dependent on the parent entity. It mainly
depends on other entities.
Weak Entity is represented by double rectangle −

In the above example, "Address" is a weak entity type with attributes such as House No., City, Location, and
State.
The relationship between a strong and a weak entity type is known as an identifying relationship.
Using a double diamond, the Entity-Relationship Diagram represents a relationship between the strong and
the weak entity type.
Let us see an example of the relationship between the Strong entity type and weak entity type with the help
of ER Diagram:

Attribute
In a database management system (DBMS), an attribute is a piece of data that describes an entity. In a
student database, the attributes might be roll no., name, and age.
Types of attributes

Simple Attributes
Simple attributes in an ER model diagram are independent attributes that can't be classified further and also,
can't be subdivided into any other component. These attributes are also known as atomic attributes.
Example Diagram:

As we can see in the above example, Student is an entity represented by a rectangle, and it consists of
attributes: Roll_no, class, and Age. Also, there is a point to be noted that we can't further subdivide the
Roll_no attribute and even the other two attributes into sub-attributes. Hence, they are known as simple
attributes of the Student entity.

Composite Attributes
Composite attributes have opposite functionality to that of simple attributes as we can further subdivide
composite attributes into different components or sub-parts that form simple attributes. In simple terms,
composite attributes are composed of one or more simple attributes.
Example Diagram:

As we can see in the above example, Address is a composite attribute represented by an elliptical shape,
and it can be further subdivided into many simple attributes like Street, City, State, Country, Landmark, etc.
Single-Valued Attributes
Single valued attributes are those attributes that consist of a single value for each entity instance and can't
store more than one value. The value of these single-valued attributes always remains the same, just like the
name of a person.
Example Diagram:

As we can see in the above example, Student is an entity instance, and it consists of attributes: Roll_no, Age,
DOB, and Gender. These attributes can store only one value from a set of possible values. Each entity instance
can have only one Roll_no, which is a unique, single DOB by which we can calculate age and also fixed gender.
Also, we can't further subdivide these attributes, and hence, they are simple as well as single-valued
attributes.

Multi-Valued Attributes
Multi-valued attributes have opposite functionality to that of single-valued attributes, and as the name
suggests, multi-valued attributes can take up and store more than one value at a time for an entity instance
from a set of possible values. These attributes are represented by co-centric elliptical shape, and we can also
use curly braces { } to represent multi-valued attributes inside it.
Example Diagram:

As we can see in the above example, the Student entity has four attributes: Roll_no and Age are simple as
well as single-valued attributes as discussed above but Mob_no and Email_id are represented by co-centric
ellipse are multi-valued attributes. Each student in the real world can provide more than one email-id as well
as a mobile contact number, and therefore, we need these attributes to be multi-valued so that they can
store multiple values at a time for an entity instance.

Derived Attributes
Derived attributes in DBMS are those attributes whose values can be derived from the values of other
attributes. They are always dependent upon other attributes for their value.
For example, As we were discussing above, DOB is a single-valued attribute and remains constant for an
entity instance. From DOB, we can derive the Age attribute, which changes every year, and can easily
calculate the age of a person from his/her date of birth value. Hence, the Age attribute here is derived
attribute from the DOB single-valued attribute.
Example Diagram:

Derived attributes are always represented by dashed or dotted elliptical shapes.

Key Attributes
Key attributes are special types of attributes that act as the primary key for an entity and they can uniquely
identify an entity from an entity set. The values that key attributes store must be unique and non-repeating.
Example Diagram:

As we can see in the above example, we can say that the Roll_no attribute of the Student entity is not only
simple and single-valued attribute but also, a key valued attribute as well. Roll_no of a student will always be
unique to identify the student. Also note that the Gender and Age of two or more persons can be same and
overlapping in nature and obviously, we can't identify a student on the basis of them.
Hence, gender and age are not key-valued attributes.

Relationship
In a Database Management System (DBMS), a relationship is the way two or more data sets are linked. This
is especially true for Relational Database Management Systems (RDBMS). One dataset may be termed as the
Foreign key and the ones linked to it may be termed the Primary Key. There may be multiple Foreign and
Primary keys linked to each other.
There are three types of relationships that can exist between two entities, which are given below and also
discussed in this article with examples and diagrams.
• One-to-One relationship
• One-to-Many relationship
• Many-to-Many relationship
One-to-One Relationship
According to this relationship, a single record in Table A is related to a single record in Table B, and vice-
versa is also true.
Example: Consider 2 entities -
Person - ID, name, age, address
Passport - passport_ID, passport_no

Only one passport is allowed per person, and each passport belongs to only one person. Therefore it is a one-
to-one relationship.
Example :
Let’s consider a database of an online bookstore. The main table is “Users,” which stores basic user
information, and an auxiliary table is “UserProfiles,” which stores additional information about the users that
not all users may have.
‘Users’ Table

UserID Username Email

1 john_doe john_doe@gmail.com

2 john_doe john_doe@gmail.com

‘UserProfiles’ Table:

ProfileID UserID Address Date Of Birth

1 1 123 Elm St. 1990-01-01

2 2 456 Oak St. 1992-02-02

One-to-Many or Many-to-One Relationship


According to this relationship, each record of Table A can be related to one or more than one record of
Table B.
Example: Consider 2 entities -

Each customer can have multiple accounts, so it is a one-to-many relationship.


But if we see the other way, many accounts are held by one customer only. Therefore it is a many-to-one
relationship also.
Example :
Let's use an example of a ‘Library’ database system with two tables: ‘Authors’ and ‘Books’.
‘Authors’ Table:

AuthorID AuthorName

1 Agatha Christie

2 J.K. Rowling

3 Mark Twain

‘Books’ Table:

BookID Title AuthorID

1 Murder on the Orient Express 1

2 The ABC Murders 1

3 Harry Potter and the Philosopher's Stone 2

4 Harry Potter and the Chamber of Secrets 2

5 The Adventures of Tom Sawyer 3

In this example:
‘AuthorID’ is a primary key in the Authors table.
'BookID' is a primary key in the Books table.
‘AuthorID’ in the Books table is a foreign key because it references the primary key of the Authors table.
Many-to-Many Relationship
According to this relationship, each record of Table A can be related to one or more than one record of
table B and vice versa is also true, i.e., each record of Table B can be connected to one or more than one
record of table A. Example: Consider 2 entities -
Each customer can buy more than 1 product, and many customers can buy one. Therefore it is a many-to-
many relationship.
Example :
Let's consider a database for a school system where we have ‘Students’ and ‘Courses.’ A student can enroll
in many courses, and a course can have many students.
‘Students’ Table

StudentID StudentName

1 Akash

2 Abhishek

3 Atul

‘Courses’ Table

CourseID CourseName

1 Computer Science

2 Physics

3 Chemistry

‘Enrollments’ Junction Table:

EnrollmentID StudentID CourseID

1 1 1

2 1 3

3 2 2

4 3 1

5 3 2

6 3 3

In this example:
• StudentID is the primary key in the Students table
• CourseID is the primary key in the Courses table.
• Enrollments are the junction table with its primary key, EnrollmentID.
• StudentID and CourseID in the Enrollments table are foreign keys referencing the respective primary
keys in the Students and Courses tables.

ER Diagrams Symbols & Notations


Since we know the entity-relationship diagram has entities, attributes, and the relationship between the
data. However, all these components of the ER diagram are represented with the help of certain symbols.
Three symbols are used to make an ER diagram, which are, rectangle, oval, and diamond.
However, all other symbols that are used to make an ER diagram are sub-elements and are based on the
main elements of the ER diagram.
Let us discuss each symbol and its usage in an ER diagram :
• Rectangle :
It is used to represent the entities in an entity-relationship diagram.

• Ellipses/Oval :
This symbol is used to represent the attributes in an entity-relationship diagram.

• Diamond :
This symbol is used to represent the type of relationship that exists between the entities such
as one-to-one, many-to-one, and many-to-many.

• Lines :
It links the entities to the relationship types whereas the attributes to the entity types.

• Double Ellipses :
It is used to represent a multivalued attribute.

• Double rectangle :
It is used to represent a weak entity.

• Double diamond :
It is used to represent a weak relationship between entities.

Note :
To represent a primary key in an entity-relationship diagram, the attributes are underlined.
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.
Example:

Employee ID FirstName LastName

11 Andrew Johnson

22 Tom Wood

33 Alex Hale

In the above-given example, employee ID is a primary key because it uniquely identifies an employee
record. In this table, no other employee can have the same employee ID.
Types of Keys in DBMS
There are mainly Eight different types of Keys in DBMS and each key has it’s different functionality:
1. Super Key
2. Primary Key
3. Candidate Key
4. Alternate Key
5. Foreign Key
6. Compound Key
7. Composite Key
8. Surrogate Key
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 name are superkeys.


Candidate Key
CANDIDATE KEY in SQL 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 in SQL may have multiple attributes
• Must not contain null values
• It should contain minimum fields to ensure uniqueness
• Uniquely identify each record in a table
Candidate key 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 abc@gmail.com

2 12 Nick Wright xyz@gmail.com

3 13 Dana Natan mno@yahoo.com

Primary Key
PRIMARY KEY in DBMS 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, StudID is a Primary Key.
StudID Roll No First Name LastName Email

1 11 Tom Price abc@gmail.com

2 12 Nick Wright xyz@gmail.com

3 13 Dana Natan mno@yahoo.com

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 abc@gmail.com

2 12 Nick Wright xyz@gmail.com

3 13 Dana Natan mno@yahoo.com

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 two tables as it references the primary key of another table.
Example:

DeptCode DeptName

001 Science

002 English

005 Computer

Teacher ID Fname Lname

B002 David Warner

B017 Sara Joseph

B009 Mike Brunton

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.


Difference Between Primary key & Foreign key
Following is the main difference between primary key and foreign key:

Primary Key Foreign Key

It is a field in the table that is the primary key of another


Helps you to uniquely identify a record in the table.
table.

Primary Key never accept null values. A foreign key may accept multiple null values.

Primary key is a clustered index and data in the DBMS A foreign key cannot automatically create an index,
table are physically organized in the sequence of the clustered or non-clustered. However, you can manually
clustered index. create an index on the foreign key.

You can have the single Primary key in a table. You can have multiple foreign keys in a table.

Specialization
o Specialization is a top-down approach, and it is opposite to Generalization. In specialization, one
higher level entity can be broken down into two lower level entities.
o Specialization is used to identify the subset of an entity set that shares some distinguishing
characteristics.
o Normally, the superclass is defined first, the subclass and its related attributes are defined next, and
relationship set are then added.
For example: In an Employee management system, EMPLOYEE entity can be specialized as TESTER or
DEVELOPER based on what role they play in the company.
Generalization
o Generalization is like a bottom-up approach in which two or more entities of lower level combine to
form a higher level entity if they have some attributes in common.
o In generalization, an entity of a higher level can also combine with the entities of the lower level to
form a further higher level entity.
o Generalization is more like subclass and superclass system, but the only difference is the approach.
Generalization uses the bottom-up approach.
o In generalization, entities are combined to form a more generalized entity, i.e., subclasses are
combined to make a superclass.
For example, Faculty and Student entities can be generalized and create a higher level entity Person.

Aggregation
In aggregation, the relation between two entities is treated as a single entity. In aggregation, relationship
with its corresponding entities is aggregated into a higher level entity.
For example: Center entity offers the Course entity act as a single entity in the relationship which is in a
relationship with another entity visitor. In the real world, if a visitor visits a coaching center then he will never
enquiry about the Course only or just about the Center instead he will ask the enquiry about both.
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes have to be
performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database.
Types of Integrity Constraint

1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc. The value of the
attribute must be available in the corresponding domain.
Example:

2. Entity integrity constraints


o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows in relation and if the primary
key has a null value, then we can't identify those rows.
o A table can contain a null value other than the primary key field.
Example:
3. Referential Integrity Constraints
o A referential integrity constraint is specified between two tables.
o In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of Table 2,
then every value of the Foreign Key in Table 1 must be null or be available in Table 2.
Example:

4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary key. A primary key
can contain a unique and null value in the relational table.

Example:

You might also like