Presentation is loading. Please wait.

Presentation is loading. Please wait.

Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.

Similar presentations


Presentation on theme: "Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL."— Presentation transcript:

1 Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.

2 ACBD 4 6 4 3 4 6 R 5 3 8 9 5 1 1 2 1 7 2 2 4 7 5 6 8 2 Example of Restriction (1) 45144528ACBD R Restrict[ B = 5 ] B 5 3 8 9 5 1 451463274815397645286122

3 ACBD 4 6 4 3 4 6 R 5 3 8 9 5 1 1 2 1 7 2 2 4 7 5 6 8 2 Example of Restriction (2) 4528ACBD6327 R Restrict[ B < D ] B 5 3 8 9 5 1 D 4 7 5 6 8 2 6327612245144815397645286122

4 Definition of ‘Restrict’ 1. Creates a new relation formed from tuples of the operand relation. 2. A boolean condition provided as a parameter is used to decide which of the operand’s tuples should appear in the result : l The condition is applied to every tuple in the operand. l IF the condition is true for a tuple THEN that tuple is copied into the result ELSE that tuple is not copied into the result 3. The result’s attribute names are those of the operand.

5 Restriction Conditions l Each tuple is evaluated against the condition in isolation from all the other tuples in the operand. l The condition can : l compare any attribute in the tuple with : any constant value (e.g. 3, ‘Julie’), any other attribute value of the same type in the same tuple; l have any kind of comparison allowed by that attribute’s data type; l be built up of a Boolean combination of comparisons, i.e. using AND, OR and NOT. l Calculations can also be incorporated into comparisons. Example :Salary / 3 < 500 + (2 * Bonus )

6 Boundary Cases l IF the restriction condition is true for all the operand’s tuples THEN the result will be the same as the operand. l IF the restriction condition is false for all the tuples THEN the result will be an empty relation, i.e. a relation having no tuples. Note : a condition containing one or more ORs in it is more likely to evaluate to true, because only one of the individual conditions needs to evaluate to true, so that as a consequence the result is likely to be larger. Conversely, a condition with one or more ANDs in it is more likely to evaluate to false, leading to a smaller result.

7 SQL : Restriction The SQL equivalent of a Restriction has the syntax :- Select* FromRELATION_NAME Wherecondition ; Principles : l Put Select * to get all the attributes in the result. l Put the relation name in the From phrase. l Put the required condition in the Where phrase. The SQL statement also retrieves the result from the DB. SQL fulfils all the requirements of the Restrict operation.

8 SQL Examples The 2 previous examples are written in SQL as follows :- l Select * From R Where B = 5 ; l Select * From R Where B < D ; Two more examples that include boolean operators in the condition : l Select * From R Where B = 5 Or D <> 4 ; l Select * From R Where B > 5 And B < D ; Note the SQL “  ” symbol.

9 Available Comparators in SQL All the standard comparators and operators are available for use. l Every type must have the “=“ and “<>” comparators. l Many types also have : >= For these comparators to be valid, the type must either be numeric or orderable, i.e. it is possible to sort any set of values of that type into an order. Examples : alphabetic types can be sorted into alphabetic order; date-times can be sorted into a temporal order.

10 Comparisons with NULL l SQL provides a special facility for use in the Where phrase to check whether an attribute value is NULL :- X Is NULL The result is true or false, depending on whether X is Null (or not). One can also check whether X is not Null :- X Is Not NULL l Let X and Y be attributes of the same data type. Consider the comparison X = Y Suppose either of X and Y are NULL (or both are NULL ) The result is maybe not true.

11 Boolean Operators l Multiple comparisons can be combined into one Restriction condition using And, Or and Not in the usual way. Example :- Where comparison 1 And Not comparison 2 Or comparison 3 ; l The comparisons are combined in a left-to-right order, except that Not has a higher priority than And and Or. l Parentheses can be used to alter the order in which comparisons are combined. Example :- Where comparison 1 And Not (comparison 2 Or comparison 3 ) ; Different to the previous example.

12 LIKE (1) The purpose of Like is to match character strings. It is much more powerful for this than “=” and “<>”. l The syntax of Like is : AttributeName Like ‘pattern’ l The type of AttributeName must be character string, although it can be a string of any length. l pattern is made up of character constants and/or one or more of the following wildcard characters : % : represents zero, one or more occurrences of any character(s); _(underscore) : represents any character, but just one character. l Like returns true when an attribute value matches the pattern, and false otherwise. Example : Where Name Like ‘Sm_th%’ ; would find any ‘Smith’ or ‘Smythe’, including those which are the first part of a double-barrelled name.

13 LIKE (2) l If a pattern is required that needs % or _ in it, then there is a problem in constructing the pattern. l The problem is solved by having an escape character which prefixes the % or _ in the pattern and designates that % or _ as standing for itself instead of a wildcard. Example : Where Discount Like ‘%=%’ Escape ‘=’ ; would find any character string ending in %. The “=“ is designated an escape character, so that the second % in the pattern stands for itself while the first one represents a wildcard. l The full syntax is : AttributeName Like ‘pattern’ Escape ‘character’ The Escape clause is optional.

14 IN l The purpose of In is to make it easier to compare an attribute value with a set of values. l The syntax of In is : AttributeName In ( set_of_values ) l AttributeName can be of any type. l set_of_values consists of a list of the set of values, with values being separated by commas. l In returns true when an attribute value is equal to one of those in set_of_values, and false otherwise. Example : Where Number In ( 3, 5, 7, 19 ) ; would find any of the numbers 3, 5, 7 or 19. l Att In ( v 1, v 2, v 3 ) is a logical shorthand for ( Att = v 1 ) Or ( Att = v 2 ) Or ( Att = v 3 )

15 BETWEEN l The purpose of Between is to make it easier to determine whether an attribute value lies within a given range of values. l The syntax of Between is : AttributeName Between value 1 And value 2 l AttributeName must be a numeric or orderable type. l Let value 1 and value 2 denote the minimum and maximum values respectively of the range. Between returns true when an attribute value lies in the range value 1 to value 2 inclusive, and false otherwise. Example : Where Number Between 10 And 50 ; would find any number in the range 10 to 50. l Att Between value 1 And value 2 is a logical shorthand for ( Att >= value 1 ) And ( Att <= value 2 )

16 Negating the Special Comparators l It is possible to have the negative of each of these comparators. l The negatives are : l AttributeName Not Like ‘pattern’ l AttributeName Not In ( set_of_values ) l AttributeName Not Between value 1 And value 2 l Note that this is in addition to being able to use the Boolean Not in front of the comparator expression in the usual way. The following are logically equivalent to the above : l Not AttributeName Like ‘pattern’ l Not AttributeName In ( set_of_values ) l Not AttributeName Between value 1 And value 2

17 Combining ‘Restrict’ & ‘Project’ l By the nature of relational algebra, we could apply both operators to a relation R in either of the following ways : l R Project[ AttributeNames ] Restrict[ condition ] l R Restrict[ condition ] Project[ AttributeNames ] l In SQL, we apply both operators by writing them in the standard SQL set of phrases, as follows : Select AttributeNames From R Where condition ; l As SQL has its own in-built sequence of operations, in fact it will do the Restrict first and the Project second.


Download ppt "Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL."

Similar presentations


Ads by Google