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

Slides:



Advertisements
Similar presentations
CSC271 Database Systems Lecture # 11.
Advertisements

Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Introduction to Structured Query Language (SQL)
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
JavaScript, Third Edition
Introduction to Structured Query Language (SQL)
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
IFS Intro. to Data Management Chapter 6 Filtering your data.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
 The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data retrieved.  Comparison Operators Comparison OperatorDefinition.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Decision Structures and Boolean Logic
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Lesson 5: Working with Formulas and Functions Logical and 3D Formulas.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL).
Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Insert & Delete Objectives of the Lecture : To consider the insertion of tuples into a relation; To consider the deletion of tuples from a relation; To.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Restricting and Sorting Data
More SQL: Complex Queries,
Numeric and Functional
Chapter 5 Introduction to SQL.
Excel IF Function.
Writing Basic SQL SELECT Statements
Basic select statement
ATS Application Programming: Java Programming
Topics The if Statement The if-else Statement Comparing Strings
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Topics The if Statement The if-else Statement Comparing Strings
Chapter 8 JavaScript: Control Statements, Part 2
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CMSC 202 Java Primer 2.
Python Primer 1: Types and Operators
Restricting and Sorting Data
Objectives of the Lecture :
Operator King Saud University
Restricting and Sorting Data
Decision Making Using the IF and EVALUATE Statements
Presentation transcript:

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

ACBD R Example of Restriction (1) ACBD R Restrict[ B = 5 ] B

ACBD R Example of Restriction (2) 4528ACBD6327 R Restrict[ B < D ] B D

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.

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 < (2 * Bonus )

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.

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.

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.

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.

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.

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.

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.

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.

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 )

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 )

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

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.