Lectures 2: Introduction to SQL 1

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

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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.
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.
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 Lecture 4: Database Modeling (end) The Relational Data Model April 8, 2002.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
1 The Relational Model Instructor: Mohamed Eltabakh
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 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.
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.
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.
Jennifer Widom Relational Databases The Relational Model.
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.
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.
The Relational Data Model Database Model (ODL, E/R) Relational Schema Physical storage ODL definitions Diagrams (E/R) Tables: row names: attributes rows:
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.
Lectures 2&3: Introduction to SQL
Relational Data Model Lu Chaojun, SJTU.
Modeling Constraints Extracting constraints is what modeling is all about. But how do we express them? Examples: Keys: social security number uniquely.
COP Introduction to Database Structures
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Database Management Systems (CS 564)
Chapter 2: Intro to Relational Model
Programming with Android:
Database Models Relational Model
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
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.
SQL OVERVIEW DEFINING A SCHEMA
Introduction to SQL Wenhao Zhang October 5, 2018.
Lecture 06 Data Modeling: E/R Diagrams
CMPT 354: Database System I
CMPT 354: Database System I
SQL-1 Week 8-9.
Lecture 9: The E/R Model II
Lectures 3: Introduction to SQL 2
Introduction to Database Systems CSE 444 Lecture 02: SQL
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Intro to Relational Model
Lecture 5: The Relational Data Model
CS639: Data Management for Data Science
Instructor: Mohamed Eltabakh
Terminology Product Attribute names Name Price Category Manufacturer
DATABASE MANAGEMENT SYSTEM
Instructor: SAMIA ARSHAD
Lecture 08: E/R Diagrams and Functional Dependencies
The Relational Model Content based on Chapter 3
The Relational Model Content based on Chapter 3
SQL (Structured Query Language)
Presentation transcript:

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

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

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

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

Lecture 2 > Section 1 > SQL 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 SQL stands for Structured Query Language

SQL is a… Data Definition Language (DDL) Lecture 2 > Section 1 > SQL 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!

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

Lecture 2 > Section 1 > Definitions Tables in SQL A multiset is an unordered list (or: a set with multiple duplicate instances allowed) Product PName Price Manufacturer Gizmo $19.99 GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Canon MultiTouch $203.99 Hitachi 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 Tables in SQL Product An attribute (or column) is a typed data entry present in each tuple in the relation PName Price Manufacturer Gizmo $19.99 GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Canon MultiTouch $203.99 Hitachi NB: Attributes must have an atomic type in standard SQL, i.e. not a list, set, etc.

Lecture 2 > Section 1 > Definitions Tables in SQL Product PName Price Manufacturer Gizmo $19.99 GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Canon MultiTouch $203.99 Hitachi 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

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

Data Types in SQL Atomic types: Lecture 2 > Section 1 > Definitions 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

Lecture 2 > Section 1 > Definitions 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) Product(Pname: string, Price: float, Category: string, Manufacturer: string)

Lecture 2 > Section 1 > Keys & constraints Key constraints A key is a minimal subset of attributes that acts as a unique identifier for tuples in a relation 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! Students(sid:string, name:string, gpa: float) 1. Which would you select as a key? 2. Is a key always guaranteed to exist? 3. Can we have more than one key?

Say, Jim just enrolled in his first class. Lecture 2 > Section 1 > Keys & constraints NULL and NOT NULL To say “don’t know the value” we use NULL NULL has (sometimes painful) semantics, more detail later Students(sid:string, name:string, gpa: float) sid name gpa 123 Bob 3.9 143 Jim NULL 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

General Constraints Performance! Lecture 2 > Section 1 > Keys & constraints General Constraints We can actually specify arbitrary assertions E.g., “There cannot be 25 people in the DB class” In practice, we don’t specify many such constraints. Why? Performance! Whenever we do something ugly (or avoid doing something convenient) it’s for the sake of performance

Summary of Schema Information Lecture 2 > Section 1 > Summary Summary of Schema Information Schema and Constraints are how databases understand the semantics (meaning) of data They are also useful for optimization SQL supports general constraints: Keys and foreign keys are most important We’ll give you a chance to write the others

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