CS317 File and Database...

33
September 24, 2017 Sam Siewert CS317 File and Database Systems Lecture 5 – More SQL and Intro to Stored Procedures http://dilbert.com/strips/comic/1995-10-11/

Transcript of CS317 File and Database...

Page 1: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

September 24, 2017 Sam Siewert

CS317File and Database Systems

Lecture 5 – More SQL and Intro to Stored Procedures

http://dilbert.com/strips/comic/1995-10-11/

Page 2: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

SQL Theory and Standards

Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8]

Part-1

Sam Siewert

2

Page 3: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Mid-term Survey Results

Sam Siewert 3

Page 4: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Bottom 4Materials Useful - Primarily for SQL Examples and Example DBs along with General Concepts and Terms [CB Ref Material?]

Technical Rigor - One last practice assignment– Normalization - Required for Safe/Good DB Design!– Learn How to Do– Ex #5 and #6 are DB Project of your choice!

LO Not Clear (Revisit Quickly)– LO Review– Q&A on LO

Technical Rigor– RA and TRC Behind Us after Exam #1 - Necessary for “Knowing”

SQL is Correct vs. Magic– Normalization Next Technical Section - Necessary for Referential

Integrity

Sam Siewert 4

Page 5: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Learning Objectives - Revisited!Structured and Unstructured Data: Block Storage

– Block Storage Devices [Disk Drives, SSD, Persistent Memory]– Split between Files and Databases [Integration of Both]

DML - Data Manipulation using SQL (ISO Standard)

DDL - Data Definition Language, Database Design with SQL– Logical Design [Schema] - MySQL Workbench– Physical Design [Hosting a DB]

Theory of Databases– Relational ER Models [Workbench] - Data vs. Information– OODBMS (SQL Extensions, C++ Alternatives) - Quickly– NoSQL - By Reading– Relational Algebra <-> SQL– Normalization for Referential Integrity (Avoid Duplication, Consistency, Veracity)

DBA - Database Administration and Security

Physical Aspects of File and Database Systems– Physical Implementation and Scaling [Block Partition, File], – Indexing [ISAM, B Tree, B+ Tree, R-Tree], – Network Access [Connectors], – Web Front Ends

Sam Siewert 5

Page 6: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

MySQL on DDL vs. DML

Sam Siewert 6

DMLDDL

Query

DBA [DCL]https://dev.mysql.com/doc/refman/5.7/en/sql-syntax.html

TCL

Page 7: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Reading AdviceRead SQL Chapter 5 using SQ3R (Scan, Question, Read, Recite, Review)

Experiment Using Views and Understand they are Normally Stored Queries Rather than New Tables

IGNORE View Materialization

Use Simple Multi-Table Queries - Nested or Multi-table Theta, Equi and Natural Join– Less Emphasis on other JOIN directives (INNER, OUTER,

RIGHT and LEFT)– To Compare Joins, See this TUTORIAL

Sam Siewert 7

Page 8: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

For Discussion…SQL is An Evolving ISO Standard – SQL:20161. What We’ve Learned So Far is SQL:1992 – Core SQL2. Latest SQL Standards are SQL:2011 and SQL:20163. Better integration of Procedural Language with SQL – PL/SQL,

SQL/PSM [E.g. MySQL Stored Procedures, Create Procedure]4. Extensions for Convenience? Other?5. Security Issues?6. OO Support? – Two Positions (Object RDBMS, New OODBMS)

Assignment #3 – SQL Advanced Features and Extensions Beyond Core, More Core SQL, ODBMS - Alternative OODBMS and NoSQL Exploration– Import ZAGI, HAFH or DreamHome Schema and Data– Goal is to Practice SQL for Proficiency and Schema Improvement– Practice with Stored Procedures that work with MySQL– Compare RDBMS to OODBMS and NoSQL - Structured,

Unstructured

Sam Siewert 8

Page 9: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Overview of Example DBsDreamHome - v1.0, v1.3, …, v2.0 [Ref: CB]

DBS - ZAGI, Data

DBS - HAFH, Schema & Data

Learning Objectives– Practice SQL DDL– Practice SQL DML– Practice Loading DBs, Migrating Data, Improving Schemas– Understand Limitations of RDBMS and Alternatives

Sam Siewert 9

Page 10: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Schema UpdatesRequires Table “Alter”, Creation of new Tables, Migration of DataPossible Good Use of Nested Query in Insert -Replicated Data to Migrate, Drop Old Tablehttps://dev.mysql.com/doc/refman/5.7/en/insert-select.html

Sam Siewert 10

Page 11: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

SQL Data Types (Domains) - DDLhttps://dev.mysql.com/doc/refman/5.7/en/data-types.html

Lowest Level of Data integrity, validation, veracity

Sam Siewert 11

Page 12: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

ISO SQL Data Types

Pearson Education © 2014 12

Page 13: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

ViewsStored Queries by name -https://dev.mysql.com/doc/refman/5.7/en/create-view.html

Sam Siewert 13

CREATE VIEW MyStaff AS SELECT * FROM Staff WHERE branchNo=’B003’;select * from MyStaff;DROP VIEW MyStaff;

Page 14: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Chapter 5 - Continued1. More Practice with SQL

1. Stored Procedures – SQL/PSM (General ISO Standard 9075-4:1996 – Persistent Stored Modules)

– PL/SQL (Oracle only) Close to SQL/PSM, – MySQL Stored Procedures for PRClab also Similar (Stored Programs

and Views, Defining, Using)

2. Trigger Basics (MySQL Ref Manual 19.3) - We will use more in Part-2, but understand concepts (covered in Chapter 6)

3. Study Nested Queries and Multi-table

4. Understand ALTER, but also use CREATE and DROP

Sam Siewert 14

Page 15: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Notes - Stored ProgramsPL/SQL from CB Ref. does not work on MySQL on PRClab– You will not be held accountable for syntax and semantics, just

awareness of Stored Programs and Triggers (covered in more depth in Part-2)

– Stored Programs in MySQL are useful for project DB work

I will Provide Simple MySQL Stored Program Examples Today

Sam Siewert 15

Page 16: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

The SQL Programming LanguageImpedance mismatch– Mixing different programming paradigms– SQL is a declarative language– High-level language such as C is a procedural language– SQL and 3GLs use different models to represent data

“Impedance is the opposition by a system to the flow of energy from a source.” [wikipedia] “opposition to blood flow in the circulatory system” [webster, 3rd definition]

By mismatch, this implies that some of the power of SQL is limited by the Java or C++ embedding and some of the power of Java or C++ is perhaps limited by SQL (they were not designed to be combined – not orthogonal, not at same level) – E.g. assigning a variable or object to contain tuples returned from select.

[Idea - instead of embedding SQL in say C/C++ or Java, rather build in Procedural Extensions to SQL that can be Stored, Like any Other Data]

16

Page 17: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

MySQL SQL/PSM The “;” is default delimiter, so we need to redefine to enter a stored procedure on the command line

Select is like printfin MySQL

Helloworld is in table proc

Sam Siewert 17

use mysql;

delimiter $$

drop procedure if exists helloworld$$

create procedure helloworld()Begin

select ‘hello world’;end$$

delimiter ;

Page 18: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Simple MySQL Stored Procedure Example

Classic C/C++ Hello World is Entered into mysql schema in the proc table (relation)It is Stored like any other tupleAny number of procedures using SQL/PSM can be storedThey are Invoked with Call

Sam Siewert 18

Page 19: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Where are SQL Stored Procedures?Don’t worry about “sql_mode”Advantages are:1. Security - on

Server rather than Client

2. Managed like any other Data in DB in the “mysql” DB, in table “proc”

3. Less data to marshal back and forth between a Client and the DBMS Server

4. Privileges managed

Sam Siewert 19

Page 20: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Examine mysql DB and tablesSQL Procedures are Stored at Tuples in “proc”

Sam Siewert 20

Page 21: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

MySQL PSM ExampleNote SET

REPEAT

= for Assignment, but := OK too

Does Not Agree with PL/SQL, but Equivalent Capability

Sam Siewert 21

Page 22: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Triggers - Cover in Chap 6, Part-2Trigger – Defines an action that the database should take when some event

occurs in the application– Based on Event-Condition-Action (ECA) model

Types– Row-level– Statement-level

Event: INSERT, UPDATE or DELETETiming: BEFORE, AFTER or INSTEAD OFAdvantages and disadvantages of triggers

22

Page 23: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

MySQL Trigger ExampleINSERT Trigger to Sum Debit/Credit for all Accounts

Sam Siewert 23

Page 24: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

MySQL Trigger ExampleUPDATE Trigger to Range Limit Data 0…100– Can’t use CALL in Trigger– Can’t begin or end a transaction

Sam Siewert 24

Page 25: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Triggers – AdvantagesElimination of redundant codeSimplifying modificationsIncreased securityImproved integrityImproved processing powerGood fit with client-server architecture

25

Page 26: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Triggers – DisadvantagesPerformance overheadCascading effectsCannot be scheduledLess portable

26

Page 27: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

DiscussionGoal of RDBMS is Minimal Redundant Data– Foreign Keys are Ideally the Only Redundant Data– Used for Hierarchical Relations– Goal of Normalization is to Eliminate Redundancy (Chapter 14)

Insertion AnomaliesDeletion AnomaliesModification Anomalies

Recall E.F. Codd’s Concerns with Redundancy

So, Not Just Efficiency Issues

OOA/OOD and OOP Have Redundancy via Inheritance –Refinement of Classes and Instantiation

RDBMS minimizes redundancy via PK to FK hierarchy in relations

Sam Siewert 27

Page 28: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

Connolly-Begg Chapter 7

SQL RDBMS Practice(Data Types & Schema Level

Commands)

Sam Siewert

28

Page 29: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

DreamHome SchemaCB Ref. Page 112, Figure 4.3

CB Ref. Chapter 7, Problem 7.21, 7.22

Create table(s) for schema loaded on Bb with tab delimited data

Can be Used to Verify Examples in CB Ref. Chapter 5 & 6

Sam Siewert 29

Page 30: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

CB Ref - ExamplesDream Home v. 1.0 -http://mercury.pr.erau.edu/~siewerts/cs317/code/Dreamhome-db-v1.0/

CB Ref. has good RA and TRC Examples as well as Dream Home DDL and DML examples

Practice on SQL Friday(s) in class - E.g. Dream Home

Found also in Dream Home Exercise and Notes from Week 6 - Lecture-Week-6-2

Sam Siewert 30

Page 31: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

DBS - Jukic et al. Chapter 5

SQL RDBMS Practice(Data Types & Schema Level

Commands)

Sam Siewert

31

Page 32: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

DBS Book SQL ExamplesPart 1 - Basic Core SQL and ZAGI Example– DCL is what we call DBA Commands– TCL we will cover in Part-2 of the Course– Students can DROP tables and views, but not whole databases– Single Table SELECT with predicates– Ordering and Aggregate Functions

Part 2 - Advanced SQL and HAFH Example– Nested Queries vs. Multi-Table (Theta-JOIN or Equi-JOIN)

Note key-word JOIN not used for theseEqui-join is a special case of Theta-joinNatural join is special case of Equi-join over common attributesMySQL Manual on JOIN, Join Types TutorialViews are stored Queries (rather than storing duplicate data)SQL Implementation Differences (MySQL, MS SQL, Oracle Enterprise, SQLite, etc.)

Sam Siewert 32

Page 33: CS317 File and Database Systemsmercury.pr.erau.edu/.../Lectures/Fall-2017/Lecture-Week-5-1-grayscal… · Completion of SQL in DBS Chapter 5 [Ref: Connolly-Begg Chapters 7 & 8] Part-1

DBS ExamplesHAFH and ZAGI Examples

Schema IM and DDL provided

Many query examples - DBS Chapter 5 Notes

View examples - Stored Queries

Schema update commands to alter tables, insert data, etc.

Practice with DreamHome (CB Ref.) and with DBS HAFH and ZAGI to Learn

Bring Questions to Class!

Sam Siewert 33