Advanced SQL. Nov 21, CS445 Pacific University 1

Similar documents
Introduction to MySQL /MariaDB and SQL Basics. Read Chapter 3!

Overview of MySQL Structure and Syntax [2]

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Data Definition Language with mysql. By Kautsar

Careerarm.com. 1. What is MySQL? MySQL is an open source DBMS which is built, supported and distributed by MySQL AB (now acquired by Oracle)

Data Types in MySQL CSCU9Q5. MySQL. Data Types. Consequences of Data Types. Common Data Types. Storage size Character String Date and Time.

TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.

Indexing & Storage Engines

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

The M in LAMP: MySQL CSCI 470: Web Science Keith Vertanen Copyright 2014

Lecture 7 Stored procedure

CMPT 354: Database System I. Lecture 2. Relational Model

SQL CSCI 201 Principles of Software Development

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

COMP283-Lecture 6 Applied Database Management

Databases CSCI 201 Principles of Software Development

CS143: Relational Model

Data and Tables. Bok, Jong Soon

MySQL Introduction. By Prof. B.A.Khivsara

Load Data Fast! BILL KARWIN PERCONA LIVE OPEN SOURCE DATABASE CONFERENCE 2017

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

EE 495M - Lecture 3. Overview

Basis Data Terapan. Yoannita

Chapter 8 How to work with data types

Percona XtraDB: Compressed Columns with Dictionaries an alternative to InnoDB table compression. Yura Sorokin, Senior Software Engineer at Percona

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Oracle 1Z MySQL 5.6 Developer.

DATABASE MANAGEMENT SYSTEMS

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

*this is a guide that was created from lecture videos and is used to help you gain an understanding of what is mysql used for.

Using MySQL on the Winthrop Linux Systems

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Database Systems. phpmyadmin Tutorial

Percona XtraDB: Compressed Columns with Dictionaries an alternative to InnoDB table compression. Yura Sorokin, Senior Software Engineer at Percona

Chapter 9: Working With Data

General References on SQL (structured query language) SQL tutorial.

Downloaded from

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

Acknowledgments About the Authors

Simple Quesries in SQL & Table Creation and Data Manipulation

Web accessible Databases PHP

Web Database Programming

Databases (MariaDB/MySQL) CS401, Fall 2015

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

Oracle SQL Developer. Supplementary Information for MySQL Migrations Release 2.1 E

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

Module 3 MySQL Database. Database Management System

1.8 Database and data Data Definition Language (DDL) and Data Manipulation Language (DML)

Tool/Web URL Features phpmyadmin. More on phpmyadmin under User Intefaces. MySQL Query Browser

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

SQL Commands & Mongo DB New Syllabus

Database Technology. Topic 6: Triggers and Stored Procedures

The Blackhole and Federated Storage Engines: The Coolest Kids on the Block

Relational databases and SQL

Lecture 5. Monday, September 15, 2014

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #23: SQLite

DB Programming. Database Systems

Create Basic Databases and Integrate with a Website Lesson 1

The Relational Model. Suan Lee

3.1. Keys: Super Key, Candidate Key, Primary Key, Alternate Key, Foreign Key

MySQL. A practical introduction to database design

Unit 27 Web Server Scripting Extended Diploma in ICT

Why Choose Percona Server For MySQL? Tyler Duzan

CS2300: File Structures and Introduction to Database Systems

varchar(30), Primary Key (name))engine=innodb; bigint not null auto_increment, Primary Key (accountid))engine=innodb;

Principles of Data Management

COMP519: Web Programming Autumn 2015

Database Management Systems. Chapter 5

Sheeri Cabral. Are You Getting the Best Out of Your MySQL Indexes? Slides

STORED PROCEDURE AND TRIGGERS

COMP 430 Intro. to Database Systems. Encapsulating SQL code

What s New in MariaDB Server Max Mether VP Server

Stored procedures - what is it?

SQL (and MySQL) Useful things I have learnt, borrowed and stolen

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

Database: Introduction

EE221 Databases Practicals Manual

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

CSC 3300 Homework 3 Security & Languages

Chapter 5: Physical Database Design. Designing Physical Files

MariaDB 10.3 vs MySQL 8.0. Tyler Duzan, Product Manager Percona

MySQL Cluster An Introduction

Mysql Information Schema Update Time Null >>>CLICK HERE<<< doctrine:schema:update --dump-sql ALTER TABLE categorie

Introduction. Who wants to study databases?

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

How to use SQL to create a database

<Insert Picture Here> Boosting performance with MySQL partitions


Dealing with schema changes on large data volumes. Danil Zburivsky MySQL DBA, Team Lead

CS 327E Lecture 5. Shirley Cohen. February 8, 2016

Database Management Systems. Chapter 5

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

sqoop Automatic database import Aaron Kimball Cloudera Inc. June 18, 2009

Database Systems Fundamentals

Transcription:

Advanced SQL Nov 21, 2017 http://zeus.cs.pacificu.edu/chadd/cs445f17/advancedsql.tar.gz Pacific University 1

Topics Views Triggers Stored Procedures Control Flow if / case Binary Data Pacific University 2

College Database E-R Diagram Pacific University 3

Views A View is a logical table backed up by a query Changes automatically when the results of the query change CREATE VIEW CS150_VW AS SELECT LName, FName, Grade, StudentID FROM Courses, CurrentlyEnrolled, People WHERE Courses.CourseID= CurrentlyEnrolled.CourseID and People.PersonID=StudentID and Title like CS150% ; Pacific University 4

Views SELECT * FROM CS150_VW; DELETE FROM People WHERE PersonID=5; SELECT * FROM CS150_VW Order by Grade; DROP VIEW CS150_VW; Pacific University 5

Rebuild CS150_VW Exercise Determine how closely a student's grade in CS150 matches their GPA. (1.0 = perfect match, 0.5 = 150 Grade is half the GPA, 1.5 150 Grade is 50% better than GPA) GPA goes from 0.0 to 4.0, Grade goes from 0.0 to 100.0 You can do this without a View Pacific University 6

Triggers CREATE TRIGGER name BEFORE INSERT ON table FOR EACH ROW BEGIN -- SQL Statements or control flow (IF) INSERT INTO test2 SET a2 = NEW.a1; END ; The row being inserted BEFORE AFTER INSERT DELETE UPDATE https://mariadb.com/kb/en/library/trigger-overview/ Pacific University 7

Example (triggerexample.sql) CREATE TABLE user ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name CHAR(20), last_name CHAR(20), email CHAR(100) ) ENGINE = MyISAM; https://mariadb.com/kb/en/library/trigger-overview/ DELIMITER // CREATE TRIGGER validateemail BEFORE INSERT ON user FOR EACH ROW BEGIN IF NEW.email NOT LIKE '_%@_%. %' THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Email field is not valid'; END IF; END; // DELIMITER ; INSERT INTO user (first_name, last_name, email) VALUES ('John', 'Doe', 'john_doe.example.net'); ERROR 1644 (45000): Email field is not valid SELECT * FROM user; Empty set (0.00 sec Pacific University 8

// delete trigger to enforce bold line see boldlinetriggerexample.sql Pacific University 9

Stored Procedures see StoredProcedureExample.sql https://mariadb.com/kb/en/library/create-procedure/ https://mariadb.com/kb/en/library/stored-routines/ Pacific University 10

Control Flow IF( condition, truevalue, falsevalue) SELECT ActorID, IF( salary>1000, 1, 0) FROM WasIn; IFNULL(Value, returnifvalueisnull) SELECT ActorID, IFNULL(StageName, NONAME ) FROM WasIn; There is also a case (switch) statement Pacific University 11

Blob: Binary Data BLOB Binary data can be images, sounds, video, OpenOffice documents, executable files, etc. DataType TINYBLOB, TINYTEXT Max Size (Bytes) 2 8 BLOB, TEXT 2 16 MEDIUMBLOB, MEDIUMTEXT 2 24 TEXT Long chunk of text LONGBLOB, LONGTEXT 2 32 Pacific University 12

Binary Data CREATE TABLE pictures ( PicID int(11) NOT NULL auto_increment, image mediumblob NOT NULL, type varchar(255) NOT NULL, PRIMARY KEY (`PicID`)) ENGINE=InnoDB; For binary data, we need to track the type of data we have stored. For data to display on the web, store the MIME type Multipurpose Internet Mail Extensions image/gif image/png Pacific University 13

INSERT Binary Data INSERT INTO pictures (image, type) VALUES (LOAD_FILE('/tmp/g0/image.png'), "mime/png"); From the command line you must specify the entire path to the file The file must be world readable chadd@db:~> mkdir /tmp/g0 chadd@db:~> chmod o+r /tmp/g0/image.png chadd@db:~> ls -al /tmp/g0/image.png -rw-r--r-- /tmp/g0/image.png This INSERT can be run via dbeaver IF the file is already in place on the db machine! Pacific University 14

INTO OUTFILE Save a query to a text file SELECT StudenID, count(*) as Total FROM CurrentlyEnrolled GROUP BY StudentID HAVING Total > 1 INTO OUTFILE '/tmp/punetid.txt'; -- writes data on the server gray> scp /tmp/punetid.txt c@zeus: mysql -u user -p -D database -e select... > outfile Pacific University 15

LOAD DATA INFILE mysql> source /tmp/createtest.sql; mysql> ALTER TABLE test DISABLE KEYS; mysql> SET FOREIGN_KEY_CHECKS=0; mysql> LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test COLUMNS TERMINATED BY ','; mysql> SET FOREIGN_KEY_CHECKS=1; mysql> ALTER TABLE test ENABLE KEYS; Query OK, 69679427 rows affected (21 min 34.26 sec) with a well tuned MySQL (innodb_buffer_pool_size, innodb_log_*) Pacific University 16