Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-310 Database Systems

Similar presentations


Presentation on theme: "CPSC-310 Database Systems"— Presentation transcript:

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

2 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 ) SQL languages in DDL/DML (Ch. 6) How does SQL get executed (Chs ) More on SQL (Chs. 7-9) Transition processing (Chs )

3 How does SQL get executed?

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

5 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;

6 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;

7 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;

8 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.

9 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.

10 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

11 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

12 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

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

14 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

15 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 | … | … | …

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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 ) SQL languages in DDL/DML (Ch. 6) How does SQL get executed (Chs ) More on SQL (Chs. 7-9) Transition processing (Chs )

28 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 ) SQL languages in DDL/DML (Ch. 6) How does SQL get executed (Chs ) More on SQL (Chs. 7-9) Transition processing (Chs )

29 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.

30 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.

31 Goods and Bads of SQL

32 Goods and Bads of SQL Shortcomings of SQL:

33 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)

34 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

35 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)

36 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.

37 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

38 Combining SQL and Conventional Programming Languages
Three ways to combine:

39 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

40 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

41 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)

42 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)

43 Persistent Stored Modules (PSM)

44 Persistent Stored Modules (PSM)
A recent SQL standard

45 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

46 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

47 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

48 Basic PSM Form

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

50 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.

51 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.

52 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);

53 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

54 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

55 Invoking Procedures

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

57 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);

58 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.

59 Types of PSM statements

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

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

62 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.

63 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’;

64 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.

65 IF Statements

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

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

68 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;

69 Example: IF Statement

70 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 ( ), popular (200)

71 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 ( ), 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;

72 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 ( ), 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

73 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 ( ), 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

74 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 ( ), 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


Download ppt "CPSC-310 Database Systems"

Similar presentations


Ads by Google