Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation,...

30
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd , 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275: Database Design (Fall 2015)

Transcript of Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation,...

Page 1: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

1

Instructor: Craig Duckett

Lecture 08: Thursday, October 22nd, 2015Patterns, Order of Evaluation, Concatenation,

Substrings, Trim, Position

BIT275: Database Design (Fall 2015)

Page 2: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

2

Quick Reminder: If you need to email me about anything, please do so using my [email protected] account which I check several times a day. Do not use or reply to the [email protected] account which I use only for returning files from StudentTracker and only check once or twice a month!

NO CLASS NEXT TUESDAY, October 27th . It is a non-instructional day.

• MID-TERM EXAM is LECTURE 10, Tuesday, November 3rd

• Assignment 2 is due LECTURE 11, Thursday, November 5th, in StudentTracker by MIDNIGHT

It will cover everything discussed through Thursday’s lecture, Lecture 9, including any chapter reading from The DATABASE DESIGN FOR MERE MORTALS and THE LANGUAGE OF SQL books. You’re allowed to use one large index card for crib notes during the mid-term. I’ll post a Mid-Term Study Guide on the BIT275 website sometime on Friday, May 8.

Page 3: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

3

Tuesday (LECTURE 8)• Database Design for Mere Mortals: Chapter 6

Thursday (LECTURE 9)• The Language of SQL: Chapters 7, 8

Page 4: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

4

• Patterns• Order of Evaluations• Concatenation• Substring• TRIM• POSITION

Page 5: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

5

Patterns

• http://www.w3schools.com/sql/sql_like.asp• http://www.w3schools.com/sql/sql_in.asp• http://www.w3schools.com/sql/sql_between.asp• http://www.w3schools.com/sql/sql_and_or.asp • https://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html• http://www.w3schools.com/sql/sql_wildcards.asp

Page 6: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

Matching Patterns with LIKEYou can use LIKE to retrieve rows based on partial information. LIKE is useful if you don’t know an exact value (“The author’s last name is Kel-something”) or you want to retrieve rows with similar values (“Which authors live in the San Francisco Bay Area?”).

The LIKE condition’s important characteristics are:

• LIKE works with only character strings, not numbers or datetimes.• LIKE uses a pattern that values are matched against. A pattern is a quoted string that contains

the literal characters to match and any combination of wildcards. Wildcards are special characters used to match parts of a value.

Wildcard Operators

Page 7: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

String comparisons are case insensitive or case sensitive, depending on your DBMS.

You can negate a LIKE condition with NOT LIKE.

You can combine LIKE conditions and other conditions with AND and OR.

Page 8: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
Page 9: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

Escape and Unescaped Patterns

Page 10: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

[ ] and [ ^ ] Patterns

Page 11: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

BETWEENUse BETWEEN to determine whether a given value falls within a specified range.

The BETWEEN condition’s important characteristics are:

• BETWEEN works with character strings, numbers, and datetimes.• The BETWEEN range contains a low value and a high value, separated by AND. The low value

must be less than or equal to the high value.• BETWEEN is a convenient, shorthand clause that you can replicate by using AND.

BETWEEN is equivalent to: WHERE (test_column >= low_value) AND (test_column <= high_value)BETWEEN specifies an inclusive range, in which the high value and low value are included in the search.

To specify an exclusive range, which excludes endpoints, use > and comparisons instead of BETWEEN: WHERE (test_column > low_value) AND (test_column < high_value)

• String comparisons are case insensitive or case sensitive.• You can negate a BETWEEN condition with NOT BETWEEN.• You can combine BETWEEN conditions and other conditions with AND and OR.

Page 12: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

BETWEEN

Page 13: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

INUse IN to determine whether a given value matches any value in a specified list. The INcondition’s important characteristics are:

• IN works with character strings, numbers, and datetimes.• The IN list is a parenthesized listing of one or more comma-separated values. The list items

needn’t be in any particular order.• IN is a convenient, shorthand clause that you can replicate by using OR.

is equivalent to:

Page 14: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

14

Order of EvaluationsOperator Precedence

• https://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html

Page 15: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

Operators and Order of Evaluation

1 + 2 * 3 + 4 * 2 + 4 * 3 – 2 = ?

1 + (2 * 3) + (4 * 2) + (4 * 3) – 2 = 1 + (6 + 8 + 12) – 2 = (1 + 26) – 2 = 27 – 2 = 5

Page 16: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

16

Concatenation

Page 17: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

ConcatenationUse the operator || to combine, or concatenate, strings. The operator’s importantcharacteristics are:

• The operator || is two consecutive vertical-bar, or pipe, characters• Concatenation doesn’t add a space between strings.• || combines two strings into a single string:

'formal' || 'dehyde' is 'formaldehyde‘• You can chain concatenations to combine multiple strings into a single string:

'a' || 'b' || 'c' || 'd' is 'abcd‘• Concatenation with an empty string ('') leaves a string unchanged:

'a' || ' ' || 'b' is 'ab‘• The result of any concatenation operation that involves a null is null:

'a' || NULL || 'b' is NULL• To concatenate a string and a nonstring (such as a numeric or datetime value), you must

convert the nonstring to a string if your DBMS doesn’t convert it implicitly (CAST).

• NOTE: MySQL uses the CONCAT() function for concatenation• http://www.mysqltutorial.org/sql-concat-in-mysql.aspx• http://www.techonthenet.com/mysql/functions/concat.php• http://www.electrictoolbox.com/mysql-string-concatenation/• http://www.electrictoolbox.com/mysql-string-concatenation-part2/ (CONCAT_WS)

Concatenation with separator

Page 18: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

Concatenation

Page 20: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

SUBSTRINGUse the function SUBSTRING() to extract part of a string. The function’s importantcharacteristics are:

• A substring is any sequence of contiguous characters from the source string, including an empty string or the entire source string itself.

• SUBSTRING() extracts part of a string starting at a specified position and continuing for specified number of characters.

• A substring of an empty string is an empty string.• If any argument is null, SUBSTRING() returns null.

SUBSTRING(string FROM start [FOR length])

string is the source string from which to extract the substring. string is a string expression such as a column that contains character strings, a string literal, or the result of an operation or function that returns a string.

start is an integer that specifies where the substring begins.

length is an integer that specifies the length of the substring (the number of characters to return).

If FOR length is omitted, SUBSTRING() returns all the characters from start to the end of string

Page 21: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

SUBSTRING

Page 22: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

Changing String CaseUse the function UPPER() to return a string with lowercase letters converted to uppercase, anduse the function LOWER() to return a string with uppercase letters converted to lowercase. Thefunctions’ important characteristics are:

• A cased character is a letter that can be lowercase (a) or uppercase (A).• Case changes affect only letters. Digits, punctuation, and whitespace are left unchanged.• Case changes have no effect on empty strings ('').• If its argument is null, UPPER() and LOWER() return null.

Page 23: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

Changing String CaseEXAMPLE OF WILDCARD WITH STRING CASE:

List the titles that contain the characters MO, regardless of case.

All the letters in the LIKE pattern must be uppercase for this query to work.

Page 24: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

24

TRIM Function

http://www.w3resource.com/mysql/string-functions/mysql-trim-function.php

Page 25: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

TRIMUse the function TRIM() to remove unwanted characters from the ends of a string. Thefunction’s important characteristics are:• You can trim leading characters, trailing characters, or both. (You can not use TRIM() to

remove characters from within a string.)• By default, TRIM() trims spaces, but you can strip off any unwanted characters, such a leading

and trailing zeros or asterisks.• TRIM() typically is used to format results and make comparisons in a WHERE clause.• TRIM() is useful for trimming trailing spaces from CHAR values. • Trimming has no effect on empty strings ('').• If any argument is null, TRIM() returns null.

To trim spaces from a string:

TRIM([ [LEADING | TRAILING | BOTH] FROM] string)

string is a string expression such as a column that contains character strings, a stringliteral, or the result of an operation or function that returns a string. Specify LEADING toremove leading spaces, TRAILING to remove trailing spaces, or BOTH to remove leading andtrailing spaces. If this specifier is omitted, BOTH is assumed.

Page 26: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

TRIM

Page 27: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

27

POSITION Function

http://www.w3resource.com/mysql/string-functions/mysql-position-function.php

Page 28: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

POSITIONUse the function POSITION() to locate a particular substring within a given string. Thefunction’s important characteristics are:

• POSITION() returns an integer (=0) that indicates the starting position of a substring’s first occurrence within a string.

• If the string doesn’t contain the substring, POSITION() returns zero.• String comparisons are case insensitive or case sensitive, depending on your DBMS• The position of any substring within an empty string ('') is zero. • If any argument is null, POSITION() returns null.

POSITION (substring IN string)

Page 29: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

POSITION

Page 30: Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:

30

BIT 275 ICE 8