the relational modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. ·...

31
The Relational Model T. M. Murali August 25, 2010 T. M. Murali August 25, 2010 CS4604: The Relational Model

Upload: others

Post on 11-Nov-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

The Relational Model

T. M. Murali

August 25, 2010

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 2: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Course Outline

I Weeks 1–5, 13: Query/Manipulation Languages

I The relational modelI Relational AlgebraI SQLI Data definitionI Programming with SQL

I Weeks 6–8: Data Modelling

I Weeks 9–13: Relational Design

I Week 14–15: Students’ choice

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 3: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

What is a Data Model?

I Data model: notation for describing data or information.

I Structure of the data: models in databases are conceptual as opposedto physical.

I Operations on the data: databases data models allow limited set ofqueries and modifications.

I Constraints on the data: database data models support specificationsof constraints on the data.

I Examples of Data Models:

1. Relational data model.2. Semi-structured data model (e.g., XML).3. Object-relational models.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 4: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

What is a Data Model?

I Data model: notation for describing data or information.I Structure of the data: models in databases are conceptual as opposed

to physical.I Operations on the data: databases data models allow limited set of

queries and modifications.I Constraints on the data: database data models support specifications

of constraints on the data.

I Examples of Data Models:

1. Relational data model.2. Semi-structured data model (e.g., XML).3. Object-relational models.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 5: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

What is a Data Model?

I Data model: notation for describing data or information.I Structure of the data: models in databases are conceptual as opposed

to physical.I Operations on the data: databases data models allow limited set of

queries and modifications.I Constraints on the data: database data models support specifications

of constraints on the data.

I Examples of Data Models:

1. Relational data model.2. Semi-structured data model (e.g., XML).3. Object-relational models.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 6: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

The Relational Model

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

I Structure: Table (like an array of structs)

I Operations: Relational algebra (selection, projection, conditions, etc.)

I Constraints: E.g., grades can be from a fixed list.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 7: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 8: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

The Semi-structured Model<CoursesTaken>

<Student>Hermione Grainger</Student>

<Course>Potions</Course>

<Grade>A-</Grade>

<Student>Draco Malfoy</Student>

<Course>Potions</Course>

<Grade>B</Grade>

...

</CoursesTaken>

I Structure: Trees or graphs, tags define role played by different piecesof data.

I Operations: Follow paths in the implied tree from one element toanother.

I Constraints: Can express limitations on data types.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 9: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Comparison of Models

I Flexibility:

XML models can represent graphs.I Limitations of relational model:

1. Simple, limited approach to structuring data.2. Limited but useful set of operations on the data.

I Ease of use: SQL enables programmer to express wishes at high level.

I Efficiency of access/modifications: Dramatic query optimisationspossible.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 10: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Comparison of Models

I Flexibility: XML models can represent graphs.

I Limitations of relational model:

1. Simple, limited approach to structuring data.2. Limited but useful set of operations on the data.

I Ease of use: SQL enables programmer to express wishes at high level.

I Efficiency of access/modifications: Dramatic query optimisationspossible.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 11: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Comparison of Models

I Flexibility: XML models can represent graphs.I Limitations of relational model:

1. Simple, limited approach to structuring data.2. Limited but useful set of operations on the data.

I Ease of use: SQL enables programmer to express wishes at high level.

I Efficiency of access/modifications: Dramatic query optimisationspossible.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 12: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Comparison of Models

I Flexibility: XML models can represent graphs.I Limitations of relational model:

1. Simple, limited approach to structuring data.2. Limited but useful set of operations on the data.

I Ease of use: SQL enables programmer to express wishes at high level.

I Efficiency of access/modifications: Dramatic query optimisationspossible.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 13: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Comparison of Models

I Flexibility: XML models can represent graphs.I Limitations of relational model:

1. Simple, limited approach to structuring data.2. Limited but useful set of operations on the data.

I Ease of use: SQL enables programmer to express wishes at high level.

I Efficiency of access/modifications: Dramatic query optimisationspossible.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 14: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

What is the Relational Model?

I Built around a single concept for modelling data: the relation or table.

I Supports high-level programming language (SQL).

I Has an elegant mathematical design theory.

I Most current DBMS are relational.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 15: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

The Relation

I A relation is a two-dimensional table:I Relation ≡ table.I Attribute ≡ column name.I Tuple ≡ row (not the header row).

I Database ≡ collection of relations.

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 16: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

The Schema

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

I The schema of a relation is the name of the relation followed by aparanthetised list of attributes.

CoursesTaken(Student, Course, Grade)

I A design in a relational model consists of a set of schemas.I Such a set of schemas is called a relational database schema.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 17: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Equivalent Representations of a Relation

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

I A relation is a set of tuples and not a list of tuples.I Order in which we present the tuples does not matter.

I The attributes in a schema are a set (not a list).I Schema is the same irrespective of order of attributes.I We specify a “standard” order when we introduce a schema.I If we reorder attributes, we must also reorder tuples.

I How many equivalent representations are there for a relation with mattributes and n tuples?

m!n!.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 18: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Equivalent Representations of a Relation

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

I A relation is a set of tuples and not a list of tuples.I Order in which we present the tuples does not matter.

I The attributes in a schema are a set (not a list).I Schema is the same irrespective of order of attributes.I We specify a “standard” order when we introduce a schema.I If we reorder attributes, we must also reorder tuples.

I How many equivalent representations are there for a relation with mattributes and n tuples?

m!n!.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 19: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Equivalent Representations of a Relation

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

I A relation is a set of tuples and not a list of tuples.I Order in which we present the tuples does not matter.

I The attributes in a schema are a set (not a list).I Schema is the same irrespective of order of attributes.I We specify a “standard” order when we introduce a schema.I If we reorder attributes, we must also reorder tuples.

I How many equivalent representations are there for a relation with mattributes and n tuples? m!n!.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 20: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Schema vs. Instance

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

I Schema of a relation rarely changes.

I Instance of a relation is the set of tuples in it.

I Relation instances can and are expected to change over time.

I A conventional database maintains the “current” instances of therelations in it.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 21: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Keys of Relations

I A set of attributes forms a key if we forbid any pair of tuples in aninstance from having the same values for the attributes in the key.

I Indicate the attributes in the key by underlining them.

I What is the key for CoursesTaken?

I Just Student if the relation stores information for only one course.I The pair (Student, Course) if the relation stores information for many

courses.I If a student can take the same course many times,

add Semester andYear as attributes of the relation and include both in the key.

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 22: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Keys of Relations

I A set of attributes forms a key if we forbid any pair of tuples in aninstance from having the same values for the attributes in the key.

I Indicate the attributes in the key by underlining them.

I What is the key for CoursesTaken?

I Just Student if the relation stores information for only one course.I The pair (Student, Course) if the relation stores information for many

courses.I If a student can take the same course many times,

add Semester andYear as attributes of the relation and include both in the key.

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 23: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Keys of Relations

I A set of attributes forms a key if we forbid any pair of tuples in aninstance from having the same values for the attributes in the key.

I Indicate the attributes in the key by underlining them.

I What is the key for CoursesTaken?I Just Student if the relation stores information for only one course.

I The pair (Student, Course) if the relation stores information for manycourses.

I If a student can take the same course many times,

add Semester andYear as attributes of the relation and include both in the key.

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 24: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Keys of Relations

I A set of attributes forms a key if we forbid any pair of tuples in aninstance from having the same values for the attributes in the key.

I Indicate the attributes in the key by underlining them.

I What is the key for CoursesTaken?I Just Student if the relation stores information for only one course.I The pair (Student, Course) if the relation stores information for many

courses.

I If a student can take the same course many times,

add Semester andYear as attributes of the relation and include both in the key.

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 25: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Keys of Relations

I A set of attributes forms a key if we forbid any pair of tuples in aninstance from having the same values for the attributes in the key.

I Indicate the attributes in the key by underlining them.

I What is the key for CoursesTaken?I Just Student if the relation stores information for only one course.I The pair (Student, Course) if the relation stores information for many

courses.I If a student can take the same course many times,

add Semester andYear as attributes of the relation and include both in the key.

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 26: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Keys of Relations

I A set of attributes forms a key if we forbid any pair of tuples in aninstance from having the same values for the attributes in the key.

I Indicate the attributes in the key by underlining them.

I What is the key for CoursesTaken?I Just Student if the relation stores information for only one course.I The pair (Student, Course) if the relation stores information for many

courses.I If a student can take the same course many times, add Semester and

Year as attributes of the relation and include both in the key.

CoursesTakenStudent Course Grade

Hermione Grainger Potions A-

Draco Malfoy Potions B

Harry Potter Potions A

Ron Weasley Potions C

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 27: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Example: Class Enrollments in a Single Semester

1. Student: a name, a unique PID, and an address.

2. Professor: a name, a unique PID, and belongs to a department. Alsorecord the age and office of the professor.

3. Course: a name, a number, an offering department, a classroom, andan enrollment. (No CRNs.) Each department offers only one coursewith each number.

4. Department: a unique name, ≤ 1 chairperson. Each chairperson canbe the head of at most one department.

5. At most one professor teaches each course. Each student receives agrade in each course. Each student evaluates the professor teachingthe course.

6. Each course can have ≥ 0 pre-requisites, excluding itself. A studentenrolled in a course must have enrolled in all its pre-requisites.

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 28: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Relational Design for the Example

I What should the keys be?

Students(PID: string, Name: string, Address: string)

Professors(PID: string, Name: string, Office: string,

Age: integer, DepartmentName: string)

Courses(Number: integer, DeptName: string,

CourseName: string, Classroom: string, Enrollment:

integer)

Departments(Name: string, ChairPID: string)

Take(StudentPID: string, Number: integer, DeptName:

string, Grade: string, ProfessorEvaluation: integer)

Teach(ProfessorPID: string, Number: integer, DeptName:

string)

PreReq(Number: integer, DeptName: string,

PreReqNumber: integer, PreReqDeptName: string)

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 29: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Relational Design for the Example

I What should the keys be?

Students(PID: string, Name: string, Address: string)

Professors(PID: string, Name: string, Office: string,

Age: integer, DepartmentName: string)

Courses(Number: integer, DeptName: string,

CourseName: string, Classroom: string, Enrollment:

integer)

Departments(Name: string, ChairPID: string)

Take(StudentPID: string, Number: integer, DeptName:

string, Grade: string, ProfessorEvaluation: integer)

Teach(ProfessorPID: string, Number: integer, DeptName:

string)

PreReq(Number: integer, DeptName: string,

PreReqNumber: integer, PreReqDeptName: string)

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 30: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Relational Design for the Example

I What should the keys be?

Students(PID: string, Name: string, Address: string)

Professors(PID: string, Name: string, Office:

string, Age: integer, DepartmentName: string)

Courses(Number: integer, DeptName: string,

CourseName: string, Classroom: string, Enrollment:

integer)

Departments(Name: string, ChairPID: string)

Take(StudentPID: string, Number: integer, DeptName:

string, Grade: string, ProfessorEvaluation: integer)

Teach(ProfessorPID: string, Number: integer, DeptName:

string)

PreReq(Number: integer, DeptName: string,

PreReqNumber: integer, PreReqDeptName: string)

T. M. Murali August 25, 2010 CS4604: The Relational Model

Page 31: The Relational Modelcourses.cs.vt.edu/.../lecture-02-intro-relational-model.pdf · 2010. 9. 2. · I Limitations of relational model: 1.Simple, limited approach to structuring data

Issues to Consider in the Design

I Can we merge Courses and Teach since each course is taught by atmost one professor?

I Do we need a separate relation to store evaluations?

I How can we handle pre-requisites that are “or”s, e.g., you can takeCS 4604 if you have taken either CS 2604 or CS 2606?

I What should we do if more than one professor can teach the samecourse?

I How do we generalise this schema to handle data over more than onesemester?

I How do we handle the situation when a student is allowed to re-takea course?

T. M. Murali August 25, 2010 CS4604: The Relational Model