2018, 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.
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
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.
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.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Relational Algebra Basic Operations Algebra of Bags.
Relational Model 2015, Fall Pusan National University Ki-Joune Li.
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.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases : Relational Algebra 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
File Processing : Query Processing 2008, Spring Pusan National University Ki-Joune Li.
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 Database Design: DBS CB, 2 nd Edition Relational Algebra: Basic Operations & Algebra of Bags Ch. 5.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Structured Query Language (SQL) DDL
CPSC-310 Database Systems
Basic Operations Algebra of Bags
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Relational Data Model Lu Chaojun, SJTU.
CPSC-310 Database Systems
COMP3017 Advanced Databases
COP4710 Database Systems Relational Model.
COP4710 Database Systems Relational Algebra.
CS 480: Database Systems Lecture 13 February 13,2013.
Introduction to Structured Query Language (SQL)
THE RELATIONAL MODEL OF DATA
Database Design: DBS CB, 2nd Edition
Chap 2. The Relational Model of Data
Introduction to Database Systems, CS420
CPSC-310 Database Systems
CPSC-608 Database Systems
CS 440 Database Management Systems
Chapter 2: Intro to Relational Model
CPSC-608 Database Systems
Relational Algebra Chapter 4, Part A
Database Models Relational Model
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
File Processing : Query Processing
File Processing : Query Processing
Relational Databases The Relational Model.
Relational Databases The Relational Model.
SQL OVERVIEW DEFINING A SCHEMA
2018, Fall Pusan National University Ki-Joune Li
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:
Chapter 2: Intro to Relational Model
CS4433 Database Systems Relational Model.
SQL – Constraints & Triggers
CPSC-608 Database Systems
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Lecture 14: SQL Wednesday, October 31, 2001.
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
SQL (Structured Query Language)
2019, Fall Pusan National University Ki-Joune Li
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

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

Basic Ideas of Relational Model A mathematical notation rather than diagrammatic notations such as UML diagram Structures A Database : Set of Tables A Table: Relation R between A1, A2, ..., An R  A1x A2 x ...x An = { (a1,a2,a3,...,an)| ai  Ai } Ai : attribute (or domain, field) ai : attribute value schema: R (A1, A2, ..., An) tuple: (a1,a2,a3,...,an)

Beers (Name, Manufacturer) Relation as a Table Attributes (column headers) Beers Name Manufacturer Hite Jinro Cass OB Tuples (rows) Beers (Name, Manufacturer)

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

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

Schema Definition in SQL SQL (pronounced as “sequel” or SQL) A declarative Language for relational databases DDL (Data Definition Language) – Schema Definition Table View - External Layer of DB DML (Data Manipulation Language) – Querying Schema Definition in SQL Example CREATE TABLE Movies ( title CHAR(100), year INT, length INT, genre CHAR(10), studioName CHAR(20), producerC# INT, PRIMARY KEY (title, year) );

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

More about SQL for Relation Schema Modification DROP TABLE Movies; ALTER TABLE Movies ADD address CHAR(100); or ALTER TABLE Movies DROP producerC#; Default Values and Keys CREATE TABLE Movies ( movie# INT PRIMARY KEY, title CHAR(100), year INT, length INT, genre CHAR(10) DEFAULT ‘UNKNOWN’, studioName CHAR(20), producerC# INT, );

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

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

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

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

Cartesian Product Cartesian Product () Join ( ) Two Tables : R1  R2 Produce all cross products Join ( ) r11 … r21 r22 r2n r12 r1m r11 r12 … r1m R1 r21 r22 … r2n R2  =

Join Join ( ) Select combined records of Cartesian product with same value of a common attribute (Natural Join) Example Student (StudentName, AdvisorProfessorID, Department, Score) Professor(ProfessorName, ProfessorID, Department) Student AdivsorProfessorID=ProfessorID Professor =  AdivsorProfessorID=ProfessorID(Student Professor) 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 Sells (bar, beer, price) Bars (name,addr) BarInfo = Sells Sells.bar = Bars.name Bars = Sells Bars BarInfo (bar, beer, price, name, addr)  BarInfo(bar, beer, price, addr) bar beer price addr Joe’s Bud 2.50 Maple St. Miller 2.75 Sue’s River Rd. Coors 3.00 River Rd

Relational Algebra Relational Algebra Operand : Table (Relation) Operator : Relational Operator (, , , etc) 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) ) Relational Algebra Specifies the sequence of operations   

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

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

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

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

From Class to Relation Employee CREATE TABLE Employee ( name CHAR(30), + address + citizen ID + division + salary CREATE TABLE Employee ( name CHAR(30), address CHAR(100), citizenID CHAR(12) PRIMARY KEY, division CHAR(20), salary REAL );

From Association to Relation Schema One-way association Why not in Company ? Person Company worksFor + name + age + ID: Key + name + address + category PK(name, address) CREATE TABLE Person ( name CHAR(30), age INT, ID CHAR(12) PRIMARY KEY, companyName CHAR(40), companyAddress CHAR(100) );

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

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

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

Handling Weak Entity Set belong to Player Football team 1..* 1..1 - name - back number - position - team name - city - sponsor Weak Entity Set CREATE TABLE Player ( name CHAR(30), backNumber INT, position CHAR(3), teamName CHAR(30), Primary Key (backNumber, teamName) );

Handling Weak Entity Set belong to Player Football team 1..* 1..1 - name - back number - position - team name - city - sponsor CREATE TABLE belongTo ( backNumber INT, teamName CHAR(30), Primary Key (backNumber, teamName) );