Lectures 3: Introduction to SQL 2

Slides:



Advertisements
Similar presentations
1 Lecture 02: SQL. 2 Outline Data in SQL Simple Queries in SQL (6.1) Queries with more than one relation (6.2) Recomeded reading: Chapter 3, Simple Queries.
Advertisements

SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
Relational Algebra (end) SQL April 19 th, Complex Queries Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store,
1 Lecture 12: SQL Friday, October 26, Outline Simple Queries in SQL (5.1) Queries with more than one relation (5.2) Subqueries (5.3) Duplicates.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
1 Lecture 02: Basic SQL. 2 Outline Data in SQL Simple Queries in SQL Queries with more than one relation Reading: Chapter 3, “Simple Queries” from SQL.
1 Lecture 2: SQL Wednesday, January 7, Agenda Leftovers from Monday The relational model (very quick) SQL Homework #1 given out later this week.
1 Information Systems Chapter 6 Database Queries.
Complex Queries (1) Product ( pname, price, category, maker)
1. Midterm summary Types of data on the web: unstructured, semi- structured, structured Scale and uncertainty are key features Main goals are to model,
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
IM433-Industrial Data Systems Management Lecture 5: SQL.
Intro. to SQL DSC340 Mike Pangburn. Learning Objectives Understand the data-representation terminology underlying relational databases Understand core.
More SQL: Complex Queries, Triggers, Views, and Schema Modification UMM AL QURA UNIVERSITY College of Computer Dr. Ali Al Najjar 1.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
1 Introduction to Database Systems CSE 444 Lecture 02: SQL September 28, 2007.
1 Lecture 02: SQL Friday, September 30, Administrivia Homework 1 is out. Due: Wed., Oct. 12 Did you login on IISQLSRV ? Did you change your password.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
Hassan Tariq INTRODUCTION TO SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query. – A query is a user–request.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
CSE 326: Data Structures Lecture #22 Databases and Sorting Alon Halevy Spring Quarter 2001.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
The Relational Data Model Database Model (ODL, E/R) Relational Schema Physical storage ODL definitions Diagrams (E/R) Tables: row names: attributes rows:
SQL.
Lectures 2&3: Introduction to SQL
Modeling Constraints Extracting constraints is what modeling is all about. But how do we express them? Examples: Keys: social security number uniquely.
COP4710 Database Systems Relational Model.
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
SQL Introduction Standard language for querying and manipulating data
Programming with Android:
CS 405G: Introduction to Database Systems
Translation of ER-diagram into Relational Schema
Monday 3/27 and Wednesday 3/29, 2006
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
CS 405G: Introduction to Database Systems
Cse 344 January 12th –joins.
March 30th – intro to joins
Cse 344 January 10th –joins.
The Relational Model Relational Data Model
Relational Databases The Relational Model.
Relational Databases The Relational Model.
Introduction to SQL Wenhao Zhang October 5, 2018.
CSE544 SQL Wednesday, March 31, 2004.
CMPT 354: Database System I
Lecture 9: The E/R Model II
Lectures 7: Introduction to SQL 6
Introduction to Database Systems CSE 444 Lecture 02: SQL
Lecture 5: The Relational Data Model
CS639: Data Management for Data Science
Sampath Jayarathna Cal Poly Pomona
Terminology Product Attribute names Name Price Category Manufacturer
SQL: Structured Query Language
Lectures 2: Introduction to SQL 1
DATABASE MANAGEMENT SYSTEM
Instructor: SAMIA ARSHAD
CS 405G: Introduction to Database Systems
Presentation transcript:

Lectures 3: Introduction to SQL 2 Lecture and activity contents are based on what Prof Chris Ré used in his CS 145 in the fall 2016 term with permission.

Lecture 2 > Section 2 2. Single-table queries

What you will learn about in this section Lecture 2 > Section 2 What you will learn about in this section The SFW query Other useful operators: LIKE, DISTINCT, ORDER BY ACTIVITY: Single-table queries

SQL Query Basic form (there are many many more bells and whistles) Lecture 2 > Section 2 > SFW SQL Query Basic form (there are many many more bells and whistles) SELECT <attributes> FROM <one or more relations> WHERE <conditions> Call this a SFW query.

Simple SQL Query: Selection Lecture 2 > Section 2 > SFW Simple SQL Query: Selection PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Selection is the operation of filtering a relation’s tuples on some condition SELECT * FROM Product WHERE Category = ‘Gadgets’ PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99

Simple SQL Query: Projection Lecture 2 > Section 2 > SFW Simple SQL Query: Projection PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Projection is the operation of producing an output table with tuples that have a subset of their prior attributes SELECT Pname, Price, Manufacturer FROM Product WHERE Category = ‘Gadgets’ PName Price Manufacturer Gizmo $19.99 GizmoWorks Powergizmo $29.99

Notation Input schema Output schema Lecture 2 > Section 2 > SFW Notation Input schema Product(PName, Price, Category, Manfacturer) SELECT Pname, Price, Manufacturer FROM Product WHERE Category = ‘Gadgets’ Output schema Answer(PName, Price, Manfacturer)

A Few Details SQL commands are case insensitive: Values are not: Lecture 2 > Section 2 > SFW A Few Details SQL commands are case insensitive: Same: SELECT, Select, select Same: Product, product Values are not: Different: ‘Seattle’, ‘seattle’ Use single quotes for constants: ‘abc’ - yes “abc” - no

LIKE: Simple String Pattern Matching Lecture 2 > Section 2 > Other operators LIKE: Simple String Pattern Matching SELECT * FROM Products WHERE PName LIKE ‘%gizmo%’ s LIKE p: pattern matching on strings p may contain two special symbols: % = any sequence of characters _ = any single character

DISTINCT: Eliminating Duplicates Lecture 2 > Section 2 > Other operators DISTINCT: Eliminating Duplicates Category Gadgets Photography Household SELECT DISTINCT Category FROM Product Versus Category Gadgets Photography Household SELECT Category FROM Product

ORDER BY: Sorting the Results Lecture 2 > Section 2 > Other operators ORDER BY: Sorting the Results SELECT PName, Price, Manufacturer FROM Product WHERE Category=‘Gadgets’ AND Price < 20 ORDER BY Price, PName Ties are broken by the second attribute on the ORDER BY list, etc. Ordering is ascending, unless you specify the DESC keyword.

ACTIVITY: Activity-2-2.ipynb Lecture 2 > Section 2 > ACTIVITY ACTIVITY: Activity-2-2.ipynb

Lecture 2 > Section 3 3. Multi-table queries

What you will learn about in this section Lecture 2 > Section 3 What you will learn about in this section Foreign key constraints Joins: basics Joins: SQL semantics ACTIVITY: Multi-table queries

Foreign Key constraints Lecture 2 > Section 3 > Foreign Keys Foreign Key constraints Suppose we have the following schema: And we want to impose the following constraint: ‘Only bona fide students may enroll in courses’ i.e., a student must appear in the Students table to enroll in a course Students(sid: string, name: string, gpa: float) Enrolled(student_id: string, cid: string, grade: string) student_id alone is not a key in Enrolled - what is? Students Enrolled sid name gpa 101 Bob 3.2 123 Mary 3.8 student_id cid grade 123 564 A 537 A+ We say that student_id is a foreign key in Enrolled that refers to Students

Declaring Foreign Keys Lecture 2 > Section 3 > Foreign Keys Declaring Foreign Keys Students(sid: string, name: string, gpa: float) Enrolled(student_id: string, cid: string, grade: string) CREATE TABLE Enrolled( student_id CHAR(20), cid CHAR(20), grade CHAR(10), PRIMARY KEY (student_id, cid), FOREIGN KEY (student_id) REFERENCES Students(sid) )

Foreign Keys and update operations Lecture 2 > Section 3 > Foreign Keys Foreign Keys and update operations Students(sid: string, name: string, gpa: float) Enrolled(student_id: string, cid: string, grade: string) What if we insert a tuple into Enrolled, but no corresponding student? INSERT is rejected (foreign keys are constraints)! What if we delete a student in Students? Disallow the delete Remove all of the courses for that student SQL allows a third via NULL (not yet covered) DBA chooses (syntax in the book)

What is a foreign key vs. a key here? Lecture 2 > Section 3 > Foreign Keys Keys and Foreign Keys Company What is a foreign key vs. a key here? CName StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi