Information Resources Management March 6, 2001. Agenda n Administrivia n SQL Part 2 n Homework #6.

Slides:



Advertisements
Similar presentations
Database Languages Chapter 7. The Relational Algebra.
Advertisements

Basic SQL Introduction Presented by: Madhuri Bhogadi.
1 Constraints, Triggers and Active Databases Chapter 9.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
Information Resources Management February 27, 2001.
Introduction to Structured Query Language (SQL)
Midterm Review Lecture 14b. 14 Lectures So Far 1.Introduction 2.The Relational Model 3.Disks and Files 4.Relational Algebra 5.File Org, Indexes 6.Relational.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 8 Foundations of Relational Implementation.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Information Resources Management April 3, Agenda n Administrivia n Physical Database Design n Database Integrity n Performance.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 8 Foundations of Relational Implementation.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Introduction to SQL Steve Perry
FEN  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Structured Query Language Chris Nelson CS 157B Spring 2008.
Using Special Operators (LIKE and IN)
FEN  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
1 Announcements Reading for next week: Chapter 4 Your first homework will be assigned as soon as your database accounts have been set up.  Expect an .
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Chapter 3: Relational Databases
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Structured Query Language (SQL) IBM RESEARCH PROJECT (SEQUEL) –E.F. CODD, JUNE 70, ACM –CONTINUED RESEARCH THROUGH 70S –CLONE MAKER BANDWAGON AMERICAN.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
SQL - Training Rajesh Charles. Agenda (Complete Course) Introduction Testing Methodologies Manual Testing Practical Workshop Automation Testing Practical.
Structured Query Language
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
SQL 101.
مقدمة في قواعد البيانات
Database systems Lecture 3 – SQL + CRUD
SQL Fundamentals in Three Hours
SQL-1 Week 8-9.
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Contents Preface I Introduction Lesson Objectives I-2
DATABASE Purpose of database
Presentation transcript:

Information Resources Management March 6, 2001

Agenda n Administrivia n SQL Part 2 n Homework #6

Administrivia n Mid-term Exam n Homework #4 n Homework #5

SQL Structured Query Language n The standard relational database language n Two Parts n DDL - Data Definition Language n DML - Data Manipulation Language

SQL - DDL n Data Definition: Define schemas, delete relations, create indices, modify schemas n View Definition n Authorization n Integrity

SQL - DML n Insert, Modify, Delete Tuples n Interactive n Embedded n Transaction Control

Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= 5 tableX (b, c, e)tableY(a, c)tableZ (a, d)

Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= Join all tables (Cartesian product)

Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= For each row, apply WHERE conditions

Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= WHERE subquery, each row evaluated separately

Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= GROUP BY (with HAVING) - Order remaining data (all rows) by groups

Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= Apply HAVING to each group

Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= ALL (SELECT AVG(d) from tableZ as Z) 5a - HAVING with “standalone” SELECT

Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= Calculate aggregate functions for each (remaining) group

Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= SELECT desired output columns

Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= ORDER BY (sort) is processed last

SQL - DML n INSERT n UPDATE n DELETE

INSERT INSERT INTO table VALUES (………) INSERT INTO table(attributes) VALUES (………)

INSERT Example n Add an employee INSERT INTO Employee VALUES(‘ ’,’John Smith’,,212) INSERT INTO Employee(EmpID, Name, OfficeNBR) VALUES(‘ ’,’John Smith’,212) MgrFlag is NULL in both cases

UPDATE UPDATE table SET attribute = value or calculation UPDATE table SET attribute = value or calculation WHERE conditions

UPDATE Example n Increase the prices of all properties by 5% UPDATE Property SET Price = Price * 1.05

UPDATE Example n Increase the prices of all properties in St. Paul, MN by 7.5%

UPDATE Example n Increase the prices of all properties in St. Paul, MN by 7.5% UPDATE Property SET Price = Price * WHERE City = ‘St. Paul’ AND State = ‘MN’

UPDATE Example n Change the zip code of all offices in to

UPDATE Example n Change the zip code of all offices in to UPDATE Office SET Zip = ‘ ’ WHERE Zip LIKE ‘15214%’

DELETE DELETE FROM table WHERE conditions

DELETE Example n Delete everything from the gift table DELETE FROM Gift

DELETE Example n Delete all employees who do not have access to a PC

DELETE Example n Delete all employees who do not have access to a PC DELETE FROM Employee WHERE EmpID NOT IN (SELECT EmpID FROM PCAccess)

SQL - DDL n CREATE TABLE n DROP TABLE n ALTER TABLE

CREATE TABLE CREATE TABLE name (attributedefn, attributedefn, … attributedefn, … ) )

CREATE TABLE CREATE TABLE name (attributedefn, attributedefn, … attributedefn, … ) ) n Attribute Definitions - Table 9-1, p. 329 n Attribute Constraints n NOT NULL n UNIQUE

CREATE TABLE CREATE TABLE name (attributedefn, attributedefn, … attributedefn, … ) ) n Integrity Constraints n PRIMARY KEY (attribute, …) n FOREIGN KEY (attribute,…) REFERENCES (table name)

CREATE TABLE Example n Create the Office table CREATE TABLE Office (OfficeNbrINTEGER NOT NULL UNIQUE, AddressVARCHAR(50), AddressVARCHAR(50), CityVARCHAR(25), CityVARCHAR(25), StateCHAR(2), StateCHAR(2), ZipCHAR(10), ZipCHAR(10), PhoneNbrCHAR(13), PhoneNbrCHAR(13), PRIMARY KEY (OfficeNbr)) PRIMARY KEY (OfficeNbr))

CREATE TABLE Example n Create the Manager table CREATE TABLE Manager (EmpIDCHAR(9), OfficeNbrINTEGER, OfficeNbrINTEGER, PRIMARY KEY (EmpID), PRIMARY KEY (EmpID), FOREIGN KEY (EmpID) REFERENCES (Employee), FOREIGN KEY (EmpID) REFERENCES (Employee), FOREIGN KEY (OfficeNbr) REFERENCES (Office)) FOREIGN KEY (OfficeNbr) REFERENCES (Office))

CREATE TABLE Example n Create the MgrPCAccess table -- access type is required

CREATE TABLE Example n Create the MgrPCAccess table -- access type is required CREATE TABLE MgrPCAccess (PC#INTEGER, EmpIDCHAR(9), EmpIDCHAR(9), AccessTypeCHAR(15) NOT NULL, AccessTypeCHAR(15) NOT NULL, PRIMARY KEY (PC#, EmpID), PRIMARY KEY (PC#, EmpID), FOREIGN KEY (EmpID) REFERENCES (Employee), FOREIGN KEY (EmpID) REFERENCES (Employee), FOREIGN KEY (PC#) REFERENCES (PC)) FOREIGN KEY (PC#) REFERENCES (PC))

DROP TABLE n DROP TABLE name n DROP TABLE Office n DROP vs. DELETE

ALTER TABLE n ALTER TABLE name ADD attributes n ALTER TABLE name DROP attributes n Add - existing tuples get NULLs n Nulls must be allowed n Drop - cannot drop the primary key

Other SQL DDL n CREATE INDEX n DROP INDEX n CREATE VIEW n DROP VIEW n CREATE SCHEMA

Multiple Tables - JOINs n FROM multiple tables WHERE cond n “INNER” join n Equi-join & Natural join variations n What if second table is “optionally” included? n “OUTER” join

Outer Join n List the address, city, and manager name (if any) of all offices. SELECT address, city, e.name FROM Office as O, Employee as E WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1 What happens to offices without a mgr?

Outer Join n List the address, city, and manager name (if any) of all offices. SELECT address, city, e.name FROM Office as O, Employee as E WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1 UNION SELECT address, city, ‘’ FROM Office as O WHERE O.OfficeNbr NOT IN (SELECT OfficeNbr FROM Employee WHERE MgrFlag = 1)

Outer Join - Access n List the address, city, and manager name (if any) of all offices. SELECT address, city, e.name FROM Office as O LEFT JOIN Employee as E WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1 LEFT JOIN - all rows for table on left included (RIGHT JOIN)

Outer Join - Oracle n List the address, city, and manager name (if any) of all offices. SELECT address, city, e.name FROM Office as O, Employee as E WHERE O.OfficeNbr = E.OfficeNbr (+) AND MgrFlag = 1 (+) here LEFT JOIN WHERE O.OfficeNbr (+) = E.OffficeNbr RIGHT JOIN

Outer Join n Outer Joins are not SQL standard n Not always available n Not consistent n Can always do the same query using standard SQL (UNION & NOT IN)

Other Relational Languages n Chapter 10 of book n Query-by Example (QBE) n Access

In-Class Exercise n SQL n All 21 queries

Homework #6 n Do remaining 5 from HW #5 n Keep problem numbers