You are on page 1of 57

Chapter 2.

Relational Database
A relation databae is built based on a relation model
Outline

2.1 Relational Model


2.2 Structured Query Language
2.3 Database Design
2.1 Relational Model

 Data model
A collection of tools for describing
 Data
 Data relationships
 Data semantics
 Data constraints
 Data models used in Database
 Hierarchical model(1960s, IMS, Windows Registry)
 Network model(1960s, IDS, IDMS)
 Relational model(1970s)
 Object-oriented model(1980s, Gemstone, ObjectStore, db4o)
2.1 Relational Model

 A type of logical data model(1970, Edgar Codd of IBM)


 The purpose of the relational model is to provide a declarative
method for specifying data and queries
 Widely used data model that supports most modern database
systems
 Oracle
 MS SQL Server
 DB2
 MySQL
 PostgreSQL
2.1.1 Concepts
 A relational model consists of multiple relations
 What is a relation?
 n-ary relation is a subset of the Cartesian product of n domains
 e.g. R⊆A*B={(a, b)|a∈A and b∈B)
 A relation can be represented by a 2-dimensional data table
 A relation is used to describe an entity set Heading
 A relation is called as a table in a relational database

Student
ID Name Birthday Gender Department
100001 Alice 1990-1-1 Female CS
100002 Bob 1991-2-20 Male ME
100003 Carol 1990-5-1 Male CS
Body
2.1.1 Concepts

 Attribute
 A column in the table is an attribute of the relation
 Example
 ID, Name, Birthday, Gender, and Department

ID Name Birthday Gender Department


100001 Alice 1990-1-1 Female CS
100002 Bob 1991-2-20 Male ME
100003 Carol 1990-5-1 Male CS
2.1.1 Concepts

 Domain
 A domain is the value range of an attribute
 Example
 DName={Alice, Bob, Carol, ….}
 DGender={Female, Male}

ID Name Birthday Gender Department


100001 Alice 1990-1-1 Female CS
100002 Bob 1991-2-20 Male ME
100003 Carol 1990-5-1 Male CS
2.1.1 Concepts

 Tuple
 A row in the table
 A tuple represents an entity in the entity set
 Example: (100001, Alice, Female) is a tuple
2.1.1 Concepts

Terminology correspondence 
Relation Model Relation database
relation table
attribute column
tuple row
domain datatype

(Note: Not synonyms)


2.1.1 Concepts

Relational database may have some deviations from the


relational model.
 e.g. The same row can appear more than once in an SQL table.
The same tuple cannot appear more than once in a relation.
2.1.2 Key

 A key can uniquely identify a tuple in the relation


 A key is made up of one or several attributes
 One attribute: simple key
 Several attributes: composite key
Primary Key

 There can be several keys in a relation (candidate


keys)
 We choose one (and only one) to be the primary key
of the relation
 Primary key example
 Student (ID, Name, Birthday, Gender, Department)
Foreign Key

 A relation may have an attribute that corresponds to


the primary key of another relation. This attribute is
called a foreign key
 Foreign key is used to represent references between
relations
Department 1 Belong n Student

Department
ID DepartmentName Abbr Head
101 Computer Science CS Boss 1
102 Management & Economics ME Boss 2
103 Internet Technology IT Boss 3

Student
ID Name Birthday Gender DepartmentID
100001 Alice 1990-1-1 Female 101
100002 Bob 1991-2-20 Male 102
100003 Carol 1990-5-1 Male 101
Grade

Student m Enroll n Course

Student
ID Name Gender Dept
100001 Alice Female CS Composite PK
100002 Bob Male ME
Enroll
StudentID CourseID Grade
100003 Carol Male CS
100001 1001 80
100002 1001 null
Course
ID CourseName Teacher 100001 1002 100
1001 Computer Basics Teacher 1
1002 Management Basics Teacher 2
FK 1 FK 2
1003 College English Teacher 3
What is a “null” value?

 A “null” value indicate that the data value does not


exist in the relation
 Examples:
 The value has not been generated
 We do not know the value
Enroll
StudentID CourseID Grade
100001 1001 80
100002 1001 null
100001 1002 100
2.1.3 Relational Algebra

 Operations to relations in relational model


 Conventional Set operations
 Union(U), difference(-), intersection(∩), Cartesian
product(X)
 Specific relation operations
 Selection(F), projection(x), join( )
 The formal query language which relation database
based on.
2.2 Structured Query Language

 Structured Query Language (SQL) is a standardized declarative


(nonprocedural) language for querying, altering and defining
relational databases, using declarative statements.
 Application programs generally access databases through one of
 Language extensions to allow embedded SQL
 Application program interface (e.g., ODBC/JDBC) which allow SQL
queries to be sent to a database
 Components:
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Data Control Language (DCL)
2.2.1 Data Definition Language

 Define and modify database schema


 CREATE statement
 Make a new database, table, index, etc
 ALTER statement
 Modify an existing database, table, index, etc
 DROP statement
 Destroy an existing database, table, index, etc
2.2.2 Data Manipulation Language

 Access and manipulate data in a database


 SELECT statement
 INSERT statement
 UPDATE statement
 DELETE statement
2.2.3 Data Control Language

 Control access to data in a database


 GRANT statement
 Authorize a user to perform an operation on some
data
 DENY statement
 Deny a permission to a user
 Prevents that user from inheriting the permission
through its group or role memberships
 REVOKE statement
 Eliminate a grant
2.3 Database Design

The process of designing the general structure of


the database:
 Logical Design – Deciding on logical structure of the
database(Logical data model).
 E-R Diagram,
 Shema
 Normalization
 Physical Design – Deciding on the physical layout of the
database (physical data model).
Logical data model

 A data model of a specific problem domain expressed


independently of a particular database management
product or storage technology (physical data model)
but in terms of data structures such as relational tables
and columns.
Physical data model

 Describe how data is to be stored in secondary storage


using a particular database management system
(DBMS); it deal with
 Run-time performance
 Storage utilization and compression
 File organization and access methods
 Data encryption
2.3.1 Entity-Relationship (E-R) Diagram

 Conceptual model
 Represented diagrammatically by an entity-relationship
diagram
 Model data from a user’s point of view
 Mainly used for database design
 Rectangles represent entity sets.
 Diamonds represent relationships.
 Ellipses represent attributes.
Entity

 A thing which is recognized as being capable of an


independent existence and which can be uniquely
identified
 Physical object: a computer, a book, a student
 Event or abstract concept: a course, a match

Student Teacher
Entity Sets
Attribute

 A feature of an entity
 An entity can be described by several attributes
 Student (ID, name, age, gender, department)
 For attribute “age”:
 Name of the attribute: age
 Value of the attribute (for this specific record): 19

Student

ID Name Birthday Gender Department


Relationship

 Represents the relationship between entitys


 Intra-entity relationships
 Relationships between entities in a single entity set
 Inter-entity relationships
 Relationships between entities in two different entity sets
Types of Relationships

 One-to-one (1:1) relationship


Entity 1 1 Relationship 1 Entity 2

 One-to-many (1:n) relationship


Entity 1 1 Relationship n Entity 2

 Many-to-many (m:n) relationship


Entity 1 m Relationship n Entity 2
1:1 Relationship

 An entity in entity set A can have relationship with at


most one entity in entity set B, and vice versa
 Example
 A student can have at most one student card
 A student card can belong to at most one student

Student 1 Have 1 Card


1:n Relationship

 An entity in entity set A can have relationship with


more than one entity in entity set B; an entity in entity
set B can have relationship with at most one entity in
entity set A
 Example
 A class can have more than one student
 A student can belong to at most one class

Student n Belong 1 Class


m:n Relationship

 An entity in entity set A can have relationship with


more than one entity in entity set B, and vice versa
 Example
 A course can enroll more than one student
 A student can be enrolled in more than one course

Student n Enroll m Course


Intra-entity relationships

 Relationships can be among different entities in a


single entity set
 Example
 For entity set Staff that represents all the staff in a
company
 A staff member A can be a supervisor of other staff
members B and C

1
Staff Supervise
n
2.3.2 Schema

 Database Schema
 Description of logical structure of database(logical data
model)
 The result of logical design
 Independent of DBMS and physical storage
 A relational database built based on a relational database
schema R
 Language: DDL (Data Definition Language)
 A database generally stores its schema in a data
dictionary
2.3.2 Schema

 Relational schema
 A relational schema is a description to the structure of a
relation
 R (Attr 1, Attr 2, …, Attr n)
 Example
 Student (ID, Name, Birthday, Gender, Department)
 Relational database schema
 R = {R1, R2, …, Rp}
2.3.3 Schema normalization

 In the design of a relational database, the


process of organizing data to minimize
redundancy is called normalization
 The way of schema normalization is to
decompose relations with anomalies in order
to produce smaller, well-structured relations
Bad Design Example

 redundant data(Student, Department, Book)

Borrow
StuID Name Dept Mngr BookID Date ….
S0001 Alice CS Boss1 B001 2010-1-1
S0001 Alice CS Boss1 B002 2010-1-2
S0001 Alice CS Boss1 B003 2010-1-3
S0002 Bob CS Boss1 B004 2010-1-5
S0003 Carol ME Boss2 B005 2011-2-6
Bad Design Example

 Insertion anomaly
 Primary key: {StuID, BookID}
 Cannot insert a student without a book
Borrow
StuID Name Dept Mngr BookID Date ….
S0001 Alice CS Boss1 B001 2010-1-1
S0001 Alice CS Boss1 B002 2010-1-2
S0001 Alice CS Boss1 B003 2010-1-3
S0002 Bob CS Boss1 B004 2010-1-5
S0003 Carol ME Boss2 B005 2011-2-6
Bad Design Example

 Deletion anomaly
 When a student returns all his books, the personal and
dept data will also be deleted
Borrow
StuID Name Dept Mngr BookID Date ….
S0001 Alice CS Boss1 B001 2010-1-1
S0001 Alice CS Boss1 B002 2010-1-2
S0001 Alice CS Boss1 B003 2010-1-3
S0002 Bob CS Boss1 B004 2010-1-5
S0003 Carol ME Boss2 B005 2011-2-6
Bad Design Example

 Update anomaly
 If the dept data for a student is changed, all his rows must
be updated: potential inconsistency

Borrow
StuID Name Dept Mngr BookID Date ….
S0001 Alice CS Boss1 B001 2010-1-1
S0001 Alice CS Boss1 B002 2010-1-2
S0001 Alice CS Boss1 B003 2010-1-3
S0002 Bob CS Boss1 B004 2010-1-5
S0003 Carol ME Boss2 B005 2011-2-6
Functional Dependency

 In a given table, an attribute Y is said to have a


functional dependency on a set of attributes X
(written X → Y) if and only if each X value is
associated with precisely one Y value
 X → Y: Y is functionally dependent on X
Functional Dependency

Borrow(StuID, Name, Dept, Mngr, BookID, Date)


 StuID → Name
 StuID → Dept
 StuID → Mngr
 Dept → Mngr
 (StuID, BookID) → Date
Functional Dependency

 Full functional dependency: An attribute is fully


functionally dependent on a set of attributes X if it is:
(1) functionally dependent on X, and
(2) not functionally dependent on any proper subset of X
Functional Dependency

 Full functional dependency


 StuID → Name
 StuID → Dept
 (StuID, BookID) → Date
 Partial functional dependency
 (StuID, BookID) → Name
 (StuID, Name) → Dept
Functional Dependency

 Transitive functional dependency: A transitive


functional dependency is an indirect functional
dependency
 If X → Y, Y → Z, then X → Z
 If StuID → Dept, Dept → Mngr, then StuID → Mngr is
a transitive functional dependency
Functional Dependency

 Prime attribute: A prime attribute is an attribute that


occurs in a candidate key
 Non-prime attribute: A non-prime attribute is an
attribute that does not occur in any candidate key
 StuID is a prime attribute
 Name is not a prime attribute
Normal Forms

 The normal forms provide criteria for determining a table’s


degree of vulnerability to logical inconsistencies and anomalies
 1NF, 2NF, 3NF, …, 6NF
 Normalization is a process to design a relational database table
up to higher normal form.  
 The process is progressive, and a higher level of database
normalization cannot be achieved unless the previous levels
have been satisfied. 
 normal forms beyond 4NF are mainly of academic interest, as
the problems they exist to solve rarely appear in practice
1st Normal Form(1NF)

 1NF: a table should faithfully represent a relation and


has no repeating groups
 Each attribute value contains exactly one value
 There are no duplicate rows (i.e. there is a primary key)
 1NF is the basic requirement for a database schema to
be called relational database schema
 Borrow(StuID, Name, Dept, Mngr, BookID, Date) 
1NF
2nd Normal Form(2NF)

 2NF: If relational schema R1NF, and every non-prime


attribute in R is fully functionally dependent on every
candidate key of R, then R2NF
2nd Normal Form(2NF)

 Borrow(StuID, Name, Dept, Mngr, BookID, Date)  2NF


 StuID → Name
 StuID → Dept
 StuID → Mngr
 Dept → Mngr
 (StuID, BookID) → Date
 Name, Dept, and Mngr partially dependent on the
candidate key
2nd Normal Form(2NF)

 Normalize the schema Borrow to 2NF


 Student(StuID, Name, Dept, Mngr)  2NF
 Borrow2(StuID, BookID, Date)  2NF
2nd Normal Form(2NF)

Student Borrow2
StuID Name Dept Mngr StuID BookID Date
S0001 Alice CS Boss1 S0001 B001 2010-1-1
S0002 Bob CS Boss1 S0001 B002 2010-1-2
S0003 Carol ME Boss2 S0001 B003 2010-1-3
S0002 B004 2010-1-5
S0003 B005 2011-2-6

 2NF does not completely solve anomalies


3rd Normal Form(3NF)

 3NF: If relational schema R1NF, and every non-prime


attribute is non-transitively dependent on every
candidate key in R, then R3NF
 If R3NF, then R2NF
3rd Normal Form(3NF)

 Student(StuID, Name, Dept, Mngr)  3NF


 Functional dependencies:
 StuID → Dept, Dept → Mngr
 So StuID → Mngr is a transitively dependent
3rd Normal Form(3NF)

 Normalize the schema Student to 3NF


 Student2(StuID, Name, Dept)  3NF
 Department(Dept, Mngr)  3NF
Student2 Department
StuID Name Dept Dept Mngr
S0001 Alice CS CS Boss1
S0002 Bob CS ME Boss2
S0003 Carol ME
Review

2.1 Relational Model


2.2 Structured Query Language
2.3 Database Design

You might also like