Structured Query Language (SQL) IBM RESEARCH PROJECT (SEQUEL) –E.F. CODD, JUNE 70, ACM –CONTINUED RESEARCH THROUGH 70S –CLONE MAKER BANDWAGON AMERICAN.

Slides:



Advertisements
Similar presentations
SQL Rohit Khokher.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Information Resources Management February 27, 2001.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
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.
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”
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Information Resources Management March 6, Agenda n Administrivia n SQL Part 2 n Homework #6.
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
Introduction to SQL J.-S. Chou Assistant Professor.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Database A collection of related data. Database Applications Banking: all transactions Airlines: reservations, schedules Universities: registration, grades.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
BY SATHISH SQL Basic. Introduction The language Structured English Query Language (SEQUEL) was developed by IBM Corporation, Inc., to use Codd's model.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
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.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
SQL “Structured Query Language; standard language for relational data manipulation” DB2, SQL/DS, Oracle, INGRES, SYBASE, SQL Server, dBase/Win, Paradox,
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
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.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Chapter 11 Database and SQL. Flat Files and Databases Flat files Databases Advantages Efficient use of resources Access control Disadvantages Security.
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.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Big Data Yuan Xue CS 292 Special topics on.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Structured Query Language
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Web Services שפת SQL כתבה: זהבה יעקובסון ליווי מקצועי : ארז קלר
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Database systems Lecture 3 – SQL + CRUD
Chapter 7 Introduction to Structured Query Language (SQL)
Presentation transcript:

Structured Query Language (SQL) IBM RESEARCH PROJECT (SEQUEL) –E.F. CODD, JUNE 70, ACM –CONTINUED RESEARCH THROUGH 70S –CLONE MAKER BANDWAGON AMERICAN NATIONAL STANDARDS INSTITUTE –ANSI SQL-86, ANSI SQL-89, ANSI SQL-92 or SQL-2 Products providing SQL include: –Oracle from Oracle Corporation –SQL Server from Microsoft –SQLBase from Sybase –Ingres from Relational Technologies –DB2 Universal Database from IBM

SQL 의 구성 DDL(Data Definition Language) DML(Data Manipulation Language) –Interactive Embedded DML –C, Java 와 같은 일반언어 프로그램에서 DB 작업 View Definition Authorization –User, Group, 권한 Integrity – 제약 조건 지정 Transaction Control

DDL: Data definition language CREATE TABLE DROP TABLE ALTER TABLE CREATE INDEX DROP INDEX CREATE VIEW DROP VIEW

CREATE TABLE CREATE TABLE table_name ( attribute_name data-type [NOT NULL] [Unique],... PRIMARY KEY (attribute_name), PRIMARY KEY (attribute_name),... FOREIGN KEY (attribute_name) REFERENCES table_name, FOREIGN KEY (attribute_name) REFERENCES table_name ) ;

CREATE TABLE CREATE TABLE Employee ( EID INTEGER NOT NULL UNIQUE, PERHOUR DECIMAL(5,2) NOT NULL, ADDRESS CHAR(40), ZIP INTEGER, JOBTITLE CHAR(20), PRIMARY KEY (EID) ); CREATE TABLE Timesheets ( TID INTEGER NOT NULL UNIQUE, EID INTEGER, DATEWORKED DATE, HOURSWORKED INTEGER, PRIMARY KEY (TID), FOREIGN KEY (EID) REFERENCES EMPLOYEE );

DML: Data manipulation language DELETE UPDATE INSERT SELECT

SELECTION SELECT column_names, formulas,... FROM table_names WHERE row conditions are true ORDER BY column_name [ ASC | DESC ] ;

Selection 과 Relational Algebra Select A1, A2, …, An From R1, R2, …, Rm Where P 는 다음 RA 수식과 같다 Project A1, A2, …, An ( Selection P ( R1 X R2 X … X Rm ) –A1, A1, …, An 은 테이블들의 Attribute 로 중복되는 경우 Ri.Aj 로 테이블명의 명시 –P 는 조건식 필요한 경우 AND, OR, NOT 을 이용 –R1, R2,.., Rm 은 테이블 이름

SELECT ENAME, PERHOUR FROM EMPLOYEE WHERE CITY = ‘AUSTIN’ AND STATE = ‘TX’ ORDER BY ENAME ; SELECTION

SELECT AVG( PERHOUR ), MAX( PERHOUR ), MIN ( PERHOUR ) FROM EMPLOYEE ; SELECT COUNT( * ) FROM EMPLOYEE WHERE CITY = ‘AUSTIN’ AND STATE = ‘TX’ ; AGGREGATION (numeric functions)

SELECTION SELECT column_names, formulas,... FROM table_names WHERE row conditions are true GROUP BY column_names HAVING group conditions are true ORDER BY sort order ; * column_names should be matched.

SELECT AVG( PERHOUR ) FROM EMPLOYEE ; SELECT STATE, AVG( PERHOUR ) FROM EMPLOYEE GROUP BY STATE ; BY STATE

SELECT AVG( PERHOUR ) FROM EMPLOYEE ; SELECT STATE, AVG( PERHOUR ) FROM EMPLOYEE GROUP BY STATE ; SELECT CITY, STATE, AVG( PERHOUR ) FROM EMPLOYEE GROUP BY CITY, STATE ; BY CITY

SMALL GROUPS ? SELECT CITY, STATE, AVG( PERHOUR ) FROM EMPLOYEE GROUP BY CITY, STATE ; SELECT CITY, STATE, AVG( PERHOUR ) FROM EMPLOYEE GROUP BY CITY, STATE HAVING COUNT( * ) >= 3 ;

SELECT CITY, COUNT( * ) FROM EMPLOYEE WHERE STATE = ‘TX’ GROUP BY CITY HAVING COUNT( * ) >= 3 ; ROW & GROUP CONDITIONS

SELECT CITY, COUNT( * ) FROM EMPLOYEE WHERE STATE = ‘TX’ GROUP BY CITY HAVING COUNT( * ) >= 3 ; ELIMINATES ELIMINATES NON-TX ROWS ROW & GROUP CONDITIONS

SELECT CITY, COUNT( * ) FROM EMPLOYEE WHERE STATE = ‘TX’ GROUP BY CITY HAVING COUNT( * ) >= 3 ; ELIMINATES ELIMINATES SMALL GROUPS ELIMINATES ELIMINATES NON-TX ROWS ROW & GROUP CONDITIONS

SELECT ENAME, DATEWORKED, HOURSWORKED, PERHOUR FROM EMPLOYEE, TIMESHEETS WHERE EMPLOYEE.EID = TIMESHEETS.EID ORDER BY ENAME, DATEWORKED ; EMPLOYEETIMESHEETS REPORT JOINING TABLES

SELECT ENAME, DATEWORKED, HOURSWORKED, PERHOUR FROM EMPLOYEE {INNER | LEFT | RIGHT} JOIN TIMESHEETS ON EMPLOYEE.EID = TIMESHEETS.EID ORDER BY ENAME, DATEWORKED ; JOINING TABLES (Inner/left/right join)

SELECT ENAME, DATEWORKED, HOURSWORKED, PERHOUR FROM EMPLOYEE, TIMESHEETS WHERE EMPLOYEE.EID = TIMESHEETS.EID ORDER BY ENAME, DATEWORKED ; SMITH200X SMITH200X SMITH200X WILSON200X WILSON200X WILSON200X WILSON200X

SELECT ENAME, DATEWORKED, HOURSWORKED, PERHOUR, HOURSWORKED * PERHOUR AS PAY FROM EMPLOYEE, TIMESHEETS WHERE EMPLOYEE.EID = TIMESHEETS.EID ORDER BY ENAME, DATEWORKED ; SMITH200X SMITH200X SMITH200X WILSON200X WILSON200X WILSON200X WILSON200X

SELECT ENAME, SUM( HOURSWORKED * PERHOUR ) FROM EMPLOYEE, TIMESHEETS WHERE EMPLOYEE.EID = TIMESHEETS.EID GROUP BY TIMESHEETS.EID, ENAME ORDER BY ENAME ; SMITH WILSON

WHICH REPAIRPERSON HAS THE HIGHEST HOURLY RATE ? ( 1 ) Find the highest HOURLY RATE ( 2 ) Identify REPAIRPERSON(S) with that RATE MULTI-STEP QUERY

(1) Find the highest rate SELECT MAX( PERHOUR ) FROM EMPLOYEE WHERE JOBTITLE = ‘REPAIR’ ; $ 26.50

(2) Identify matches SELECT ENAME FROM EMPLOYEE WHERE JOBTITLE = ‘REPAIR’ AND PERHOUR = ; JACKSON, JEFF MITCHELL, TOM

TWO STEPS SELECT MAX( PERHOUR ) FROM EMPLOYEE WHERE JOBTITLE = ‘REPAIR’ ; SELECT ENAME FROM EMPLOYEE WHERE JOBTITLE = ‘REPAIR’ AND PERHOUR = ;

SUBQUERIES SELECT ENAME FROM EMPLOYEE WHERE JOBTITLE = ‘REPAIR’ AND PERHOUR = ( SELECT MAX( PERHOUR ) FROM EMPLOYEE WHERE JOBTITLE = ‘REPAIR’ ) ;

FIND THE NAMES OF ALL SALESPEOPLE WHO SOLD AT LEAST ONE VEHICLE FOR MORE THAN $50,000 LAST MONTH QUERY OPTIMIZATION

FIND THE NAMES OF ALL SALESPEOPLE WHO SOLD AT LEAST ONE VEHICLE FOR MORE THAN 50,000 LAST MONTH TABLES: –EMPLOYEE ( ENAME, EID,... ) –SALES ( EID, VID, CID, SID, PRICE, SDATE,... )

SELECT ENAME FROM EMPLOYEE, SALES WHERE EMPLOYEE.EID = SALES.EID AND PRICE >= AND month(SDATE) = month(date())-1 ; SELECTENAME FROMEMPLOYEE WHEREEID IN ( SELECT DISTINCT EID FROM SALES WHERE PRICE >= AND month(SDATE) = month(date())-1);

100 EMPLOYEE 2,000 SALES 200,000 EMPLOYEE-SALES 200,000 EMPLOYEE-SALES CROSS PRODUCT RECORDS 5 ENAMES

SELECT ENAME FROM EMPLOYEE, SALES WHERE EMPLOYEE.EID = SALES.EID AND PRICE >= AND month(SDATE) = month(date())-1 ; 200,000 “RECORDS” ARE CHECKED AN INDEX ON FOREIGN KEY, SALES.EID, HELPS

SELECTENAME FROMEMPLOYEE WHEREEID IN ( SELECT DISTINCT EID FROM SALES WHERE PRICE >= AND month(SDATE) = month(date())-1); 2,000 RECORDS ARE CHECKED TO GENERATE LIST OF 8 EIDS ( 5 UNIQUE )

SELECTENAME FROMEMPLOYEE WHEREEID IN ( 1234, 2345, 3456,..., 6789 ) ; 500 “RECORDS” ARE CHECKED TO GENERATE LIST OF 5 NAMES

SELECTENAME FROMEMPLOYEE WHEREEID IN ( SELECT DISTINCT EID FROM SALES WHERE PRICE >= AND month(SDATE) = month(date())-1); 2,000 RECORDS CHECKED IN SUBQUERIES 500 RECORDS CHECKED IN MAIN SELECT 500 RECORDS CHECKED IN MAIN SELECT

DELETION DELETE FROM table_name WHERE condition is true ; DELETE FROM EMPLOYEE WHERE EID = ; DELETE FROM EMPLOYEE WHERE ENAME = ‘SMITH, WILLIAM’ ;

MODIFICATIONS UPDATE table_name SETcolumn_name = expression, column_name = expression,... WHERE condition is true ;

UPDATE EMPLOYEE SETADDRESS = ‘4321 AVENUE J’, CITY = ‘AUSTIN’, STATE = ‘TX’, ZIP = WHERE EID = ; MODIFICATIONS

UPDATE EMPLOYEE SETADDRESS = ‘4321 AVENUE J’, CITY = ‘AUSTIN’, STATE = ‘TX’, ZIP = WHERE EID = ; UPDATE EMPLOYEE SET PERHOUR = 6.75 WHERE PERHOUR < 6.75 ; MODIFICATIONS

INSERTION INSERT INTO table_name ( column_list ) VALUES ( data_definition ) ; INSERT INTO table_name ( column_list ) SELECT statement ;

INSERT INTO EMPLOYEE ( EID, ENAME, PERHOUR, MINPAY ) VALUES ( 12345, ‘DOE, JOHN’, 16.20, 1200 ) ; EXTERNAL SOURCE

INTERNAL SOURCE INSERT INTO PAYROLL ( EID, PAYDATE, AMOUNT ) SELECT EID, CURRENT_DATE, SUM( HOURSWORKED * PERHOUR ) FROM EMPLOYEE, TIMESHEETS WHERE EMPLOYEE.EID = TIMESHEETS.EID AND DATEWORKED IS BETWEEN (CURRENT_DATE-14) AND CURRENT_DATE GROUP BY TIMESHEETS.EID ;

SELECT DEPTNAME, ENAME, DATEWORKED, HOURSWORKED, PERHOUR FROM DEPT, EMPLOYEE, TIMESHEETS WHERE DEPT.DID = EMPLOYEE.DID AND EMPLOYEE.EID = TIMESHEETS.EID GROUP BY DEPTNAME ; JOINING THREE TABLES DEPTTIMESHEETS REPORT EMPLOYEE

Review of SQL DML SELECT & SELECT DISTINCT FROM WHERE –Comparison operators: =, <>,>,>=,<,<= –Boolean operators: AND, OR, NOT ORDER BY GROUP BY UPDATE STATEMENTS DELETE STATEMENTS INSERT STATEMENTS