CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.

Slides:



Advertisements
Similar presentations
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Advertisements

1 Lecture 11: Basic SQL, Integrity constraints
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
CS405G: Introduction to Database Systems Final Review.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
SQL: Interactive Queries (1) John Ortiz Lecture 11SQL: Interactive Queries (1)2 Basic Select Statement  Basic form of the select statement: select target-attribute-list.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Database: SQL and MySQL
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
1 Querying a Single Table Structured Query Language (SQL) - Part II.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CS 405G: Introduction to Database Systems Relational Algebra.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
CS 405G: Introduction to Database Systems SQL III.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL SELECT Statements Lecture
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 5 Introduction to SQL.
Chapter 3 Introduction to SQL
Chapter 3 Introduction to SQL(2)
Lecture#6: Fun with SQL (Part 1)
Writing Basic SQL SELECT Statements
Basic select statement
Introduction to SQL.
EECS 647: Introduction to Database Systems
CS 405G: Introduction to Database Systems
Lecture#7: Fun with SQL (Part 2)
CS 405G: Introduction to Database Systems
CS405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
Relational Databases The Relational Model.
Relational Databases The Relational Model.
CS 405G: Introduction to Database Systems
CMPT 354: Database System I
Writing Basic SQL SELECT Statements
Lectures 3: Introduction to SQL 2
CS 405G: Introduction to Database Systems
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
Recap – Relational languages
CS 405G: Introduction to Database Systems
Presentation transcript:

CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007

3/13/2016Jinze University of Kentucky2 Database Design

3/13/2016Jinze University of Kentucky3 SQL SQL: Structured Query Language Pronounced “S-Q-L” or “sequel” The standard query language supported by most commercial DBMS A brief history IBM System R ANSI SQL89 ANSI SQL92 (SQL2) ANSI SQL99 (SQL3) ANSI SQL 2003 (+OLAP, XML, etc.)

3/13/2016Jinze University of Kentucky4 Creating and dropping tables CREATE TABLE table_name ( …, column_name i column_type i, … ); DROP TABLE table_name ; Examples create table Student (SID integer, name varchar(30), varchar(30), age integer, GPA float); create table Course (CID char(10), title varchar(100)); create table Enroll (SID integer, CID char(10)); drop table Student; drop table Course; drop table Enroll; -- everything from -- to the end of the line is ignored. -- SQL is insensitive to white space. -- SQL is insensitive to case (e.g.,...Course... is equivalent to --...COURSE...)

3/13/2016Jinze University of Kentucky5 Basic queries: SFW statement SELECT A 1, A 2, …, A n FROM R 1, R 2, …, R m WHERE condition ; Also called an SPJ (select-project-join) query (almost) Equivalent to relational algebra query π A 1, A 2, …, A n (σ condition (R 1 X R 2 X … X R m ))

3/13/2016Jinze University of Kentucky6 Semantics of SFW SELECT E 1, E 2, …, E n FROM R 1, R 2, …, R m WHERE condition; For each t 1 in R 1 : For each t 2 in R 2 : … … For each t m in R m : If condition is true over t 1, t 2, …, t m : Compute and output E 1, E 2, …, E n as a row t 1, t 2, …, t m are often called tuple variables Not 100% correct, we will see

3/13/2016Jinze University of Kentucky7 Example: selection and projection Name of students under 18 SELECT name FROM Student WHERE age < 20; π name ( σ age <20 (Student)) sidnameagegpa 1234John Smith Mary Carter Bob Lee Susan Wong Kevin Kim182.9 sidnameagegpa 1234John Smith Mary Carter Bob Lee Susan Wong Kevin Kim182.9

3/13/2016Jinze University of Kentucky8 Example: Operations When was Lisa born? SELECT 2006 – age FROM Student WHERE name = ’Lisa’; SELECT list can contain expressions Can also use built-in functions such as SUBSTR, ABS, etc. String literals (case sensitive) are enclosed in single quotes

3/13/2016Jinze University of Kentucky9 Example: reading a table SELECT * FROM Student; Single-table query, so no cross product here WHERE clause is optional * is a short hand for “all columns”

3/13/2016Jinze University of Kentucky10 Example: join SID’s and names of students taking the “Database” courses SELECT Student.SID, Student.name FROM Student, Enroll, Course WHERE Student.SID = Enroll.SID AND Enroll.CID = Course.CID AND title = ’Database’; Okay to omit table_name in table_name. column_name if column_name is unique A better way to deal with string is to use “LIKE” and %, which matches with any string with length 0 or more AND title LIKE ’%Database%’; \ is the escape operator ‘_’ is used for representing any a single character

3/13/2016Jinze University of Kentucky11 ORDER BY SELECT... FROM … WHERE ORDER BY output_column [ ASC | DESC ], … ; ASC = ascending, DESC = descending Operational semantics After SELECT list has been computed, sort the output according to ORDER BY specification

3/13/2016Jinze University of Kentucky12 ORDER BY example List all students, sort them by GPA (descending) and name (ascending) SELECT SID, name, age, GPA FROM Student ORDER BY GPA DESC, name; ASC is the default option Strictly speaking, only output columns can appear in ORDER BY clause (although some DBMS support more) Can use sequence numbers instead of names to refer to output columns: ORDER BY 4 DESC, 2;

3/13/2016Jinze University of Kentucky13 Example: rename SID’s of all pairs of classmates Relational algebra query: π e1.SID, e2.SID ( …ρ e1 Enroll e1.CID = e2.CID ^ e1.SID > e2.SID ρ e2 Enroll ) SQL: SELECT e1.SID AS SID1, e2.SID AS SID2 FROM Enroll AS e1, Enroll AS e2 WHERE e1.CID = e2.CID AND e1.SID > e2.SID; AS keyword is optional

3/13/2016Jinze University of Kentucky14 A more complicated example Titles of all courses that Bart and Lisa are taking together Tip: Write the FROM clause first, then WHERE, and then SELECT FROM Student sb, Student sl, Enroll eb, Enroll el, Course c WHERE sb.name = ’Bart’ AND sl.name = ’Lisa’ AND eb.SID = sb.SID AND el.SID = sl.SID AND eb.CID = c.CID AND el.CID = c.CID; SELECT c.title

3/13/2016Jinze University of Kentucky15 Why SFW statements? Out of many possible ways of structuring SQL statements, why did the designers choose SELECT - FROM - WHERE ? A large number of queries can be written using only selection, projection, and cross product (or join) Any query that uses only these operators can be written in a canonical form: π L (σ p (R 1 X … X R m )) Example: π R.A, S.B (R p1 S) p2 (π T.C σ p3 T) = π R.A, S.B, T.C σ p1 ^ p2^p3 ( R X S X T ) SELECT - FROM - WHERE captures this canonical form

3/13/2016Jinze University of Kentucky16 Set versus bag semantics Set No duplicates Relational model and algebra use set semantics Bag Duplicates allowed Number of duplicates is significant SQL uses bag semantics by default

3/13/2016Jinze University of Kentucky17 Set versus bag example π SID Enroll Enroll SELECT SID FROM Enroll; sidcidgrade A B A A sid sid

3/13/2016Jinze University of Kentucky18 A case for bag semantics Efficiency Saves time of eliminating duplicates Which one is more useful? π GPA Student SELECT GPA FROM Student; The first query just returns all possible GPA’s The second query returns the actual GPA distribution Besides, SQL provides the option of set semantics with DISTINCT keyword

3/13/2016Jinze University of Kentucky19 Forcing set semantics SID’s of all pairs of classmates SELECT e1.SID AS SID1, e2.SID AS SID2 FROM Enroll AS e1, Enroll AS e2 WHERE e1.CID = e2.CID AND e1.SID > e2.SID; Say Bart and Lisa both take CPS116 and CPS114 SELECT DISTINCT e1.SID AS SID1, e2.SID AS SID2... With DISTINCT, all duplicate ( SID1, SID2 ) pairs are removed from the output

3/13/2016Jinze University of Kentucky20 Operational semantics of SFW SELECT [ DISTINCT ] E 1, E 2, …, E n FROM R 1, R 2, …, R m WHERE condition; For each t 1 in R 1 : For each t 2 in R 2 : … … For each t m in R m : If condition is true over t 1, t 2, …, t m : Compute and output E 1, E 2, …, E n as a row If DISTINCT is present Eliminate duplicate rows in output t 1, t 2, …, t m are often called tuple variables

3/13/2016Jinze University of Kentucky21 Review SELECT - FROM - WHERE statements (select-project-join queries) Set and bag operations  Next: how to nest SQL queries