Show navigation

JOINS (Part ✌️)!
👉👈

Plus bonus datatypes

This week we'll cover types of table relationships; using OUTER JOINs to retrieve rows from multiple tables along with the various set operators, including the use of a self-join. We'll also discuss the various data types used in MySQL in preparation for learning about Data Manipulation Language starting next week.






Let's throw an error!

Try repeating the steps above, only replace the values

349, 'Paranoid', 12

with the values

'Three hundred and fifty', 'Master of Reality', 12

Technically speaking, there are two other categories of data type: Geospatial Opens in a new window and JSON Opens in a new window. But I'm not gonna teach those in class unless a majority of you are like "oh, no, please sir, teach us how to handle geospatial data!"


NamePurposeExample
DATE_ADD/DATE_SUBAdds or subtracts an interval (like minutes, days, months, etc.)
DATE_ADD('2018-05-01', INTERVAL 1 DAY)
NOWGets the current date and time (based on the server's clock).
NOW()
EXTRACTGets the a segment of date and time data (i.e. year, month).
EXTRACT(YEAR FROM '2019-07-02')
DATEDIFFSubtracts one date from another, giving the difference between two dates.
DATEDIFF('2021-03-31', NOW())
MONTHFor each segment of time and date data, there is a function that will extract that segment - YEAR(), MINUTE(), etc.
MONTH('2021-03-31')
MONTHNAMEGiven a numbered month, returns the name of the month.
MONTHNAME('2021-03-31')
DATE_FORMATLets you format a date to a given format. This is it's own weird little syntax Opens in a new window. You can use this, as it's fairly concise, but I won't blame you if you want to just use EXTRACT, or any of the specific segment functions.
DATE_FORMAT('2021-03-31', '%W %M %Y')
Batman villain Temple Fugate

Exercises!

  1. Insert a row into your demo table. Specify the column names in your insert statement.
  2. Insert another row into your demo table, this time without specifying the column names.
  3. Insert another row, this time specify only three of the column names, and only insert data for those columns.
    Note: some columns may require a value, while others may be okay being left empty.
  4. Insert another row, with data for each column. Use arithmetic operations to populate any columns with numerical data types.
  5. Insert another row, with data for each column. Use the NOW function to populate the dates and date_and_time columns.
  6. Insert another row, with data for each column. To populate the whole_numbers column, use the NOW function, along with DATEDIFF, to enter the number of days between now and Hallowe'en.
  7. Insert another row, with data for each column. Use NOW and the DATE_SUB function to populate the dates column with the date 20 months before today. Use NOW and the DATE_ADD function to populate the date_and_time column with the date 500 hours from today.
  8. Write a select statement that only returns the month names from your dates and date_and_time columns. Check back in the notes for the best function to accomplish this.
  9. Write a select statement that only returns values from your decimalNumbers column as the data type DECIMAL, with zero decimal places.
  10. Write a select statement with a WHERE condition that uses the EXTRACT function.
  11. Determine the number of bytes for each character in the following sentence:
    🔶 This string is 32 bytes ⭐
    (Reminder that LENGTH() returns the number of bytes in a string.)
JOINS (Part ✌️)!👉👈Plus bonus datatypes