Top Banner
Data Models The Relational Model Example The Relational Model T. M. Murali August 26, 2009 T. M. Murali August 26, 2009 CS4604: The Relational Model
31

The Relational Modelcs4604/Fall09/lectures/...The Schema CoursesTaken Student Course Grade Hermione Grainger Potions A-Draco Malfoy Potions B Harry Potter Potions A Ron Weasley Potions

Jan 27, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • Data Models The Relational Model Example

    The Relational Model

    T. M. Murali

    August 26, 2009

    T. M. Murali August 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    T. M. Murali August 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    The Semi-structured Model

    Hermione GraingerPotionsA-

    Draco MalfoyPotionsB

    ...

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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, ChairmanPID: 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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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, ChairmanPID: 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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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, ChairmanPID: 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 26, 2009 CS4604: The Relational Model

  • Data Models The Relational Model Example

    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 handle the situation when a student is allowed to re-takea course?

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

    I What modifications does the schema need if more than one professorcan teach a course?

    T. M. Murali August 26, 2009 CS4604: The Relational Model

    Data ModelsThe Relational ModelExample