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

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

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

Relational model and basic SQL

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

Lecture 6 - More SQL

MySQL Views & Comparing SQL to NoSQL

Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL

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

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

Indexing: B + -Tree. CS 377: Database Systems

Relational Algebra. Procedural language Six basic operators

Chapter 11: Indexing and Hashing" Chapter 11: Indexing and Hashing"

QQ Group

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

Physical Database Design

Database index structures

Explain in words what this relational algebra expression returns:

Chapter 6: Formal Relational Query Languages

Chapter 2 Introduction to Relational Models

Chapter 2: Intro to Relational Model

Principles of Data Management

Chapter 2: Intro to Relational Model

Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

CS425 Fall 2017 Boris Glavic Chapter 4: Introduction to SQL

Introduction CHAPTER. 1.1 Database-System Applications

ERRATA for Database System Concepts, 6 th Edition, 2010 Silberschatz, Korth, and Sudarshan Last updated: December 6, 2010

Database System Concepts, 6 th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Unit 2: SQL AND PL/SQL

Chapter 11: Indexing and Hashing

Final Review. CS 377: Database Systems

2.2.2.Relational Database concept

Instructor: Amol Deshpande

Chapter 11: Indexing and Hashing

CS 582 Database Management Systems II

Chapter 6 Formal Relational Query Languages

Chapter 4: Intermediate SQL

Triggers- View-Sequence

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

Chapter 4: Intermediate SQL

SQL Retrieving Data from Multiple Tables

Chapter 4: Intermediate SQL

Natural-Join Operation

Textbook: Chapter 4. Chapter 5: Intermediate SQL. CS425 Fall 2016 Boris Glavic. Chapter 5: Intermediate SQL. View Definition.

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Chapter 11: Indexing and Hashing

CS3DB3/SE4DB3/SE6M03 TUTORIAL

CS425 Fall 2017 Boris Glavic Chapter 5: Intermediate SQL

Chapter 2: Relational Model

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

Relational Database: The Relational Data Model; Operations on Database Relations

CMP-3440 Database Systems

CS425 Fall 2016 Boris Glavic Chapter 2: Intro to Relational Model

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

Midterm Review. Winter Lecture 13

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

An Introduction to Structured Query Language

SQL Data Definition Language

Chapter 4: SQL. Basic Structure

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

Chapter 1: Introduction

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

ER to Relational Model. Professor Jessica Lin

Database Management Systems

An Introduction to Structured Query Language

Table Joins and Indexes in SQL

An Introduction to Structured Query Language

Tables From Existing Tables

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

SQL (Structured Query Language)

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

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

An Introduction to Structured Query Language

Chapter 3: SQL. Chapter 3: SQL

Other Relational Languages

An Introduction to Structured Query Language

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

Indexing: Overview & Hashing. CS 377: Database Systems

Database Systems SQL SL03

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Relational Algebra. Spring 2012 Instructor: Hassan Khosravi

Midterm Exam #2 (Version A) CS 122A Winter 2017

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

CS2300: File Structures and Introduction to Database Systems

Database Systems SQL SL03

Database Technology Introduction. Heiko Paulheim

Relational Algebra and SQL

Querying Data with Transact SQL

Query Processing & Optimization

SQL DATA DEFINITION LANGUAGE

CS3DB3/SE4DB3/SE6M03 TUTORIAL

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

CSC 261/461 Database Systems Lecture 13. Fall 2017

MySQL Creating a Database Lecture 3

Chapter 7: Entity-Relationship Model. Chapter 7: Entity-Relationship Model

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

Transcription:

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

This Week Intro to SQL and MySQL Mapping Relational Algebra to SQL queries We will focus on queries to start assume tables and database exist. Time permitting: creating tables, more involved queries

Projection Symbol is Π Selection of attributes. Π ID, salary (instructor) SELECT col_1,, col_n FROM instructor Or SELECT * FROM instructor (means select all columns) ID name dept name salary 10101 Srinivasan Comp. Sci. 65000 12121 Wu Finance 90000 15151 Mozart Music 40000 22222 Einstein Physics 95000 32343 El Said History 60000 33456 Gold Physics 87000 45565 Katz Comp. Sci. 75000 58583 Califieri History 62000 76543 Singh Finance 80000 76766 Crick Biology 72000 83821 Brandt Comp. Sci. 92000 98345 Kim Elec. Eng. 80000 SELECT ID, salary FROM instructor

Selection Notation is σ p (x). σ salary >= 85000 (instructor) ID name dept name salary 10101 Srinivasan Comp. Sci. 65000 12121 Wu Finance 90000 15151 Mozart Music 40000 22222 Einstein Physics 95000 32343 El Said History 60000 33456 Gold Physics 87000 45565 Katz Comp. Sci. 75000 58583 Califieri History 62000 76543 Singh Finance 80000 76766 Crick Biology 72000 83821 Brandt Comp. Sci. 92000 98345 Kim Elec. Eng. 80000 SELECT * FROM instructor WHERE salary >= 85000 SELECT col_1,, col_n FROM instructor WHERE salary >= 85000

Natural Join Recall we combine two relations into a single relation. The tuples are joined if the attributes common to both relations are equal. instructor department ID name dept name salary 10101 Srinivasan Comp. Sci. 65000 12121 Wu Finance 90000 15151 Mozart Music 40000 22222 Einstein Physics 95000 32343 El Said History 60000 33456 Gold Physics 87000 45565 Katz Comp. Sci. 75000 58583 Califieri History 62000 76543 Singh Finance 80000 76766 Crick Biology 72000 83821 Brandt Comp. Sci. 92000 98345 Kim Elec. Eng. 80000 dept name building budget Biology Watson 90000 Comp. Sci. Taylor 100000 Elec. Eng. Taylor 85000 Finance Painter 120000 History Painter 50000 Music Packard 80000 Physics Watson 70000

Natural Join instructor department The tuples are joined if the attributes common to both relations are equal. ID name salary dept name building budget 10101 Srinivasan 65000 Comp. Sci. Taylor 100000 12121 Wu 90000 Finance Painter 120000 15151 Mozart 40000 Music Packard 80000 22222 Einstein 95000 Physics Watson 70000 32343 El Said 60000 History Painter 50000 33456 Gold 87000 Physics Watson 70000 45565 Katz 75000 Comp. Sci. Taylor 100000 58583 Califieri 62000 History Painter 50000 76543 Singh 80000 Finance Painter 120000 76766 Crick 72000 Biology Watson 90000 83821 Brandt 92000 Comp. Sci. Taylor 100000 98345 Kim 80000 Elec. Eng. Taylor 85000 SELECT * FROM instructor NATURAL JOIN department

Cartesian Product Example Relations r, s: r x s: SELECT * FROM r INNER JOIN s or SELECT * FROM r, s Note: can have as many relations as needed but what may be a concern?

Cartesian Product Example Relations r, s: r x s: SELECT * FROM r INNER JOIN s What if we don t want ALL rows? For example, we want rows where A s value and C s value are equal? SELECT * FROM r INNER JOIN s ON A = C

Inner Join SELECT Column1, Column2,, ColumnK FROM TableA INNER JOIN TableB ON join_constraints WHERE contraints ORDER BY ColumnX There are many other options, we will see these later

Self Join Suppose we want to join a table to itself. We want to find those departments that are in the same building. department A department B dept name building budget Biology Watson 90000 Comp. Sci. Taylor 100000 Elec. Eng. Taylor 85000 Finance Painter 120000 History Painter 50000 Music Packard 80000 Physics Watson 70000 dept name building budget Biology Watson 90000 Comp. Sci. Taylor 100000 Elec. Eng. Taylor 85000 Finance Painter 120000 History Painter 50000 Music Packard 80000 Physics Watson 70000 SELECT A.dept_name, B.dept_name FROM department A INNER JOIN department B ON A.building = B.building

Union Relations r, s: For r s to be valid. 1. r, s must have the same arity (same number of attributes) 2. The attribute domains must be compatible My (SELECT * FROM r) UNION (SELECT * FROM s) Use UNION ALL to keep duplicates. r s:

Intersection Relation r, s: r s = r (r s) (SELECT * FROM r) INTERSECT (SELECT * FROM s)

Intersection (SELECT * FROM r) INTERSECT Does NOT Work in MySQL (SELECT * FROM s) MySQL Options: LEFT AS EXERCISE

Difference What would you expect them to be? Relations r, s: r s (SELECT * FROM r) EXCEPT (SELECT * FROM s)

Difference (SELECT * FROM r) EXCEPT Does NOT Work in MySQL (SELECT * FROM s) MySQL Options: LEFT AS EXERCISE

SQL Types char(n): A fixed- length character string. varchar(n): A variable- length character string with max length n. int: An integer. numeric(p, d): A fixed- point number with p digits of which d of the digits are to the right of the decimal point. real, double precision: Floating point and double precision floating point. float(n): A floating point with at least n digits of precision.

Null Value Every type can have the special value null. A value of null indicates the value is unknown or that it may not exist at all. Sometimes we do not want a null value at all we can add such a constraint.

Creating a Table CREATE TABLE table_name (col_name 1 type 1, col_name 2 type 2,, col_name n type n, <integrity- constraint 1 >,, <integrity- constraint k >);

Integrity Constraints Primary key( list of aiributes) : These aiributes form the primary keys for the relation. Primary keys must be non- null and unique. Foreign key( list of aiributes) references s : The values of these aiributes for any tuple in the relation must correspond to values of the primary key a5ributes of some tuple in relation s. not null: Specifies that this airibute may not have the null value. We list this constraint when defining the type of the airibute.

Examples CREATE TABLE department (dept_name VARCHAR(20), building VARCHAR(15), budget NUMERIC(12,2), PRIMARY KEY (dept_name)); CREATE TABLE course (course_id VARCHAR(7), title VARCHAR(50), dept_name VARCHAR(20), dept name building budget Biology Watson 90000 Comp. Sci. Taylor 100000 Elec. Eng. Taylor 85000 Finance Painter 120000 History Painter 50000 Music Packard 80000 Physics Watson 70000 credit NUMERIC(2,0), PRIMARY KEY (course_id), FOREIGN KEY (dept_name) REFERENCES department );

Editing Tables DROP TABLE table_name; remove the table DELETE FROM table_name delete tuples satisfying WHERE predicate; the predicate ALTER TABLE table_name ADD column type; add a column ALTER TABLE table_name DROP column; remove a column

Inserting In MySQL we can insert into a table with the command: INSERT INTO table_name VALUES (value 1, value 2,, value n,); OR INSERT INTO table_name (col 1, col 2,, col n ) VALUES (value 1, value 2,, value n,); OR INSERT INTO table_name SELECT QUERY For example: INSERT INTO instructor SELECT ID, name, dept_name, 18000 FROM student WHERE dept_name = Music AND tot_cred > 144;

Updating In MySQL we can update a table with the command: UPDATE table_name SET airibute = new_value OR OR UPDATE table_name SET airibute = new_value WHERE predicate or select statement; UPDATE table_name SET airibute = CASE WHEN predicate 1 THEN result 1 WHEN predicate 2 THEN result 2 WHEN predicate n THEN result n ELSE result 0 END