0% found this document useful (0 votes)
14 views53 pages

CSI 11 Tim

The document provides an overview of databases, including definitions, architecture, and various database models, particularly focusing on the relational model. It outlines the objectives for students, such as understanding database management systems (DBMS), operations in SQL, and the importance of database design. Additionally, it discusses the advantages of databases over flat file systems, emphasizing data integrity, efficiency, and reduced redundancy.
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)
14 views53 pages

CSI 11 Tim

The document provides an overview of databases, including definitions, architecture, and various database models, particularly focusing on the relational model. It outlines the objectives for students, such as understanding database management systems (DBMS), operations in SQL, and the importance of database design. Additionally, it discusses the advantages of databases over flat file systems, emphasizing data integrity, efficiency, and reduced redundancy.
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/ 53

CSI106

DATABASES

FAL2024
CONTENT

•Introduction

•Database architecture

•Database model

•The relational database model

•Database design

•Guide do practice set (database)

2
Objectives

After studying this chapter, the student should be able to:


• Define a database and a database management system (DBMS) and describe the components of
a DBMS.
• Describe the architecture of a DBMS based on the ANSI/SPARC definition.
• Define the three traditional database models: hierarchical, networking, and relational.
• Describe the relational model and relations.
• Understand operations on a relational database based on commands available in SQL.
• Describe the steps in database design.
• Define ERM and E-R diagrams and explain the entities and relationships in this model.
• Define the hierarchical levels of normalization and understand the rationale for normalizing the
relations.
• List database types other than the relational model.

3
CONTENT

•Introduction

•Database architecture

•Database model

•The relationship database model

•Database design

•Guide do practice set (self-study)

4
Introduction: Data and Database

Data
• Data is a collection of distinct small units of information.
o Person: name, age, email, dob
o Online purchase: order number, description, order quantity, date, customer’s email
• Data is crucial for both individuals & businesses.

Databases
• [informally] A database is an organized collection of data, so that it can be easily
accessed and managed.

5
Introduction: Flat File Systems

• Flat files were used during the 1970s –


1990s.
• This is a type of database system that
stores data in a single file or table.
• They are basically text files, where
every line contains one record, and
fields either have fixed lengths or are
separated by commas, whitespaces
Text file stores lines of data, where each line represents
and tabs. Such a file cannot contain
a record. The fields OrderID, CustomerID and OrderDate
multiple tables. are separated by commas.

6
Introduction: Flat File Systems (cont.)

Example: In a university, each department might have its own set of files:
• the Record office kept a file about the student information and their grades,
• the Financial Aid office kept its own file about students that needed financial aid
to continue their education,
• the Scheduling office kept the name of the professors and the courses they were
teaching,
• the Payroll department kept its own file about the whole staff (including
professors).

A flat file model allows each department or function in an organisation to own


its data file instead of sharing them.
7
Introduction: Flat File Systems (cont.)

Problems of Flat Files:


• Data storage. Data must be stored multiple times → more costs to the
organization. Costs will be incurred for multiple collections and multiple
storages of such data.
• Currency of information. A department or function may fail to update its data
file → relying on outdated information in making decisions.
• Data integrity. Data are stored in several separate places → Updating process
is error-prone & affect the organization's data integrity.
• Difficulty in sharing information. Information system is structured as separate
file systems for separate departments.
8
Introduction: the need for databases

Today, however, all these flat-files can be combined in a single entity, the
database for the whole organisation (i.e., the university as in the previous
example) .

Definition: A database is a collection of related, logically coherent


data used by the application programs in an organization.

9
Advantages of databases

• Less redundancy
• Inconsistency avoidance
• Efficiency
• Data integrity
• Confidentiality

10
CONTENT

•Introduction

•Database architecture

•Database model

•The relationship database model

•Database design

•Guide do practice set (self-study)

11
Database Management Systems (DBMS)

A database management system (DBMS) defines, creates, and maintains a DB. The
DBMS also allows controlled access to data in the database.
Software
• Hardware: the physical computer system that allows access to data.
• Software is the actual program that allows users to access, maintain,
and update data. Hardware
• Data in a DB is stored physically on the storage devices. In a DB, data
is a separate entity from the software that accesses it.
• Procedures refer to general rules and instructions that help to design
the database and to use a DBMS.
Database Access
• Users are (1) the people who control and manage the databases and Data Procedures
Language
perform different types of operations on the databases in
the database management system; and (2) application programs
• Database Access Language is a language used to write commands to User
access, update, and delete data stored in a DB.
Figure 11.1. DBMS components
12
The 3-schema DBMS Architecture

The American National Standards


Institute/Standards Planning and EXTERNAL External External
●●●
Requirements Committee (ANSI/SPARC) LEVEL view view
has established a three-level
architecture for a DBMS: CONCEPTUAL
CONCEPTUAL SCHEMA
LEVEL
• internal,
• conceptual, and INTERNAL
INTERNAL SCHEMA
• external LEVEL

Figure 11.2 Database architecture 13


1. Introduction

The American National Standards Institute/Standards Planning and Requirements Committee


(ANSI/SPARC) has established a three-level architecture for a DBMS: internal, conceptual, and external

• Internal level determines where data is stored on


the storage devices. This level deals with low-level
access methods and how bytes are transferred to
and from storage devices.

• Conceptual level defines the logical view of the data.


The data model is defined on this level, and the main
functions of the DBMS, such as queries, are also on
this level.

• External level interacts directly with the user (end


users or application programs). It changes the data
coming from the conceptual level to a format and
view that is familiar to the users. Figure 11.2. Database architecture 14
CONTENT

•Introduction

•Database architecture

•Database model

•The relationship database model

•Database design

•Guide do practice set (self-study)

15
1. Introduction

A database model defines the logical


design of data. The model also describes
the relationships between different
parts of the data.

In the history of DB design, three


models have been in use:
• the hierarchical model,
• the network model, and
• the relational model. Figure 11.3. Database models

16
2. The hierarchical model

• In the hierarchical model, data is organized as an inverted tree. Each entity has only
one parent but can have several children. At the top of the hierarchy, there is one
entity, which is called the root.
• As the hierarchical model is obsolete, no further discussion of this model is necessary

Figure 11.4 An example of the hierarchical model representing a university

17
3. The network model

• In the network model, the entities are organized in a graph, in which some
entities can be accessed through several paths. There is no hierarchy. This
model is also obsolete and needs no further discussion.

Figure 11.5. An example of the network model representing a university

18
4. The relational model

• In the relational model, data is organized in two-dimensional tables called


relations. There is no hierarchical or network structure imposed on the data.
The tables or relations are, however, related to each other.

Figure 11.6. An example of the relational model representing a university


19
CONTENT

•Introduction

•Database architecture

•Database model

•The relational database model

•Database design

•Guide do practice set (self-study)

20
RELATION

Relation, in appearance, is a two-dimensional table. The RDBMS organizes the data so that its
external view is a set of relations or tables. This does not mean that data are stored as tables: the
physical storage of the data is independent of the way in which the data is logically organized.

A relation in an RDBMS has the following features:


• Name. Each relation in a relational database
should have a name that is unique among other
relations.
• Attributes. Each column in a relation is called an
attribute. Each attribute gives meaning to the
data
• Tuples. Each row in a relation is called a tuple. Figure 11.7. An example of a relation
A tuple defines a collection of attribute values.
The total number of rows in a relation is called
the cardinality of the relation.
21
OPERATIONS ON RELATIONS

• In a relational database we can define several operations to create new relations based on
existing ones. 9 operations in this section: insert, delete, update, select, project, join, union,
intersection, and difference.
• Structured Query Language (SQL) is the language standardized by the American National
Standards Institute (ANSI) and the International Organization for Standardization (ISO) for
use on relational databases.

Expression
UPDATE clause UPDATE country
SET clause SET population = population + 1 Statement

WHERE clause WHERE name = 'USA';


Expression
Predicate
Figure 11.8 An example of a SQL statement
22
OPERATIONS ON RELATIONS: Insert

The insert operation is a unary operation — that is, it is applied to a single relation.
The operation inserts a new tuple into the relation. The insert operation uses the
following format:

INSERT INTO RELATION-NAME(…, ….,….)


VALUES (…, …, …)
Courses
Courses
course_id course_name unit
course_id course_name unit
CIS15 Intro to C 5
CIS15 Intro to C 5
INSERT INTO Courses VALUES CIS17 Intro to Java 5
CIS17 Intro to Java 5
('CIS52', 'TCP/IP', 6); CIS19 UNIX 4
CIS19 UNIX 4
CIS51 Networking 5
CIS51 Networking 5
CIS52 TCP/IP 6

Figure 11.9. Example of an Insert statement 23


OPERATIONS ON RELATIONS: Delete

The delete operation is a unary operation. The operation deletes a tuple defined
by a criterion from the relation. The delete operation uses the following format:

DELETE FROM RELATION-NAME WHERE criteria


Courses
Courses
course_id course_name unit
course_id course_name unit
CIS15 Intro to C 5
CIS15 Intro to C 5
CIS17 Intro to Java 5 DELETE FROM Courses WHERE
CIS17 Intro to Java 5
CIS19 UNIX 4 course_id='CIS19';
CIS51 Networking 5
CIS51 Networking 5
CIS52 TCP/IP 6
CIS52 TCP/IP 6

Figure 11.10 An example of a Delete statement 24


OPERATIONS ON RELATIONS: Update

The update operation is a unary operation that is applied to a single relation. The
operation changes the value of some attributes of a tuple. The update operation uses
the following format:
UPDATE RELATION-NAME
SET attribute1 = value1, attribute2 = value2, …
WHERE criteria
Courses
Courses
course_id course_name unit
course_id course_name unit
CIS15 Intro to C 5
CIS15 Intro to C 5
CIS17 Intro to Java 5 UPDATE Courses SET unit=6
CIS17 Intro to Java 5
CIS19 UNIX 4 WHERE course_id='CIS51';
CIS51 Networking 6
CIS51 Networking 5
CIS52 TCP/IP 6
CIS52 TCP/IP 6

Figure 11.11 An example of an Update statement 25


OPERATIONS ON RELATIONS: Select

The select operation is a unary operation. The tuples (rows) in the resulting relation
are a subset of the tuples in the original relation.

SELECT * FROM RELATION-NAME WHERE criteria

Courses
course_id course_name unit Courses
CIS15 Intro to C 5 course_id course_name unit
CIS17 Intro to Java 5 SELECT * FROM Courses CIS15 Intro to C 5
CIS19 UNIX 4 WHERE unit=5; CIS17 Intro to Java 5
CIS51 Networking 5 CIS51 Networking 5
CIS52 TCP/IP 6

Figure 11.12 An example of a Select statement


26
OPERATIONS ON RELATIONS: Project

The project operation is a unary operation and creates another relation. The attributes
(columns) in the resulting relation are a subset of the attributes in the original relation. The
project operation creates a relation in which each tuple has fewer attributes.

SELECT attribute-list FROM RELATION-NAME


Courses Courses
course_id course_name unit course_id unit
CIS15 Intro to C 5 CIS15 5
CIS17 Intro to Java 5 SELECT course_id, unit CIS17 5
CIS19 UNIX 4 FROM Courses; CIS19 4
CIS51 Networking 5 CIS51 5
CIS52 TCP/IP 6 CIS52 6

Figure 11.12 An example of a Project statement


27
OPERATIONS ON RELATIONS: Join

The join operation is a binary operation that combines two relations on common
attributes.
Courses Taught-by
course_id course_name unit course_id prof
CIS15 Intro to C 5 CIS15 Lee
SELECT course_id, course_name, unit, prof
CIS17 Intro to Java 5 FROM Courses CIS17 Liu
CIS19 UNIX 4 JOIN Taught-by CIS19 Walter
ON Courses.course_id=Taught-by.course_id;
CIS51 Networking 5 CIS51 Liu
CIS52 TCP/IP 6 CIS52 Lee

course_id course_name unit prof


CIS15 Intro to C 5 Lee
SELECT attribute-list CIS17 Intro to Java 5 Liu
FROM RELATION1 CIS19 UNIX 4 Walter
JOIN RELATION2 ON criteria CIS51 Networking 5 Liu
CIS52 TCP/IP 6 Lee 28
8. Union

The union operation takes two relations with the same set of attributes. It creates a
new relation in which each tuple is either in the first relation, in the second, or in both.

SELECT *
FROM RELATION1
UNION
SELECT *
FROM RELATION2

Figure 11.14. Example of a Union statement


29
9. Intersection

The intersection operation takes two relations with the same set of attributes. It creates
a new relation in which each tuple is a member in both relations.

SELECT *
FROM RELATION1
INTERSECTION
SELECT *
FROM RELATION2

Figure 11.15. Example of Intersection sentence


30
CONTENT

•Introduction

•Database architecture

•Database model

•The relationship database model

•Database design

•Guide do practice set (database)

31
1. Introduction

The design of any database is a lengthy and involved task that can only be done
through a step-by-step process.
• The first step normally involves a lot of interviewing of potential users of the
database, for example in a university, to collect the information needed to be stored
and the access requirements of each department.
• The second step is to build an entity – relation model (ERM) that defines the entities
for which some information must be maintained, the attributes of these entities,
and the relationship between these entities.

32
2. Entity – relation model (ERM)

In this step, the database designer creates an entity – relationship diagram (ERD) to
show the entities for which information needs to be stored and the relationship
between those entities. E-R diagrams uses several geometric shapes.

• Rectangles represent entity sets


• Ellipses represent attributes
• Diamonds represent relationship sets
• Lines link attributes to entity sets and
link entity sets to relationship sets

Figure 11.16. Entities, attributes, and relationships in


an ER diagram
33
3. From E-R diagrams to relations

Relations for entity sets


For each entity set in the E-R diagram, we create a relation (table) in which there are 𝒏
columns related to the 𝒏 attributes defined for that set.
• Example: We can have three relations (tables), one for each entity set defined in the
previous slide (Course, Student, Professor)

Figure 11.17. Relations for entity set in Figure 11.16


34
3. From E-R diagrams to relations (cont.)

Relations for relationship sets


For each relationship set in the E-R diagram, we create a relation (table). This relation
has one column for the key of each entity set involved in this relationship, and one
column for each attribute of the relationship itself if the relationship has attributes
• Example: There are two relationship sets, teaches and takes, each connected to two
entity sets.

Figure 11.18. Relations for E-R diagram in Figure 11.16


35
4. Normalization

• Normalization is the process by which a given set of relations are transformed to a


new set of relations with a more solid structure.
• The main purpose is to avoid complexities, eliminate duplicates, and organize data in a
consistent way.
• The normalization process defines a set of hierarchical normal forms (NFs). Several
normal forms have been proposed, including 1NF, 2NF, 3NF, BCNF (Boyce–Codd
Normal Form), 4NF, PJNF (Projection/Joint Normal Form), 5NF, and so on.

36
First normal form (1NF)

When we transform entities or relationships into tabular relations, there may


be some relations in which there are more values in the intersection of a row
or column.

Figure 11.19. An example of 1NF

E.g., 2 relations, teaches and takes, that are not in first NF. A professor can teach more than one
course, and a student can take more than one course. These two relations can be normalized by
repeating the rows in which this problem exists.

37
Second normal form (2NF)

• In each relation we need to have a key (called a primary key) on which all other attributes
(column values) needs to depend.
• However, it may happen that when relations are established based on the E-R diagram, we
may have some composite keys (a combination of two or more keys). In this case, a relation
is in second normal form if every non-key attribute depends on the whole composite key.

Figure 11.20. An example of 2NF 38


CONTENT

•Introduction

•Database architecture

•Database model

•The relationship database model

•Database design

•Guide do practice set (self-stu)

39
Building a Data Model (cont.)

Track Len Artist Album Genre Rating Count

Is the column an object or an


attribute of another object?

40
Building a Data Model (cont.)

belongs to
TRACK
ARTIST - Id (PK)
- id - title
- name belongs to - len
- rating belongs to
- count
belongs to ALBUM - Artist_id (FK)
- id GENRE
- Name - id
- Artist_id (FK) - name
Track Len Artist Album Genre Rating Count

41
Building a Data Model (cont.)

Three kinds of Keys:

• Primary key - generally an integer auto-increment field


• Logical key - what the outside world uses for lookup
• Foreign key - generally an integer key pointing to a row in another table

Best practices:
• Never use logical key as the primary key.
• Logical keys can and do change, albeit slowly.
• Relationships that are based on matching string fields are less efficient than integers.

42
Building a Data Model (cont.)

TRACK
ALBUM belongs to - title
- name - len
- rating
- count

Track
Primary key track_id
Logical key title
Album
Foreign key len
album_id
rating
title
count
album_id
43
Building a Data Model (cont.)

Track
track_id
Artist title
Album
artist_id len
album_id
name rating
title
artist_id count
album_id
genre_id
Primary key
Logical key
Genre
Foreign key
genre_id
name

44
Building a Data Model (cont.)

45
6- Guide to practice
set (database in Ms
SQL)

46
1. Install the SQL Server


Install the SQL Server Management Studio software. This software
is available for free from Microsoft, and allows you to connect to and
manage your SQL server from a graphical interface instead of having
to use the command line

47
2. Start up SQL Server Management Studio

• Start up SQL Server Management


Studio. When you first start the
program, you will be asked what
server you would like to connect
to.
• If you already have a server up
and running, and have the
permissions necessary to connect
to it, you can enter the server
address and authentication
information.
• If you want to create a local
database, set the Database Name
to … and the authentication type
to "Windows Authentication".

48
3. Locate the Database folder

• Locate the Database


folder. After the connection to
the server, either local or
remote, is made, the Object
Explorer window will open on
the left side of the screen.
• At the top of the Object
Explorer tree will be the server
you are connected to. if it is
not expanded, click the "+"
icon next to it. Located the
Databases folder.[

49
4. Create a new database

• Create a new database. Right-


click on the Databases folder
and select "New Database...".
• A window will appear, allowing
you to configure the database
before creating it. Give the
database a name that will help
you identify it.
• Most users can leave the rest of
the settings at their default.

50
5. Create a table

• Create a table. A database


can only store data if you
create a structure for that
data.
• A table holds the information
that you enter into your
database, and you will need
to create it before you can
proceed.
• Expand the new database in
your Database folder, and
right-click on the Tables
folder and select "New
Table...".

51
6. Create the Primary Key

• Create the Primary Key. It is highly


recommended that you create a
Primary Key as the first column on your
table. This acts as an ID number, or
record number, that will allow you to
easily recall these entries later.
• To create this, enter "ID" in the Column
Name field, type int into the Data Type
field, and uncheck the "Allow Nulls."
Click the Key icon in the toolbar to set
this column as the Primary Key.

52
7. Understand how tables are structured

• Understand how tables are


structured. Tables are composed
of fields or columns. Each column
represents one aspect of a
database entry.
• For example, if you were creating
a database of employees, you
might have a "FirstName" column,
a "LastName" column, an
"Address" column, and a
"PhoneNumber" column.

53

You might also like