Presentation is loading. Please wait.

Presentation is loading. Please wait.

Texas State Technical College DISCOVER! Common Predicates Would you like paper or plastic?

Similar presentations


Presentation on theme: "Texas State Technical College DISCOVER! Common Predicates Would you like paper or plastic?"— Presentation transcript:

1 Texas State Technical College DISCOVER! Common Predicates Would you like paper or plastic?

2 Texas State Technical College DISCOVER! Common Predicates A predicate is a special operator or function that returns a boolean. Unlike an operator, a predicate can work with more than two values. At the semantic level, a predicate tests a set of objects to see if the supplied value or object is a member. Overview

3 Texas State Technical College DISCOVER! Given the set fatherOf = { { (‘Edwin’, ‘David’), (‘Edwin’, ‘David’), (‘Marvin’, ‘Robert’), (‘Marvin’, ‘Robert’), (‘Pinkie’, ‘Brain’), (‘Pinkie’, ‘Brain’), (‘Bill’, ‘John’), (‘Bill’, ‘John’), (‘Willie’, ‘Mary Jane’) (‘Willie’, ‘Mary Jane’) } Imaginary predicate to determine if Bill is the father of John. fatherOf(‘Bill’, ‘John’) returns TRUE Technical Example Common Predicates

4 Texas State Technical College DISCOVER! Predicates are most useful in the WHERE clause that modifies a statement. They are perfectly suited because they return a boolean. Usage Common Predicates

5 Texas State Technical College DISCOVER! NOT boolean Used to negate any boolean or boolean expression. IS NULL / IS NOT NULL Used to determine if a field has a value or not. BETWEEN x AND y Simple predicate to determine value range. LIKE pattern Used to determine if a text value matches a pattern. Used to determine if a text value matches a pattern. Common Predicates

6 Texas State Technical College DISCOVER! IN list Used to determine membership in a defined list. Used to determine membership in a defined list. ANY list Used to make a comparison to any item in the list. Used to make a comparison to any item in the list. ALL list Used to make a comparison against all items in a list. Used to make a comparison against all items in a list. Common Predicates

7 Texas State Technical College DISCOVER! The NOT predicate is used to negate or reverse a boolean value (ie. True becomes False). It is technically more of a boolean operator. Syntax NOT [boolean | boolean expression] Example True if the person is below 21 years of age. NOT (age >= 21) True if the person is between 18 and 21. NOT ( (age > 21) OR (age 21) OR (age < 18) ) NOT Predicate Common Predicates

8 Texas State Technical College DISCOVER! There is no standard way (using an operator) to test if a field has a null value. The IS NULL and IS NOT NULL predicates are used to determine if a field has no value. Syntax Field IS [NOT] NULL Example Returns records where address is empty. address IS NULL Returns records that have a phone number. phone IS NOT NULL IS NULL Predicate Common Predicates

9 Texas State Technical College DISCOVER! The BETWEEN predicate allows a quick determination if a value falls within a range. Syntax value BETWEEN lower AND upper Example Returns records in the Abilene zip code range. zip BETWEEN 79601 AND 79607 True if the person is between 18 and 21. age BETWEEN 18 AND 21 Caveat Equivalent: (value >= lower) AND (value = lower) AND (value <= upper) BETWEEN Predicate Common Predicates

10 Texas State Technical College DISCOVER! The LIKE predicate allows the matching of strings to generic patterns or templates. Syntax field LIKE ‘pattern’ Pattern Consists of literal and wildcard values Wildcards _Any single character %Any character string of any length LIKE Predicate Common Predicates

11 Texas State Technical College DISCOVER! Example Find customers whose last name starts with An. Find customers whose last name starts with An. lastname LIKE ‘An%’ Find customers whose last name ends with A. Find customers whose last name ends with A. lastname LIKE ‘%A’ Find customers whose middle name isn’t abbreviated. Find customers whose middle name isn’t abbreviated. middlename LIKE ‘_ _%’ LIKE Predicate Common Predicates

12 Texas State Technical College DISCOVER! Allows the determination of whether a given value is in a defined list. Syntax valueIN (list) Example True if the state is in the list provided. state IN (‘TX’, ‘GA’, ‘LA’) True if the status is in the list provided. status IN (‘NOT STARTED’, ‘PENDING’) IN Predicate Common Predicates

13 Texas State Technical College DISCOVER! Checks a comparison or test against all values in a list. Returns true if the comparison is true for ANY value in the list. Syntax valueoperator ANY (list) Example True if the tax rate is less than any in the list. tax_rate < ANY (.0825,.05,.0325) True if the discount is greater than any value. discount > ANY (5.00, 10.00, 15.00) ANY Predicate Common Predicates

14 Texas State Technical College DISCOVER! Similar to ANY. Checks a comparison against all values in a list. It returns true if the comparison is true for ALL values in the list. Syntax valueoperator ALL (list) Example True if the tax rate is less than all in the list. tax_rate < ALL (.0825,.05,.0325) True if the discount is greater than all value. discount > ALL (5.00, 10.00, 15.00) ALL Predicate Common Predicates

15 Texas State Technical College DISCOVER! The ANY statement has the following equivalent. tax_rate < ANY (.0825,.05,.0325) Equivalent tax_rate <.0825 OR tax_rate <.05 OR tax_rate <.0325 ANY Equivalence Common Predicates

16 Texas State Technical College DISCOVER! The ALL statement has the following equivalent. tax_rate < ALL (.0825,.05,.0325) Equivalent tax_rate <.0825 AND tax_rate <.05 AND tax_rate <.0325 ALL Equivalence Common Predicates

17 Texas State Technical College DISCOVER! In Summary… Predicates take two or more values and return a boolean value. They are more complex evaluations that generally involve a test for membership of a value in a set. The most common predicates in Oracle SQL NOTNOT IS [NOT] NULLIS [NOT] NULL BETWEENBETWEEN LIKELIKE ININ ANYANY ALLALL Common Predicates


Download ppt "Texas State Technical College DISCOVER! Common Predicates Would you like paper or plastic?"

Similar presentations


Ads by Google