Relational Model 2015, Fall Pusan National University Ki-Joune Li.

Slides:



Advertisements
Similar presentations
IS698: Database Management Min Song IS NJIT. The Relational Data Model.
Advertisements

1 Relational Algebra* and Tuple Calculus * The slides in this lecture are adapted from slides used in Standford's CS145 course.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
Foundations of Relational Implementation n Defining Relational Data n Relational Data Manipulation n Relational Algebra.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Midterm Review Lecture 14b. 14 Lectures So Far 1.Introduction 2.The Relational Model 3.Disks and Files 4.Relational Algebra 5.File Org, Indexes 6.Relational.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.
Database Systems Lecture 5 Natasha Alechina
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Relational Algebra Basic Operations Algebra of Bags.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
Databases 1 First lecture. Informations Lecture: Monday 12:15-13:45 (3.716) Practice: Thursday 10:15-11:45 (2-519) Website of the course:
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Switch off your Mobiles Phones or Change Profile to Silent Mode.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Relational Algebra Spring 2012 Instructor: Hassan Khosravi.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
RELATIONAL DATA MODEL 1. 2 What is a Data Model? 1.Mathematical representation of data. wExamples: relational model = tables; semistructured model = trees/graphs.
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Databases : Relational Algebra 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
From Professor Ullman, Relational Algebra.
1 Database Systems Defining Database Schema Views.
Lu Chaojun, SJTU Relational Data Model 1. Lu Chaojun, SJTU What’s a Data Model? A notation (collection of conceptual tools) for describing data as seen.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
CSCE 520- Relational Data Model Lecture 2. Oracle login Login from the linux lab or ssh to one of the linux servers using your cse username and password.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
Relational Algebra BASIC OPERATIONS 1 DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA.
Relational Algebra p BIT DBMS II.
File Processing : Query Processing 2008, Spring Pusan National University Ki-Joune Li.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
1 Introduction to Database Systems, CS420 Relational Algebra.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
CPSC-310 Database Systems
Basic Operations Algebra of Bags
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
CPSC-310 Database Systems
COMP3017 Advanced Databases
COP4710 Database Systems Relational Algebra.
CS 480: Database Systems Lecture 13 February 13,2013.
THE RELATIONAL MODEL OF DATA
Chap 2. The Relational Model of Data
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CS 440 Database Management Systems
Relational Algebra Chapter 4, Part A
Database Models Relational Model
SQL OVERVIEW DEFINING A SCHEMA
2018, Fall Pusan National University Ki-Joune Li
Defining a Database Schema
Basic Operations Algebra of Bags
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CPSC-608 Database Systems
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CPSC-608 Database Systems
CMSC-461 Database Management Systems
SQL (Structured Query Language)
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

Relational Model 2015, Fall Pusan National University Ki-Joune Li

Basic Ideas of Relational Model A mathematical notation o rather than diagrammatic notations such as UML diagram Structures o A Database : Set of Tables o A Table: Relation R between A 1, A 2,..., A n  R  A 1 x A 2 x...x A n = { (a 1,a 2,a 3,...,a n )| a i  A i }  A i : attribute (or domain, field)  a i : attribute value  schema: R (A 1, A 2,..., A n )  tuple: (a 1,a 2,a 3,...,a n ) 2

Relation as a Table 3 NameManufacturer HiteJinro CassOB Attributes (column headers) Tuples (rows) Beers (Name, Manufacturer) Beers

Relational Schema Relation schema = o relation name o attributes, in order (+ types of attributes).  Example: Beers(name, manufacturer) or Beers(name: string, manf: string) o key definition o Any additional constraints Example o Schema: Movie(title, year, length, genre) 4 Movie + title + year + length + genre In UML

Why Relational Model Very simple model. Often matches how we think about data. Relational Model and SQL o Most important database language today o Not Procedural but Declarative  Once optimized by DBMS, then Ideal Algebraic Background o Relational Algebra 5

Schema Definition in SQL SQL (pronounced as “sequel” or SQL) o A declarative Language for relational databases o DDL (Data Definition Language) – Schema Definition  Table  View - External Layer of DB o DML (Data Manipulation Language) – Querying Schema Definition in SQL o Example CREATE TABLE Movies ( titleCHAR(100), yearINT, lengthINT, genreCHAR(10), studioNameCHAR(20), producerC#INT, PRIMARY KEY(title, year) ); 6

Basic Data Types in SQL CHAR(n), or VARCHAR(n) BIT(n), or BIT VARYING(n) BOOLEAN INT, or INTEGER, SHORTINT FLOAT or REAL, DOUBLE PRECISION, DECIMAL(n,d) DATE, TIME 7

More about SQL for Relation Schema Modification o DROP TABLE Movies; o ALTER TABLE Movies ADD address CHAR(100); or o ALTER TABLE Movies DROP producerC#; Default Values and Keys CREATE TABLE Movies ( movie#INT PRIMARY KEY, titleCHAR(100), yearINT, lengthINT, genreCHAR(10) DEFAULT‘UNKNOWN’, studioNameCHAR(20), producerC#INT, ); 8

Relational Algebra Algebra: Set (Operands) + Operators In Relational Algebra o Operand: Relation (Table) o Operator: Relational Operators  Set Operator: Union, Intersection, Difference  Selection  Projection  Join  Aggregate

Relational Operators: Set Operators Union (  ) Intersection (  ) Difference (-) 10

Relational Operators : Select Selection (  condition ) o Retrieve records satisfying predicates o Example  Find Student where Student.Score > 3.5   score>3.5 (Student) o Index or Hash Select Predicate

Relational Operators : Project Project (  attributes ) o Extract interesting attributes o Example  Find Student.name where score > 3.5   name (  acore>3.5 (Student)) o Full Scan Interesting attributes to get Extract

Cartesian Product Cartesian Product (  ) o Two Tables : R 1  R 2 o Produce all cross products Join ( ) r 11 r 12 … r1mr1m R1R1 r 21 r 22 … r2nr2n R2R2  = r 11 … r 21 r 22 … r2nr2n r 12 … r 21 r 22 … r2nr2n r1mr1m r 21 r 22 … r2nr2n … r1mr1m r1mr1m … …

Join Join ( ) o Select combined records of Cartesian product with same value of a common attribute (Natural Join) o Example Student (StudentName, AdvisorProfessorID, Department, Score) Professor(ProfessorName, ProfessorID, Department) Student AdivsorProfessorID=ProfessorID Professor =  AdivsorProfessorID=ProfessorID (Student  Professor) o Natural Join vs. Theta Join  Natural Join  Theta Join - as for SELECT, condition can be any Boolean condition.  Historic versions of this operator allowed only A theta B, where theta was =, <, etc.; hence the name “ theta-join. ”

Join – Natural Join Example 15 BarInfo = Sells Sells.bar = Bars.name Bars = Sells Bars BarInfo (bar, beer, price, name, addr)  BarInfo(bar, beer, price, addr) Sells (bar, beer, price) Bars (name,addr) barbeerpriceaddr Joe’sBud2.50Maple St. Joe’sMiller2.75Maple St. Sue’sBud2.50River Rd. Sue’sCoors3.00River Rd

Relational Algebra o Operand : Table (Relation) o Operator : Relational Operator ( , ,, etc) o Example: SQL and relational algebra  find Student.Name from Student, Professor where Student.Score > 3.5 and Student.AdvisorProfessorID=Professor.ID and Professor.Department=‘CSE’  student.name (  score>3.5 (Student)  Department=‘CSE’ (Professor) ) o Relational Algebra Specifies the sequence of operations  

Expression Tree, and execution order 17 Expression Tree  student.name  score>3.5 Student Professor  Department=‘CSE’  student.name  Student.score>3.5 and Professor.Department =‘CSE’ Student Professor X Equivalent R  S = R – (R – S) R C S =  C (R X S)

Constraints on relations Referential constraints o One-way association in UML o Student(name, ID, dept, score), Department(name, college, office) o then  dept (Student)   name (Department) or  dept (Student) -  name (Department) =  o i.e. every value of Student.dept must be found in Department.name o i.e. the reference of Department from Student must exist. 18 StudentDepartment

Key Constraint At least one attribute or one combination of attributes to identify each tuple. Example o Student (name, ID, dept, score) If ID is key attribute of Student, then  Stud1.ID=Stud2.ID AND Stud2.dept≠Stud2.dept (Stud1 X Stud2) =  where  Stud1 (Student) and  Stud2 (Student) 19 renaming

From UML to Relational Model Class to Relational Schema Association Aggregation/Composition Inheritance Weak Entity Set 20

From Class to Relation 21 Employee + name + address + citizen ID + division + salary CREATE TABLE Employee ( nameCHAR(30), addressCHAR(100), citizenIDCHAR(12) PRIMARY KEY, divisionCHAR(20), salaryREAL );

From Association to Relation Schema One-way association Why not in Company ? 22 Person Company + name + age + ID: Key + name + address + category PK(name, address) worksFor CREATE TABLE Person ( nameCHAR(30), ageINT, IDCHAR(12) PRIMARY KEY, companyNameCHAR(40), companyAddressCHAR(100) );

From Association to Relation Schema Two-way association Why not with Player and Team? 23 Player Team + name + age + position + ID PK(ID) + name + address + city + sponsor PK (name) Contract CREATE TABLE Contract( signYearYear, salaryINT, playerIDCHAR(12), teamNameCHAR(40), Primary Key(playerID, teamName) ); - signYear - salary 20..* 1..*

From Aggregation to Relation Schema Either as one-way association or Two-way association 24

From inheritance to relational schema 25 StudentEmployee Person + name + age + ID: Key + dept + studentID + score + division + salary + position CREATE TABLE Person ( nameCHAR(30), ageINT, IDCHAR(12) PRIMARY KEY, ); CREATE TABLE Student( name CHAR(30), age INT, ID CHAR(12) PRIMARY KEY, dept CHAR(30), studentID INT Key, score REAL );

Handling Weak Entity Set 26 PlayerFootball team * - name - back number - position - team name - city - sponsor belong to Weak Entity Set CREATE TABLE Player ( nameCHAR(30), backNumberINT, positionCHAR(3), teamNameCHAR(30), Primary Key(backNumber, teamName) );

Handling Weak Entity Set 27 PlayerFootball team * - name - back number - position - team name - city - sponsor belong to CREATE TABLE belongTo ( backNumberINT, teamNameCHAR(30), Primary Key(backNumber, teamName) );