Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 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

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
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.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
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,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 8i: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY Fall, 2004.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #10 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
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.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
1. Midterm summary Types of data on the web: unstructured, semi- structured, structured Scale and uncertainty are key features Main goals are to model,
The Relational Model These slides are based on the slides of your text book.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
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 Instructor: Mohamed Eltabakh
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
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.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
SQL SQL Review. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
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.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
Hassan Tariq MULTIPLE TABLES: SQL provides a convenient operation to retrieve information from multiple tables.SQL provides a convenient operation to.
1 Introduction to Database Systems CSE 444 Lecture 02: SQL September 28, 2007.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
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.
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.
Jennifer Widom Relational Databases The Relational Model.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),
SQL.
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Programming with Android:
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
Instructor: Mohamed Eltabakh
Cse 344 January 10th –joins.
Relational Databases The Relational Model.
Relational Databases The Relational Model.
Introduction to SQL Wenhao Zhang October 5, 2018.
CMPT 354: Database System I
CMPT 354: Database System I
SQL-1 Week 8-9.
Lectures 3: Introduction to SQL 2
Introduction to Database Systems CSE 444 Lecture 02: SQL
CS639: Data Management for Data Science
CS639: Data Management for Data Science
Instructor: Mohamed Eltabakh
Lectures 2: Introduction to SQL 1
Instructor: SAMIA ARSHAD
Presentation transcript:

Lectures 2&3: Introduction to SQL

Lecture 2: SQL Part I Lecture 2

Today’s Lecture 1.SQL introduction & schema definitions ACTIVITY: Table creation 2.Basic single-table queries ACTIVITY: Single-table queries! 3.Multi-table queries ACTIVITY: Multi-table queries! 3 Lecture 2

1. SQL Introduction & Definitions 4 Lecture 2 > Section 1

What you will learn about in this section 1.What is SQL? 2.Basic schema definitions 3.Keys & constraints intro 4.ACTIVITY: CREATE TABLE statements 5 Lecture 2 > Section 1

SQL Motivation Dark times 5 years ago. Are databases dead? Now, as before: everyone sells SQL Pig, Hive, Impala “Not-Yet-SQL?” Lecture 2 > Section 1 > SQL

7 Basic SQL Lecture 2 > Section 1 > SQL

SQL Introduction SQL is a standard language for querying and manipulating data SQL is a very high-level programming language This works because it is optimized well! Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3), …. Vendors support various subsets NB: Probably the world’s most successful parallel programming language (multicore?) SQL stands for Structured Query Language SQL stands for Structured Query Language Lecture 2 > Section 1 > SQL

9 SQL is a… Data Definition Language (DDL) Define relational schemata Create/alter/delete tables and their attributes Data Manipulation Language (DML) Insert/delete/modify tuples in tables Query one or more tables – discussed next! Lecture 2 > Section 1 > SQL

10 Tables in SQL PNamePriceManufacturer Gizmo$19.99GizmoWorks Powergizmo$29.99GizmoWorks SingleTouch$149.99Canon MultiTouch$203.99Hitachi Product A relation or table is a multiset of tuples having the attributes specified by the schema Let’s break this definition down Lecture 2 > Section 1 > Definitions

11 Tables in SQL PNamePriceManufacturer Gizmo$19.99GizmoWorks Powergizmo$29.99GizmoWorks SingleTouch$149.99Canon MultiTouch$203.99Hitachi Product A multiset is an unordered list (or: a set with multiple duplicate instances allowed) List: [1, 1, 2, 3] Set: {1, 2, 3} Multiset: {1, 1, 2, 3} i.e. no next(), etc. methods! Lecture 2 > Section 1 > Definitions

12 Tables in SQL PNamePriceManufacturer Gizmo$19.99GizmoWorks Powergizmo$29.99GizmoWorks SingleTouch$149.99Canon MultiTouch$203.99Hitachi Product An attribute (or column) is a typed data entry present in each tuple in the relation NB: Attributes must have an atomic type in standard SQL, i.e. not a list, set, etc. Lecture 2 > Section 1 > Definitions

13 Tables in SQL PNamePriceManufacturer Gizmo$19.99GizmoWorks Powergizmo$29.99GizmoWorks SingleTouch$149.99Canon MultiTouch$203.99Hitachi Product A tuple or row is a single entry in the table having the attributes specified by the schema Also referred to sometimes as a record Lecture 2 > Section 1 > Definitions

14 Tables in SQL PNamePriceManufacturer Gizmo$19.99GizmoWorks Powergizmo$29.99GizmoWorks SingleTouch$149.99Canon MultiTouch$203.99Hitachi Product Lecture 2 > Section 1 > Definitions The number of tuples is the cardinality of the relation The number of attributes is the arity of the relation

15 Data Types in SQL Atomic types: Characters: CHAR(20), VARCHAR(50) Numbers: INT, BIGINT, SMALLINT, FLOAT Others: MONEY, DATETIME, … Every attribute must have an atomic type Hence tables are flat Why? Lecture 2 > Section 1 > Definitions

16 Table Schemas The schema of a table is the table name, its attributes, and their types: A key is an attribute whose values are unique; we underline a key Product(Pname: string, Price: float, Category: string, Manufacturer: string) Lecture 2 > Section 1 > Definitions Product(Pname: string, Price: float, Category: string, Manufacturer: string)

Key constraints A key is an implicit constraint on which tuples can be in the relation i.e. if two tuples agree on the values of the key, then they must be the same tuple! 1. Which would you select as a key? 2. Is a key always guaranteed to exist? 3. Can we have more than one key? A key is a minimal subset of attributes that acts as a unique identifier for tuples in a relation Lecture 2 > Section 1 > Keys & constraints Students(sid:string, name:string, gpa: float)

NULL and NOT NULL To say “don’t know the value” we use NULL NULL has (sometimes painful) semantics, more detail later sidnamegpa 123Bob JimNULL Say, Jim just enrolled in his first class. In SQL, we may constrain a column to be NOT NULL, e.g., “name” in this table Students(sid:string, name:string, gpa: float) Lecture 2 > Section 1 > Keys & constraints

2. Single-table queries 19 Lecture 2 > Section 2

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

21 Simple SQL Query: Selection PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks Lecture 2 > Section 2 > SFW SELECT * FROM Product WHERE Category = ‘Gadgets’ Selection is the operation of filtering a relation’s tuples on some condition

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

23 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 Lecture 2 > Section 2 > SFW

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

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

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

3. Multi-table queries 27 Lecture 2 > Section 3

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

Foreign Key constraints student_id alone is not a key- what is? sidnamegpa 101Bob Mary3.8 student_idcidgrade A A+ StudentsEnrolled We say that student_id is a foreign key that refers to Students Lecture 2 > Section 3 > Foreign Keys Students(sid: string, name: string, gpa: float) Enrolled(student_id: string, cid: string, grade: string) Students(sid: string, name: string, gpa: float) Enrolled(student_id: string, cid: string, grade: string) 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 class

Declaring Foreign Keys Lecture 2 > Section 3 > 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 ) 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 )

Foreign Keys and update operations DBA chooses (syntax in the book) Lecture 2 > Section 3 > Foreign Keys Students(sid: string, name: string, gpa: float) Enrolled(student_id: string, cid: string, grade: string) 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? 1.Disallow the delete 2.Remove all of the courses for that student 3.SQL allows a third via NULL (not yet covered)

32 Keys and Foreign Keys PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Company CNameStockPriceCountry GizmoWorks25USA Canon65Japan Hitachi15Japan What is a foreign key vs. a key here? Lecture 2 > Section 3 > Foreign Keys

33 Joins Ex: Find all products under $200 manufactured in Japan; return their names and prices. SELECT PName, Price FROM Product, Company WHERE Manufacturer = CName AND Country=‘Japan’ AND Price <= 200 SELECT PName, Price FROM Product, Company WHERE Manufacturer = CName AND Country=‘Japan’ AND Price <= 200 Lecture 2 > Section 3 > Joins: Basics Product(PName, Price, Category, Manufacturer) Company(CName, StockPrice, Country) Product(PName, Price, Category, Manufacturer) Company(CName, StockPrice, Country) Note: we will often omit attribute types in schema definitions for brevity, but assume attributes are always types

34 Joins Ex: Find all products under $200 manufactured in Japan; return their names and prices. SELECT PName, Price FROM Product, Company WHERE Manufacturer = CName AND Country=‘Japan’ AND Price <= 200 SELECT PName, Price FROM Product, Company WHERE Manufacturer = CName AND Country=‘Japan’ AND Price <= 200 Lecture 2 > Section 3 > Joins: Basics A join between tables returns all unique combinations of their tuples which meet some specified join condition Product(PName, Price, Category, Manufacturer) Company(CName, StockPrice, Country) Product(PName, Price, Category, Manufacturer) Company(CName, StockPrice, Country)

35 Joins Several equivalent ways to write a basic join in SQL: SELECT PName, Price FROM Product, Company WHERE Manufacturer = CName AND Country=‘Japan’ AND Price <= 200 SELECT PName, Price FROM Product, Company WHERE Manufacturer = CName AND Country=‘Japan’ AND Price <= 200 Lecture 2 > Section 3 > Joins: Basics SELECT PName, Price FROM Product JOIN Company ON Manufacturer = Cname AND Country=‘Japan’ WHERE Price <= 200 SELECT PName, Price FROM Product JOIN Company ON Manufacturer = Cname AND Country=‘Japan’ WHERE Price <= 200 A few more later on… Product(PName, Price, Category, Manufacturer) Company(CName, StockPrice, Country) Product(PName, Price, Category, Manufacturer) Company(CName, StockPrice, Country)