You are on page 1of 48

Querying Data

Introduction toUsing SQL Database


Relational Server Management Systems
Rationale

Database is an integral part of an organization. Aspiring


database developers should be able to efficiently design
and implement databases.
This knowledge will enable the developers to build robust
database solutions.
This course will help students to understand the concepts
related to relational databases as well as equip them with
basic and advanced knowledge of SQL Server.

Ver.
Ver. 1.0
1.0 Slide
Slide11of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Objectives

In this session, you will learn to:


Define a database management system
Describe the types of data models
Define a relational database management system
Describe the operators that work on relations
Map an ER diagram to a table
Identify tips of logical database design

Ver.
Ver. 1.0
1.0 Slide
Slide22of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Database Management System

A database is a collection of logically related data.


Database Management System (DBMS) is the software
required to perform the task of maintaining databases.
Management of data involves:
Defining structures for data storage.
Providing methods for data manipulation.
Providing data security.
The main advantages of any DBMS is to:
Provide an efficient and easy way to store, update, and retrieve
data.
Manage information about users who interact with the DBMS,
and the tasks that they can perform.

Ver.
Ver. 1.0
1.0 Slide
Slide33of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Data Models

A data model is a description of the data in a database.


Data models can be broadly classified into the following
categories:
Object-based logical model
Record-based logical model

Ver.
Ver. 1.0
1.0 Slide
Slide44of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Object-Based Logical Model

The ER model:
Views the real world as a collection of objects or entities and
the relationship among them.
An entity:
Is any object, place, person, or activity about which the data is
recorded.
Can be categorized as entity type and entity instance.
An entity can be of the following types:
Dependent entity
Independent entity

Ver.
Ver. 1.0
1.0 Slide
Slide55of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Object-Based Logical Model (Contd.)

A relationship is:
Defined as an association among entities.
Depicted in a diamond shaped box with the name of the
relationship type inside it.
The following diagram depicts a relationship between the
entities (TEACHER and STUDENT).

Relationship

Ver.
Ver. 1.0
1.0 Slide
Slide66of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Just a minute

Consider the following statement:


“A supplier ships certain parts.”
Identify the entities and the relationship which exists
between them.
Entities: SUPPLIER and PARTS
Relationship: SHIPS
Entities: SUPPLIER and SHIPS
Relationship: PARTS
Entities: PARTS and SHIPS
Relationship: SUPPLIER
Entities: SUPPLIER, PARTS, and SHIPS
Relationship: SUPPLIES

Ver.
Ver. 1.0
1.0 Slide
Slide77of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Just a minute (Contd.)

Solution:
Entities: SUPPLIER and PARTS
Relationship: SHIPS

Ver.
Ver. 1.0
1.0 Slide
Slide88of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Object-Based Logical Model (Contd.)

The three types of relationships are:


One-to-One
One-to-Many (or Many-to-One)
Many-to-Many
An attribute:
Is a property of a given entity.
Is depicted as ellipses, labeled with the name of the property.

Ver.
Ver. 1.0
1.0 Slide
Slide99of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Just a minute

An attribute is a property of a given entity, which is depicted


as ___________, labeled with the name of the property.

Solution:
ellipses

Ver.
Ver. 1.0
1.0 Slide
Slide10
10of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Just a minute

Consider the following statement:


“A supplier ships certain parts. A particular part is not necessarily

supplied by only one supplier. No supplier supplies only a


single
part.”
What type of relationship is possible between the two
entities?
One-to-One relationship
One-to-Many relationship
Many-to-Many relationship
Solution:
Many-to-One relationship
Many-to-Many relationship

Ver.
Ver. 1.0
1.0 Slide
Slide11
11of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Object-Based Logical Model (Contd.)

A subtype:
Is a subset of another entity.
Is always dependent on the supertype for its existence.
The attributes of a supertype apply to all of its subtypes.
The following diagram depicts the relationship between
supertype (COURSE) and subtypes (STANDLONE and
SEMESTER).

The supertype is connected


to the relationship with a line
containing a crossbar.

Ver.
Ver. 1.0
1.0 Slide
Slide12
12of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Just a minute

There are two types of suppliers. One type of supplier allows


credit, while the other type insists on payment in cash before
delivery. The manufacturer wishes to maintain separate
information on these two types of suppliers. For the credit
supplier, “credit period” and “credit limit” have to be recorded. For
the cash supplier, “date of payment” has to be stored. Identify the
subtype and supertype entities.
Subtypes: Suppliers and Cash
Supertype: Credit
Subtype: Credit
Supertypes: Cash and Suppliers
Subtypes: Cash and Credit
Supertype: Suppliers
Subtype: Suppliers
Supertypes: Cash and Credit

Ver.
Ver. 1.0
1.0 Slide
Slide13
13of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Just a minute (Contd.)

Solution:
Subtypes: Cash and Credit
Supertype: Suppliers

Ver.
Ver. 1.0
1.0 Slide
Slide14
14of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Record-Based Logical Model

The three types of record-based logical models are:


Hierarchical model:
Represents data in the form of a tree, and relationships between
the data by links.
Network model:
Is similar to a hierarchical model.
Represents records in a database graphically.
Relational model:
Represents data in the form of tables in a database.
Is the most popular model.

Ver.
Ver. 1.0
1.0 Slide
Slide15
15of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Record-Based Logical Model (Contd.)

The following diagram depicts the parent-child relationship in a


relational model.

Column A is a primary key of Table1, which is


also present in Table2 as a foreign key.

Table3, which consists of columns, A, B, and F


can have a combination of columns (A and B)
as a foreign key.

Ver.
Ver. 1.0
1.0 Slide
Slide16
16of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Steps for Creating an ER Diagram

The following steps should be followed while creating an ER


diagram:
1. Identify the entities.
2. Find the relationships.
3. Identify the key attribute.
4. Identify other important attributes.
5. Draw a complete ER diagram.
6. Review the ER diagram.

Ver.
Ver. 1.0
1.0 Slide
Slide17
17of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Just a minute

Which one of the following is NOT a record-based logical


model?
Hierarchical model
Network model
ER model
Relational model

Solution:
ER model

Ver.
Ver. 1.0
1.0 Slide
Slide18
18of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Steps for Creating an ER Diagram (Contd.)

Click the following link to view an animation on constructing


an ER diagram.

Constructing an
ER Diagram

Ver.
Ver. 1.0
1.0 Slide
Slide19
19of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Relational Operators

Relational algebra is a collection of operators that operate


on relations.
Each operator takes one or two relations as its input and
produces a new relation as its output.
The following operators have been defined as relational
operators:
RESTRICT
PROJECT
PRODUCT
UNION
INTERSECT
DIFFERENCE
JOIN
DIVIDE

Ver.
Ver. 1.0
1.0 Slide
Slide20
20of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Relational Operators (Contd.)

Click the following link to view an animation on relational


algebra.

Relational Algebra

Ver.
Ver. 1.0
1.0 Slide
Slide21
21of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
RESTRICT

The restrict operator extracts specified tuples or rows from a


given relation based on a condition, as shown in the
following diagram.

Extracted Rows

Ver.
Ver. 1.0
1.0 Slide
Slide22
22of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
PROJECT

The project operator extracts specified attributes or columns


from a given relation, as shown in the following diagram.

Extracted Columns

Ver.
Ver. 1.0
1.0 Slide
Slide23
23of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
PRODUCT

The product operator joins two relations such that every tuple of
the first relation is matched with every tuple of the second relation.
To be product compatible, the two tables must have common
attributes.
The following diagram shows an example of the product operator.

Ver.
Ver. 1.0
1.0 Slide
Slide24
24of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
UNION

The union operator builds a relation from tuples appearing


in either or both of the specified relations.
The following diagram shows the mathematical
representation of the union operator.

Ver.
Ver. 1.0
1.0 Slide
Slide25
25of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
INTERSECT

The intersect operator builds a relation consisting of tuples


that appear in both the relations.
The following diagram shows the mathematical
representation of the intersect operator.

Ver.
Ver. 1.0
1.0 Slide
Slide26
26of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
DIFFERENCE

The difference operator builds a relation of tuples appearing


in the first but not in the second of the two specified
relations.
The following diagram shows the mathematical
representation of the difference operator.

Ver.
Ver. 1.0
1.0 Slide
Slide27
27of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
JOIN

The join operator builds a relation that consists of all the


possible combinations of tuples, one from each relation that
satisfies the specified condition.
The following diagram depicts the working of the join operator.

Ver.
Ver. 1.0
1.0 Slide
Slide28
28of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
DIVIDE

The divide operator builds a relation that consists of values


of an attribute of one relation that matches all the values in
the other relation.
The divide operation is the opposite of the product
operation.
The following diagram depicts the working of the divide
operator.

Ver.
Ver. 1.0
1.0 Slide
Slide29
29of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Just a minute

Which operator is used to extract specified tuples or rows


from a given relation based on a condition in a relational
algebra?
Project
Join
Restrict
Product

Solution:
Restrict

Ver.
Ver. 1.0
1.0 Slide
Slide30
30of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Conceptual Model

The conceptual model can be mapped to a relational,


hierarchical, or network model.
Data analysis is the first step in designing a conceptual
model.
Data analysis involves identifying entities, their attributes,
and relationships between entities based on the data
collected.
An entity-relationship diagram gives a detailed overview of
the database design.

Ver.
Ver. 1.0
1.0 Slide
Slide31
31of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Mapping ER Diagrams to Tables

The following components play an important part while


mapping an ER diagram to a table:
Regular entities
Attributes
Relationships
Weak entities
Subtypes and supertypes

Ver.
Ver. 1.0
1.0 Slide
Slide32
32of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Mapping ER Diagrams to Tables (Contd.)

Regular entities:
They can exist alone, independent of any other entity.
They are the “building blocks” of the database.
Each regular entity maps to a table.
For example, STUDENT and BOOKS are two separate entities
in the following ER diagram.

Ver.
Ver. 1.0
1.0 Slide
Slide33
33of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Mapping ER Diagrams to Tables (Contd.)

Attributes:
Each property or attribute shown in the ER diagram maps to an attribute in the
appropriate table.
In the following ER diagram, STUDENT and BOOKS individually have different
attributes.

Entity becomes the table and the


attributes of the entity become
columns of the table.

Ver.
Ver. 1.0
1.0 Slide
Slide34
34of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Mapping ER Diagrams to Tables (Contd.)

Relationships:
Map to tables in a different manner in the relational DBMS,
which depends on the type of the relationship.
Click the following link to view an animation on mapping
an ER diagram to table.

RDBMS_02_Relationship.swf

Ver.
Ver. 1.0
1.0 Slide
Slide35
35of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Mapping ER Diagrams to Tables (Contd.)

Weak entity:
It is an entity whose existence depends on some other entity.
The following diagram depicts the weak entity (EMPLOYEEADDRESS).

The EMPLOYEEADDRESS
entity can be mapped to a
separate table.

Ver.
Ver. 1.0
1.0 Slide
Slide36
36of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Mapping ER Diagrams to Tables (Contd.)

Subtypes and supertypes:


A subtype is a subset of another entity.
A subtype is always dependent on supertype for its existence.
The primary key of the supertype creates a link between the
supertype and subtypes.

Ver.
Ver. 1.0
1.0 Slide
Slide37
37of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Tips on Logical Database Design

There are some tips and guidelines to be followed in


database design.

Ver.
Ver. 1.0
1.0 Slide
Slide38
38of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Attributes

Do not introduce any unnecessary attributes.


An attribute serves the following purposes:
It identifies its owner entity.
It refers to another entity.
It simplifies the description of an entity.
If there are any entities with common attributes, merge
the entities.

Ver.
Ver. 1.0
1.0 Slide
Slide39
39of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Keys

An RDBMS identifies and locates rows by value.


The physical address is transparent to the user.
Relational systems require keys that can uniquely identify
the rows of a table.
The various types of keys used in an RDBMS are:
Primary
Foreign
Candidate
Alternate
Composite
Click the following link to view an animation on keys.

RDBMS_02_Keys.swf

Ver.
Ver. 1.0
1.0 Slide
Slide40
40of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Just a minute

Which one of the following keys could be a candidate for the


primary key but is not the primary key?
Candidate
Alternate
Composite
Foreign

Solution:
Alternate

Ver.
Ver. 1.0
1.0 Slide
Slide41
41of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Just a minute

Which one of the following keys is made up of more than


one attribute and uniquely identifies the rows of the table?
Candidate
Alternate
Composite
Primary

Solution:
Composite

Ver.
Ver. 1.0
1.0 Slide
Slide42
42of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Entities

You can create a new entity to represent important recurring


groups of attributes, as shown in the following diagram.

Attribute
ADDRESS
becomes an
entity.

Ver.
Ver. 1.0
1.0 Slide
Slide43
43of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Subentities

Entities that have optional attributes can be replaced with subentities.


Specialization is the result of taking a subset of a
higher-level entity set to form a lower-level entity set.
Generalization is the result of taking the union of two or more lower-level entity sets to produce a higher-level entity set.

Ver.
Ver. 1.0
1.0 Slide
Slide44
44of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Demo: Mapping the ER Diagram to Tables

Problem Statement:
Map the following ER diagram to its corresponding tables.

Ver.
Ver. 1.0
1.0 Slide
Slide45
45of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Summary

In this session, you learned that:


Data models can be classified as:
Object-based logical model
Record-based logical model
In the ER diagramming technique:
Entities are represented as rectangles.
Relationships are represented as diamonds.
Attributes are represented as ellipses.
Weak entities are represented by double-lined boxes.
Subtypes are connected to the supertype by an unnamed
relationship, marked with a crossbar on top.
The number of tuples is called the cardinality of the table, and the
number of attributes is called the degree of the table.
An attribute (or set of attributes) that is unique in every tuple is
called the primary key.

Ver.
Ver. 1.0
1.0 Slide
Slide46
46of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
Summary (Contd.)

The foreign key is a column in one table that matches the


primary key of another table.
The eight operators that operate on relations are restrict,
project, product, union, intersect, difference, join, and divide.
Regular entities are not dependent. They can exist in isolation,
independent of any other entity.
A weak entity is an entity whose existence depends on some
other entity.
A subtype is a subset of another entity. A subtype is always
dependent on supertype for its existence.

Ver.
Ver. 1.0
1.0 Slide
Slide47
47of
of68
48
Querying Data
Introduction toUsing SQL Database
Relational Server Management Systems
What’s Next?

Before the next session, please ensure to:


Read Chapter 1 of the book, Introduction to Relational
Database Management Systems.
Go through the recorded lectures on the following topics
through Cloudscape:
ER Model
Using the Intersect, Product, and Union Operators
Complete the Lab@Home exercises based on the learning
plan available on the technology page on Cloudscape.

Ver.
Ver. 1.0
1.0 Slide
Slide48
48of
of68
48

You might also like