nr B1 B2 wk 45 46 ma 7-11 14-11 do 10-11 17-11 SQL (1) SQL (2) Thema Casus Opstellen datamodel B3 47 21-11 24-11 Data transformatie B4 B5 48 49 28-11 5-12 1-12 8-12 SQL (3) Toets B Mapping/constraints/SQL "# "$ # #" % % " &' &# " ( & ) #( % ) ( "# $ "# closer to the user Modelling At the end: SQLknowledge required more technical Transformation In general: a relational database is a database that is perceived by its users as a collection of tables (and nothing but tables). (from: C.J. Date; An Introduction to Database Systems) - $. / Players Plnr Playername Street Town Teamname A very simple relational database structure (a sportsclub): 1 K. Huisman Annastraat 24 Nijmegen Heren 2 P.J. van Onderen Willemstraat 13 Nijmegen Dames Fines P. van Oosterom Past. Pietstraat 14 Weurt 4 Heren 6 Heren W. De Bom Huilstraat 67 Wychen 8 Date Dames Finenr F.G. Habank Playernr Vossendijk Amount 288 Nijmegen Teams H. Flits Zwanenveld 89-90 Nijmegen 9 Jeugd1 10 Jeugd1 L. Willemsen Kasteel 4 Wychen Class 1 12/8/89 Captain 12 25.00 Teamname M.E.P. Graag Broerdijk 234 Nijmegen 12 Heren 2 12/8/89 H. Nijboer 12 Plein 5.00 44 Arnhem 13 Dames 13 Jeugd1 13/8/89 K. 10 De 34 Huilstraat Groot 100.00 63 Wychen 19 Jeugd2 20 Jeugd2 34 Jeugd2 13/8/89 B.E. 20 Melmans 28 Kasteel 625.00 Wychen W. De Bom Huilstraat 67 Wychen 23 Jeugd1 19/8/89 Dames 25 36 6 100.00 Annastraat 24 T. Huisman Nijmegen 24 Jeugd1 16 Heren 20/8/89 12 25 25.00 H. Fluit Snelweg 673 Lent 25 Dames 7 20/8/89 42 1.00 Wolvendijk 123 W.Jansen Elst 28 Jeugd2 8 30 20/8/2003 D. van 12 De Agt Beurs 25.00 12 Groesbeek Jeugd1 9 322/9/2003 K. Zwartjes 12 Past. Weurt Pietstraat 5.00 2 Jeugd1 33 Heren J. Mienen Ekkersrijt 23 Eindhoven 34 Jeugd2 H. Blaak Ekkersrijt 28 Eindhoven 36 Dames D.F. Janssen Groenstraat 45 Oss 42 R. Heymans Dreef 24 Grave Jeugd1 44 P. van Oss Dreef 24 Oss Jeugd1 1
table "# $ %&& For instance: about the President -table: column row field value data types: char smallint varchar decimal ( m n ). 0 1 ' ( %% ' % (% ( 3 4 ' ( " " ' () "6"( "78 " 9' : " ; ( " # &1%&2&22 < = > ""$? % > ' > @ " > ")) > -? :" ) " ( ";A )# > % ( A ) # 2 5 ' ( ##(# $ (#( B > - > 4 " %) % ' ' '' > B% % % ' :': > B( " ' B ' B > B % ) DDL ( data definition ) CREATE tables indexes views Establish primary / foreign keys DROP / ALTER tables etc. DML ( data manipulation ) INSERT / UPDATE / DELETE SELECT... etc. DCL ( data control ) COMMIT / ROLLBACK work GRANT / REVOKE etc. Physical Design Implementation Maintenance 2
' ( % -.(/ only what we want to have/see is the part that has to be specified and not how it has to be done. only the desired results must be indicated Multiple-element-at-a-time-processing Example: SELECT playername teamname FROM players WHERE town = Nijmegen AND playernr NOT IN (SELECT playernr FROM fines WHERE amount > 25) ( C ( B@@B SELECT List the columns (and expressions) that should be returned from the query FROM Indicate the table(s) or view(s) from which data will be obtained WHERE Indicate the conditions under which a row will be included in the result GROUP BY Indicate categorization of results HAVING Indicate the conditions under which a category (group) will be included ORDER BY Sorts the result according to specified criteria Attention: This is the order in which (maybe) the SQL-query is processed by the RDMS. It is not the order in which we analyse the information demand and construct such SQL-query ' (' % SELECT FROM WHERE [DISTINCT] target-list relation-list qualification & - & - & ( & B - - - % D%E%F%DF%EF%DE%G %###G C %# -G '%H 9G H # ' G B " % #' 6-8 I % % ( " ) " & B & &# & ' "(# & ' &# & ' G B % # = " "I H A # 0 1 3
0 %1 % J) > ##% > ##%KLM# > )) # "# > 9E1 NN FN O -G '%H 9G H )N G C )?# > ' G C )G H G C ## > O &) % # > ##%O P @9@ M) # ' NNN " I > G Q # 2 ) SELECT column-name or list-of-column-names or FROM table-name [ WHERE search-condition ] [ ORDER BY clause] Example: SELECT FROM WHERE pres_name birth_yr death_age state_born president state_born = 'New York' PRES_NAME BIRTH_YR DEATH_AGE STATE_BORN --------------------------------------------------------------------------- Van Buren M 1782 79 New York Fillmore M 1800 74 New York Roosevelt T 1858 60 New York Roosevelt F D 1882 63 New York.O P @ 9@/& I Comparison operators: = equal to <> not equal to (or NOT = ) > greater than >= greater than or equal to < less than <= less than or equal to Also:... IS NULL or... IS NOT NULL IN ( list-of-possible-values ) (or: = ANY...) BETWEEN... AND... LIKE..... string ; wild cards: % _ or... Combination of conditions linked by logical operators AND OR NOT $O P @ 9@& WHERE STATE_BORN = 'Texas' WHERE YRS_SERV >= 7 OR PARTY = 'Democratic' WHERE PARTY = 'Republican AND NOT STATE_BORN = 'Texas' WHERE YRS_SERV = 12 AND DEATH_AGE IS NOT NULL WHERE STATE_BORN IN ( 'Texas' 'California' 'Georgia ) WHERE DEATH_AGE BETWEEN 60 AND 70 ( limits included ) WHERE PRES_NAME LIKE '_e% AND PRES_NAME NOT LIKE 'R%' 4
H ) & " The following expression can have several different meanings depending on the position of parentheses: NOT YRS_ SERV 10 > DEATH_AGE AND DEATH_AGE > 60 BIRTH_YR / 100 One can introduce parentheses to this expression without affecting its meaning: ( NOT (YRS_SERV 10 > DEATH_AGE ) ) AND ( DEATH_AGE > (60 BIRTH_YR / 100 ) ) However the meaning of the expression is changed by repositioning the parentheses: NOT ( ( YRS_SERV 10 > DEATH_ AGE ) AND ( DEATH_AGE > 60 BIRTH_YR /100 ) ) => If you have any doubt about operator precedence: use parentheses ) "#" 2###< - 3 4#/ If used then only at the end of a query (the last clause); it is just the way to present the encountered results from the SELECT / FROM / WHERE-combination; Example: SELECT FROM president WHERE state_born IN ( 'Ohio' 'New York ) ORDER BY birth_yr More arguments can be mencioned for the ordering way: SELECT pres_name birth_yr death_age state_born WHERE death_age BETWEEN 60 AND 70 AND state_born IN ('Ohio' 'New York') ORDER BY state_born birth_yr DESC Ordering option ASC is default PRES_NAME BIRTH_YR DEATH_AGE STATE_BORN ------------------------------------------------------------------------------- Roosevelt F D 1882 63 New York Roosevelt T 1858 60 New York Harrison B 1833 67 Ohio Hayes R B 1822 70 Ohio Grant U S 1822 63 Ohio 0 " #$" % $"& ' @@B&K M ( 5%6 M Aggregate functions: AVG SUM MIN MAX COUNT Examples: SELECT COUNT ( ) SUM ( NR_CHILDREN ) FROM PRES_MARRIAGE WHERE mar_year BETWEEN 1800 AND 1840 COUNT () or : COUNT ( column-name ) MIN (... ) MAX (... ) on alpha-numeric (column-) values SUM (... ) AVG (... ) on numeric (column-) values SELECT COUNT ( ) MIN ( death_age ) AVG (death_age) WHERE death_age IS NOT NULL AND state_born = 'Ohio Warning: it is not allowed to mix in the SELECT-clause column-name(s) and statistical functions (... at least: if there is no additional GROUPING-clause...). So: SELECT pres_name AVG ( birth_yr ) <= NOT ALLOWED (why not allowed?) Explanation: - the syntax doesn t allow so ; - more important: such mix cannot have any real meaning 2 5 5
=?K M @@B& Possible problems: rounding off truncation and NULL-values SELECT SUM(death_age) / COUNT() AVG(death_age) Operators: DISTINCT... (shows each (combined) value only once...) (default: ALL...) F_1 AVG DOUBLE PRECISION SMALLINT ---------------------------------------------------- 617948717948718 69 Further SELECT-clause -operators and -functions: depending on data-type and RDBMS Example: SELECT FROM WHERE SUM(death_age) / COUNT() AVG(death_age) president death_age IS NOT NULL SELECT pres_name birth_yr death_age WHERE birth_yr IS NOT NULL AND birth_yr death_age > 1950 F_1 AVG DOUBLE PRECISION SMALLINT --------------------------------------------------- 688571428571429 69 9' : ##.# /& &"$.& / " ' (. % ("' Partitioning rows into groups with the same value for column a. Basic syntax: select group-property1... from... [ where row-criterion(s) ] group by group-criterion1... Example: select sex count () avg ( iq ) from Pupils where iq is not null group by sex R J -. %7 38 7/ R K M SELECT < group-properties properties to be shown > FROM table-name [ WHERE <condition on rows > ] GROUP BY < way of grouping > HAVING < conditions on groups > ] [ ORDER BY clause].") 9 :26 SELECT state_born COUNT() WHERE death_age >= 80 GROUP BY state_born SELECT party COUNT() WHERE state_born <> 'New York' GROUP BY party HAVING COUNT() > 2 ORDER BY 2 DESC STATE_BORN COUNT ------------------------------ Iowa 1 Massachusetts 2 Missouri 1 Virginia 2 PARTY COUNT ----------------------------- Republican 15 Democratic 11 Demo-Rep 4 Whig 3 8 5 6 8 K M BH C G?C:?:G?:-S?-T R Always look out what can be meaningful grouping possibilities in a given table. 6
.") 9 :26 -;/ SELECT pres_name COUNT () FROM pres_hobby GROUP BY pres_name HAVING COUNT () > 4 SELECT hobby COUNT () FROM pres_hobby WHERE pres_name NOT LIKE 'Wash%' GROUP BY hobby HAVING COUNT() > 4 ORDER BY 2 PRES_NAME COUNT ---------------------------------- Coolidge C 5 Eisenhower D D 5 Roosevelt T 7 HOBBY COUNT -------------------------------- Walking 5 Golf 6 Fishing 8 Riding 10 PRES_NAME COUNT SUM ----------------------------------------------- SELECT pres_name COUNT () SUM(nr_children) Harrison W H 1 10 Tyler J 2 15 FROM pres_marriage Reagan R 2 4 GROUP BY pres_name Fillmore M 2 2 HAVING COUNT()>= 2 OR SUM(nr_children)>8 Harrison B 2 3 ORDER BY 2 Wilson W 2 3 Roosevelt T 2 6 0 $ ) ' ( %% ' % (% 5 &< " 6?8 &) 6###8 ;&%=% " &) 6 &###8 2 < B B $ K M X - Ł Ł @) " K M ) " K M# Ł &4&( " M K) "M# - - - 5 Q ) R "$ 4 SELECT < the properties to be shown > FROM table1 table2.... WHERE < join - criterion > [ AND <search - condition > ] ### ) " ### $ P_TABLE PRES_NAME BIRTH_YR ----------------------------------------- Buchanan J 1791 Harrison B 1833 Nixon R M 1913 Reagan R 1911 M_TABLE PRES_NAME SPOUSE_NAME ------------------------------------------------ Harrison B Scott C L Harrison B Dimmick M S L Nixon R M Ryan T C Reagan R Wyman J Reagan R Davis N Q 4& ->?% / If you try: PRES_NAME BIRTH_YR PRES_NAME1 SPOUSE_NAME ------------------------------------------------------------------------- SELECT Buchanan J 1791 Harrison B Scott C L Buchanan J 1791 Harrison B Dimmick M S L FROM P_table M_table Buchanan J 1791 Nixon R M Ryan T C Buchanan J 1791 Reagan R Wyman J Buchanan J 1791 Reagan R Davis N Ł Harrison B 1833 Harrison B Scott C L Harrison B 1833 Harrison B Dimmick M S L Harrison B 1833 Nixon R M Ryan T C Harrison B 1833 Reagan R Wyman J Harrison B 1833 Reagan R Davis N Nixon R M 1913 Harrison B Scott C L Nixon R M 1913 Harrison B Dimmick M S L ##O I Nixon R M 1913 Nixon R M Ryan T C Nixon R M 1913 Reagan R Wyman J Nixon R M 1913 Reagan R Davis N Reagan R 1911 Harrison B Scott C L Question: how can Reagan R 1911 Harrison B Dimmick M S L Reagan R 1911 Nixon R M Ryan T C we get a useful result? Reagan R 1911 Reagan R Wyman J Reagan R 1911 Reagan R Davis N 7
@ " Join-criterionŁ 9 SELECT P_table.pres_name birth_yr spouse_name FROM P_table M_table WHERE P_table.pres_name = M_table.pres_name PRES_NAME BIRTH_YR SPOUSE_NAME ---------------------------------------------------------- Harrison B 1833 Scott C L Harrison B 1833 Dimmick M S L Nixon R M 1913 Ryan T C Reagan R 1911 Wyman J Reagan R 1911 Davis N % % & ) ( " KM )) SELECT P.pres_name birth_yr spouse_name FROM P_table P M_table M WHERE P.pres_name = M.pres_name - 4& Example: show the names of the presidents born in the 20th century. Show also their birth years death ages native states and the names of their vicepresidents. The result must be ordered by birth year. We will need a join of the tables president and admin_pr_vp (because...) and as join-criterion the columns: pres_name and pres_name... Query: Explain the emergence of 3 rows with Nixon R M. SELECT P.pres_name birth_yr death_age state_born vice_pres_name P admin_pr_vp A WHERE P.pres_name = A.pres_name AND birth_yr >= 1900 ORDER BY birth_yr PRES_NAME BIRTH_YR DEATH_AGE STATE_BORN VICE_PRES_NAME --------------------------------------------------------------------------------------------------------- Johnson L B 1908 65 Texas Humphrey H H Reagan R 1911 (null) Illinois Bush G Ford G R 1913 (null) Nebraska Rockefeller N Nixon R M 1913 (null) California Agnew S T Nixon R M 1913 (null) California Agnew S T Nixon R M 1913 (null) California Ford G R Kennedy J F 1917 46 Massachusetts Johnson L B Carter J E 1924 (null) Georgia Mondale W F 1 @ %@5 "" SELECT Male.firstname Female.firstname FROM Male Female Ł ( " & ### 7 ):?J& 7 Ł -4 4& K 4M# %=% A subquery permits a SQL user to phrase in one query a complicated question which would otherwise have required more than one query Example: show the president(s) which had the highest number of children (in one marriage). Show also that number of children. Solution: SELECT pres_name nr_children FROM pres_marriage WHERE nr_children = ( SELECT MAX ( nr_children ) FROM pres_marriage ) PRES_NAME NR_CHILDREN ------------------------------------------ Harrison W H 10 Remarks: With = you can test if one value is equal to one (other) value. To test if one value can be in a set (collection) of values: use the IN -operator. 0 %=% #G 5 / To test if one value can be in a set (collection) of values: use the IN -operator. Example: show the president -data of the presidents with more than 3 hobbies. Solution: SELECT WHERE pres_name IN ( SELECT pres_name FROM pres_hobby GROUP BY pres_name HAVING COUNT() > 3 ) PRES_NAME BIRTH_YR YRS_SERV DEATH_AGE PARTY STATE_BORN ---------------------------------------------------------------------------------------------------------------- Roosevelt T 1858 7 60 Republican New York Coolidge C 1872 5 60 Republican Vermont Eisenhower D D 1890 8 79 Republican Texas Remark: with a subquery you just can compare one-value with a set of one-values. So not admitted is: WHERE pres_name IN ( SELECT pres_name COUNT()... 1 8
%=% #H "# SELECT pres_name death_age WHERE pres_name IN ( SELECT pres_name FROM pres_marriage GROUP BY pres_name HAVING COUNT () > 1 ) ORDER BY pres_name 9 PRES_NAME DEATH_AGE ---------------------------------- Fillmore M 74 Harrison B 67 Reagan R (null) Roosevelt T 60 Tyler J 71 Wilson W 67 2 "'" $ # %=% -A/ Question 1 : Establish an SQL-query to find a correct answer to the next information demand on the presidential database: R ) ) ) K9M % ) K&M % % ) # ' % A5 () ) ) K9M ) K&M % % ) # Analysis:... )? U = U G ) U P " B ### Analysis:... U : K9M " U G & " 9( U = U = L" Analysis:... ' % A5 3 > = > G ) > P " - &&&/ > : K9M " > G & " "=% > = > = L" Strategy:... : "& 9 " Ł )K M " Ł 4 )KM ' % AB -' (5=% / Strategy:... : "& 9 " Ł )K M " Ł 4 )KM The SQL-query:... ( C") $ 8 " =#= L%L %" = =%= L"P =# LFP # L 0 # "FV9K 0 # L G C 9
% =% ; Question 2 : Establish an SQL-query to find a correct answer to the next information demand on the presidential database: R) ) ) % % % ) ) # Analysis:... ' % ;5 (R ) ) ) % % % ) ) # )? U = U G ) U B ### U = 9( U = U = L Analysis:... ' % ;B 3 > = > G ) > - &&&/ > = "=% > = > = L ' % ;B 7 Ł Analyse the result of such a (just) join-query : > : > Q 4 & % # 0 U @) " 4 U ) " L U O " & U O ) 1 ' %;B 7 O ) N " &I ' %;5 J9H : =9@'@G =%=9@L:-99-R@ : O P @9@ =# L F:# L =" R 9H C = W =# L%L Ł ". ###%L / N P -TG R BH CG X E 2 5 10
' %;B -' (5=% / @ @ B =# L%L %C: L J9H : =9@'@G =%=9@L:-99-R@ : O P @9@ =# L F:# L R9H C=W =# L%L P -TG R BH CG X E - 9 PRES_NAME STATE_BORN SUM ---------------------------------------------------------- Fillmore M New York 2 Harrison B Ohio 3 Reagan R Illinois 4 Roosevelt T New York 6 Tyler J Virginia 15 Wilson W Virginia 3 #J "%G ) ;55# R)"&( ""? )$ # "?? ###$ If necessary: study the content of parts of the electronic SQL-manual till page 32. Establish SQL-queries to give correct answers to the next information demands on the presidential database (and test your queries...): 1. Which presidents died before the age of 65 years and were born in the state Ohio or in the state Virginia? 2. Give an overview with for each administration period the numbers of states that joined the USA. 3. What is the longest period (years of service) that a person was president? 4. Show the hobbies (and names) of the presidents Eisenhower and Truman. 5. Which persons where vice-president from one of the Adams -presidents? 6. Give in one result table the overall number of presidents the sum of their birth years that sum divided by their number and finally their average birth year (did you expect the relation between the results? Why?) 7. Which presidents got in at least one marriage (or in both marriages separate) more than 5 children? Show their names spouse-names marriage-year and number of children of such a marriage. 8. Give in opposite-ordered way (from Z => A ) the names of the various political parties that provided one or more presidents (each name may appear just one time) % 3 3 &&&-% %=% @/-6 D/ If necessary: study the content of parts of the electronic SQL-manual page 42-51. Try to establish SQL-queries as well with a join - as with a subquery-construction to give correct answers to the next information demands on the presidential database (and test your queries...). If it is not possible to use one of these constructions explain why you can not use it 1. Out of which states came presidents that never participated as candidate in elections. Show the names of those states and also from those presidents. a) with a join-construction:? b) with a subquery-construction:? 2. Give for the president that was born in the same year as the state Minnesota entered in the USA his name his native state and the name of his spouse. a) with a join-construction:? b) with a subquery-construction:? 3. Who was vice-president of the president that married Mrs. 'Smith M M'? a) with a join-construction:? b) with a subquery-construction:? 4. Which vice-presidents that participated in elections to become a real president have never been president? Take care that each name appears just once in the result table. a) with a join-construction:? b) with a subquery-construction:? 11