
Unit 4 Relational Algebra (Using SQL DML Syntax): Data Manipulation Language For Relations © 2016 Zvi M. Kedem 1 Relational Algebra in Context UUsseerr LLeevveell DDeerriivveedd TTaabblleess QQuueerriieess ((DDMMLL)) ((VViieeww LLeevveell)) CCoonnssttrraaiinnttss,, PPrriivviilleeggeess Derived Derived AApppplliiccaattiioonn DDaattaa AAnnaallyyssiiss ((EERR)) CCoommmmuunniittyy LLeevveell BBaassee TTaabblleess NNoorrmmaalliizzaattiioonn ((NNFFss)) ((BBaassee LLeevveell)) CCoonnssttrraaiinnttss,, PPrriivviilleeggeess SSccHHeemmaa SSppeecciiffiiccaattiioonn ((DDDDLL)) QQuueerriieess ((DDMMLL)) IImmpplleemmeenntteedd FFiilleess + PPHHyyssiiccaall LLeevveell QQuueerryy EExxeeccuuttiioonn ((BB+,, ……,, EExxeeccuuttiioonn PPllaann)) IInnddeexxeess,, DDiissttrriibbuuttiioonn RReelliieess oonn CCoonnccuurrrreennccyy DDBBMMSS OOSS LLeevveell TTrraannssaaccttiioonn PPrroocceessssiinngg ((AACCIIDD,, SSHHaarrddiinngg)) RReeccoovveerryy RRuunnss oonn CCeennttrraalliizzeedd SSttaannddaarrdd OOSS OOrr DDiissttrriibbuutteedd SSttaannddaarrdd HHaarrddwwaarree © 2016 Zvi M. Kedem 2 Relational Algebra And SQL ◆ SQL is based on relational algebra with many extensions • Some necessary • Some unnecessary ◆ “Pure” relational algebra uses mathematical notation with Greek letters ◆ I will cover it using SQL syntax; that is in this unit I will cover relational algebra, but it will look like SQL • and will be really valid SQL ◆ Pure relational algebra is used in research, scientific papers, and some textbooks (mainly because of inertia) ◆ But some may want to know it, and I provide at the end of this unit all the material from which one can learn it ◆ But in anything practical, including all relational commercial systems, you will be using SQL © 2016 Zvi M. Kedem 3 Sets And Operations On Them ◆ If A, B, and C are sets, then we have the operations ◆ È Union, A È B = { x | x Î A Ú x Î B } ◆ Ç Intersection, A Ç B = { x | x Î A Ù x Î B } ◆ - Difference, A - B = { x | x Î A Ù x Ï B } ◆ In mathematics, difference is frequently denoted by a symbol similar to a backslash: A \ B ◆ ´ Cartesian product, A ´ B = { (x,y) | x Î A Ù y Î B }, A ´ B ´ C = { (x,y,z) | x Î A Ù y Î B Ù z Î C }, etc. ◆ The above operations form an algebra, that is you can perform operations on results of operations, such as (A Ç B) ´ (C ´ A) and such operations always produce sets ◆ So you can write expressions and not just programs! © 2016 Zvi M. Kedem 4 Relations in Relational Algebra ◆ Relations are sets of tuples, which we will also call rows, drawn from some domains ◆ These domains do not include NULLs ◆ Relational algebra deals with relations (which look like tables with fixed number of columns and varying number of rows) ◆ We assume that each domain is linearly ordered, so for each x and y from the domain, one of the following holds • x < y • x = y • x > y ◆ Frequently, such comparisons will be meaningful even if x and y are drawn from different columns • For example, one column deals with income and another with expenditure: we may want to compare them © 2016 Zvi M. Kedem 5 Reminder: Relations in Relational Algebra ◆ The order of rows and whether a row appears once or many times does not matter ◆ The order of columns matters, but as our columns will always be labeled, we will be able to reconstruct the order even if the columns are permuted. ◆ The following two relations are equal: R A B R B A 1 10 20 2 2 20 10 1 20 2 20 2 © 2016 Zvi M. Kedem 6 Many Empty Relations ◆ In set theory, there is only one empty set ◆ For us, it is more convenient to think that for each relation schema, that for specific choice of column names and domains, there is a different empty relation ◆ And of, course, two empty relations with different number of columns must be different ◆ So for instance the two relations below are different ◆ The above needs to be stated more precisely to be “completely correct,” but as this will be intuitively clear, we do not need to worry about this too much © 2016 Zvi M. Kedem 7 Relational Algebra Versus Full SQL ◆ Relational algebra is restricted to querying the database ◆ Does not have support for • Primary keys • Foreign keys • Inserting data • Deleting data • Updating data • Indexing • Recovery • Concurrency • Security • … ◆ Does not care about efficiency, only about specifications of what is needed, so do not worry about efficiency now © 2016 Zvi M. Kedem 8 Operations on relations ◆ There are several fundamental operations on relations ◆ We will describe them in turn: • Projection • Selection • Cartesian product • Union • Difference • Intersection (technically not fundamental) ◆ The very important property: Any operation on relations produces a relation ◆ This is why we call our structure an algebra © 2016 Zvi M. Kedem 9 Projection: Choice Of Columns R A B C D 1 10 100 1000 1 20 100 1000 1 20 200 1000 ◆ SQL statement SELECT B, A, D FROM R; B A D 10 1 1000 20 1 1000 20 1 1000 ◆ We could have removed the duplicate row, but did not have to © 2016 Zvi M. Kedem 10 Intuitive Explanation (Formally Not Permitted) ◆ R is a file of records ◆ Each record is tuple ◆ Each record consists of fields (values of attributes) ◆ Execute the following “program” 1. Create a new empty file 2. Read one-by-one the records of the file 3. Keep only some specific fields of each record and append this “modified” record to the new file © 2016 Zvi M. Kedem 11 Selection: Choice Of Rows R A B C D 5 5 7 4 5 6 5 7 4 5 4 4 5 5 5 5 4 6 5 3 4 4 3 4 4 4 4 5 4 6 4 6 ◆ SQL statement: SELECT * (this means all columns) FROM R WHERE A <= C AND D = 4; (this is a predicate, i.e., condition) A B C D 5 5 7 4 4 5 4 4 © 2016 Zvi M. Kedem 12 Intuitive Explanation (Formally Not Permitted) ◆ R is a file of records ◆ Each record is tuple ◆ Each record consists of fields (values of attributes) ◆ Execute the following “program” 1. Create a new empty file 2. Read one-by-one the records of the file 3. Check if a record satisfies some conditions on the values of the field 4. If the conditions are satisfied append the record to the new file, otherwise discard it ◆ Note: If the condition is empty, it is satisfied by every record © 2016 Zvi M. Kedem 13 Selection ◆ In general, the condition (predicate) can be specified by a Boolean formula with NOT, AND, OR on atomic conditions, where a condition is: • a comparison between two column names, • a comparison between a column name and a constant • Technically, a constant should be put in quotes • Even a number, such as 4, perhaps should be put in quotes, as '4', so that it is distinguished from a column name, but as we will never use numbers for column names, this not necessary © 2016 Zvi M. Kedem 14 Cartesian Product R A B S C B D 1 10 40 10 10 2 10 50 20 10 2 20 ◆ SQL statement SELECT A, R.B, C, S.B, D FROM R, S; (comma stands for Cartesian product) A R.B C S.B D 1 10 40 10 10 1 10 50 20 10 2 10 40 10 10 2 10 50 20 10 2 20 40 10 10 2 20 50 20 10 © 2016 Zvi M. Kedem 15 Intuitive Explanation (Formally Not Permitted) ◆ R and S are files of records ◆ Each record is tuple ◆ Each record consists of fields (values of attributes) ◆ Execute the following “program” 1. Create a new empty file 2. Outer loop: Read one-by-one the records of file R 3. Inner loop: Read one-by-one the records of file S 4. Combine the record from R with the record from S 5. Append to the new file the new “combined” record © 2016 Zvi M. Kedem 16 A Typical Use Of Cartesian Product R Size Room# S ID# Room# YOB 140 1010 40 1010 1982 150 1020 50 1020 1985 140 1030 ◆ SQL statement: SELECT ID#, R.Room#, Size FROM R, S WHERE R.Room# = S.Room#; ID# R.Room# Size 40 1010 140 50 1020 150 © 2016 Zvi M. Kedem 17 A Typical Use Of Cartesian Product ◆ After the Cartesian product, we got Size R.Room# ID# S.Room# YOB 140 1010 40 1010 1982 140 1010 50 1020 1985 150 1020 40 1010 1982 150 1020 50 1020 1985 140 1030 40 1010 1982 140 1030 50 1020 1985 This allowed us to correlate the information from the two original tables by examining each tuple in turn © 2016 Zvi M. Kedem 18 A Typical Use Of Cartesian Product ◆ This example showed how to correlate information from two tables • The first table had information about rooms and their sizes • The second table had information about employees including the rooms they sit in • The resulting table allows us to find out what are the sizes of the rooms the employees sit in ◆ We had to specify R.Room# or S.Room# in SELECT, even though they happen to be equal because we need to specify from which relation a specific column in the output is drawn ◆ We could, as we will see later, rename a column, to get Room# ID# Room# Size 40 1010 140 50 1020 150 © 2016 Zvi M. Kedem 19 Union R A B S A B 1 10 1 10 2 20 3 20 ◆ SQL statement SELECT * FROM R UNION SELECT * FROM S; A B 1 10 2 20 3 20 ◆ Note: We happened to choose to remove duplicate rows ◆ Note: we could not just write R UNION S (syntax quirk) © 2016 Zvi M. Kedem 20 Union Compatibility ◆ We require same -arity (number of columns), otherwise the result is not a relation ◆ Also, the operation “probably” should make sense, that is the values in corresponding columns should be drawn from the same domains ◆ Actually, best to assume that the column names are the same and that is what we will do from now on ◆ We refer to these as union compatibility of relations ◆ Sometimes, just the term compatibility is used © 2016 Zvi M.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages66 Page
-
File Size-