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

Similar documents
Advanced SQL. Nov 21, CS445 Pacific University 1

Principles of Data Management

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

Relational Database Management Systems for Epidemiologists: SQL Part II

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

1) Introduction to SQL

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

MySQL 5.0 Certification Study Guide

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

GridDB Advanced Edition SQL reference

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

Sql Server Syllabus. Overview

Databases (MariaDB/MySQL) CS401, Fall 2015

SQL CSCI 201 Principles of Software Development

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Module 3 MySQL Database. Database Management System

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

T-SQL Training: T-SQL for SQL Server for Developers

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

CSE 344 Introduction to Data Management. Section 2: More SQL

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #17: MySQL Gets Done

SQL functions fit into two broad categories: Data definition language Data manipulation language

MariaDB Crash Course. A Addison-Wesley. Ben Forta. Upper Saddle River, NJ Boston. Indianapolis. Singapore Mexico City. Cape Town Sydney.

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

CS2300: File Structures and Introduction to Database Systems

Oracle Syllabus Course code-r10605 SQL

CS348 INFORMATION SYSTEMS

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

Chapter 11 How to create databases, tables, and indexes

CGS 3066: Spring 2017 SQL Reference

Data and Tables. Bok, Jong Soon

3/3/2008. Announcements. A Table with a View (continued) Fields (Attributes) and Primary Keys. Video. Keys Primary & Foreign Primary/Foreign Key

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

CSC Web Programming. Introduction to SQL

Querying Data with Transact-SQL

CS 327E Class 3. February 11, 2019

Database design process

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

E-R diagrams and database schemas. Functional dependencies. Definition (tuple, attribute, value). A tuple has the form

Overview Relational data model

What s New in MariaDB Server Max Mether VP Server

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

Course Outline and Objectives: Database Programming with SQL

Exact Numeric Data Types

CSCU9Q5 Introduction to MySQL. Data Definition & Manipulation (Over ~two Lectures)

Indexing & Storage Engines

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Unit Assessment Guide

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Part 1: IoT demo Part 2: MySQL, JSON and Flexible storage

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

SQL Commands & Mongo DB New Syllabus

MySQL Database Administrator Training NIIT, Gurgaon India 31 August-10 September 2015

OKC MySQL Users Group

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

MySQL Workshop. Scott D. Anderson

Operating systems fundamentals - B07

Databases CSCI 201 Principles of Software Development

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

MySQL by Examples for Beginners

basic db architectures & layouts

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

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

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

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

II (The Sequel) We will use the following database as an example throughout this lab, found in students.db.

Model Question Paper. Credits: 4 Marks: 140

Create a simple database with MySQL

MySQL Workshop. Scott D. Anderson

Chapter 3 Introduction to relational databases and MySQL

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

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

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

Integrity constraints, relationships. CS634 Lecture 2

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Covering indexes. Stéphane Combaudon - SQLI

Course Outline. MySQL Database Administration & Design. Course Description: Pre-requisites: Course Content:

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

Ref1 for STUDENT RECORD DB: Ref2 for COMPANY DB:

Simple Quesries in SQL & Table Creation and Data Manipulation

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)

Database. Quiz with Explainations. Hans-Petter Halvorsen, M.Sc.

Introduction to Databases. MySQL Syntax Guide: Day 1

Chapter 3: Introduction to SQL

Database Technology. Topic 3: SQL. Olaf Hartig.

Provider: MySQLAB Web page:

SQL Interview Questions

MySQL: an application

CS127 Homework #3. Due: October 11th, :59 P.M. Consider the following set of functional dependencies, F, for the schema R(A, B, C, D, E)

MySQL Creating a Database Lecture 3

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

MySQL 8.0 What s New in the Optimizer

origin destination duration New York London 415 Shanghai Paris 760 Istanbul Tokyo 700 New York Paris 435 Moscow Paris 245 Lima New York 455

Introduction to Computer Science and Business

Lecture 6 - More SQL

Transcription:

Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3! http://dev.mysql.com/doc/refman/ https://mariadb.com/kb/en/the-mariadb-library/documentation/ MySQL / MariaDB 1

College Database E-R Diagram 2

Database tasks https://mariadb.com/kb/en/the-mariadb-library/documentation start MariaDB setup user passwords shutdown MariaDB create database create table primary key index foreign key insert data source a file delete data drop query data where join group order subquery 3

MariaDB ssh to db.cs.pacificu.edu (64.59.233.245) ssh -X db.cs.pacificu.edu [you@db ~]# $ mysql -u PUNetID -p mysql> set password = PASSWORD('NEWPASSWORD'); mysql> show databases; mysql> use PUNetID_test; 4

Data types https://mariadb.com/kb/en/the-mariadb-library/data-types/ TINYINT/SMALLINT/INT/BIGINT SIGNED/UNSIGNED BIT FLOAT/DOUBLE BOOLEAN CHAR / BINARY VARCHAR(###) / VARBINARY(###) DATE / TIME / DATETIME / TIMESTAMP [TINY MEDIUM LONG]TEXT [TINY MEDIUM LONG]BLOB ENUM SET JSON 5

M M indicates the maximum display width for integer types. The maximum display width is 255. Display width is unrelated to the range of values a type can contain,. For floating-point and fixed-point types, M is the total number of digits that can be stored. https://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html 6

Strings Character Set Collation set of characters (ASCII, UTF8,... the order of the characters within the set MariaDB [will4614_test]> show variables like 'character_set_server'; +----------------------+-------+ Variable_name Value +----------------------+-------+ character_set_server utf8 +----------------------+-------+ 1 row in set (0.00 sec) MariaDB [will4614_test]> show variables like 'collation_server'; +------------------+-----------------+ Variable_name Value +------------------+-----------------+ collation_server utf8_general_ci +------------------+-----------------+ 1 row in set (0.01 sec) https://mariadb.com/kb/en/mariadb/supported-character-sets-and-collations/ https://mariadb.com/kb/en/mariadb/character-set-and-collation-overview/ 7

CREATE TABLE People ( Create Table PersonID INT NOT NULL AUTO_INCREMENT, FName VARCHAR(50), LName VARCHAR(50), Login VARCHAR(20) NOT NULL, CONSTRAINT People_PersonID_PK PRIMARY KEY (PersonID), CONSTRAINT People_Login_U UNIQUE (Login) ) Engine=InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin; 8

Create a Table CREATE TABLE People ( PersonID INT NOT NULL AUTO_INCREMENT, FName VARCHAR(50), LName VARCHAR(50), Login VARCHAR(20) NOT NULL, CONSTRAINT People_PersonID_PK PRIMARY KEY (PersonID), CONSTRAINT People_Login_U UNIQUE (Login) ) Engine=InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin; mysql> show tables; mysql> show create table People; 9

Insert One Row INSERT INTO People ( FName, LName, Login) VALUES ( "Chadd", "Williams","chadd"); INSERT INTO tablename ( ColumnName, ) VALUES ( data, ); 10

Insert INSERT INTO People ( FName, LName, Login) VALUES ( "Chadd", "Williams","chadd"); INSERT INTO People ( FName, LName, Login) VALUES ( "Doug", "Ryan","ryand"); INSERT INTO People ( FName, LName, Login) VALUES ("Shereen", "Khoja","shereen"); INSERT INTO People ( FName, LName, Login) VALUES ("Chris", "Lane", "lanec"); mysql> SELECT * FROM People; 11

Retrieve Data SELECT * FROM People WHERE PersonID > 2; SELECT column, FROM table WHERE condition ; 12

MySQL mysql> SELECT * FROM People WHERE PersonID > 2; mysql> SELECT * FROM People WHERE LName = Ryan ; mysql> SELECT * FROM People WHERE FName like C% ; mysql> SELECT * FROM People WHERE FName like c% ; mysql> SELECT FName, LName FROM People WHERE PersonID > 1; 13

Create another table Create a table CREATE TABLE Professors ( ProfID INT NOT NULL, Rank ENUM ('Assistant', 'Associate', 'Full', 'Emeritus') NOT NULL, ) Engine=InnoDB; CONSTRAINT Professors_ProfID_PK PRIMARY KEY (ProfID) 14

Constraints mysql> ALTER TABLE Professors ADD CONSTRAINT Professors_ProfID_FK FOREIGN KEY (ProfID) REFERENCES People(PersonID) ON DELETE CASCADE; 15

MySQL Insert some data INSERT INTO Professors (ProfID, Rank) VALUES (1, 'Associate'); -- chadd INSERT INTO Professors (ProfID, Rank) VALUES (2, 'Full'); -- doug INSERT INTO Professors (ProfID, Rank) VALUES (3, 'Associate'); -- shereen INSERT INTO Professors (ProfID, Rank) VALUES (4, 'The Boss'); -- chris 16

Let's make this go faster Load data from a SQL script This file is full of INSERT and CREATE statements. mysql> source /tmp/createdatabase.sql; Let's look at that file. 17

Deleting Data Let's delete some data mysql> SELECT * FROM People; mysql> SELECT * FROM CurrentlyTeaching; mysql> DELETE FROM People WHERE PersonID=1; mysql> SELECT * FROM People; mysql> SELECT * FROM CurrentlyTeaching; mysql> SHOW TABLES; mysql> DROP TABLE People; mysql> source /tmp/createdatabase.sql; 18

dbeaver Let s use a GUI to make this easier./dbeaver alias 19

Queries What Courses have a MaxSize of greater than 5? mysql> SELECT * FROM Courses WHERE MaxSize > 5; 20

Let's sort the output Order By mysql> SELECT * FROM Courses ORDER BY MaxSize; mysql> SELECT * FROM Courses ORDER BY MaxSize DESC ; mysql> SELECT * FROM People ORDER BY LName, FName; 21

Aggregate selected rows Group By mysql> SELECT LName FROM People ; mysql> SELECT LName, COUNT(*) FROM People GROUP BY LName; mysql> SELECT AVG(MaxSize) FROM Courses; mysql> SELECT AVG(Grade) FROM CurrentlyEnrolled GROUP BY CourseID; Other useful functions: AVG(), STDDEV(), MAX(), SUM() 22

Let's Query the Data List all the Full professors in our database (ProfID). List every student with a GPA less than 1.0 (StudentID) 23

Joins List all the Full professors in our database (FName, LName). mysql> SELECT FName, LName FROM People, Professors WHERE People.PersonID=Professors.ProfID AND Rank= Full ; List every student with a GPA less than 1.0 (StudentID, FName, LName) 24

Joins Inner Join matching records in each table SELECT * FROM People, Students WHERE (People.PersonID=Students.StudentID); Outer Join all records in each table (maybe not matching) may produce NULL values for some columns SELECT * FROM People LEFT JOIN Students ON (People.PersonID=Students.StudentID); Produce all rows from the LEFT table RIGHT JOIN Produce all rows from the RIGHT table; 25

Let's Query the Data List all the courses in our database taught by an Associate professor. (CourseID) (CourseID, Title) How many courses are taught by each rank of professor? How many professors teach zero courses? 26

Joins Four table joins, show all courses taught by Associate Profs (Title, FName, LName) mysql> SELECT * FROM Courses, CurrentlyTeaching, Professors, People WHERE and Rank = Associate ; 27

Joins A join looks a one row at a time Some queries need more information Who is in a class with Bart Simpson? 28

Subquery Who was in class with Bart Simpson? Let s find the each course each person is in Let s find all the courses Bart Simpson is in Let s combine these two queries The goal of a subquery is to send data into or out of a subquery and filter using that data. 29

Subquery Who was in class with Bart Simpson? Let s find the each course each person is in exclude Bart Simpson SELECT People.*, CurrentlyEnrolled.* FROM Students, CurrentlyEnrolled, People WHERE (Students.StudentID=CurrentlyEnrolled.StudentID) AND People.PersonID = Students.StudentID AND (FName!= "Bart" or LName!= "Simpson") order by PersonID; Which courses was Bart enrolled in? 30

Subquery Which courses was Bart enrolled in? SELECT * FROM CurrentlyEnrolled AS BSClass WHERE BSClass.StudentID=5 -- Bart Simpson; Let s only return rows from the previous query where the CourseID matches a row in the above query. 31

Subquery SELECT People.*, CurrentlyEnrolled.* FROM Students, CurrentlyEnrolled, People WHERE (Students.StudentID=CurrentlyEnrolled.StudentID) AND People.PersonID = Students.StudentID AND (FName!= "Bart" or LName!= "Simpson") AND EXISTS ( SELECT * FROM CurrentlyEnrolled AS BSClass WHERE (CurrentlyEnrolled.CourseID=BSClass.CourseID) AND BSClass.StudentID=5 -- Bart Simpson; ) order by PersonID; 32

Subquries Who has the maximum grade in each class? (Fname, Lname, grade, class name) Does this require a subquery? 33

Views A View is a logical table backed up by a query Changes automatically when the results of the query change mysql> 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% ; 34

Views mysql> SELECT * FROM CS150_VW; mysql> DELETE FROM People WHERE PersonID=5; mysql> SELECT * FROM CS150_VW Order by Grade; mysql> DROP VIEW CS150_VW; 35

Exercise Rebuild CS150_VW Determine how closely a student's grade in CS150 matches their GPA. (1.0 = perfect match, 0.5 = Grade is half the GPA, 1.5 Grade is 50% better than GPA) GPA goes from 0.0 to 4.0, Grade goes from 0.0 to 100.0 36

Control Flow IF( condition, truevalue, falsevalue) SELECT Title, IF( MaxSize > 50, 1, 0) FROM Courses; IFNULL(Value, returnifvalueisnull) SELECT IFNULL(Title, ITISNULL ) FROM Courses; There is also a case (switch) statement 37

GROUP BY and HAVING Allows SQL to filter on calculated/aggregate values Similar to WHERE must be last SELECT StudentID, avg(grade) as AvgGrade, count(*) as NumberRows FROM CurrentlyEnrolled WHERE Grade > 20 GROUP BY StudentID HAVING AvgGrade > 80 and NumberRows > 1; 38

Limit only show some results SELECT StudentID, count(*) as Total FROM WasIn GROUP BY StudentID HAVING Total > 1 LIMIT 2; -- show only first two rows LIMIT 2,4; -- skip the first two rows, then show the next 4 LIMIT 3; is equivalent to LIMIT 0, 3 39

Backup Your Database! @db:~> mysqldump PUNetID_test -u PUNetID -p > backup_test.sql Database Name Output file To see what this file looks like: @db:~> cat backup_test.sql less Copy to Zeus for safe keeping! @db:~> scp backup_test.sql PUNetID@zeus: Don't forget the colon! 40

Practice List all Course titles and CourseID. For each Course, display the CourseID if Chadd teaches it and Not A Chadd Course otherwise. Find all courses whose maximum and minimum grade is at least 50 points different. Display each student name, course title, and student's grade in that course and the string passing or not passing if the student is not passing the course. Find each course that does not contain a Simpson. Display all students whose grade for a class is above the average grade for that class. 41

Explain mysql> SHOW CREATE TABLE People; mysql> SHOW CREATE TABLE CurrentlyTeaching; mysql> EXPLAIN SELECT * FROM People, CurrentlyTeaching WHERE (PersonID=ProfID); mysql> EXPLAIN SELECT * FROM People, CurrentlyTeaching WHERE (PersonID=ProfID) AND FName like '%a%'; EXPLAIN TYPE: system, const, eq_ref, ref, index, all ROWS: number of rows scanned 42

mysql> USE chadd_test; Indexes mysql> SHOW TABLE STATUS LIKE 'EnronVocab'; mysql> SHOW TABLE STATUS LIKE 'EnronWordCount'; mysql> SHOW CREATE TABLE EnronVocab; mysql> SHOW CREATE TABLE EnronWordCount; mysql> SHOW PROCESSLIST; mysql> EXPLAIN SELECT WordCount FROM EnronWordCount WHERE DocID =??; mysql> EXPLAIN SELECT WordCount FROM EnronWordCount WHERE WordID =??; 1 to 39861 DocID 1 to 28102 WordID http://archive.ics.uci.edu/ml/datasets/bag+of+words 43

Practice How many students are in each class? For each class, what was the min, max, average grade? do this with and without using the AVG() function. Who took a class with Bart Simpson and received a higer grade than Bart? Lower Grade? 44

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 45

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_*) 46

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 Cannot stop an insert! 47

Trigger CREATE TRIGGER name BEFORE INSERT ON table FOR EACH ROW BEGIN ; SIGNAL SQLSTATE '99991' SET MESSAGE_TEXT = 'ERROR MESSAGE'; END DROP TRIGGER name; 48

Stored Procedures 49

Control Flow CASE IF() IFNULL() NULLIF() 50

Control Flow Functions Advanced SQL http://dev.mysql.com/doc/refman/5.5/en/control-flowfunctions.html Trigger http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html http://dev.mysql.com/doc/refman/5.5/en/select.html Having Limit load data Binary Data into outfile Stored Procedures http://dev.mysql.com/doc/refman/5.5/en/load-data.html 51