History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)



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

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

How To Create A Table In Sql (Ahem)

SQL. Short introduction

Oracle Database 10g: Introduction to SQL

A Brief Introduction to MySQL

Oracle Database 12c: Introduction to SQL Ed 1.1

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

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

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

CSC 443 Data Base Management Systems. Basic SQL

Instant SQL Programming

T-SQL STANDARD ELEMENTS

Oracle SQL. Course Summary. Duration. Objectives

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

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

Oracle Database: Introduction to SQL

Introduction to Microsoft Jet SQL

Chapter 5. SQL: Queries, Constraints, Triggers

Database Query 1: SQL Basics

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

Oracle Database: Introduction to SQL

Information Systems SQL. Nikolaj Popov

Basic Concepts of Database Systems

Oracle Database: Introduction to SQL

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Oracle Database: SQL and PL/SQL Fundamentals

Lecture 6. SQL, Logical DB Design

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Oracle Database: SQL and PL/SQL Fundamentals

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

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

Relational Databases

Advance DBMS. Structured Query Language (SQL)

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Oracle Database 11g SQL

SQL NULL s, Constraints, Triggers

Databases in Engineering / Lab-1 (MS-Access/SQL)

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

Relational Databases. Christopher Simpkins

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle 10g PL/SQL Training

BCA. Database Management System

MySQL Command Syntax

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

Netezza SQL Class Outline

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

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

SQL: Queries, Programming, Triggers

Database Programming with PL/SQL: Learning Objectives

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Financial Data Access with SQL, Excel & VBA

Data Mining Extensions (DMX) Reference

Relational Algebra and SQL

Introduction to SQL ( )

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Relational Database: Additional Operations on Relations; SQL

Introduction to SQL: Data Retrieving

Fundamentals of Database Design

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Programming with SQL

1 Structured Query Language: Again. 2 Joining Tables

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

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.


IT2305 Database Systems I (Compulsory)

SQL: Queries, Programming, Triggers

6 CHAPTER. Relational Database Management Systems and SQL Chapter Objectives In this chapter you will learn the following:

Oracle Database: SQL and PL/SQL Fundamentals NEW

EECS 647: Introduction to Database Systems


SQL Programming. Student Workbook

Microsoft Access Lesson 5: Structured Query Language (SQL)

FileMaker 13. SQL Reference

IT2304: Database Systems 1 (DBS 1)

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

3.GETTING STARTED WITH ORACLE8i

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

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

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

Part 4: Database Language - SQL

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

The Structured Query Language. De facto standard used to interact with relational DB management systems Two major branches

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

SQL Server 2008 Core Skills. Gary Young 2011

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

The Relational Model. Why Study the Relational Model?

New SQL Features in Firebird 3

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

Chapter 4. SQL Concepts & Facilities. Is SQL an End User Tool? AS/400 ANSI SQL Advanced Facilities

Microsoft Access 3: Understanding and Creating Queries

Databases 2011 The Relational Model and SQL

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Structured Query Language (SQL)

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

SQL Injection. The ability to inject SQL commands into the database engine through an existing application

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

Transcription:

Relational Database Languages Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG) Domain relational calculus QBE (used in Access) History of SQL Standards: SQL-86 (ANSI, ISO) SQL-89 SQL-92 (SQL2) SQL:1999 (SQL3) SQL:2003 SQL:2006 SQL:2008 SQL:2011 Many different flavors of SQL: sqlplus, SQLServer, MySQL, etc. Here: Oracle SQL IBM developed SEQUEL in early 70s (Structured English Query Language) Renamed SQL (Structured Query Language) 1

Parts of SQL DDL DML Creating (CREATE), Modifying (ALTER), and Removing (DROP) Retrieving (SELECT), Inserting (INSERT), Modifying (UPDATE), and Removing (DELETE). Catalogs Schemas Relations (Tables) Constraints Domains Triggers DCL Grant privileges (GRANT) Revoke privileges (REVOKE) SELECT Combination of select and project operations. Basic Syntax: SELECT attribute_list FROM table_list WHERE condition GROUP BY attribute_list HAVING condition ORDER BY attribute_list; Example: SELECT FROM WHERE LastName, SID student career = 'UGRD' AND started < 2008; SELECT * and empty WHERE SELECT * FROM student; SELECT * FROM student, studentgroup; SELECT * FROM student, studentgroup WHERE presidentid = SID 2

SELECT Examples University List the SSNs of all students List all names of all student groups List the names of all programs List the name of all students in the IT department Duplicates in SQL Duplicates can occur if no key attribute is selected SQL keeps duplicates, for several reasons: Cheaper to implement (duplicate elimination is expensive) Duplicates might be required (e.g., aggregate functions) Removal of duplicates can be forced using DISTINCT SELECT DISTINCT SELECT ALL Example: List cities in which students live (university). 3

How to Write Simple SQL Query 3 Stages Before you write the SQL Writing the SQL After Writing the SQL Before you write the SQL Make sure you understand the problem. Clarify if necessary Do it by hand. Reflect on what you did. Writing the SQL Start with FROM which tables are involved, how often? Then do WHERE first join all tables (n tables need n-1 equals) - test then add particular conditions Finally, do SELECT What info do you need to display 4

After Writing the SQL Test Run the query Compare output to what you expected sanity check: does it make sense? In case of problems: read query Renaming (Aliases) If necessary we can rename attributes: SELECT LastName ', ' FirstName AS Name FROM student; and tables (drop AS in Oracle for table renaming) SELECT S.Lastname, SG.Name FROM student AS S, studentgroup AS SG WHERE S.SID = SG.PresidentID; 5

Renaming Examples University List the names of all students and expected graduation year List the names of student groups and the names of their presidents Conditions = Equality < Less than <= Less than or equal > Greater than >= Greater than or equal <> or!= not equal (depends on system) LIKE Allows Wildcards (Oracle) % (any number of characters) _ (single character) Also (Oracle): REGEXP_LIKE (text, pattern) Operators + Addition (works for dates in Oracle/Access) - Subtraction (works for dates in Oracle/Access) * Multiplication / Division Concatenation (for strings) and, or, not Boolean operations 6

Functions (Oracle) Strings and Numbers String type: Length(s) Rtrim(s), Ltrim(s) Length of string s Delete trailing (leading) spaces Numeric type: Round(x) Round(x,k) Abs(x) Exp(x) Round x to integer Round x to k positions Absolute value of x e x See http://download.oracle.com/docs/cd/b19306_01/server.102/b14200/functions.htm#i1482196 Functions (Oracle) Date/Timestamp extract last_day months_between to_char(date, fmt) date to integers last day of month months between two dates format date (e.g. MM, MON, MONTH) SELECT extract (year from current_date) FROM dual; Between and Ordering For number types, ranges can be defined using BETWEEN. SELECT LastName, SID FROM student WHERE started between 2001 and 2003; The ORDER BY clauses allows ordered output multiple attributes, expressions allowed ASC (default) and DESC to specify order SELECT LastName, SID FROM student ORDER BY started ; SELECT LastName, SID FROM student ORDER BY started DESC, LastName; 7

Truth and Tables Definition: A function is a truth-function iff its truth only depends on the truth of its arguments. Theorem (Post): Every truth-function can be written using only negation and disjunction. (Or negation and conjunction.) Logic examples I Students in computer science and information systems Graduate students unless they are PhDs. List all graduate students in computer science, computer gaming and information systems. List graduate and undergraduate students not from Chicago that started before 2010 and after 2012. List courses that violate the rule: All gaming courses must be 300 or above, all computer science courses must be 400 or above and IT courses must be at most 200 level. Check whether there are students that violate the following rules: Computer gaming students must be undergraduates, information system students must be graduates. List students that violate the rule: A PhD student cannot be an undergraduate. Logic examples II List students that have both a graduate and an undergraduate record (go by name). List courses that have both grad and undergrad versions (go by title). List students that don t have a SSN listed. List years in which no undergraduate computer science student started. 8

Logic examples II List students that have both a graduate and an undergraduate record (go by name). List courses that have both grad and undergrad versions (go by title). List students that don t have a SSN listed. List years in which no undergraduate computer science student started. Some of these we can t do yet (4), for some we need additional knowledge (3), and some we can do (1,2) but there must be better solutions. Nulls Reasons: we don t know value value isn t applicable we don t know whether value is applicable Null is not a value (well), to test for it use is null is not null Examples: List students that don t have a SSN listed. List studentgroups that don t currently have a president. List students that do have a SSN listed. We require that students that don t have a last name do enter a first name. List students that violate this requirement. Nulls in Conditions Example SELECT * FROM student WHERE SSN = 123123123; SELECT * FROM student WHERE NOT(SSN = 123123123);? 9

Three-valued Logic p q p OR q p AND q True True True True True Unknown True Unknown True False True False Unknown True True Unknown Unknown Unknown Unknown Unknown Unknown False Unknown False False True True False False Unknown Unknown False False False False False p True Unknown False NOT p False Unknown True WHERE conditions: have to be true CHECK constraints: can t be false Nulls in Expressions Null in Operations null x = null ( operation like +, -, *, /, etc.) SELECT EmpID, salary * (1 + level *.05) FROM employee; Null in Functions f(, null, ) = null (for most functions f, not all, e.g. ) SELECT least(price_new, price_used) FROM bookwprices; Solution: coalesce(x, y, z, ) evaluates to first non-null value Objections to Nulls we don t know what the Null value represents Codd suggested two values: Missing Applicable/Missing Inapplicable 3-valued behavior is counterintuitive and leads to SQL programs behaving incorrectly in presence of Nulls wastes space (needs extra indicator bit) E.g., see the Third Manifesto: http://en.wikipedia.org/wiki/the_third_manifesto 10

Date s Example From Chris Date: SQL and Relational Theory, O Reilly, 2009. 11