IST 210 The Relational Language Todd S. Bacastow January 2005.

Slides:



Advertisements
Similar presentations
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Advertisements

1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Relational Algebra Content based on Chapter 4 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
FALL 2004CENG 351 File Structures and Data Managemnet1 Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra.
Relational Algebra, R. Ramakrishnan and J. Gehrke (with additions by Ch. Eick) 1 Relational Algebra.
Relational Algebra.  Introduction  Relational Algebra Operations  Projection and Selection  Set Operations  Joins  Division  Tuple Relational Calculus.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Relational Algebra - Chapter (7th ed )
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Relational Algebra  Souhad M. Daraghma. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
Relational Algebra.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
Advanced Relational Algebra & SQL (Part1 )
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
IST 210 The Relational Language Todd S. Bacastow January 2004.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
Relational Algebra p BIT DBMS II.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Jianping Fan.
1 CS122A: Introduction to Data Management Lecture #7 Relational Algebra I Instructor: Chen Li.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Relational Algebra & Calculus
Chapter (6) The Relational Algebra and Relational Calculus Objectives
COMP3017 Advanced Databases
Relational Algebra Chapter 4 1.
Chapter 2: Intro to Relational Model
Relational Algebra Chapter 4, Part A
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 1.
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Relational Algebra & Calculus
Presentation transcript:

IST 210 The Relational Language Todd S. Bacastow January 2005

IST History - Review Introduced by Codd in 1970 and provides : a simple data structure for modelling all data mathematically based becoming a standard for implementation data models Consequences : Simplicity means that correctness is easier to establish Standardization means that distributed data can be combined more easily Sharing of improvements to the facilities and the implementation can be shared easily

IST Description of the Relational Model - Review All of the information stored in a Relational Database is held in relations (a.k.a. tables) No other data structures! A relation may be thought of as a table STUDENT namematricexam1exam2 Mounia Jane Thomas A relation has: a name an unchanging set of columns; named and typed a time varying set of rows

IST Definitions (1) - Review An attribute is a column of a relation: a name: the role the column has in this relation a domain: the set of values it may take. A domain is a set of atomic values (indivisible): its meaning - e.g. the set of course numbers its format - e.g. a 6 digit integer - a range e.g. the days of the week - a set of value A tuple is a row of a relation a set of values which are instances of the attributes of a relation

IST Definitions (2) – Review Relational schema: a set of attributes and is written R (A 1, A 2,... A n ) e.g., STUDENT (name, course, exam1, exam2). Relation: a set of tuples which accords with some relational schema. The degree of a relation: the number of attributes. The cardinality: the number of tuples.

IST Definitions (3) - Review Keys, or candidate Keys any set of attributes which are unique for each row Primary key one candidate key is chosen to identify the tuples it is underlined e.g., STUDENT (name, course, exam1, exam2)

IST Definitions (4) - Review Relational database schema: set of relation schemas together with a set of "integrity constraints" Relational database instance: a set of relations realizing a relational database schema Base relation: relation which actually exists as a stored file (vs. temporary or view relations) Foreign Key: an attribute or set of attributes which match the primary key of another relation and thus link them

IST Characteristics of the Relational Model (1) No duplicate tuples - as the tuples are a set Must be checked when: a new tuple is added a value is modified Implies that a primary key always exists (at worst all of the attributes) The tuples are unordered - again a property of sets But, a physical storage of a relation must have an order

IST Characteristics of the Relational Model (2) The attributes are also unordered set of attributes no notion of getting the first attribute, next attribute, etc. (also no first tuple, next tuple,...) All values are atomic SPQSPQ S1{(P1,200), must becomeS1P1200 (P2,300)}S1P2300 (First Normal Form - Normalization)

IST Characteristics of the Relational Model (3) Unknown Values Must be Represented These are replaced by null - a distinguished value.

IST Manipulating a Relational Database Three standard ways of doing this: Two formal "languages": Relational Algebra - allows the description of queries as a series of operations; Relational Calculus - allows the description of the desired result. One real language: SQL - the standard relational database manipulation language.

IST Query Languages Query languages Allow manipulation and retrieval of data from a database. QLs not intended to be used for complex calculations. QLs support easy, efficient access to large data sets.

IST Formal Relational Query Languages Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation:  Relational Algebra: More operational, very useful for representing execution plans.  Relational Calculus: Lets users describe what they want, rather than how to compute it.

IST Relational Calculus Comes in two flavours: Tuple relational calculus (TRC) Domain relational calculus (DRC) Calculus has variables, constants, comparison ops, logical connectives and quantifiers. TRC: Variables range over (i.e., get bound to) tuples. DRC: Variables range over domain elements (= field values) Expressions in the calculus are called formulas. An answer tuple is essentially an assignment of constants to variables that make the formula evaluate to true.

IST Relational Algebra in a DBMS parser SQL Relational algebra expression Optimized Relational algebra expression Query optimizer Code generator Query execution plan Executable code DBMS

IST Relational Algebra (1) Extract from the database a subset of the information which answers some question: "What are the department names?" "Tell me all the data held about male employees." "What are the names of the employees in the R&D department?" Extraction consists of programs built out of: retrieving part of some relation linking two relations together in a meaningful way

IST Relational Algebra (2) Set of operations which can be combined to provide the result of a query in the form of a relation. The algebra: A collection of operations of two categories: Special Relational Operations Traditional Set Operations A "relational assignment" statement so that partial results can be assigned a name Renaming: change attribute names Querying process: A sequence of operation calls of the form: newRelation := op(parameters including relation names, column names and conditions)

IST Relational Algebra Operations Principal relation operations: select - pick rows from a relation by some condition; project - pick columns by name; join - connect two relations usually by a Foreign Key. Set operations: union - make the table containing all the rows of two relations; intersection - pick the rows which are common to two relations; difference - pick the rows which are in one table but not another; Cartesian product - pair off each of the tuples in one relation with those in another - creating a double sized row for each pair. All of the operations return relations.

IST Relational Algebra Symbols Selection ( ) Projection ( ) Cross-product (Cartesian product) ( ) Union (U) Intersection ( ) Set-difference ( ) Join ( ) U

IST Selection Extract the tuples of a relation which satisfy some condition on the values of their rows and return these as a relation. LOCALS :=  (EMPLOYEE, CITY = ”LONDON") Syntax (there are many):  ( RelationName, Condition ) where the condition can contain: literals column names comparison operators ( =, >, etc. ) boolean operators ( and, not, or ) LONDONorYOUNGRICH :=  (EMPLOYEE, CITY = “LONDON” or ( SALARY > 60K and AGE < 30 ) )

IST Projection Extracts some of the columns from a relation. SexSalary :=  (EMPLOYEE, (SEX, SALARY)) No attribute may occur more than once. Duplicate will be removed. Projection and selection combined.  (  (EMPLOYEE, CITY = “LONDON”), (NAME,SSN)) This does a selection followed by a projection. The DBMS may re-organize this for faster retrieval.

IST Union Produce a relation which combines two relations by containing all of the tuples from each - removing duplicates. The two relations must be "union compatible" i.e. have the same number of attributes drawn from the same domain (but maybe having different names). LondonOrRich: =  EMPLOYEE, CITY = “LONDON”)   (EMPLOYEE, SALARY > 60K) If attribute names differ, the names from the first one are taken.

IST Intersection Similar to union but returns tuples that are in both relations. FemalesInLondon :=  EMPLOYEE, CITY = “LONDON”)   (EMPLOYEE, SEX = “F”)

IST Difference Similar to union but returns tuples that are in the first relation but not the second. NonLocals := EMPLOYEE - LOCALS Union, intersection and difference require union compatibility. Intersection and difference use column names from the first relation.

IST Cartesian Product (Cross Product) The Cartesian Product of two relations A (a tuples) and B (b tuples), which have attributes A 1... A m and B 1... B n, is the relation with m + n attributes containing row for every pair of rows one from A and one from B. The result has a x b tuples. EMPLOYEEDEPENDENT nameNIdept ENInamesex ML SMM JR TCF 456JAF EMPLOYEE x DEPENDENT nameNIdeptENInamesex * ML SMM * ML TCF ML JAF JR SMM JR TCF * JR JAF

IST Cartesian Product Fairly meaningless as it stands and is rarely used on its own. More meaningful is the subset of rows marked with a star. This could be created with the following selection:  ( EMPLOYEE x DEPENDENT, NI = ENI) The selection essentially makes use of the foreign key. Cartesian product followed by this kind of selection is called a join because it joins together two relations.

IST Join The join of relations A and B pairs off the tuples of A and B so that named attributes from the relations have the same value. The common column then appears just once. EMPLOYEEDEPARTMENT NameNIDept DnumDname Manager RLC1235 5R&D 456 MPA4565 6Production 111 RCW3457 7Admin 345 Join is written:  ( EMPLOYEE, Dept, DEPARTMENT, Dnum )

IST Join Joining the relations on EMPLOYEE.Dept = DEPARTMENT.Dnum puts together an employee's record with that of the department he or she works in: EMPLOYEE-DEPARTMENT NameNIDeptDnameManager RLC1235R&D456 MPA4565R&D456 RCW3457Admin345 Joining the relations on EMPLOYEE.NI = DEPARTMENT.Manager puts an department 's record together with that of the employee who manages it: EMPLOYEE-DEPARTMENT NameNIDeptDnumDname MPA45655R&D RCW34577Admin Unmatched tuples disappear - example "RLC" and "Production". There are other forms which perform differently - this one is called natural join.

IST Division "Give the names of employees who work on all projects that John Smith works on" JSPnosWorksOnResult PNoNIPNoNI Result := WorksOn ÷ JSPnos R ( r 1,...r m, c 1,... c n ) ÷ S( c 1,... c n ) returns the relation with attributes ( r 1,...r m ) such that each tuple in the result appears once in R associated with every tuple in S.

IST Example DEPARTMENT(Dnumber, Dname, Manager) EMPLOYEE(Name, Address, NI, Dept, DateOfBirth) PROJECT(Pname, Plocation, Dnum, Pnumber) WorksOn (ENI, P)

IST Examples of Relational Algebra Query Give the name and addresses of all employees who work for the R&D department. ResDept :=  ( DEPARTMENT, Dname = "R&D" ) this should return just one tuple ResDeptEmps :=  ( ResDept, Dnumber, EMPLOYEE, Dept ) this picks out the employees in that department. Result :=  ( ResDeptEmps, Name, Address ) Could be written as:  (  (  ( DEPARTMENT, Dname = "R&D" ), Dnumber, EMPLOYEE, Dept ), Name, Address )

IST Examples of Relational Algebra Query List the name, controlling department name, the department manager's name, address and date of birth for every project in IST. ISTProjs :=  ( PROJECT, PLocation = "Stafford" ) it is common to start by restricting to an area of interest ISTProjDepts:=  ( ISTProjs, Dnum, DEPARTMENT, Dnumber ) this brings together the department information ISTProjDeptManagers:=  ( ISTProjDepts, Manager, EMPLOYEE, NI ) this brings in the manager information Result :=  ( ISTProjDeptManagers, Pname, Dname, Name, Address, DateOfBirth )

IST Examples of Relational Algebra Query List the names of employees who work on all the projects controlled by department 5. Dept5ProjNumbers :=  (  ( PROJECT, Dnum = 5 ), Pnumber ) EmpProj :=  ( WorksOn, ENI, P) remove the hours column EmpNIs := EmpProj ÷ Dept5ProjNumbers Employees :=  ( EmpNIs, ENI, EMPLOYEE, NI ) bring in the rest of the employee information Result :=  ( Employees, Name )

IST Summary The relational model has rigorously defined query languages that are simple and powerful. Relational algebra is more operational; useful as internal representation for query evaluation plans. SQL is an implementation of relational algebra. There may be several ways of expressing a given query.