מודל היחסים (Relational Model)

Slides:



Advertisements
Similar presentations
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Other Relational.
Advertisements

Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Domain Relational Calculus and Query-by-Example CS157a John Eagle.
BRANCH-SCHEME (BRANCH-NAME, ASSETS, BRANCH-CITY)
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
Relational Calculus Ameetinder Singh CS 157A. Tuple Relational Calculus  non-procedural query language as compared to relational algebra that is procedural.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
Instructor: Mohamed Eltabakh
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Holliday - COEN 1781 Introduction to SQL. Holliday - COEN 1782 DB Tables and SQL The data is stored in the database in relations or tables Data types.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model III (based on notes by Silberchatz,Korth, and.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
CS 370 Database Systems Lecture 11 Relational Algebra.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
SCUHolliday - coen 1785–1 Schedule Today: u Relational Algebra. u Read Chapter 5 to page 199. Next u SQL Queries. u Read Sections And then u Subqueries,
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Relational Algebra Instructor: Mohamed Eltabakh 1 Part II.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 5: Other Relational Languages.
Relational Algebra HW2 Turn in as a hardcopy at the start of next class period. You may work this assignment in groups.
ICOM 5016 – Introduction to Database Systems Lecture 6 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
Copyright © 2004 Pearson Education, Inc.. Chapter 6 The Relational Algebra and Relational Calculus.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Session 3 Welcome: To session 3-the fourth learning sequence
Chapter 3: Relational Model
Introduction to Relational Model
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Session 3 Welcome: To session 3-the fifth learning sequence
Chapter 3: Relational Model III
Chapter 3: Relational Model
Chapter 3: Relational Model
Chapter 2: Intro to Relational Model
Chapter 3: Relational Model
Introduction to Database
הקדמה היתרון במודל הטבלאי: המודל כקבוצה של יחסים (טבלאות)
אילוצי שלמות אילוצי תחום מפתחות ומפתחות זרים טריגרים triggers
מודל היחסים (Relational Model)
Chapter 2: Relational Model
Introduction to Database
Chapter 3: Relational Model
Session 3 Welcome: To session 3-the second learning sequence
Instructor: Mohamed Eltabakh
Chapter 3: Relational Model
Introduction to SQL Holliday - COEN 178.
Chapter 3: Relational Model
Chapter 3: Relational Model
Chapter 2: Relational Model
Relational Model.
Relational Algebra.
Schedule Today: Next And then Relational Algebra.
Chapter 2: Intro to Relational Model
Chapter 3: Relational Model
Schema Used in Examples
Database Dr. Roueida Mohammed.
Chapter 3: Relational Model
Relational Model B.Ramamurthy 5/28/2019 B.Ramamurthy.
Chapter 4 Relational Algebra
DB:Relational Algebra
Presentation transcript:

מודל היחסים (Relational Model) תכונה (attributes): עמודה בטבלה. nיה (tuple): שורה בטבלה. בסיס נתונים יחסי (relational database) : אוסף של יחסים (טבלאות), כל אחד בעלת שם ייחודי.

הגדרה מתמטית יחס הכולל n תכונות (עמודות) הוא תת-קבוצה של המכפלה הקרטזית של תחומי התכונות המתאימות: D1D2D3…Dn-1 Dn

תבנית בסיס הנתונים שם תבנית + תכונות: Deposit-scheme=(branch-name, account-number, customer-name, balance) הגדרה מלאה של תבנית כוללת גם תחומי התכונות. דוגמא נוספת: Customer-scheme=(customer-name, street,customer-city) התכונה customer-name המשותפת לשתי התבניות היא הדרך לקשר nיות בין יחסים שונים

מערכת הבנק תבניות היחסים Branch=(branch-name, assets, branch-city) customer branch borrow name street city assets balance deposit number amount תבניות היחסים Branch=(branch-name, assets, branch-city) Customer=(customer-name, street,customer-city) Deposit=(branch-name, account-number, customer-name, balance) Borrow=(branch-name, loan-number, customer-name, amount)

דוגמאות ליחסים - מערכת הבנק branch customer borrow deposit

שפות שאילתות (query languages) שפות פורמליות: אלגברת יחסים - שפה פרוצדורלית. תחשיבי יחסים - שפות לא פרוצדורלית.

אלגברת היחסים (relational algebra) פעולות נוספות פעולות בסיסיות חיתוך צירוף טבעי חילוק בחירה הטלה מכפלה קרטזית כינוי איחוד הפרש

בחירה (select) הבחירה פועלת על היחס r בעל תבנית R ומגדירה יחס חדש בעל תבנית R הכולל רק nיות המקיימות את הפרדיקט p. p(r) בחר את חשבונות ההלוואה בסניף ‘Aviv’ branch-name=‘Aviv’(borrow) ניתן לצרף פרדיקטים אחדים באמצעות  או  branch-name=‘Aviv’ amount>1200(borrow)

הטלה (project) A1,A2,…An(r) branch-name,customer-name(borrow) ההטלה פועלת על היחס r בעל תבנית R ומגדירה יחס חדש הכולל את העמודות המתאימות לתכונות A1,A2,…An. (A1,A2,…An R). לכל חשבון הלוואה מצא שם הלקוח ושם הסניף אליו שייך החשבון branch-name,customer-name(borrow)

מכפלה קרטזית (cartesian product) המכפלה הקרטזית של שני יחסים r ו s בעלי תבניות (R=(A1,A2,…An) S=(B1,B2,...Bm מגדירה יחס חדש בעל תבנית (A1,A2,…An,B1,B2,...Bm) הכולל את כל השרשורים האפשריים של nיות מ r עם nיות מ s . rs r s rxs

customer.customer-name,customer-city,balance לכל חשבון חיסכון מצא את שם בעל החשבון, את עיר מגוריו ואת סכום החיסכון customer.customer-name,customer-city,balance (customer.customer-name= deposit.customer-name (customerdeposit)) customer x deposit תוצאת ביניים תוצאה סופית

כינוי (rename) הכינוי של יחס r מגדיר יחס חדש x הזהה ליחס r . x(r) מצא את שמות כל הלקוחות הגרים בעיר שבה גר ‘morag’ customer.customer-name (customer.customer-city=cust2.customer-city (customercust2(customer-city(customer-name=‘morag’(customer)))))

איחוד (union) customer-name(branch-name=‘Hamerkaz’(borrow))  האיחוד של שני יחסים r ו s מגדיר יחס חדש שתבניתו זהה לתבנית של שני היחסים וכולל את איחוד היחסים r ו s . תבניות היחסים של r ו s חייבות לכלול אותו מספר תכונות. התחום של התכונה ה i בשתי התבניות חייב להיות זהה. מצא את שמות כל הלקוחות של סניף Hamerkaz (בעלי חשבונות חיסכון או חשבונות הלוואה) rs customer-name(branch-name=‘Hamerkaz’(borrow))   customer-name(branch-name=‘Hamerkaz’(deposit))

הפרש (difference) customer-name(branch-name=‘Hamerkaz’(deposit)) - ההפרש של שני יחסים r ו s מגדיר יחס חדש שתבניתו זהה לתבנית של שני היחסים וכולל את הפרש היחסים (במובן של תורת הקבוצות). התנאי על תבניות היחסים - כנ”ל. מצא את שמות הלקוחות של סניף Hamerkaz שיש להם חשבון חיסכון אך לא חשבון הלוואה. r-s customer-name(branch-name=‘Hamerkaz’(deposit)) -  customer-name(branch-name=‘Hamerkaz’(borrow))

deposit.balance(deposit.balance<d.balance (depositd(deposit))) מצא את סכום החיסכון המרבי שאילתת ביניים: כל סכומי החיסכון הקטנים מסכום חיסכון כלשהו deposit.balance(deposit.balance<d.balance (depositd(deposit))) השאילתה הסופית balance(deposit) - deposit.balance(deposit.balance<d.balance (depositd(deposit)))

חיתוך (intersection) החיתוך של שני יחסים r ו s מגדיר יחס חדש שתבניתו זהה לתבנית של שני היחסים וכולל את חיתוך היחסים (במובן של תורת הקבוצות). מצא את שמות הלקוחות של סניףHamerkaz שיש להם גם חשבון הלוואה וגם חשבון חיסכון. rs customer-name(branch-name=‘Hamerkaz’(deposit))   customer-name(branch-name=‘Hamerkaz’(borrow))

צירוף טבעי (natural join) הצירוף הטבעי של שני יחסים r ו s מגדיר יחס חדש, שתבניתו היא איחוד תבניות היחסים, וכולל את כל השרשורים האפשריים של nיות מ r עם nיות מ s שערכיהן זהים בכל התכונות המשותפות. r⋈s לכל חשבון חיסכון מצא את שם בעל החשבון, את עיר מגוריו ואת סכום החיסכון customer-name,customer-city,balance (customer⋈deposit )

branch-name,assets(customer-city=‘Haifa’(customer ) ⋈ deposit ⋈ branch)

חילוק (division) החילוק של r ו s בעלי תבניות R ו S בהתאמה (מוגדר בתנאי ש SR - הכלה ממש) מגדיר יחס חדש, שתבניתו היא R-S . rs היא תת הקבוצה המקסימלית של שמכפלתה הקרטזית ב s מהווה תת קבוצה של r. rs R-S(r) r s r s דוגמא

customer-name,branch-name(deposit)  מצא שמות הלקוחות שיש להם חשבונות חיסכון בכל הסניפים בעיר ‘Jaffa’ customer-name,branch-name(deposit)  branch-name(branch-city=‘Jaffa’ (branch))

תחשיב יחסים לפי תחומים (domain relational calculus) הגדרה פורמלית ביטוי בתחשיב יחסים לפי תחומים הוא מהצורה {<x1,x2…xn >| P(x1,x2…xn )} כאשר x1,x2…xn מייצגים משתני תחום. P היא נוסחה המורכבת מאטומים. משתנה תחום נקרא משתנה קשור אם הוא מופיע בצמוד לכמת  או . אחרת הוא נקרא משתנה חופשי. לאטום בתחשיב יחסים לפי תחומים יש אחת מהצורות הבאות: כאשר r הוא יחס על n תכונות ו x1,x2…xn הם משתני תחום או קבועים. x  y כאשר x ו y משתני תחום , ו  הוא אופרטור השוואה (<, , ,= , >). נדרוש כי x ו y הם בעלי תחומים שניתנים להשוואה באמצעות . x  c כאשר x משתנה תחום,  הוא אופרטור השוואה ו c הוא קבוע מהתחום של התכונה שעבורה x הוא משתנה תחום. <x1,x2…xn>  r

לקיצור הסימון נהוג לכתוב במקום נוסחה בנויה מאטומים בעזרת הכללים הבאים: אטום הוא נוסחה. אם P הוא נוסחה אז גם ¬Pו (P) . אם P ו Q הם נוסחאות אזי גם P  Q , P Q, P  Q . אם (P(x הוא נוסחה כאשר x הוא משתנה תחום אזי .הם נוסחאות x (P(x) ) x (P(x) ) a,b,c(P(a,b,c)) לקיצור הסימון נהוג לכתוב a(b( c(P(a,b,c)))) במקום

דוגמאות מצא את שם הסניף, מס’ הלוואה, שם הלקוח וסכום ההלוואה עבור חשבונות ההלוואה בסכום גדול מ 1200 {<b,l,c,a> | < b,l,c,a >  borrow  a>1200} מצא את שמות הלקוחות בעלי חשבונות ההלוואה בסכום גדול מ1200 {<c> | b,l,a(<b,l,c,a>  borrow  a>1200)} מצא את שמות הלקוחות בעלי חשבונות ההלוואה בסניף merkazi ואת עיר מגוריהם {<c,t> |  l,a (< ‘merkazi’,l,c,a>borrow  y (<c,y,t>customer)) }

מצא את שמות הלקוחות שיש להם חשבון חיסכון בכל הסניפים שבעיר ‘jaffa’ מצא את שמות כל הלקוחות של סניף merkazi (בעלי חשבון הלוואה או חשבון חיסכון או שניהם) {<c> |  l,a (< ‘merkazi’,l,c,a>borrow)  a,n (<‘merkazi’,a,c,n>deposit)} מצא את שמות הלקוחות שיש להם חשבון חיסכון בכל הסניפים שבעיר ‘jaffa’ {<c> |  x,y (<x,y, ‘jaffa’>branch   a,n (<x,a,c,n>deposit ) )} הערה: ניתן להתגבר על המקרה שבו אין אף סניף ב Jaffa {<c> |  x,y (<x,y, ‘jaffa’ >branch)   x,y (<x,y, ‘jaffa’ >branch   a,n (<x,a,c,n>deposit ) )}