Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 311 Foundations of Computing I Lecture 22 Relations, Database Autumn 2012 CSE 311 1 TexPoint fonts used in EMF. Read the TexPoint manual before you.

Similar presentations


Presentation on theme: "CSE 311 Foundations of Computing I Lecture 22 Relations, Database Autumn 2012 CSE 311 1 TexPoint fonts used in EMF. Read the TexPoint manual before you."— Presentation transcript:

1 CSE 311 Foundations of Computing I Lecture 22 Relations, Database Autumn 2012 CSE 311 1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A A A A AA

2 Announcements Reading assignments – 7 th Edition, Sections 9.3 and 13.3 – 6 th Edition, Section 8.3 and 12.3 – 5 th Edition, Section 7.3 and 11.3 Autumn 2012CSE 311 2

3 Lecture highlights Autumn 2012CSE 311 3 Let A and B be sets, A binary relation from A to B is a subset of A  B S  R = {(a, c) |  b such that (a,b)  R and (b,c)  S} R 1 = R; R n+1 = R n  R Matrix and graph representation of relations

4 Paths in relations Autumn 2012CSE 311 4 Let R be a relation on a set A. There is a path of length n from a to b if and only if (a,b)  R n

5 Connectivity relation Autumn 2012CSE 311 5 Let R be a relation on a set A. The connectivity relation R* consists of the pairs (a,b) such that there is a path from a to b in R.

6 Transitive-Reflexive Closure Autumn 2012CSE 311 6 Add the minimum possible number of edges to make the relation transitive and reflexive The transitive-reflexive closure of a relation R is the connectivity relation R*

7 How is Anderson related to Bernoulli? Autumn 2012CSE 311 7 Copernicus Anderson

8 How is Anderson related to Bernoulli? Autumn 2012CSE 311 8 Copernicus Suciu

9 Autumn 2012CSE 311 9 http://genealogy.math.ndsu.nodak.edu/

10 BauerCaratheodoryMinkowski Klein LipschitzDirichletFourier LagrangeEuler Johann BernoulliJacob BernoulliLeibniz RheticusCopernicus Anderson Weigel Mayr

11 SuciuTannenMeyerFischerRogers ChurchVeblenMooreH.A.NewtonChasles PoissonLagrangeEulerJohann BernoulliJacob Bernoulli

12 Nicolaus Copernicus Georg Rheticus Moritz Steinmetz Christoph Meurer Philipp Muller Erhard Weigel Gottfried Leibniz Noclas Malebranache Jacob Bernoulli Johann Bernoulli Leonhard Euler Joseph Lagrange Jean-Baptiste Fourier Gustav Dirichlet Rudolf Lipschitz Felix Klein C. L. Ferdinand Lindemann Herman Minkowski Constantin Caratheodory Georg Aumann Friedrich Bauer Manfred Paul Ernst Mayr Richard Anderson Autumn 2012CSE 311 12 Nicolaus Copernicus Georg Rheticus Moritz Steinmetz Christoph Meurer Philipp Muller Erhard Weigel Gottfried Leibniz Noclas Malebranache Jacob Bernoulli Johann Bernoulli Leonhard Euler Joseph Lagrange Michel Chasles H. A. (Hubert Anson) Newton E. H. (Eliakim Hastings) Moore Oswald Veblen Alonzo Church Hartley Rogers, Jr. Patrick Carl Fischer Albert Ronald da Silva Meyer Val Tannen Dan Suciu

13 n-ary relations Let A 1, A 2, …, A n be sets. An n-ary relation on these sets is a subset of A 1  A 2 ...  A n. Autumn 2012CSE 31113

14 Relational databases Student_NameID_NumberOfficeGPA Knuth3280120980224.00 Von Neuman4810802205553.78 Russell2380823880223.85 Einstein2380019200222.11 Newton17270173333.61 Karp3488828110223.98 Bernoulli29219380223.21 Autumn 2012CSE 31114 STUDENT

15 Relational databases Student_NameID_NumberOfficeGPACourse Knuth3280120980224.00CSE311 Knuth3280120980224.00CSE351 Von Neuman4810802205553.78CSE311 Russell2380823880223.85CSE312 Russell2380823880223.85CSE344 Russell2380823880223.85CSE351 Newton17270173333.61CSE312 Karp3488828110223.98CSE311 Karp3488828110223.98CSE312 Karp3488828110223.98CSE344 Karp3488828110223.98CSE351 Bernoulli29219380223.21CSE351 What wrong? STUDENT

16 Relational databases ID_NumberCourse 328012098CSE311 328012098CSE351 481080220CSE311 238082388CSE312 238082388CSE344 238082388CSE351 1727017CSE312 348882811CSE311 348882811CSE312 348882811CSE344 348882811CSE351 2921938CSE351 Better Student_NameID_NumberOfficeGPA Knuth3280120980224.00 Von Neuman4810802205553.78 Russell2380823880223.85 Einstein2380019200222.11 Newton17270173333.61 Karp3488828110223.98 Bernoulli29219380223.21 STUDENT TAKES

17 Database Operations: Projection Autumn 2012CSE 31117 Find all offices: Π Office (STUDENT) Office 022 555 333 Find offices and GPAs: Π Office,GPA (STUDENT) OfficeGPA 0224.00 5553.78 0223.85 0222.11 3333.61 0223.98 0223.21

18 Database Operations: Selection Autumn 2012CSE 31118 Find students with GPA > 3.9 : σ GPA>3.9 (STUDENT) Student_NameID_NumberOfficeGPA Knuth3280120980224.00 Karp3488828110223.98 Retrieve the name and GPA for students with GPA > 3.9 : Π Student_Name,GPA (σ GPA>3.9 (STUDENT)) Student_NameGPA Knuth4.00 Karp3.98

19 Database Operations: Natural Join Student_NameID_NumberOfficeGPACourse Knuth3280120980224.00CSE311 Knuth3280120980224.00CSE351 Von Neuman4810802205553.78CSE311 Russell2380823880223.85CSE312 Russell2380823880223.85CSE344 Russell2380823880223.85CSE351 Newton17270173333.61CSE312 Karp3488828110223.98CSE311 Karp3488828110223.98CSE312 Karp3488828110223.98CSE344 Karp3488828110223.98CSE351 Bernoulli29219380223.21CSE351 Student ⋈ Takes

20 Disambiguating Attribute Names Names of students who share an office with Einstein: Π STUDENT1.Student_Name (STUDENT ⋈ STUDENT1.Office = STUDENT2.Office (σ Student_Name=‘Einstein’ (STUDENT))) Student_NameID_NumberOfficeGPA Knuth3280120980224.00 Von Neuman4810802205553.78 Russell2380823880223.85 Einstein2380019200222.11 Newton17270173333.61 Karp3488828110223.98 Bernoulli29219380223.21 STUDENT Student_Name Knuth Russell Einstein Karp Bernoulli Student_NameID_NumberOfficeGPA Knuth3280120980224.00 Von Neuman4810802205553.78 Russell2380823880223.85 Einstein2380019200222.11 Newton17270173333.61 Karp3488828110223.98 Bernoulli29219380223.21 STUDENT Answer:

21 Database Operations: Relational Algebra Find the names and GPAs of all students who take 311 Find the names of all students who take at least two courses

22 Database Operations: SQL Find the names and GPAs of all students who take 311 SELECT DISTINCT Student_name, GPA FROM STUDENT Natural Join TAKES WHERE Course = ‘CSE311’ Find the names of all students who take at least two courses SELECT DISTINCT Student_name, GPA FROM STUDENT s, TAKES t1, TAKES t2 WHERE s.ID_number = t1.ID_number and s.ID_number = t2.ID_number and t1.course != t2.course

23 Relational Databases: Keys An attribute is a key if all its values in the database are always distinct Autumn 2012CSE 31123 Which attribute is the key? Why is Student_Name not a key? Student_NameID_NumberOfficeGPA Knuth3280120980224.00 Von Neuman4810802205553.78 Russell2380823880223.85 Einstein2380019200222.11 Newton17270173333.61 Karp3488828110223.98 Bernoulli29219380223.21

24 Relational Databases: Relationships Autumn 2012CSE 31124 WORKS_ON is a relationship between students and project Student_NameID_NumberOffice Knuth328012098022 Von Neuman481080220555 Russell238082388022 Einstein238001920022 Newton1727017333 Karp348882811022 Bernoulli2921938022 PRJ_IDProject_NameDue_date P331“Flying cyphers”11/2012 P004“Virtual induction”12/2012 P901“Binary bots”12/2012 STUDENT PROJECT ID_NumberPRJ_ID 2921938P004 2921938P901 1727017P901 Who works on what?

25 Types of Relationships in Relational Databases one-one: many-one many-many 123123 abcdabcd 123123 abcdabcd 123123 abcdabcd Autumn 2012CSE 31125 What type is WORKS_ON? ID_NumberPRJ_ID 2921938P004 2921938P901 1727017P901


Download ppt "CSE 311 Foundations of Computing I Lecture 22 Relations, Database Autumn 2012 CSE 311 1 TexPoint fonts used in EMF. Read the TexPoint manual before you."

Similar presentations


Ads by Google