CS317 File and Database Systems

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

Lecture 5 – More SQL and Intro to Stored Procedures

September 24, 2017  Sam Siewert SQL Theory and Standards

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

 Sam Siewert

2 Mid-term Survey Results

 Sam Siewert 3 Bottom 4 Materials 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

 Sam Siewert 4 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 - , 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 – <-> 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 MySQL on DDL vs. DML

DDL DML

Query TCL

DBA [DCL] https://dev.mysql.com/doc/refman/5.7/en/sql-syntax.html  Sam Siewert 6 Reading Advice

Read 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- 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 For Discussion… SQL is An Evolving ISO Standard – SQL:2016 1. What We’ve Learned So Far is SQL:1992 – Core SQL 2. Latest SQL Standards are SQL:2011 and SQL:2016 3. 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 Overview of Example DBs

DreamHome - 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 Schema Updates Requires Table “Alter”, Creation of new Tables, Migration of Data Possible Good Use of Nested Query in Insert - Replicated Data to Migrate, Drop Old Table https://dev.mysql.com/doc/refman/5.7/en/insert- .html

 Sam Siewert 10 SQL Data Types (Domains) - DDL

https://dev.mysql.com/doc/refman/5.7/en/data-types.html

Lowest Level of Data integrity, validation, veracity

 Sam Siewert 11 ISO SQL Data Types

Pearson Education © 2014 12 Views

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

CREATE VIEW MyStaff AS SELECT * FROM Staff WHERE branchNo=’B003’; select * MyStaff; DROP VIEW MyStaff;  Sam Siewert 13 Chapter 5 - Continued

1. 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 Notes - Stored Programs

PL/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 The SQL Programming Language

Impedance 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 MySQL SQL/PSM

The “;” is default use mysql;

delimiter, so we delimiter $$ need to redefine to enter a stored drop procedure if exists helloworld$$

procedure on the create procedure helloworld() command line Begin select ‘hello world’; end$$ Select is like printf in MySQL delimiter ;

Helloworld is in table proc

 Sam Siewert 17 Simple MySQL Stored Procedure Example Classic C/C++ Hello World is Entered into mysql schema in the proc table () It is Stored like any other tuple Any number of procedures using SQL/PSM can be stored They are Invoked with Call

 Sam Siewert 18 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 Examine mysql DB and tables

SQL Procedures are Stored at Tuples in “proc”

 Sam Siewert 20 MySQL PSM Example

Note SET

REPEAT

= for Assignment, but := OK too

Does Not Agree with PL/SQL, but Equivalent Capability

 Sam Siewert 21 Triggers - Cover in Chap 6, Part-2

Trigger – 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 DELETE Timing: BEFORE, AFTER or INSTEAD OF Advantages and disadvantages of triggers

22 MySQL Trigger Example

INSERT Trigger to Sum Debit/Credit for all Accounts

 Sam Siewert 23 MySQL Trigger Example

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

 Sam Siewert 24 Triggers – Advantages

Elimination of redundant code Simplifying modifications Increased security Improved integrity Improved processing power Good fit with client-server architecture

25 Triggers – Disadvantages

Performance overhead Cascading effects Cannot be scheduled Less portable

26 Discussion Goal 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 Anomalies Deletion Anomalies Modification 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 Connolly-Begg Chapter 7

SQL RDBMS Practice (Data Types & Schema Level Commands)

 Sam Siewert

28 DreamHome Schema CB 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 CB Ref - Examples

Dream Home v. 1.0 - http://mercury.pr.erau.edu/~siewerts/cs317/code/Dreamh ome-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 DBS - Jukic et al. Chapter 5

SQL RDBMS Practice (Data Types & Schema Level Commands)

 Sam Siewert

31 DBS Book SQL Examples

Part 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 these Equi- is a special case of Theta-join Natural join is special case of Equi-join over common attributes MySQL Manual on JOIN, Join Types Tutorial Views are stored Queries (rather than storing duplicate data) SQL Implementation Differences (MySQL, MS SQL, Oracle Enterprise, SQLite, etc.)

 Sam Siewert 32 DBS Examples

HAFH and ZAGI Examples

Schema IM and DDL provided

Many query examples - DBS Chapter 5 Notes

View examples - Stored Queries

Schema commands to alter tables, data, etc.

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

Bring Questions to Class!

 Sam Siewert 33