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

Similar documents
Relational Database Languages

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

Chapter-14 SQL COMMANDS

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Oracle Database 10g: Introduction to SQL

CSC 453 Database Technologies. Tanu Malik DePaul University

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

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

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

UFCEKG 20 2 : Data, Schemas and Applications

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

Oracle Database 12c SQL Fundamentals


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

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

SQL STRUCTURED QUERY LANGUAGE

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

Introduction to the Structured Query Language [ SQL ] (Significant Concepts)

Slides by: Ms. Shree Jaswal

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

CSIE30600 Database Systems Basic SQL 2. Outline

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

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

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

Chapter 3: Introduction to SQL

RETRIEVING DATA USING THE SQL SELECT STATEMENT

Sample Question Paper

Views and Virtual Tables

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

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

Chapter 3: Introduction to SQL

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

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

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Relational Database Language

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions.

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Chapter 1 SQL and Data

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

Oracle Database: Introduction to SQL Ed 2

Introduction to SQL. SQL is a standard language for accessing and manipulating databases. What is SQL?

CSEN 501 CSEN501 - Databases I

Data Infrastructure IRAP Training 6/27/2016

Database Management Systems,

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

پوهنتون کابل پوهنځی كمپيوترساینس

Relational Algebra and SQL

Relational Database Management Systems for Epidemiologists: SQL Part I

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

Lab # 4. Data Definition Language (DDL)

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

UNIT-IV (Relational Database Language, PL/SQL)

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

Course Outline and Objectives: Database Programming with SQL

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

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

Introduction to Computer Science and Business

CSC Web Programming. Introduction to SQL

The SQL database language Parts of the SQL language

Database Management System 9

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Database Programming with SQL

normalization are being violated o Apply the rule of Third Normal Form to resolve a violation in the model

QQ Group

CS352 Lecture - Introduction to SQL

Relational Databases

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

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

DATABASE TECHNOLOGY - 1MB025

Structured Query Language (SQL)

Silberschatz, Korth and Sudarshan See for conditions on re-use

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

The Structured Query Language Get Started

Bonus Content. Glossary

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

Lecture 2: Chapter Objectives. Relational Data Structure. Relational Data Model. Introduction to Relational Model & Structured Query Language (SQL)

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

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

Advanced SQL Tribal Data Workshop Joe Nowinski

SQL: Queries, Constraints, Triggers (iii)

Oracle Database SQL Basics

Lecture 2: Chapter Objectives

Chapter 9: Working with MySQL

SQL. History. From Wikipedia, the free encyclopedia.

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

Downloaded from

DATABASE TECHNOLOGY - 1MB025

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

Oracle Database 11g: SQL and PL/SQL Fundamentals

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

Database Management Systems,

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 key attributes are not 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 2011 and 2013; 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 graphics and artificial intelligence. 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. 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. 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