CS317 File and Database...

Post on 26-Aug-2021

5 views 0 download

transcript

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/

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

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

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

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

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

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

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

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

ISO SQL Data Types

Pearson Education © 2014 12

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;

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

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

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

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 ;

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

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 tablesSQL Procedures are Stored at Tuples in “proc”

Sam Siewert 20

MySQL PSM ExampleNote 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-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

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

Sam Siewert 23

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

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

25

Triggers – DisadvantagesPerformance overheadCascading effectsCannot be scheduledLess portable

26

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

Connolly-Begg Chapter 7

SQL RDBMS Practice(Data Types & Schema Level

Commands)

Sam Siewert

28

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

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

DBS - Jukic et al. Chapter 5

SQL RDBMS Practice(Data Types & Schema Level

Commands)

Sam Siewert

31

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

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