CPSC-310 Database Systems

Slides:



Advertisements
Similar presentations
1 Relational Algebra* and Tuple Calculus * The slides in this lecture are adapted from slides used in Standford's CS145 course.
Advertisements

1 Combining SQL and Conventional Programming Languages Source: slides by Jeffrey Ullman.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Fall 2001Arthur Keller – CS 18010–1 Schedule Oct. 25 (TH) More Constraints, Triggers. u Read Sections Oct. 30 (T) Embedded SQL. u Read Section.
Winter 2002Arthur Keller – CS 18010–1 Schedule Today: Feb. 5 (T) u Triggers, PL/SQL. u Read Sections 7.4, 8.2. Assignment 4 due. Feb. 7 (TH) u PL/SQL,
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Announcements Read JDBC Project Step 5, due Monday.
Winter 2006Keller, Ullman, Cushing10–1 Modification to Views Via Triggers Oracle allows us to “intercept” a modification to a view through an instead-of.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
1 Real SQL Programming - 2 Persistent Stored Modules (PSM) PL/SQL Embedded SQL These slides are reused from Jeffrey Ullman’s class with the author’s permission.
Databases 1 9th lecture. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
Introduction to PL/SQL. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
CPSC-608 Database Systems Fall 2015 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #8.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
Chapter 7: Constraints and Triggers Foreign Keys Local and Global Constraints Triggers 1.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: Stored Procedure & Embedded SQL Ch. 9.3, 9.4.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
SQL in the real world 1. The Three-Tier Architecture of Database Applications browser network HTTP Web server Application server Database server database.
1 Lecture 11 PL/SQL Slides from
Web Database Programming Using PHP
CPSC-310 Database Systems
CPSC-310 Database Systems
Introduction to Database Systems, CS420
CPSC-310 Database Systems
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
CMSC-461 Database Management Systems
CS 440 Database Management Systems
Web Database Programming Using PHP
Foreign Keys Local and Global Constraints Triggers
CPSC-310 Database Systems
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
Database Application Development
CPSC-608 Database Systems
CPSC-310 Database Systems
CPSC-608 Database Systems
CS 440 Database Management Systems
CPSC-608 Database Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
Database Application Development
CPSC-310 Database Systems
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
Modification to Views Via Triggers
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Database Application Development
Presentation transcript:

CPSC-310 Database Systems Professor Jianer Chen Room 315C HRBB Lecture #21

Outline of Course Representing things by tables E-R model (Ch. 4) Good table structures Functional Dependencies (Ch.3) Basic operations on relations Relational Algebra (Chs. 2+5) Storage management (Chs. 13-14) SQL languages in DDL/DML (Ch. 6) How does SQL get executed (Chs. 15-16) More on SQL (Chs. 7-9) Transition processing (Chs. 17-19)

How does SQL get executed?

How does SQL get executed? DBMS translates a SQL program P into executable code, by

How does SQL get executed? DBMS translates a SQL program P into executable code, by Preparing a collection C of algorithms for operations in relational algebra;

How does SQL get executed? DBMS translates a SQL program P into executable code, by Preparing a collection C of algorithms for operations in relational algebra; For the given SQL program P: 1. understanding the program P;

How does SQL get executed? DBMS translates a SQL program P into executable code, by Preparing a collection C of algorithms for operations in relational algebra; For the given SQL program P: 1. understanding the program P; 2. translating P into an expression tree E;

How does SQL get executed? DBMS translates a SQL program P into executable code, by Preparing a collection C of algorithms for operations in relational algebra; For the given SQL program P: 1. understanding the program P; 2. translating P into an expression tree E; 3. converting E into an algorithm using algorithms in C.

How does SQL get executed? DBMS translates a SQL program P into executable code, by Preparing a collection C of algorithms for operations in relational algebra; For the given SQL program P: 1. understanding the program P; 2. translating P into an expression tree E; 3. converting E into an algorithm using algorithms in C. Taking care of issues in optimization, consistency, and security.

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; Translated by DBMS take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; Understand P A parse tree T Translate P into an expression tree E expression tree E Use the algorithms in C to convert E into executable code take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; Understand P A parse tree T Translate P into an expression tree E expression tree E Use the algorithms in C to convert E into executable code take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

SELECT beer FROM Likes, Frequents WHERE Frequents. drinker = Likes SELECT beer FROM Likes, Frequents WHERE Frequents.drinker = Likes.drinker; SQL program P

SELECT beer FROM Likes, Frequents WHERE Frequents. drinker = Likes SELECT beer FROM Likes, Frequents WHERE Frequents.drinker = Likes.drinker; SQL program P select-statement SELECT FROM WHERE search-condition boolean-term boolean-factor boolean-primary comparison-predicte expression comp-op = term column-name Frequents.drinker Likes.drinker table-list table-name , Linkes Frequents select-list select-sublist beer Parse tree T

SELECT beer FROM Likes, Frequents WHERE Frequents. drinker = Likes SELECT beer FROM Likes, Frequents WHERE Frequents.drinker = Likes.drinker; SQL program P select-statement SELECT FROM WHERE search-condition boolean-term boolean-factor boolean-primary comparison-predicte expression comp-op = term column-name Frequents.drinker Likes.drinker table-list table-name , Linkes Frequents select-list select-sublist beer Parse tree T select-statement ::= SELECT [DISTINCT] select-list FROM table-list [WHERE search-condition] [ORDER BY column-name] select-list ::= * | select-sublist select-sublist ::= column-name | column-name, select-sublist table-list ::= table-name | table-name, table-list search-condition ::= boolean-term | …… boolean-term ::= boolean-factor | …… boolean-factor ::= [NOT] boolean-primary boolean-primary ::= comparison-predicate | …… comparison-predicate ::= expression comp-op expression expression ::= term | ……| …… term ::= column-name | … | … | …

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; Understand P A parse tree T Translate P into an expression tree E expression tree E Use the algorithms in C to convert E into executable code take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; select-statement SELECT FROM WHERE select-list a1 , b1 table-list A B search-condition a2 = b2 Understand P A parse tree T Translate P into an expression tree E expression tree E Use the algorithms in C to convert E into executable code take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; select-statement SELECT FROM WHERE select-list a1 , b1 table-list A B search-condition a2 = b2 Understand P A parse tree T Translate P into an expression tree E expression tree E Use the algorithms in C to convert E into executable code take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; select-statement SELECT FROM WHERE select-list a1 , b1 table-list A B search-condition a2 = b2 Understand P A parse tree T Translate P into an expression tree E A B × σ π a1, b1 a2=b2 expression tree E Use the algorithms in C to convert E into executable code take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; select-statement SELECT FROM WHERE select-list a1 , b1 table-list A B search-condition a2 = b2 Understand P A parse tree T Translate P into an expression tree E A B × σ π a1, b1 a2=b2 A B ⨝ π a1, b1 expression tree E Use the algorithms in C to convert E into executable code take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; select-statement SELECT FROM WHERE select-list a1 , b1 table-list A B search-condition a2 = b2 Understand P A parse tree T Translate P into an expression tree E A B × σ π a1, b1 a2=b2 A B ⨝ π a1, b1 Optimization expression tree E Use the algorithms in C to convert E into executable code take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; select-statement SELECT FROM WHERE select-list a1 , b1 table-list A B search-condition a2 = b2 Understand P A parse tree T Translate P into an expression tree E A B × σ π a1, b1 a2=b2 A B ⨝ π a1, b1 Optimization expression tree E Use the algorithms in C to convert E into executable code take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; select-statement SELECT FROM WHERE select-list a1 , b1 table-list A B search-condition a2 = b2 Understand P A parse tree T Translate P into an expression tree E A B × σ π a1, b1 a2=b2 A B ⨝ π a1, b1 Optimization expression tree E Use the algorithms in C to convert E into executable code Read(A) Read(B) Join Proj(a1,b1) take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; select-statement SELECT FROM WHERE select-list a1 , b1 table-list A B search-condition a2 = b2 Understand P A parse tree T Translate P into an expression tree E A B × σ π a1, b1 a2=b2 A B ⨝ π a1, b1 Optimization Proj(T, a1,b1) … Loop get the next tuple t in T; output t’s attributes a1 and b1 …. expression tree E Use the algorithms in C to convert E into executable code Read(A) Read(B) Join Proj(a1,b1) Join(A, B) … Loop get the next tuple pair tA, tB; If tA and tB are joinable Then output the joined tuple; …. take care of issues in optimization and security. … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; select-statement SELECT FROM WHERE select-list a1 , b1 table-list A B search-condition a2 = b2 Understand P A parse tree T Translate P into an expression tree E A B × σ π a1, b1 a2=b2 A B ⨝ π a1, b1 Optimization Proj(T, a1,b1) … Loop get the next tuple t in T; output t’s attributes a1 and b1 …. expression tree E Read(A) Read(B) Join Proj(a1,b1) Use algorithms in C to convert E Join(A, B) … Loop get the next tuple pair tA, tB; If tA and tB are joinable Then output the joined tuple; …. take care of issues in optimization and security. Optimization … Read(A), Read(B); C = A + B; Write(C) Machine executable code

How does SQL get executed? SELECT a1, b1 FROM A, B WHERE a2=b2 An SQL program P Prepare a collection C of efficient algorithms for operations in relational algebra; select-statement SELECT FROM WHERE select-list a1 , b1 table-list A B search-condition a2 = b2 Understand P A parse tree T Translate P into an expression tree E A B × σ π a1, b1 a2=b2 A B ⨝ π a1, b1 Optimization Proj(T, a1,b1) … Loop get the next tuple t in T; output t’s attributes a1 and b1 …. expression tree E Read(A) Read(B) Join Proj(a1,b1) Use algorithms in C to convert E Join(A, B) … Loop get the next tuple pair tA, tB; If tA and tB are joinable Then output the joined tuple; …. take care of issues in optimization and security. Optimization … Read(A), Read(B); C = A + B; Write(C) Machine executable code

Outline of Course Representing things by tables E-R model (Ch. 4) Good table structures Functional Dependencies (Ch.3) Basic operations on relations Relational Algebra (Chs. 2+5) Storage management (Chs. 13-14) SQL languages in DDL/DML (Ch. 6) How does SQL get executed (Chs. 15-16) More on SQL (Chs. 7-9) Transition processing (Chs. 17-19)

Outline of Course A Related Question: How can SQL be Executed with conventional Programming languages? Representing things by tables E-R model (Ch. 4) Good table structures Functional Dependencies (Ch.3) Basic operations on relations Relational Algebra (Chs. 2+5) Storage management (Chs. 13-14) SQL languages in DDL/DML (Ch. 6) How does SQL get executed (Chs. 15-16) More on SQL (Chs. 7-9) Transition processing (Chs. 17-19)

SQL in Real Programs We have seen only how SQL is used at the generic query interface --- an environment where we sit at a terminal and enters queries to a database.

SQL in Real Programs We have seen only how SQL is used at the generic query interface --- an environment where we sit at a terminal and enters queries to a database. This is very rare in practice.

Goods and Bads of SQL

Goods and Bads of SQL Shortcomings of SQL:

Goods and Bads of SQL Shortcomings of SQL: * Relational data model doesn't match well with data model of conventional programming languages (e.g., data structure mismatch)

Goods and Bads of SQL Shortcomings of SQL: * Relational data model doesn't match well with data model of conventional programming languages (e.g., data structure mismatch) * No pointers, loops or branches in SQL

Goods and Bads of SQL Shortcomings of SQL: * Relational data model doesn't match well with data model of conventional programming languages (e.g., data structure mismatch) * No pointers, loops or branches in SQL * No convenient input and output (e.g. formatting)

Goods and Bads of SQL Shortcomings of SQL: * Relational data model doesn't match well with data model of conventional programming languages (e.g., data structure mismatch) * No pointers, loops or branches in SQL * No convenient input and output (e.g. formatting) Strength of SQL: * provides strong, easy-to-use/understand, very efficient processing for large volume of data.

Goods and Bads of SQL Shortcomings of SQL: * Relational data model doesn't match well with data model of conventional programming languages (e.g., data structure mismatch) * No pointers, loops or branches in SQL * No convenient input and output (e.g. formatting) Strength of SQL: * provides strong, easy-to-use/understand, very efficient processing for large volume of data. Combining conventional programming languages with SQL allows to do anything computable, yet still get the very-high-level SQL interface to database

Combining SQL and Conventional Programming Languages Three ways to combine:

Combining SQL and Conventional Programming Languages Three ways to combine: Persistent Stored Modules (PSM): Code stored in the DB schema and executed on command from a user

Combining SQL and Conventional Programming Languages Three ways to combine: Persistent Stored Modules (PSM): Code stored in the DB schema and executed on command from a user Embed SQL statements in programs written in some conventional language

Combining SQL and Conventional Programming Languages Three ways to combine: Persistent Stored Modules (PSM): Code stored in the DB schema and executed on command from a user Embed SQL statements in programs written in some conventional language Call-level interfaces (CLI) * SQL/CLI (SQL standard, for use with C * JDBC (for use with Java)

Combining SQL and Conventional Programming Languages Three ways to combine: Persistent Stored Modules (PSM): Code stored in the DB schema and executed on command from a user Embed SQL statements in programs written in some conventional language Call-level interfaces (CLI) * SQL/CLI (SQL standard, for use with C * JDBC (for use with Java)

Persistent Stored Modules (PSM)

Persistent Stored Modules (PSM) A recent SQL standard

Persistent Stored Modules (PSM) A recent SQL standard Mechanism for user to store in the DB schema functions and procedures that can be used in SQL statements

Persistent Stored Modules (PSM) A recent SQL standard Mechanism for user to store in the DB schema functions and procedures that can be used in SQL statements The functions and procedures are written in a simple general-purpose language

Persistent Stored Modules (PSM) A recent SQL standard Mechanism for user to store in the DB schema functions and procedures that can be used in SQL statements The functions and procedures are written in a simple general-purpose language Includes if, loop, variable declaration, as well as SQL queries and updates

Basic PSM Form

Basic PSM Form CREATE PROCEDURE <name> (<parameter list>) <optional local declarations> <body>; CREATE FUNCTION <name> (<parameter list>) RETURNS <type>

Basic PSM Form CREATE PROCEDURE <name> (<parameter list>) <optional local declarations> <body>; CREATE FUNCTION <name> (<parameter list>) RETURNS <type> PSM uses mode-name-type triples for parameters, where the mode can be: IN = procedure uses value, does not change value. OUT = procedure changes, does not use. INOUT = both.

Example: Stored Procedure Let’s write a procedure that takes two arguments b and p, and adds a tuple to Sells(bar, beer, price) that has bar = ’Joe’’s Bar’, beer = b, and price = p. (can be used by Joe to add to his menu more easily.

Example: Stored Procedure Let’s write a procedure that takes two arguments b and p, and adds a tuple to Sells(bar, beer, price) that has bar = ’Joe’’s Bar’, beer = b, and price = p. (can be used by Joe to add to his menu more easily. CREATE PROCEDURE JoeMenu ( IN b CHAR(20), IN p REAL ) INSERT INTO Sells VALUES(’Joe’’s Bar’, b, p);

Example: Stored Procedure Let’s write a procedure that takes two arguments b and p, and adds a tuple to Sells(bar, beer, price) that has bar = ’Joe’’s Bar’, beer = b, and price = p. (can be used by Joe to add to his menu more easily. CREATE PROCEDURE JoeMenu ( IN b CHAR(20), IN p REAL ) INSERT INTO Sells VALUES(’Joe’’s Bar’, b, p); Parameters are both read-only, not changed

Example: Stored Procedure Let’s write a procedure that takes two arguments b and p, and adds a tuple to Sells(bar, beer, price) that has bar = ’Joe’’s Bar’, beer = b, and price = p. (can be used by Joe to add to his menu more easily. CREATE PROCEDURE JoeMenu ( IN b CHAR(20), IN p REAL ) INSERT INTO Sells VALUES(’Joe’’s Bar’, b, p); Parameters are both read-only, not changed The body is a single insertion

Invoking Procedures

Invoking Procedures Use SQL/PSM statement CALL, with the name of the procedure and arguments.

CALL JoeMenu(’Moosedrool’, 5.00); Invoking Procedures Use SQL/PSM statement CALL, with the name of the procedure and arguments. Example: CALL JoeMenu(’Moosedrool’, 5.00);

CALL JoeMenu(’Moosedrool’, 5.00); Invoking Procedures Use SQL/PSM statement CALL, with the name of the procedure and arguments. Example: CALL JoeMenu(’Moosedrool’, 5.00); Functions used in SQL expressions wherever a value of their return type is appropriate.

Types of PSM statements

Types of PSM statements RETURN <expression> sets the return value of a function.

Types of PSM statements RETURN <expression> sets the return value of a function. DECLARE <name> <type> is used to declare local variables.

Types of PSM statements RETURN <expression> sets the return value of a function. DECLARE <name> <type> is used to declare local variables. BEGIN . . . END for groups of statements. * Separate statements by semicolons.

Types of PSM statements RETURN <expression> sets the return value of a function. DECLARE <name> <type> is used to declare local variables. BEGIN . . . END for groups of statements. * Separate statements by semicolons. Assignment statements: SET <variable> = <expression>; Example: SET b = ’Bud’;

Types of PSM statements RETURN <expression> sets the return value of a function. DECLARE <name> <type> is used to declare local variables. BEGIN . . . END for groups of statements. * Separate statements by semicolons. Assignment statements: SET <variable> = <expression>; Example: SET b = ’Bud’; Statement labels: give a statement a label by prefixing a name and a colon.

IF Statements

IF Statements Simplest form: IF <condition> THEN END IF;

IF Statements Simplest form: IF <condition> THEN END IF; Add ELSE <statements> if desired: IF . . . THEN . . . ELSE . . . END IF;

IF Statements Simplest form: IF <condition> THEN END IF; Add ELSE <statements> if desired: IF . . . THEN . . . ELSE . . . END IF; Add additional cases by ELSEIF <statements>: IF … THEN … ELSEIF … ELSEIF … ELSE … END IF;

Example: IF Statement

unpopular (<100), average (100-199), popular (200) Example: IF Statement Rate bars by the number of customers they have, based on Frequents(drinker,bar): unpopular (<100), average (100-199), popular (200)

unpopular (<100), average (100-199), popular (200) Example: IF Statement Rate bars by the number of customers they have, based on Frequents(drinker,bar): unpopular (<100), average (100-199), popular (200) CREATE FUNCTION Rate (IN b CHAR(20) ) RETURNS CHAR(10) DECLARE cust INTEGER; BEGIN SET cust = (SELECT COUNT(*) FROM Frequents WHERE bar = b); IF cust < 100 THEN RETURN ’unpopular’ ELSEIF cust < 200 THEN RETURN ’average’ ELSE RETURN ’popular’ END IF; END;

unpopular (<100), average (100-199), popular (200) Example: IF Statement Rate bars by the number of customers they have, based on Frequents(drinker,bar): unpopular (<100), average (100-199), popular (200) CREATE FUNCTION Rate (IN b CHAR(20) ) RETURNS CHAR(10) DECLARE cust INTEGER; BEGIN SET cust = (SELECT COUNT(*) FROM Frequents WHERE bar = b); IF cust < 100 THEN RETURN ’unpopular’ ELSEIF cust < 200 THEN RETURN ’average’ ELSE RETURN ’popular’ END IF; END; number of customers of bar b

unpopular (<100), average (100-199), popular (200) Example: IF Statement Rate bars by the number of customers they have, based on Frequents(drinker,bar): unpopular (<100), average (100-199), popular (200) CREATE FUNCTION Rate (IN b CHAR(20) ) RETURNS CHAR(10) DECLARE cust INTEGER; BEGIN SET cust = (SELECT COUNT(*) FROM Frequents WHERE bar = b); IF cust < 100 THEN RETURN ’unpopular’ ELSEIF cust < 200 THEN RETURN ’average’ ELSE RETURN ’popular’ END IF; END; number of customers of bar b nested IF statement

unpopular (<100), average (100-199), popular (200) Example: IF Statement Rate bars by the number of customers they have, based on Frequents(drinker,bar): unpopular (<100), average (100-199), popular (200) CREATE FUNCTION Rate (IN b CHAR(20) ) RETURNS CHAR(10) DECLARE cust INTEGER; BEGIN SET cust = (SELECT COUNT(*) FROM Frequents WHERE bar = b); IF cust < 100 THEN RETURN ’unpopular’ ELSEIF cust < 200 THEN RETURN ’average’ ELSE RETURN ’popular’ END IF; END; number of customers of bar b return occurs here, not at one of the RETURN statements nested IF statement