Data Structure: Relational Model. Programming Interface: JDBC/ODBC. SQL Queries: The Basic From



Similar documents
Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Oracle Database: SQL and PL/SQL Fundamentals

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: SQL and PL/SQL Fundamentals NEW

1 Structured Query Language: Again. 2 Joining Tables

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Relational Database: Additional Operations on Relations; SQL

Oracle Database: SQL and PL/SQL Fundamentals

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

SQL: Queries, Programming, Triggers

Oracle Database 12c: Introduction to SQL Ed 1.1

SQL Server 2008 Core Skills. Gary Young 2011

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Databases 2011 The Relational Model and SQL

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

DBMS / Business Intelligence, SQL Server

Chapter 5. SQL: Queries, Constraints, Triggers

Oracle Database 10g: Introduction to SQL

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

Instant SQL Programming

Figure 4.12.Insurancedatabase.

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Chapter 13: Query Optimization

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Simple SQL Queries (3)

Advanced SQL. Lecture 3. Outline. Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Relational Algebra and SQL

MySQL for Beginners Ed 3

Introduction to tuple calculus Tore Risch

Querying Microsoft SQL Server

Using Temporary Tables to Improve Performance for SQL Data Services

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Chapter 6 The database Language SQL as a tutorial

Oracle Database 11g SQL

Execution Strategies for SQL Subqueries

Week 4 & 5: SQL. SQL as a Query Language

Introduction to SQL C H A P T E R3. Exercises

Query-by-Example (QBE)

Advance DBMS. Structured Query Language (SQL)

IT2304: Database Systems 1 (DBS 1)

Structured Query Language (SQL)

University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao

Oracle 10g PL/SQL Training

Course ID#: W 35 Hrs. Course Content

IT2305 Database Systems I (Compulsory)

T-SQL STANDARD ELEMENTS

Oracle Database: SQL and PL/SQL Fundamentals NEW

Aggregating Data Using Group Functions

Querying Microsoft SQL Server (20461) H8N61S

Querying Microsoft SQL Server 20461C; 5 days

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Relational Division and SQL

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

SQL Nested & Complex Queries. CS 377: Database Systems

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

SQL (structured query language)

ICAB4136B Use structured query language to create database structures and manipulate data

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Introduction to Microsoft Jet SQL

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Information Systems SQL. Nikolaj Popov

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.


MOC QUERYING MICROSOFT SQL SERVER

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008

Introduction to SQL: Data Retrieving

Effective Use of SQL in SAS Programming

1. INTRODUCTION TO RDBMS

SQL NULL s, Constraints, Triggers

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

DATABASE DESIGN AND IMPLEMENTATION II SAULT COLLEGE OF APPLIED ARTS AND TECHNOLOGY SAULT STE. MARIE, ONTARIO. Sault College

Querying Microsoft SQL Server 2012

Relational Databases

Chapter 14: Query Optimization

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Introduction to database design

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Financial Data Access with SQL, Excel & VBA

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Quiz 2: Database Systems I Instructor: Hassan Khosravi Spring 2012 CMPT 354

CSE 562 Database Systems

Mini User's Guide for SQL*Plus T. J. Teorey

Unit 10: Microsoft Access Queries

How To Create A Table In Sql (Ahem)

Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM

Performing Queries Using PROC SQL (1)

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Querying Microsoft SQL Server 2012

SQL Query Evaluation. Winter Lecture 23

Transcription:

Data Structure: Relational Moel Relational atabases: Schema + Data Schema (also calle scheme): collection of tables (also calle relations) each table has a set of attributes no repeating relation names, no repeating attributes in one table Data (also calle instance): set of tuples tuples have one value for each attribute of the table they belong Movie Ti t l e Di rector Ac t o r Wil Lynch Winger Sky erto Winger Res eatty eatty erto rano erto Winger erto Snyer Scheule The a t e r Ti t l e Oeon Wil Forum Res Forum Sky Programming Interface: JDC/ODC How client opens connection with server How access & moification commans are issue SQL Queries: The asic From asic form SELECT a,, an FROM R,, RM WHERE conition WHERE clause is optional When more than one π a, relations, anσ conition of the (Rx FROM xrm) have an attribute name A we refer to a specific A attribute as <RelationName>.A Fin titles of currently playing movies Fin the titles of all movies by erto WHERE Director= erto Fin the titles an the irectors of all currently playing movies SELECT Movie.Title, Director, Scheule WHERE Movie.Title=Scheule.Title

SQL Queries: Aliases Use the same relation more than once in the FROM clause Tuple variables Example: fin actors who are also irectors SELECT t.actor t s WHERE t.actor=s.director SQL Queries: Nesting The WHERE clause can contain preicates of the form attr/value IN <SQL query> attr/value NOT IN <SQL query> The preicate is satisfie if the attr or value appears in the result of the neste <SQL query> Queries involving nesting but no negation can always be unneste, unlike queries with nesting an negation Typical use: fin objects that always satisfy property X, e.g., fin actors playing in every movie by erto : SELECT Actor WHERE Actor NOT IN (SELECT t.actor t s, WHERE s.director= erto AND t.actor NOT IN WHERE Title=s.Title)) The shae query fins actors NOT playing in some movie by erto The top lines complement the shae part Homework Problem Compare with shae sub-query of previous page. The sample ata may help you SELECT Actor WHERE Actor NOT IN WHERE Director= erto ) Actor Director a a b c X X Movie 3 3 6

3 Neste Queries: Existential an Universal Quantification A op ANY <neste query> is satisfie if there is a value X in the result of the <neste query> an the conition A op X is satisfie ANY aka SOME A op ALL <neste query> is satisfie if for every value X in the result of the <neste query> the conition A op X is satisfie Fin irectors of currently playing movies SELECT Director WHERE Title = ANY Fin the employees with the highest salary SELECT Name WHERE Salary >= ALL SELECT Salary Neste Queries: Set Comparison <neste query > CONTAINS <neste query > Fin actors playing in every movie by erto SELECT s.actor s WHERE ( t WHERE t.actor = s.actor) CONTAINS ( WHERE Director = erto ) SQL:Union, Intersection, Difference Union <SQL query > UNION <SQL query > Intersection <SQL query > INTERSECT <SQL query > Difference <SQL query > MINUS <SQL query > Fin all actors or irectors ) UNION (SELECT Director ) Fin all actors who are not irectors ) MINUS (SELECT Director )

4 SQL Queries: Aggregation an Grouping There is no relational algebra equivalent for aggregation an grouping Aggregate functions: AVG, COUNT, MIN, MAX, SUM, an recently user efine functions as well Group-by Employee Name De pt Sal ar y Joe Toys 45 Nick PCs 50 Jim Toys 35 Jack PCs 40 Fin the average salary of all employees SELECT AvgSal=Avg(Salary) AvgSal 4.5 Fin the average salary for each epartment SELECT Dept, AvgSal=Avg(Salary) De pt AvgSal Toys 40 PCs 45 GROUP-Y Dept SQL Grouping: Conitions that Apply on Groups HAVING clause Fin the average salary of for each epartment that has more than employee SELECT Dept, AvgSal=(Avg(Salary)) GROUP-Y Dept HAVING COUNT(Name)> SQL: More ells an Whistles... Select all attributes using * Pattern matching conitions <attr> LIKE <pattern> Retrieve all movie attributes of currently playing movies SELECT Movie.*, Scheule WHERE Movie.Title=Scheule.Title Retrieve all movies where the title starts with Ta SELECT * WHERE Title LIKE Ta

5 an a Few Dirty Points Duplicate elimination must be explicitly requeste SELECT DISTINCT FROM WHERE Null values all comparisons involving NULL are false by efinition all aggregation operations, except count, ignore NULL values SELECT DISTINCT Title Ti t l e Ti t l e Ti t l e Di rector Ac t o r Wil Lynch Winger Sky erto Winger Res NULL eatty erto rano erto Winger erto NULL SQL as a Data Manipulation Language: Insertions inserting tuples INSERT INTO R VALUES (v,,vk); some values may be left NULL use results of queries for insertion INSERT INTO R SELECT FROM WHERE INSERT INTO Movie VALUES ( rave, Gibson, Gibson ); INSERT INTO Movie(Title,Director) VALUES ( rave, Gibson ); INSERT INTO EuroMovie SELECT * WHERE Director = erto SQL as a Data Manipulation Language: Upates an Deletions Deletion basic form: elete every tuple that satisfies <con> DELETE FROM R WHERE <con> Upate basic form: upate every tuple that satisfies <con> in the way specifie by the SET clause UPDATE R SET A=<exp>,, Ak=<expk> WHERE <con> Delete the movies that are not currently playing DELETE WHERE Title NOT IN Change all erto entries to ertoluci UPDATE Movie SET Director= ertoluci WHERE Director= erto Increase all salaries in the Toys ept by 0% UPDATE Employee SET Salary =. * Salary WHERE Dept = Toys The rich get richer exercise: Increase by 0% the salary of the employee with the highest salary