Professional Documents
Culture Documents
Relational Database
A relation databae is built based on a relation model
Outline
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
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
Domain
A domain is the value range of an attribute
Example
DName={Alice, Bob, Carol, ….}
DGender={Female, Male}
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
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
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?
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
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
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
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
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