CS317 File and Database Systems

Similar documents
Chapter 6. SQL Data Manipulation

STRUCTURED QUERY LANGUAGE (SQL)

CS317 File and Database Systems

CS317 File and Database Systems

Chapter 6. SQL: SubQueries

Chapter 5. Relational Algebra and Relational Calculus

CMP-3440 Database Systems

Relational Algebra and Relational Calculus. Pearson Education Limited 1995,

Lecture 5 Data Definition Language (DDL)

CS317 File and Database Systems

Lecture 03. Spring 2018 Borough of Manhattan Community College

Lecture 03. Fall 2017 Borough of Manhattan Community College

Lecture 6 Structured Query Language (SQL)

3ISY402 DATABASE SYSTEMS

CS317 File and Database Systems

CMP-3440 Database Systems

Cheltenham Courseware Microsoft Access 2003 Manual - Advanced Level SAMPLE

Objective. The goal is to review material covered in Chapters 1-5. Do the following questions from the book.

Standard Query Language. SQL: Data Definition Transparencies

Lecture 06. Fall 2018 Borough of Manhattan Community College

DB Creation with SQL DDL


Example 1 - Create Horizontal View. Example 2 - Create Vertical View. Views. Views

Institute of Southern Punjab, Multan

Lecture 07. Spring 2018 Borough of Manhattan Community College

Database Technologies. Madalina CROITORU IUT Montpellier

Database Systems. A Practical Approach to Design, Implementation, and Management. Database Systems. Thomas Connolly Carolyn Begg

Transforming ER to Relational Schema

CMP-3440 Database Systems

COMP102: Introduction to Databases, 5 & 6

1) Introduction to SQL

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

SQL DDL. Intro SQL CREATE TABLE ALTER TABLE Data types Service-based database in Visual Studio Database in PHPMyAdmin

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

RELATIONAL DATA MODEL

The SQL data-definition language (DDL) allows defining :

Physical Database Design

Chapter 3: Introduction to SQL

Querying Data with Transact SQL

Chapter 3: Introduction to SQL

Relational Data Model ( 관계형데이터모델 )

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

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

Database Management Systems,

CS317 File and Database Systems

ADVANCED QUERY AND VIEWS

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

2.2.2.Relational Database concept

CS 582 Database Management Systems II

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

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

MTA Database Administrator Fundamentals Course

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

Principles of Data Management

SQL STRUCTURED QUERY LANGUAGE

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

Midterm Review. Winter Lecture 13

UFCEKG 20 2 : Data, Schemas and Applications

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

COMP102: Introduction to Databases, 4

Query Processing Transparencies. Pearson Education Limited 1995, 2005

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Database design process

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

SQL Data Query Language

CS317 File and Database Systems

Advanced SQL Tribal Data Workshop Joe Nowinski

Sql Server Syllabus. Overview

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

Intermediate SQL: Aggregated Data, Joins and Set Operators

Basant Group of Institution

Mahathma Gandhi University

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

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

QQ Group

Conceptual Database Design

DB2 SQL Class Outline

Lecture 6 - More SQL

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

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

CSIE30600 Database Systems Basic SQL 2. Outline

Mobile MOUSe MTA DATABASE ADMINISTRATOR FUNDAMENTALS ONLINE COURSE OUTLINE

SQL (Structured Query Language)

Fundamentals of Database Systems

Introduction to the Structured Query Language [ SQL ] (Significant Concepts)

Data about data is database Select correct option: True False Partially True None of the Above

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Lecture 05. Spring 2018 Borough of Manhattan Community College

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

CGS 3066: Spring 2017 SQL Reference

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

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

Database Technology. Topic 3: SQL. Olaf Hartig.

Querying Data with Transact-SQL

Transcription:

CS317 File and Database Systems Lecture 4 Intro to SQL (Chapter 6 - DML, Chapter 7 - DDL) September 17, 2018 Sam Siewert

Backup to PRClab1.erau.edu If PRClab1.erau.edu is down or slow Use SE Workstation - access notes SE Workstation Adminer Same accounts (not synch d, so use initial default password and match) Same databases (not mirrored, so reload schemas and data to replicate) Same Databases MariaDB is a fork of Oracle MySQL Documentation is here Knowledge-base Sam Siewert 2

Adminer is also Replicated Adminer can be accessed on the SE Workstation too SE Adminer Rep-Database (on campus) SE Adminer Rep-Database (Eagle and Res-net) Sam Siewert 3

Ave=80.8%, High=100 Quiz #1 Results Comments on Commonly Missed Questions Be Clear on DDL vs. DML (Definition vs. Manipulation) Tape Storage is not Dead! Still the lowest-cost method to assure data compliance (7 years of business data or more), at a cost of less than a Penny / gigabyte E.g. SpectraLogic, Quantum, IBM, Oracle (Sun/StorageTek) Used also for Digital Cinema and Animation (data backup) High Performance Computing, Archives (Internet Wayback, Stanford Digital Archives, NASA PDA) Any place with Terabytes to Petabytes or more of data Disk Drive Access is on the order of milliseconds [5 to 10 ms], so 100 to 200+ random I/O s per second (much higher performance for sequential access) Seek Time Rotational Delay ½ rotation on ave. at 7200 to at most 15K RPM Sam Siewert 4

MySQL on DDL vs. DML DDL DML Query TCL SOURCE file.sql DBA [DCL] https://dev.mysql.com/doc/refman/5.7/en/sql-syntax.html Sam Siewert 5

SQL Theory and Standards Intro to SQL Part-3 Sam Siewert 6

For Discussion SQL Does Not Adhere Strictly to Relational Model, Algebra or Calculus, but is Based Upon 1. Table Produced By SELECT Can Have Duplicate Rows 2. Ordering Imposed on Columns 3. User Can Order Rows in Result Tables 4. NULL is Allowed SQL is a FIPS (Federal Information Processing Standard) IBM SAA (System Applications Architecture) Specifies SQL ISO SQL:2011 Sam Siewert 7

A Little Help DDL Exists for Definition and Browsing of Schema (Table) Designs E.g. to view Attributes, Keys, Indexes Empty Table Schema show COLUMNS Schema show INDEX Sam Siewert 8

Or, Download *.mwb and Browse Schema Display mysql-workbench remotely and load DHv1_3.mwb [Transfer from Web to PRClab with MobaXterm] Drag and drop DHv1_3.mwb to PRClab folder Sam Siewert 9

Load DHv1_3.mwb and Browse Double Click on Table for Attributes Explore Relationships Key Information Non-Null Reqt s Sam Siewert 10

Goals SQL DML Basics Single Table SELECT Textbook: Page 149 to 163 Sub-queries for Multiple Tables Textbook: Page 164 to 167, 174 Multi-Table Queries or Join Textbook: Page 168 to 173 Must be Union Compatible (Union, Difference, Intersection) Combining Results Tables [Textbook: Page 175 to 176] Database Updates with INSERT, UPDATE, DELETE Textbook: Page 177 to 181 SQL Tutorials RA <-> SQL Sam Siewert 11

Parallel 1: Core SQL & Relational Algebra Table 5.1 #1 & #2 SELECT with PREDICATE and PROJECTION RA: Π sno, fname, lname, salary (σ salary > 10000 (Staff)) SQL: SELECT sno,fname,lname,salary from Staff WHERE salary > 10000; TC: {S.sno, S.fname, S.lname, S.salary Staff(S) ^ S.salary > 10000} Sam Siewert 12

Parallel 2: Core SQL & Relational Algebra Table 5.1 #1 & #2 SELECT with PREDICATE and PROJECTION RA: Π sno, fname, lname, salary (σ (position = Manager ) ^ (salary > 10000) (Staff)) SQL: SELECT sno,fname,lname,salary from Staff WHERE position = Manager AND salary > 10000; TC: {S.sno, S.fname, S.lname, S.salary Staff(S) ^ S.position = Manager ^ S.salary > 10000} Sam Siewert 13

Parallel 3: Core SQL & Relational Algebra 5.1 #1, #2 & #5 SELECT with PREDICATE and PROJECTION RA: Π sno, fname, lname (σ (city = Glasgow ^ Staff.sno = PropertyForRent.sno) (Staff X PropertyForRent)) SQL: SELECT DISTINCT s.sno,fname,lname FROM Staff s, Property_For_Rent pfr WHERE pfr.sno = s.sno AND pfr.city = Glasgow ; TC: {S.sno,S.fname,S.lname Staff(S) ^ (ƎP)(PropertyForRent(P) ^ (P.sno = S.sno) ^ P.city = Glasgow )} DISTINCT DISTINCT is a Convenient Predicate to Eliminate Duplicates Sam Siewert 14

Hotel Schema Example Create and Load in Your DB or Create New hoteldb create database hoteldb; use hoteldb; create table Hotel (hotelno int not null, hotelname varchar(32), city varchar(32)); create table Room (roomno int not null, hotelno int not null, type varchar(32), price int); create table Booking (hotelno int not null, guestno int not null, datefrom char(9), dateto char(9), roomno int); create table Guest (guestno int not null, guestname varchar(32), guestaddress varchar(32)); show tables; insert into Guest VALUES(1, 'Joe Biden', '22 Deer Rd'); insert into Guest VALUES(2, 'Bernie Sanders', '16 Argyll St'); insert into Guest VALUES(3, 'Donald Trump', '163 Main St'); insert into Hotel VALUES(1, 'Grossvenor', 'Boston'); insert into Hotel VALUES(2, 'Comfort Inn', 'Denver'); insert into Room VALUES(1, 1, 'Double Non-smoking', 195); insert into Room VALUES(2, 1, 'Single Non-smoking', 195); insert into Room VALUES(3, 1, 'Double Smoking', 395); insert into Room VALUES(4, 1, 'Single Non-smoking', 395); insert into Room VALUES(1, 2, 'Double Non-smoking', 195); insert into Room VALUES(2, 2, 'Single Non-smoking', 195); insert into Room VALUES(3, 2, 'Double Smoking', 395); insert into Room VALUES(4, 2, 'Single Non-smoking', 395); insert into Booking VALUES(1, 1, 'Oct-1-15', 'Oct-7-15', 1); insert into Booking VALUES(2, 2, 'Oct-2-15', 'Oct-7-15', 2); insert into Booking VALUES(2, 3, 'Oct-2-15', 'Oct-7-15', 3); Sam Siewert 15

Connolly-Begg on SQL [Reference Text - Highlights] SQL History and DML Basics Sam Siewert 16

Summary of Relational Algebra Page 132, Ch. 5, Connolly-Begg Recall 5 Necessary Operations Recall 3 that Require Unioncompatible Recall Operations Derived from 5 fundamental and extensions Sam Siewert 17

DreamHome Schema Page 112, Figure 4.3, Ch. 4, Connolly-Begg Create from Dream Home v. 1.0 Create table(s) for schema loaded on Canas with tab delimited data - dreamhome Can be Used to Verify Examples in notes here Sam Siewert 18

SQL - Objectives Purpose and importance of SQL. How to retrieve data from database using SELECT and: Use compound WHERE conditions. Sort query results using ORDER BY. Use aggregate functions. Group data using GROUP BY and HAVING. Use subqueries. Join tables together. Perform set operations (UNION, INTERSECT, EXCEPT). How to update database using INSERT, UPDATE, and DELETE. Pearson Education 2014 19

Objectives of SQL Consists of standard English words: 1) CREATE TABLE Staff(staffNo VARCHAR(5), lname VARCHAR(15), salary DECIMAL(7,2)); 2) INSERT INTO Staff VALUES ( SG16, Brown, 8300); 3) SELECT staffno, lname, salary FROM Staff WHERE salary > 10000; Pearson Education 2014 20

History of SQL In late 70s, ORACLE appeared and was probably first commercial RDBMS based on SQL. In 1987, ANSI and ISO published an initial standard for SQL. In 1989, ISO published an addendum that defined an Integrity Enhancement Feature. In 1992, first major revision to ISO standard occurred, referred to as SQL2 or SQL/92. In 1999, SQL:1999 was released with support for object-oriented data management. In late 2003, SQL:2003 was released. In summer 2008, SQL:2008 was released. In late 2011, SQL:2011 was released. Pearson Education 2014 21

SELECT Statement SELECT [DISTINCT ALL] {* [columnexpression [AS newname]] [,...] } FROM TableName [alias] [,...] [WHERE condition] [GROUP BY columnlist] [HAVING condition] [ORDER BY columnlist] Pearson Education 2014 22

SELECT Statement FROMSpecifies table(s) to be used. WHERE Filters rows. GROUP BY Forms groups of rows with same column value. HAVING Filters groups subject to some condition. SELECT Specifies which columns are to appear in output. ORDER BY Specifies the order of the output. Pearson Education 2014 23

SELECT Statement Order of the clauses cannot be changed. Only SELECT and FROM are mandatory. Pearson Education 2014 24

Example - Calculated Fields Produce list of monthly salaries for all staff, showing staff number, first/last name, and salary. SELECT staffno, fname, lname, salary/12 FROM Staff; Pearson Education 2014 25

Example - Comparison Search Condition List all staff with a salary greater than 10,000. SELECT staffno, fname, lname, position, salary FROM Staff WHERE salary > 10000; Pearson Education 2014 26

Example - Compound Comparison Search Condition List addresses of all branch offices in London or Glasgow. SELECT * FROM Branch WHERE city = London OR city = Glasgow ; Pearson Education 2014 27

Example - Set Membership List all managers and supervisors. SELECT staffno, fname, lname, position FROM Staff WHERE position IN ( Manager, Supervisor ); Pearson Education 2014 28

Example - Pattern Matching Find all owners with the string Glasgow in their address. SELECT ownerno, fname, lname, address, telno FROM PrivateOwner WHERE address LIKE %Glasgow% ; Pearson Education 2014 29

Example - Pattern Matching SQL has two special pattern matching symbols: %: sequence of zero or more characters; _ (underscore): any single character. LIKE %Glasgow% means a sequence of characters of any length containing Glasgow. Pearson Education 2014 30

Example - Single Column Ordering List salaries for all staff, arranged in descending order of salary SELECT staffno, fname, lname, salary FROM Staff ORDER BY salary DESC; Pearson Education 2014 31

Example - Multiple Column Ordering To arrange in order of rent, specify minor order: SELECT propertyno, type, rooms, rent FROM PropertyForRent ORDER BY type, rent DESC; Pearson Education 2014 32

Example - Use of COUNT(*) - Aggregate How many properties cost more than 350 per month to rent? SELECT COUNT(*) AS mycount FROM PropertyForRent WHERE rent > 350; Pearson Education 2014 33

Example - Use of COUNT(DISTINCT) How many different properties viewed in May 13? SELECT COUNT(DISTINCT propertyno) AS mycount FROM Viewing WHERE viewdate BETWEEN 1-May-13 AND 31-May-13 ; Pearson Education 2014 34

Example - Use of MIN, MAX, AVG Find minimum, maximum, and average staff salary. SELECT MIN(salary) AS mymin, MAX(salary) AS mymax, AVG(salary) AS myavg FROM Staff; Pearson Education 2014 35

Example - Use of GROUP BY Find number of staff in each branch and their total salaries. SELECT branchno, COUNT(staffNo) AS mycount, SUM(salary) AS mysum FROM Staff GROUP BY branchno ORDER BY branchno; Pearson Education 2014 36

Example - Subquery with Equality List staff who work in branch at 163 Main St. SELECT staffno, fname, lname, position FROM Staff WHERE branchno = (SELECT branchno FROM Branch WHERE street = 163 Main St ); Pearson Education 2014 37

Example - Use of ALL Find staff whose salary is larger than salary of every member of staff at branch B003. SELECT staffno, fname, lname, position, salary FROM Staff WHERE salary > ALL (SELECT salary FROM Staff WHERE branchno = B003 ); Pearson Education 2014 38

Multi-Table Queries Can use subqueries provided result columns come from same table. If result columns come from more than one table must use a join. To perform join, include more than one table in FROM clause. Use comma as separator and typically include WHERE clause to specify join column(s). Pearson Education 2014 39

Example - Simple Join List names of all clients who have viewed a property along with any comment supplied. SELECT c.clientno, fname, lname, propertyno, comment FROM Client c, Viewing v WHERE c.clientno = v.clientno; Equivalent to equi-join in relational algebra Pearson Education 2014 40

Example - Sorting a join For each branch, list numbers and names of staff who manage properties, and properties they manage. SELECT s.branchno, s.staffno, fname, lname, propertyno FROM Staff s, PropertyForRent p WHERE s.staffno = p.staffno ORDER BY s.branchno, s.staffno, propertyno; Pearson Education 2014 41

Example - Three Table Join For each branch, list staff who manage properties, including city in which branch is located and properties they manage. SELECT b.branchno, b.city, s.staffno, fname, lname, propertyno FROM Branch b, Staff s, PropertyForRent p WHERE b.branchno = s.branchno AND s.staffno = p.staffno ORDER BY b.branchno, s.staffno, propertyno; Pearson Education 2014 42

Computing a Join 4. If DISTINCT has been specified, eliminate any duplicate rows from the result table. 5. If there is an ORDER BY clause, sort result table as required. SQL provides special format of SELECT for Cartesian product: SELECT [DISTINCT ALL] FROM Table1 CROSS JOIN Table2 {* columnlist} Pearson Education 2014 43

Outer Joins The (inner) join of these two tables: SELECT b.*, p.* FROM Branch1 b, PropertyForRent1 p WHERE b.bcity = p.pcity; Pearson Education 2014 44

Example - Left Outer Join List branches and properties that are in same city along with any unmatched branches. SELECT b.*, p.* FROM Branch1 b LEFT JOIN PropertyForRent1 p ON b.bcity = p.pcity; Pearson Education 2014 45

Example - Right Outer Join List branches and properties in same city and any unmatched properties. SELECT b.*, p.* FROM Branch1 b RIGHT JOIN PropertyForRent1 p ON b.bcity = p.pcity; Pearson Education 2014 46

Example - Full Outer Join List branches and properties in same city and any unmatched branches or properties. SELECT b.*, p.* FROM Branch1 b FULL JOIN PropertyForRent1 p ON b.bcity = p.pcity; Pearson Education 2014 47

Example - Use of UNION List all cities where there is either a branch office or a property. FROM Branch (SELECT city WHERE city IS NOT NULL) UNION (SELECT city FROM PropertyForRent WHERE city IS NOT NULL); Pearson Education 2014 48

Example - Use of INTERSECT List all cities where there is both a branch office and a property. (SELECT city FROM Branch) INTERSECT (SELECT city FROM PropertyForRent); Pearson Education 2014 49

Example - INSERT VALUES Insert a new row into Staff table supplying data for all columns. INSERT INTO Staff VALUES ( SG16, Alan, Brown, Assistant, M, Date 1957-05-25, 8300, B003 ); Pearson Education 2014 50

UPDATE UPDATE TableName SET columnname1 = datavalue1 [, columnname2 = datavalue2...] [WHERE searchcondition] TableName can be name of a base table or an updatable view. SET clause specifies names of one or more columns that are to be updated. Pearson Education 2014 51

Example - UPDATE All Rows Give all staff a 3% pay increase. UPDATE Staff SET salary = salary*1.03; Give all Managers a 5% pay increase. UPDATE Staff SET salary = salary*1.05 WHERE position = Manager ; Pearson Education 2014 52

DELETE DELETE FROM TableName [WHERE searchcondition] TableName can be name of a base table or an updatable view. searchcondition is optional; if omitted, all rows are deleted from table. This does not delete table. If search_condition is specified, only those rows that satisfy condition are deleted. Pearson Education 2014 53

Example - DELETE Specific Rows Delete all viewings that relate to property PG4. DELETE FROM Viewing WHERE propertyno = PG4 ; Delete all records from the Viewing table. DELETE FROM Viewing; Pearson Education 2014 54