10
http://www.cse.msu.edu/ http://www.cse.msu.edu/ ~cse103 ~cse103 U:/msu/course/cse/103 U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 1 CSE 103 CSE 103 Students: Students: Your BTs have been graded. See Your BTs have been graded. See Erica or Jo with questions or Erica or Jo with questions or stay after class. stay after class. Makeup sign-up is now available. Makeup sign-up is now available. Others: Others: Please save your work and log out Please save your work and log out ASAP. ASAP.

Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

Embed Size (px)

Citation preview

Page 1: Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 11

CSE 103CSE 103

Students:Students:Your BTs have been graded. See Erica or Your BTs have been graded. See Erica or Jo with questions or stay after class.Jo with questions or stay after class.Makeup sign-up is now available.Makeup sign-up is now available.

Others:Others:Please save your work and log out ASAP.Please save your work and log out ASAP.

Page 2: Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 22

Review: Calculated Fields and Review: Calculated Fields and AliasesAliases

What are What are calculated fieldscalculated fields??

Calculated Fields in Access default to Calculated Fields in Access default to Expr1, Expr2, etc. when displayedExpr1, Expr2, etc. when displayed

What is an SQL What is an SQL aliasalias and how can we and how can we use it with calculated fields?use it with calculated fields?

What SQL keyword is necessary in What SQL keyword is necessary in order to designate an alias?order to designate an alias?

What do we have to remember when What do we have to remember when using using table aliasestable aliases??

Page 3: Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 33

Intro to FunctionsIntro to Functions

What are functions (in general)? What are some What are functions (in general)? What are some examples?examples?

We will deal mainly with functions in MySQL, We will deal mainly with functions in MySQL, rather than in Access.rather than in Access.

MySQL functions are similar to the SQL MySQL functions are similar to the SQL “standard”, and good HELP files are available.“standard”, and good HELP files are available.

Access functions differ from those in most other Access functions differ from those in most other systems, and have very little actual HELP for systems, and have very little actual HELP for users. (Some of the HELP is actually Excel help users. (Some of the HELP is actually Excel help and won’t even work in Access!)and won’t even work in Access!)

Page 4: Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 44

Classes of FunctionsClasses of FunctionsWhat are the main classes of functions?What are the main classes of functions?– String (for manipulating text)String (for manipulating text)– Numeric (for mathematical operations)Numeric (for mathematical operations)– Date/Time (for working with date and time fields)Date/Time (for working with date and time fields)– Cast (for converting between data types)Cast (for converting between data types)– Full-text search (useful if you have enormous text Full-text search (useful if you have enormous text

fields, like entire news or research articles)fields, like entire news or research articles)– Aggregate (we’ll cover these on Day 11)Aggregate (we’ll cover these on Day 11)– Control (e.g., IF, CASE, IFNULL)Control (e.g., IF, CASE, IFNULL)– Other (e.g., encryption, information about database)Other (e.g., encryption, information about database)

We will cover String, Numeric, and Date/TimeWe will cover String, Numeric, and Date/Time– Others are not used as often, much more advancedOthers are not used as often, much more advanced

Page 5: Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 55

String FunctionsString Functions““STRINGSTRING” refers to a group of characters, like a ” refers to a group of characters, like a word or a phrase (including spaces, punctuation)word or a phrase (including spaces, punctuation)Text fields (Text fields (char(X)char(X) and and varchar(X)varchar(X) in MySQL) in MySQL) contain stringscontain stringsExamples of string functions:Examples of string functions:– obtain obtain substringssubstrings (portions of a longer string) (portions of a longer string)– calculate the calculate the lengthlength of a string of a string– convert the convert the casecase of a string of a string– concatconcatenate strings into one longer stringenate strings into one longer string

See MySQL documentation for details (use the See MySQL documentation for details (use the link on the navigation bar on CSE 103 page)link on the navigation bar on CSE 103 page)

Page 6: Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 66

String Function ExerciseString Function ExerciseSort directors based on the length of their last name, Sort directors based on the length of their last name, longest first. Break ties by length of first name. Display full longest first. Break ties by length of first name. Display full names and lengths. names and lengths. [290107, “Pedro”, “Sanchez”][290107, “Pedro”, “Sanchez”]– Notice the duplicate records? Why are those there? Use SELECT Notice the duplicate records? Why are those there? Use SELECT

DISTINCT to fix this. DISTINCT to fix this. [69236 (69787?)`][69236 (69787?)`]

Create a listing of each movie from 2004 with a single field Create a listing of each movie from 2004 with a single field that contains the name of the movie with the rating behind that contains the name of the movie with the rating behind it following a slash (e.g., it following a slash (e.g., The Lord of the Rings – The The Lord of the Rings – The Fellowship of the Ring/PG-13Fellowship of the Ring/PG-13) . Sort the list alphabetically ) . Sort the list alphabetically by movie title. by movie title. [61, “30 Days of Night/Unrated”][61, “30 Days of Night/Unrated”]A new language code scheme has been proposed – the A new language code scheme has been proposed – the first two letters and the last letter of each language will first two letters and the last letter of each language will produce a 3-letter code.produce a 3-letter code.– Create a list of languages, old codes, and new codes Create a list of languages, old codes, and new codes [211][211]– See if this scheme will work – do conflicts exist? See if this scheme will work – do conflicts exist?

(Think about how to sort best to find conflicts. Could you write a query (Think about how to sort best to find conflicts. Could you write a query to look for conflicts?)to look for conflicts?)

Page 7: Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 77

Date/Time FunctionsDate/Time Functions

DMBSs store dates in a highly specialized DMBSs store dates in a highly specialized format that allows for detailed manipulation.format that allows for detailed manipulation.

A common use for Date/Time functions is to A common use for Date/Time functions is to extract various parts, such as month, day, hour, extract various parts, such as month, day, hour, etc., from a single date/time field.etc., from a single date/time field.

There are also functions to calculate the There are also functions to calculate the difference between two dates and to get the difference between two dates and to get the current date/time.current date/time.

See MySQL documentation for details.See MySQL documentation for details.

Page 8: Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 88

Date/Time Functions ExerciseDate/Time Functions ExerciseWhich people in the database were born in July? Which people in the database were born in July? [3402][3402]

Which people died on a Monday or Tuesday? Which people died on a Monday or Tuesday? [2670][2670]

Who was born on Valentine's day in any year? Who was born on Valentine's day in any year? How old are they now? How old are they now? [129[129, oldest is 157!, oldest is 157!]]– Think about this: If I was born in 1975 on February 14, Think about this: If I was born in 1975 on February 14,

how old am I now? how old am I now? – What if I just told you that I was born in ’75 and What if I just told you that I was born in ’75 and

haven’t had my birthday yet this year?haven’t had my birthday yet this year?

Page 9: Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 99

Date/Time Functions Exercise, Date/Time Functions Exercise, continuedcontinued

Find the current age, in order, of everyone who Find the current age, in order, of everyone who starred in starred in The Ten Commandments The Ten Commandments (1956 (1956 version), as if they were still alive today. version), as if they were still alive today. [22][22] This query should work correctly no matter what This query should work correctly no matter what year it is when you run it. year it is when you run it. [[oldest in ’05 is 143oldest in ’05 is 143]]– For simplicity, you can assume that everyone’s For simplicity, you can assume that everyone’s

birthday has already occurred this year.birthday has already occurred this year.– Remember that “A”, “An”, and “The” have been Remember that “A”, “An”, and “The” have been

moved to the end of movie titles.moved to the end of movie titles.

Now modify your query to create a list of only the Now modify your query to create a list of only the actors that are still living. actors that are still living. [8][8]

Page 10: Http://cse103 U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay

http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 10, Slide Day 10, Slide 1010

HomeworkHomework

Check the Check the HOMEWORKHOMEWORK link from today’s link from today’s Web pageWeb page

Finish queries from classFinish queries from class

There are problems that will involve using There are problems that will involve using other classes of functions, so use the other classes of functions, so use the MySQL documentation linkMySQL documentation link

Read Day 16 in the textbook on Aggregate Read Day 16 in the textbook on Aggregate FunctionsFunctions