Information Systems (Informationssysteme)

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

Unit 3. Retrieving Data from Multiple Tables

In this Lecture SQL SELECT. Example Tables. SQL SELECT Overview. WHERE Clauses. DISTINCT and ALL SQL SELECT. For more information

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

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

Relational Database: Additional Operations on Relations; SQL

Object-Oriented Query Languages: Object Query Language (OQL)

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

Advanced SQL - Subqueries and Complex Joins

SQL is capable in manipulating relational data SQL is not good for many other tasks

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

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

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

Financial Data Access with SQL, Excel & VBA

The process of database development. Logical model: relational DBMS. Relation

Relational Division and SQL

Performing Queries Using PROC SQL (1)

Database CIS 340. lab#6. I.Arwa Najdi

Advance DBMS. Structured Query Language (SQL)

CHAPTER 12. SQL Joins. Exam Objectives

SQL Injection. SQL Injection. CSCI 4971 Secure Software Principles. Rensselaer Polytechnic Institute. Spring

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

Data Warehousing. Jens Teubner, TU Dortmund Winter 2015/16. Jens Teubner Data Warehousing Winter 2015/16 1

Displaying Data from Multiple Tables

Database Query 1: SQL Basics

David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

A basic create statement for a simple student table would look like the following.

Calculations that Span Dimensions

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

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

Unit 10: Microsoft Access Queries

SQL: Queries, Programming, Triggers

AN INTRODUCTION TO THE SQL PROCEDURE Chris Yindra, C. Y. Associates

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

3. Relational Model and Relational Algebra

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

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

SQL: Queries, Programming, Triggers

5.1 Database Schema Schema Generation in SQL

Recognizing and resolving Chasm and Fan traps when designing universes

Where? Originating Table Employees Departments

Introduction to Microsoft Jet SQL

Introduction to Databases

The Relational Data Model: Structure

Outline. SAS-seminar Proc SQL, the pass-through facility. What is SQL? What is a database? What is Proc SQL? What is SQL and what is a database

SQL Tables, Keys, Views, Indexes

Relational Algebra. Basic Operations Algebra of Bags

The join operation allows you to combine related rows of data found in two tables into a single result set.

There are five fields or columns, with names and types as shown above.

SQL SELECT Query: Intermediate

Information Systems SQL. Nikolaj Popov

Introduction to database design

Introduction to tuple calculus Tore Risch

Supporting Data Set Joins in BIRT

Data Warehousing. Jens Teubner, TU Dortmund Winter 2015/16. Jens Teubner Data Warehousing Winter 2015/16 1

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

Effective Use of SQL in SAS Programming

CSC 443 Data Base Management Systems. Basic SQL

Crystal Reports Form Letters Replace database exports and Word mail merges with Crystal's powerful form letter capabilities.

IT2305 Database Systems I (Compulsory)

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Real SQL Programming 1

Performance Implications of Various Cursor Types in Microsoft SQL Server. By: Edward Whalen Performance Tuning Corporation

CS 2316 Data Manipulation for Engineers

Toad for Data Analysts, Tips n Tricks

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. Introduction to Databases. Why databases? Why not use XML?

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Databases 2011 The Relational Model and SQL

public class ResultSetTable implements TabelModel { ResultSet result; ResultSetMetaData metadata; int num cols;

An extension to DBAPI 2.0 for easier SQL queries

Query-by-Example (QBE)

EECS 647: Introduction to Database Systems

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

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

Oracle Database 12c: Introduction to SQL Ed 1.1

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

SQL Basics. Introduction to Standard Query Language

Alternatives to Merging SAS Data Sets But Be Careful

D B M G Data Base and Data Mining Group of Politecnico di Torino

Using SQL Queries in Crystal Reports

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

SQL. Short introduction

1. Download the Order Guide by clicking on the link above and following the download instructions.

Foundations & Fundamentals. A PROC SQL Primer. Matt Taylor, Carolina Analytical Consulting, LLC, Charlotte, NC

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

Data Modeling. Database Systems: The Complete Book Ch ,

T-SQL STANDARD ELEMENTS

SQL JOINs and VIEWs. Chapter 3.3 V3.0. Napier University Dr Gordon Russell

Programming with SQL

SQL NULL s, Constraints, Triggers

6NF CONCEPTUAL MODELS AND DATA WAREHOUSING 2.0

Information Systems Modelling Information Systems I: Databases

Serious Threat. Targets for Attack. Characterization of Attack. SQL Injection 4/9/2010 COMP On August 17, 2009, the United States Justice

Transcription:

Information Systems (Informationssysteme) Jens Teubner, TU Dortmund jensteubner@cstu-dortmundde Summer 2015 c Jens Teubner Information Systems Summer 2015 1

Part III A Very Brief Introduction to SQL c Jens Teubner Information Systems Summer 2015 29

SQL Structured Query Language By far the most important query language today is SQL Structured Query Language Originally meant to be used by end users Today supported by virtually any database system SQL operates on relational data: Ingredients Name Alcohol InStock Price Orange Juice 00 12 299 Campari 250 5 1295 Mineral Water 00 10 149 Bacardi 375 3 1698 Real databases may contain 100s or 1000s of tables, sometimes with billions of rows (also: tuples) c Jens Teubner Information Systems Summer 2015 30

Our First SQL Query The key construct of SQL is the SELECT-FROM-WHERE clause: SELECT Name, Price FROM Ingredients WHERE Alcohol = 0 SELECT Choose a set of columns to be reported in the query result We ll later call this projection, not selection FROM Choose a table where rows should be taken from WHERE Additional conditions that rows must satisfy in order to appear in the result (the WHERE clause is optional) This is what we call a selection c Jens Teubner Information Systems Summer 2015 31

Ingredients Name Alcohol InStock Price Orange Juice 00 12 299 Campari 250 5 1295 Mineral Water 00 10 149 Bacardi 375 3 1698 + SELECT Name, Price FROM Ingredients WHERE Alcohol = 0 = Name Price Mineral Water 149 Orange Juice 299 c Jens Teubner Information Systems Summer 2015 32

3 Delivery time in days c Jens Teubner Information Systems Summer 2015 33 Data in Multiple Tables Cocktail ingredients are sold by various suppliers (for a certain price), which could be represented as SoldBy Ingredient Supplier DelTim 3 Price Orange Juice A&P Supermarket 1 249 Orange Juice Shop Rite 3 279 Campari Joe s Liquor Store 2 1495 Bacardi Liquor s & More 5 1399 Mineral Water Shop Rite 3 189 Bacardi Joe s Liquor Store 2 1499

4 Use * in the SELECT clause when you simply want to choose all columns c Jens Teubner Information Systems Summer 2015 34 When multiple tables are reference in the FROM clause, this is interpreted as the Cartesian product of the referenced tables: 4 SELECT * FROM Ingredients, SoldBy Ingredients SoldBy Name Alcohol InStock Price Ingredient Supplier DelTim Price Orange Juice 00 12 299 Orange Juice A&P Supermarket 1 249 Orange Juice 00 12 299 Orange Juice Shop Rite 3 279 Orange Juice 00 12 299 Campari Joe s Liquor Store 2 1495 Orange Juice 00 12 299 Bacardi Liquors & More 5 1399 Orange Juice 00 12 299 Mineral Water Shop Rite 3 189 Orange Juice 00 12 299 Bacardi Joe s Liquor Store 2 1499 Campari 250 5 1295 Orange Juice A&P Supermarket 1 249 Campari 250 5 1295 Orange Juice Shop Rite 3 279 Campari 250 5 1295 Campari Joe s Liquor Store 2 1495 Campari 250 5 1295 Bacardi Liquors & More 5 1399 Campari 250 5 1295 Mineral Water Shop Rite 3 189 Campari 250 5 1295 Bacardi Joe s Liquor Store 2 1499 Mineral Water 00 10 149 Orange Juice A&P Supermarket 1 249 Mineral Water 00 10 149 Orange Juice Shop Rite 3 279

Queries over Multiple Tables In practice, you rarely want to see this Cartesian product in the final result Use a WHERE clause to select only semantically related data SELECT Name, InStock, Supplier FROM Ingredients, SoldBy WHERE Name = Ingredient Name InStock Supplier Orange Juice 12 A&P Supermarket Orange Juice 12 Shop Rite Campari 5 Joe s Liquor Store Mineral Water 10 Shop Rite Bacardi 3 Liquors & More Bacardi 3 Joe s Liquor Store c Jens Teubner Information Systems Summer 2015 35

Queries over Multiple Tables Resolve ambiguities by prepending column names with their table name: SELECT Name, InStock, Supplier, SoldByPrice FROM Ingredients, SoldBy WHERE Name = Ingredient AND SoldByPrice < IngredientsPrice Name InStock Supplier Price Orange Juice 12 A&P Supermarket 249 Orange Juice 12 Shop Rite 279 Bacardi 3 Liquors & More 1399 Bacardi 3 Joe s Liquor Store 1499 c Jens Teubner Information Systems Summer 2015 36

Tuple Variables or introduce tuple variables for easier reference: SELECT Name, InStock, Supplier, sprice FROM Ingredients AS i, SoldBy AS s WHERE Name = Ingredient AND sprice < iprice Name InStock Supplier Price Orange Juice 12 A&P Supermarket 249 Orange Juice 12 Shop Rite 279 Bacardi 3 Liquors & More 1399 Bacardi 3 Joe s Liquor Store 1499 (The keyword AS is optional; SoldBy s would mean just the same) c Jens Teubner Information Systems Summer 2015 37

Semantics of SQL SELECT-FROM-WHERE Expressions Conceptually, the query does the following: SELECT AttList FROM TableName 1, TableName 2, WHERE Condition TableName 1 TableName 2 Condition AttList FROM WHERE SELECT (But most likely, the database system will choose a better strategy to actually execute the query) c Jens Teubner Information Systems Summer 2015 38

Concluding Remarks SQL is case insensitive; use as a string delimiter It is okay to reference the same table multiple times in a FROM clause ( self-join ) Use tuple variables then to tell things apart Never, never ever, write queries where the correctness depends on the current table contents Eg, the correct answer to give me names and prices of all non-alcoholic ingredients is not SELECT Name, Price FROM Ingredients WHERE Name = Orange Juice OR Name = Mineral Water c Jens Teubner Information Systems Summer 2015 39