user specifies what is wanted, not how to find it

Similar documents
SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

The data structures of the relational model Attributes and domains Relation schemas and database schemas

What is a Database? Peter Wood

Running Example Tables name location

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

SQL: Data Definition Language

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Basic SQL. Basic SQL. Basic SQL

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Databases 1. Defining Tables, Constraints

MTAT Introduction to Databases

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

Chapter 2 The relational Model of data. Relational model introduction

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Options. Real SQL Programming 1. Stored Procedures. Embedded SQL

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

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

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

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

Principles of Data Management

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

COMP 430 Intro. to Database Systems

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

CSCI3030U Database Models

Chapter 9: Working with MySQL

SQL Introduction. CS 377: Database Systems

CS145 Introduction. About CS145 Relational Model, Schemas, SQL Semistructured Model, XML

The Relational Model of Data (ii)

Database Management Systems,

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

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

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

SQL Structured Query Language Introduction

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

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

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

Full file at

SQL Data Definition Language

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

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

Introduction to Databases CSE 414. Lecture 2: Data Models

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

CSE 344 JANUARY 8 TH SQLITE AND JOINS

CSIE30600 Database Systems Basic SQL 2. Outline

SQL. The Basics Advanced Manipulation Constraints Authorization 1. 1

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Chapter 3: Introduction to SQL

Slides by: Ms. Shree Jaswal

QQ Group

Chapter 3: Introduction to SQL

Constraints. Local and Global Constraints Triggers

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

Introduction to Databases. MySQL Syntax Guide: Day 1

Lecture 5: SQL s Data Definition Language

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

Part II Relational Databases Data as Tables

Programming and Database Fundamentals for Data Scientists

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

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

Lab # 4. Data Definition Language (DDL)

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

NULL. The special value NULL could mean: Unknown Unavailable Not Applicable

CS2300: File Structures and Introduction to Database Systems

INFORMATION TECHNOLOGY NOTES

Full file at

Databases-1 Lecture-01. Introduction, Relational Algebra

Announcements. Using Electronics in Class. Review. Staff Instructor: Alvin Cheung Office hour on Wednesdays, 1-2pm. Class Overview

1) Introduction to SQL

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

CS143: Relational Model

CMPT 354: Database System I. Lecture 3. SQL Basics

Keys, SQL, and Views CMPSCI 645

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

SQL Overview. CSCE 315, Fall 2017 Project 1, Part 3. Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch

Data Types in MySQL CSCU9Q5. MySQL. Data Types. Consequences of Data Types. Common Data Types. Storage size Character String Date and Time.

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

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

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

Advanced Constraints SQL. by Joe Celko copyright 2007

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Lecture 2: Introduction to SQL

Chapter 6 The database Language SQL as a tutorial

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 2: Relations and SQL. September 5, Lecturer: Rasmus Pagh

Database Management

Information Systems for Engineers Fall Data Definition with SQL

Simple Quesries in SQL & Table Creation and Data Manipulation

Transcription:

SQL stands for Structured Query Language sometimes pronounced sequel a very-high-level (declarative) language user specifies what is wanted, not how to find it number of standards original ANSI SQL updated in 1992 to SQL-92 or SQL2 most recent is SQL:1999 (also called SQL3) also extensions to SQL:1999, called SQL:2003 DBMS vendors all support ANSI SQL and largely SQL-92 some of SQL:1999 and SQL:2003 with variations and own extensions

SQL Functionality data definition language data manipulation language integrity constraints view definition transaction control embedded and dynamic SQL authorisation

Creating Relation Schemas Pubs table name Horse and Hound Hound and Hare March Hare Black Horse White Horse location Bloomsbury Islington Bloomsbury Islington Bloomsbury create table Pubs ( name varchar(50) primary key, location varchar(50) );

Creating Relation Schemas Sells table pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50......... create table Sells ( pub varchar(50), beer varchar(50), price numeric(4,2), primary key (pub,beer) foreign key (pub) references Pubs );

Some Types char(n) varchar(n) int smallint decimal(p, d) real boolean date time... character string of fixed length n character string of varying length up to n integer (usually 4 bytes) small integer (usually 2 bytes) fixed-point number comprising p digits, d of them to the right of the decimal point floating-point number true, false or unknown (in MySQL, true is non-zero, while false is zero) year, month and day values (e.g., YYYY-MM-DD) hour, minute and second values (e.g., HH:MM:SS)

Some Integrity Constraints primary key(a 1,..., A n ): attributes A 1,..., A n form a primary key of the relation foreign key(a 1,..., A n ) references R: values for attributes A 1,..., A n must correspond to values of primary key attributes in R

Some Integrity Constraints primary key(a 1,..., A n ): attributes A 1,..., A n form a primary key of the relation foreign key(a 1,..., A n ) references R: values for attributes A 1,..., A n must correspond to values of primary key attributes in R not null: null value not allowed for attribute

Some Integrity Constraints primary key(a 1,..., A n ): attributes A 1,..., A n form a primary key of the relation foreign key(a 1,..., A n ) references R: values for attributes A 1,..., A n must correspond to values of primary key attributes in R not null: null value not allowed for attribute unique: attributes values must be unique in relation (like a simple candidate key)

Some Integrity Constraints primary key(a 1,..., A n ): attributes A 1,..., A n form a primary key of the relation foreign key(a 1,..., A n ) references R: values for attributes A 1,..., A n must correspond to values of primary key attributes in R not null: null value not allowed for attribute unique: attributes values must be unique in relation (like a simple candidate key) check: check constraint on attribute value, e.g., check (price > 0)...

Inserting Data in a Relation Sells: pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 INSERT INTO Sells values ( White Horse, Shining Wit, 2.50);

Inserting Data in a Relation Sells: pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 INSERT INTO Sells values ( White Horse, Shining Wit, 2.50); INSERT INTO Sells (pub, beer) values ( White Horse, Bad Habit );

Inserting Data in a Relation Sells: pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 INSERT INTO Sells values ( White Horse, Shining Wit, 2.50); INSERT INTO Sells (pub, beer) values ( White Horse, Bad Habit ); INSERT INTO Sells values ( Horse and Hound, Bad Habit, 1.75); error

Inserting Data in a Relation Sells: pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 INSERT INTO Sells values ( White Horse, Shining Wit, 2.50); INSERT INTO Sells (pub, beer) values ( White Horse, Bad Habit ); INSERT INTO Sells values ( Horse and Hound, Bad Habit, 1.75); error INSERT INTO Sells (pub) values ( March Hare ); error

Changing a Relation Schema Add an attribute to a relation: ALTER TABLE Sells ADD( measure varchar(10));

Changing a Relation Schema Add an attribute to a relation: ALTER TABLE Sells ADD( measure varchar(10)); Change the data type of an attribute: ALTER TABLE Sells MODIFY( measure varchar(15));

Changing a Relation Schema Add an attribute to a relation: ALTER TABLE Sells ADD( measure varchar(10)); Change the data type of an attribute: ALTER TABLE Sells MODIFY( measure varchar(15)); Remove an attribute from a relation: ALTER TABLE Sells DROP(measure);

Changing a Relation Schema Add an attribute to a relation: ALTER TABLE Sells ADD( measure varchar(10)); Change the data type of an attribute: ALTER TABLE Sells MODIFY( measure varchar(15)); Remove an attribute from a relation: ALTER TABLE Sells DROP(measure); Delete a relation schema (relation must be empty): DROP TABLE Sells;

Some Notes on SQL Syntax Keywords (e.g., ADD, DROP) are not case sensitive Identifiers (used to name tables, columns, etc.) are not case sensitive (although in MySQL table names are case sensitive) Tables are defined within a (default) schema; for a non-default schema, use schema:table Comments can be enclosed in /* and */ (for multi-line comments) or started with -- (for a comment ended by the end of line)

SQL: Select-From-Where Overall form of an SQL query is SELECT desired attributes FROM one or more tables WHERE conditions on rows of the tables are satisfied

Running Example Pubs (name, location) Drinkers (name, location) Sells (pub, beer, price) Visits (drinker, pub) each pub has a name and location each drinker has a name and location where they live pubs sell beers at various prices drinkers visit various pubs

Running Example Tables Pubs: Sells: name location Horse and Hound Bloomsbury Hound and Hare Islington March Hare Bloomsbury Black Horse Islington White Horse Bloomsbury pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 March Hare Bad Habit 1.75 March Hare Rampant Ram 2.50 Black Horse Bad Habit 2.50 Black Horse Shining Wit 2.25 Black Horse Rampant Ram 2.50 White Horse Rampant Ram 2.75

Running Example Tables name location Alice Islington Drinkers: Bob Bloomsbury Carol Islington Dave Bloomsbury Eve Stratford Visits: drinker Alice Alice Bob Bob Carol Dave Eve pub Black Horse Hound and Hare Horse and Hound White Horse March Hare Hound and Hare March Hare

Retrieving an entire table SELECT * FROM Drinkers; name location Alice Islington Bob Bloomsbury Carol Islington Dave Bloomsbury Eve Stratford

Retrieving an entire table SELECT * FROM Drinkers; name location Alice Islington Bob Bloomsbury Carol Islington Dave Bloomsbury Eve Stratford name Alice Bob Carol Dave Eve location Islington Bloomsbury Islington Bloomsbury Stratford

Projection Find where pubs are located:

Projection Find where pubs are located: SELECT location FROM Pubs;

Projection Find where pubs are located: SELECT location FROM Pubs; name location Horse and Hound Bloomsbury Hound and Hare Islington March Hare Bloomsbury Black Horse Islington White Horse Bloomsbury

Projection Find where pubs are located: SELECT location FROM Pubs; name Horse and Hound Hound and Hare March Hare Black Horse White Horse location Bloomsbury Islington Bloomsbury Islington Bloomsbury location Bloomsbury Islington Bloomsbury Islington Bloomsbury SQL does not implement set semantics

Projection Find unique locations of pubs:

Projection Find unique locations of pubs: SELECT DISTINCT location FROM Pubs;

Projection Find unique locations of pubs: SELECT DISTINCT location FROM Pubs; name location Horse and Hound Bloomsbury Hound and Hare Islington March Hare Bloomsbury Black Horse Islington White Horse Bloomsbury

Projection Find unique locations of pubs: SELECT DISTINCT location FROM Pubs; name location Horse and Hound Bloomsbury Hound and Hare Islington March Hare Bloomsbury Black Horse Islington White Horse Bloomsbury location Bloomsbury Islington

Projection Find what beers are sold by which pubs:

Projection Find what beers are sold by which pubs: SELECT beer, pub FROM Sells;

Projection Find what beers are sold by which pubs: SELECT beer, pub FROM Sells; beer pub Bad Habit Horse and Hound Rampant Ram Horse and Hound Shining Wit Hound and Hare Rampant Ram Hound and Hare Bad Habit March Hare Rampant Ram March Hare Bad Habit Black Horse Shining Wit Black Horse Rampant Ram Black Horse Rampant Ram White Horse

Projection Find the prices of beers in euros:

Projection Find the prices of beers in euros: SELECT beer AS Ale, price*1.12 AS PriceInEuros FROM Sells;

Projection Find the prices of beers in euros: SELECT beer AS Ale, price*1.12 AS PriceInEuros FROM Sells; Ale Bad Habit 1.68 Rampant Ram 2.24 Shining Wit 3.08 Rampant Ram 2.80 Bad Habit 1.96 Rampant Ram 2.80 Bad Habit 2.80 Shining Wit 2.52 Rampant Ram 2.80 Rampant Ram 3.08 PriceInEuros

Selection Who visits the March Hare :

Selection Who visits the March Hare : SELECT drinker FROM Visits WHERE pub= March Hare ;

Selection Who visits the March Hare : SELECT drinker FROM Visits WHERE pub= March Hare ; drinker pub Alice Black Horse Alice Hound and Hare Bob Horse and Hound Bob White Horse Carol March Hare Dave Hound and Hare Eve March Hare

Selection Who visits the March Hare : SELECT drinker FROM Visits WHERE pub= March Hare ; drinker Alice Alice Bob Bob Carol Dave Eve pub Black Horse Hound and Hare Horse and Hound White Horse March Hare Hound and Hare March Hare drinker Carol Eve

Selection Which pubs sell some beer for less than 2.50?:

Selection Which pubs sell some beer for less than 2.50?: SELECT DISTINCT pub FROM Sells WHERE price < 2.50; (See Sells table)

Selection Which pubs sell some beer for less than 2.50?: SELECT DISTINCT pub FROM Sells WHERE price < 2.50; (See Sells table) pub Horse and Hound March Hare Black Horse

Selection Which pubs sell Bad Habit for less than 2.50?:

Selection Which pubs sell Bad Habit for less than 2.50?: SELECT pub FROM Sells WHERE beer= Bad Habit AND price < 2.50; (See Sells table)

Selection Which pubs sell Bad Habit for less than 2.50?: SELECT pub FROM Sells WHERE beer= Bad Habit AND price < 2.50; (See Sells table) pub Horse and Hound March Hare

Selection Condition A boolean expression in the WHERE clause may contain the following operators: AND: beer= Bad Habit AND price < 2.50 NOT: NOT beer= Bad Habit Equivalently: beer <> Bad Habit OR: beer= Bad Habit OR beer= Rampant Ram

Selection Condition A boolean expression in the WHERE clause may contain the following operators: AND: beer= Bad Habit AND price < 2.50 NOT: NOT beer= Bad Habit Equivalently: beer <> Bad Habit OR: beer= Bad Habit OR beer= Rampant Ram or any combination: beer= Bad Habit OR beer= Rampant Ram AND price < 2.50 which is equivalent to beer= Bad Habit OR (beer= Rampant Ram AND price < 2.50)