CS 405G: Introduction to Database Systems

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.
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.
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.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part1.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
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.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
 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.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
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.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
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
CH 9 SQL 9.1 Querying A Single Table 9.2 Querying Multiple Tables
Chapter 3 Introduction to SQL
Chapter 3 Introduction to SQL(2)
Lecture#6: Fun with SQL (Part 1)
Writing Basic SQL SELECT Statements
Database Systems SQL cont. Relational algebra
Basic select statement
Introduction to SQL.
Database Systems Relational algebra SQL cont.
EECS 647: 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
The Relational Model Relational Data Model
Relational Databases The Relational Model.
Relational Databases The Relational Model.
CS 405G: Introduction to Database Systems
SQL LANGUAGE and Relational Data Model TUTORIAL
CMPT 354: Database System I
CMPT 354: Database System I
Lectures 3: Introduction to SQL 2
CS 405G: Introduction to Database Systems
SQL: Structured Query Language
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
Recap – Relational languages
Presentation transcript:

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

Luke Huan Univ. of Kansas Database Design 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas 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.) 9/21/2018 Luke Huan Univ. of Kansas

Creating and dropping tables CREATE TABLE table_name (…, column_namei column_typei, …); DROP TABLE table_name; Examples create table Student (SID integer, name varchar(30), email 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...) 9/21/2018 Luke Huan Univ. of Kansas

Basic queries: SFW statement SELECT A1, A2, …, An FROM R1, R2, …, Rm WHERE condition; Also called an SPJ (select-project-join) query (almost) Equivalent to relational algebra query π A1, A2, …, An (σ condition (R1 X R2 X … X Rm)) 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas Semantics of SFW SELECT E1, E2, …, En FROM R1, R2, …, Rm WHERE condition; For each t1 in R1: For each t2 in R2: … … For each tm in Rm: If condition is true over t1, t2, …, tm: Compute and output E1, E2, …, En as a row t1, t2, …, tm are often called tuple variables Not 100% correct, we will see 9/21/2018 Luke Huan Univ. of Kansas

Example: selection and projection Name of students under 18 SELECT name FROM Student WHERE age < 20; π name (σ age <20 (Student)) sid name age gpa 1234 John Smith 21 3.5 1123 Mary Carter 19 3.8 1011 Bob Lee 22 2.6 1204 Susan Wong 3.4 1306 Kevin Kim 18 2.9 sid name age gpa 1234 John Smith 21 3.5 1123 Mary Carter 19 3.8 1011 Bob Lee 22 2.6 1204 Susan Wong 3.4 1306 Kevin Kim 18 2.9 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas 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 9/21/2018 Luke Huan Univ. of Kansas

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” 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas 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 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas 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 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas 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; 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas 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 9/21/2018 Luke Huan Univ. of Kansas

A more complicated example Titles of all courses that Bart and Lisa are taking together SELECT c.title 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; Tip: Write the FROM clause first, then WHERE, and then SELECT 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas 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 (R1 X … X Rm)) 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 9/21/2018 Luke Huan Univ. of Kansas

Set versus bag semantics No duplicates Relational model and algebra use set semantics Bag Duplicates allowed Number of duplicates is significant SQL uses bag semantics by default 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas Set versus bag example πSID Enroll sid 1234 1124 1123 Enroll sid cid grade 1234 647 A 108 B 1124 1123 SELECT SID FROM Enroll; sid 1234 1124 1123 9/21/2018 Luke Huan Univ. of Kansas

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 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas 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 9/21/2018 Luke Huan Univ. of Kansas

Operational semantics of SFW SELECT [DISTINCT] E1, E2, …, En FROM R1, R2, …, Rm WHERE condition; For each t1 in R1: For each t2 in R2: … … For each tm in Rm: If condition is true over t1, t2, …, tm: Compute and output E1, E2, …, En as a row If DISTINCT is present Eliminate duplicate rows in output t1, t2, …, tm are often called tuple variables 9/21/2018 Luke Huan Univ. of Kansas

Luke Huan Univ. of Kansas Review SELECT-FROM-WHERE statements (select-project-join queries) Set and bag operations Next: how to nest SQL queries 9/21/2018 Luke Huan Univ. of Kansas