Review : RELATIONAL ALGEBRA Relational databae ytem are expected to be equipped with a query language that can ait it uer to query the databae intance. There are two kind of query language relational algebra and relational calculu. Relational Algebra Relational algebra i a procedural query language, which take intance of relation a input and yield intance of relation a output. It ue operator to perform querie. An algebra whoe operand are relation or variable that repreent relation. Operator are deigned to do the mot common thing that we need to do with relation in a databae. The reult i an algebra that can be ued a a query language for relation. We need to know about relational algebra to undertand query execution and optimization in a relational DBMS. Relation are een a et of tuple, which mean that no duplicate are allowed. SQL behave differently in ome cae. Remember the SQL keyword ditinct. SQL i declarative, which mean that you tell the DBMS what you want, but not how it i to be calculated. A C++ or Java program i procedural, which mean that you have to tate, tep by tep, exactly how the reult hould be calculated. Relational algebra i (more) procedural than SQL (Actually, relational algebra i mathematical expreion). The fundamental operation of relational algebra are a follow : 1. Select 2. Project 3. Union 4. Set different 5. Carteian product 6. Rename Select Operation (σ) It elect tuple that atify the given predicate from a relation (chooe row). Notation: σc(r) where R i a table (relation) Condition c i a boolean expreion. It can ue comparion operator (=,,, <, >, ) and boolean operator (and, or, and not). The reult i a relation with the ame chema a the operand but with only the tuple that atify the condition tudent name gpa Country Query : Reult : Bob 3.00 Canada lit tudent with gpa > 3 John 3.00 Britain name Gpa Country Tom 3.50 Canada Relational Algebra : Tom 3.50 Canada Maria 4.00 Mexico = σ gpa>3 (tudent) Maria 4.00 Mexico Query : Reult : lit tudent with gpa > 3 and country= Canada Relational Algebra : = σ gpa>3 and country= Canada (tudent) name Gpa Country Tom 3.50 Canada
Project Operation ( ) It project column that atify a given predicate (chooe column). Notation: L(R) where R i a table (relation) L i a lit of attribute from the chema of R. The reult i a relation with all the tuple from R but with only the attribute in L, and in that order. tudent name gpa Country Query : name Bob 3.00 Canada lit attribute name Bob John 3.00 Britain John Tom 3.50 Canada Relational Algebra : Tom Maria 4.00 Mexico = name (tudent) Maria Query : name Country lit attribute name, country Bob Canada John Britain Relational Algebra : Tom Canada = name,country (tudent) Maria Mexico Union Operation ( ) It perform binary union between two given relation and i defined a r = { t t r or t } Notation: r Where r and are either databae relation or relation reult et temporaryrelation. For a union operation to be valid, the following condition mut hold : r, and mut have the ame number of attribute. Attribute domain mut be compatible. Duplicate tuple are automatically eliminated. tudent profeor name Gpa Country name rank Bob 3.00 Canada Dr. Monk Profeor John 3.00 Britain Dr. Pooh Aociate Profeor Tom 3.50 Canada Dr. Patel Aitant Profeor Maria 4.00 Mexico Dr. Smith Profeor Query : T lit name of all people in the department name Bob Can we do? T = tudent profeor John Tom Relational Algebra : T = name (tudent) name (profeor) Maria Dr. Monk Dr. Pooh Dr. Patel Dr. Smith
Set Difference ( ) The reult of et difference operation i tuple, which are preent in one relation but are not in the econd relation. Notation: r Where r and are either databae relation or relation reult et temporaryrelation. Find all the tuple that are preent in r but not in. RegiteredFor Name Topic Query : Bob Algorithm Who i regitered in the Databae coure but not in the Algorithm? John Algorithm Tom Algorithm T Bob Python Relational Algebra : Name Topic Tom Python T = σ topic = Databae (RegiteredFor) - σ topic = Algorithm Maria Databae Bob Databae (RegiteredFor) John Databae Or Maria Databae T = (σ topic = Databae (RegiteredFor)) (σ topic = Algorithm John GUI (RegiteredFor)) Maria GUI Interection ( ) The reult of interection operation i tuple, which are preent the et of tuple that are in both relation. Notation: r The interection of r and, i the et of tuple that are in both r and. RegiteredFor Name Topic Query : Bob Algorithm Who i regitered in the Databae and the Algorithm coure? John Algorithm Tom Algorithm T Bob Python Relational Algebra : Name Tom Python T = name (σ topic = Databae (RegiteredFor)) name (σ topic Bob Bob Databae = Algorithm (RegiteredFor)) John John Databae Or Maria Databae T = ( name (σ topic = Databae (RegiteredFor))) ( name (σ John GUI topic = Algorithm (RegiteredFor))) Maria GUI Carteian Product (Χ) The carteian product of two table combine each row in one table with each row in the other table. Notation: R Χ S The reult i a relation with every combination of a tuple from R concatenated to a tuple from S. It chema i every attribute from R followed by every attribute of S.
Coure Profeor Topic Year Name Rank Algorithm 2 Dr. Monk Profeor Python 2 Dr. Pooh Aociate Profeor Databae 3 Dr. Patel Aitant Profeor GUI 3 Dr. Smith Profeor T = Coure X Profeor T Topic Year Name Rank Algorithm 2 Dr. Monk Profeor Algorithm 2 Dr. Pooh Aociate Profeor Algorithm 2 Dr. Patel Aitant Profeor Algorithm 2 Dr. Smith Profeor Python 2 Dr. Monk Profeor Python 2 Dr. Pooh Aociate Profeor Python 2 Dr. Patel Aitant Profeor Python 2 Dr. Smith Profeor Databae 3 Dr. Monk Profeor Databae 3 Dr. Pooh Aociate Profeor Databae 3 Dr. Patel Aitant Profeor Databae 3 Dr. Smith Profeor GUI 3 Dr. Monk Profeor GUI 3 Dr. Pooh Aociate Profeor GUI 3 Dr. Patel Aitant Profeor GUI 3 Dr. Smith Profeor Combining Cro-Product with Select and Project Operation Student Teache RegiteredFor Name Country GPA Name Topic Name Topic Bob Canada 3.0 Dr. Monk Algorithm Bob Algorithm John Britain 3.0 Dr. Pooh Python John Algorithm Tom Canada 3.5 Dr. Patel Databae Tom Algorithm Maria Mexico 4.0 Dr. Smith GUI Bob Python Tom Python Bob Databae John Databae Maria Databae John GUI Query : Lit of the tudent (name, country and GPA) taught by Dr. Monk? Relational Algebra : Teach_Regiter = Teache.Name, RegiteredFor (σ Teache.Name = Dr. Monk and Teache.Topic = RegiteredFor.Topic (Teache X RegiteredFor)) Maria GUI Teach_Regiter Teache.Name Dr. Monk Dr. Pooh Dr. Patel RegiteredFor.Name Bob John Tom Lit_tudent = Student.Name, Country, GPA (σ Student.Name = Teach_Regiter.Name (Student X Teach_Regiter)) Lit_tudent Name Country GPA Bob Canada 3.0 John Britain 3.0 Tom Canada 3.5
Or Lit_tudent = Student.Name, Country, GPA (σ Teache.Name = Dr. Monk and Teache.Topic = RegiteredFor.Topic and Student.Name = Teach_Regiter.Name (Student X Teache X RegiteredFor)) Lit_tudent Name Country GPA Bob Canada 3.0 John Britain 3.0 Tom Canada 3.5 Rename Operation (ρ) The reult of relational algebra are alo relation but without any name. The rename operation allow u to rename the output relation. 'rename' operation i denoted with mall Greek letter rho (ρ). Notation: ρ (A1,A2,..,An)(R) 1. Reulting relation ha exactly the ame tuple a R, but the name of the relation i. 2. The attribute of the reult relation can be renamed A1, A2,..., An in order from the left. 3. If not all attribute are renamed, can pecify rename attribute : ρ, a a1, b b1 (R) E nr name dept 1 Bill A 2 Sarah B 1. rename both the table and the column ρ R(enr, ename, dept)(e) 2. rename the column nr into nomor, and the other fixed. ρ nr nomor(e) Exercie : The following table data decribe a relational model for air travel booking.
Quetion 1: Operation in Relational Algebra For each of the following querie in relational algebra, calculate the output table and give a brief tatement of what query it anwer. (a) σ cla='buine' (Seat) (b) nationality (Booking) (c) σ nationality='french' (Booking) X σ cla='buine' (Seat) (d) name(σ cla='buine' (Booking X Seat)) (f) Airport Seat Quetion 2: Contructing Querie For each of the following quetion, formulate the peci_ed querie in tuple-relational calculu and a a computation in relational algebra. (a) Retrieve all information about airport in London. The chema of the output table hould be ame a that of the Airport table. (b) Retrieve detail of all booking by Britih and French paenger. The chema of the output table hould be ame a that of the Booking table. (c) Retrieve the name of all paenger. (d) Retrieve the ight number, Departure and Arrival airport of all Britih Airway ight. (e) Retrieve the name of every paenger together with their ight number and the aociated ight company. (f) Retrieve detail of all ight from all airport in London. The output chema hould be ame a that of Flight table. (g) Find out the ticket number and name of all paenger departing from London. (h) Retrieve the ight number and company of all ight from London to Pari.