Week2 DBMS PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Introduction to Database Management System

CSC 220

Dr. Ghulam Mustafa

Department of Computer Science and IT


Bahria University Lahore Campus

February 20, 2019


Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 1 / 42
Instructor Name, Cluster Head, Subject Expert

Instructor Name: Dr. Ghulam Mustafa


Cluster Head Name: Prof. Tahir Iqbal
Subject Expert Name: Prof. Asghar Ali Shah
HOD Computer Science Prof. Farhan Saeed Sherazi
Instructor Department: Dept. of Computer Sciences, Bahria
University, Lahore Campus, Pakistan
Instructor Email: gmustafa.bulc@bahria.edu.pk
Instructor Oce Number: Tel (O ): +92-42-99233408-15 Ext: 257
Instructor Cell Number and Whatsapp : +44 7412534684
Web: https://www.bahria.edu.pk/bulc/
Instructor Contact Hours: Will be provided in class.

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 2 / 42


Reference Books

Database System Concepts, 6th Edition by Avi Silbechatz, Henry F.


Korth and S. Sudarshan.

Database Systems: A Practical Approach to Design, Implementation,


and Management, 6th Edition by Thomas Connolly and Carolyn Begg

Database Systems: The Complete Book, 2nd Edition by Hector


Garcia-Molina, Jerey D. Ullman, Jennifer Widom

Database Management Systems, 3rd Edition by Raghu Ramakrishnan,


Johannes Gehrke

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 3 / 42


Semester Calendar for Assignments/Quizzes/Project

Calender

Figure 1: Database System (Assignments Roadmap)

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 4 / 42


Outline

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 5 / 42


Relational Model Relational Model

Outline

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 6 / 42


Relational Model Relational Model

Relational Model

Example of tabular data in the relational model

Figure 2: Example of RM

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 7 / 42


Relational Model Relational Model

A Sample Relational Database

Figure 3: Example of Relational Database


Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 8 / 42
SQL

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 9 / 42


SQL

SQL

SQL: widely used non-procedural language (does not require writing


traditional programming logic)

Figure 4: Example1

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 10 / 42


SQL

Relational Model

Example of tabular data in the relational model

What is DML and DDL and DML?

Figure 5: Example of RM

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 11 / 42


Entity-Relationship Model (ERM) Database Design

Outline

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 12 / 42


Entity-Relationship Model (ERM) Database Design

Database Design

The process of designing the general structure of the database:

Logical Design  Deciding on the database schema. Database design


requires that we nd a good collection of relation schemas.
Entities, relationships, attributes etc.
Physical Design  Deciding on the physical layout of the database
Table, Columns, Primary Key etc.

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 13 / 42


Entity-Relationship Model (ERM) ERM

Outline

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 14 / 42


Entity-Relationship Model (ERM) ERM

The Entity-Relationship Model

Models an enterprise as a collection of entities and relationships

Entity: a thing or object in the enterprise that is distinguishable


from other objects

Relationship: an association among several entities

Represented diagrammatically by an entity-relationship diagram:

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 15 / 42


Entity-Relationship Model (ERM) ERM components

Outline

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 16 / 42


Entity-Relationship Model (ERM) ERM components

ERM components

Entities: The things of signicance about which information needs to


be known or held

Relationships: How the things of signicance are related

Attributes: The specic information which need to be held

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 17 / 42


Entity-Relationship Model (ERM) ERM components

Entity & Attributes diagramming conventions


.

Figure 6: Entity & Attributes diagramming conventions


Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 18 / 42
Entity-Relationship Model (ERM) ERM components

Identication of Entities

There must be a facility to check in goods on arrival at the warehouse,


and record their location. As they pass through the processing stages
in the warehouse, the system should track progress. The system
should record full information about each package

Examine the nouns. Are they things of signicance?

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 19 / 42


Entity-Relationship Model (ERM) ERM components

Identication of Entities

There must be a facility to check in goods on arrival at the


warehouse, and record their location. As they pass through the

processing stages in the warehouse, the system should track progress.

The system should record full information about each package

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 20 / 42


Entity-Relationship Model (ERM) Scenario-1

Outline

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 21 / 42


Entity-Relationship Model (ERM) Scenario-1

Example

You are managing the human resources department for a large


company. You need to keep information about each of your company's
employees including rst & last name, job, hire date & salary. Every
employee is assigned to a unique employee number.

Write down the attributes of entity in proper form.

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 22 / 42


Entity-Relationship Model (ERM) Scenario-1

Solution
.

Figure 7: Solution
Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 23 / 42
Entity-Relationship Model (ERM) Scenario-1

Tips

Follow the steps below to identify and model entities from a set of
interview notes:
Examine the nouns. Are they things of signicance? Name each entity.
Is there information of interest about the entity that the business needs
to hold?
Name each attribute.
Diagram each entity and few of its attributes.

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 24 / 42


Entity-Relationship Model (ERM) Scenario-1

The Entity-Relationship Model

ERM

Figure 8: ERM

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 25 / 42


Entity-Relationship Model (ERM) Scenario-2

Outline

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 26 / 42


Entity-Relationship Model (ERM) Scenario-2

Exercise-1

Identify and model the entities & attributes from the following set of
interview notes:

Andrew is the manager of Luton's training company (LTC) that


provides instructor-led courses in management techniques. LTC
teaches many courses, each of which has a code, a name and a fee.
Managing Information & Internet and Web Technology are 2 of its
more popular courses. Courses vary in length from 1 to 5 days. An
instructor can teach several courses. Nik & Fiaz are two of its
teachers. LTC tracks each instructor's name & phone number. Each
course is taught by only one instructor. LTC creates a course and then
lines up an instructor. The students can take several courses over
time. Maria from Bank plc took every course LTC oers. LTC tracks
each student's name and phone number..

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 27 / 42


Entity-Relationship Model (ERM) Scenario-2

Exercise-1-Descriptions

A Course has signicance as a training event oered by the LTC.

A STUDENT has signicance as a participant in one or more


COURSES. For example, Maria

An INSTRUCTOR has signicance as a teacher of one or more


COURSEs. For example, Nik & Fiaz.

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 28 / 42


Entity-Relationship Model (ERM) Scenario-2

Solution

Figure 9: Conceptual Mapping

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 29 / 42


Entity-Relationship Model (ERM) RELATIONSHIPS

Outline

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 30 / 42


Entity-Relationship Model (ERM) RELATIONSHIPS

RELATIONSHIPS

A relationship is two-directional, signicant association between two


entities.

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 31 / 42


Entity-Relationship Model (ERM) RELATIONSHIPS

Relationships
.

Figure 10: Relationship Mapping


Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 32 / 42
Entity-Relationship Model (ERM) RELATIONSHIPS

Relationships

Can you design an ERM of the above-mentioned exercise?

Figure 11: Relationship Mapping

Each COURSE may be taught by one and only one INSTRUCTOR

Each INSTRUCTOR may be assigned to one or more COURSEs.

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 33 / 42


Entity-Relationship Model (ERM) RELATIONSHIPS

Relationships

Figure 12: Relationship Mapping

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 34 / 42


Entity-Relationship Model (ERM) RELATIONSHIPS

Example
.

Figure 13: Relationship Mapping


Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 35 / 42
Entity-Relationship Model (ERM) RELATIONSHIPS

. . . tips to identify cardinality

First read a relationship in one direction, and then read the


relationship in the other direction.

Figure 14: Relationship Mapping

Each PAYCHECK must be for one and only one EMPLOYEE

Each EMPLOYEE may be the receiver of one or more PAYCHECKs

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 36 / 42


Entity-Relationship Model (ERM) Scenario-3

Outline

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 37 / 42


Entity-Relationship Model (ERM) Scenario-3

Exercise-2
Write the relationship sentences for the following ERM:

Figure 15: Relationship Mapping

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 38 / 42


Entity-Relationship Model (ERM) Scenario-3

Exercise-2-Solution

Each ORDER must be issued for one or more ITEMs

Each ITEM may be bought via one or more ORDERs

Each ORDER must be originated by one and only one CUSTOMER

Each CUSTOMER may be the originator of one or more ORDERs

Each ITEM must be stored in one and only one WAREHOUSE

Each WAREHOUSE may be the repository for one or more ITEMs

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 39 / 42


Entity-Relationship Model (ERM) Scenario-3

Mapping the E-R Model to Tables

Each entity becomes a table.

Entity name becomes table name.

Each attribute becomes a column.

The unique identier of each entity becomes the primary key of the
table.

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 40 / 42


Takeaway!

1 Relational Model
Relational Model

2 SQL

3 Entity-Relationship Model (ERM)


Database Design
ERM
ERM components
Scenario-1
Scenario-2
RELATIONSHIPS
Scenario-3

4 Takeaway!

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 41 / 42


Takeaway!

Takeaway!

Many applications of databases

DBMS are key application to deal with old le system

Relational Models

ERM and relationships

Dr. Ghulam Mustafa (BULC) Week2 February 20, 2019 42 / 42

You might also like