AS SQL Query

AS SQL Query

Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) MBDS course : « From data bases to big data » (7 lectures) Professor Serge Miranda Dept of Computer Science University of Nice Sophia Antipolis (member of UCA) Director of MBDS Master degree (www.mbds-fr.org) 2 www.mbds-fr.org Introduction to SQL2 (lecture 3) Professor Serge Miranda Dept of Computer Science University of Nice Sophia Antipolis (UCA) Director of MBDS Master degree (www.mbds-fr.org) 3 www.mbds-fr.org SQL standards SEQUEL (Structured English as a Query Language) from R System (IBM) ➢ SEQUEL1 ➢ SEQUEL2 (1976) ---------- SQL standard in the legacy of SEQUEL ---------- ➢ SQL1 : ISO (1987) and ANSI (1986) ➢ SQL2- 92 (Relational) ➢ SQL3- 99 (Hybrid : Object relational) ➢ SQL4-2003 (SQL/OLB Object Language Binding; OLAP) ➢ SQL5-2006 (SQL/XML ; Xquery) ➢ SQL6-2008 (SQL/JRT for JaAC functions) ➢ SQL7-2011 ISO/IEC 9075:2011 Dec 2011 ➢ SQL8 -? (for Big Data) 4 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) The four dimensions of SQL* Schema definition language *** DB manipulation language * SQL DB Control language *Proceeding from SEQUEL (transactional interface) **** (Structured English as a QUEry Language) IBM Research center, San Jose (1975) Communication language **** 5 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Schema definition with SQL2 6 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Relational data structures in SQL • Tables PILOT PIL# PILNAME ADDRESS (relations) • Columns 1 SERGE NICE (attributes) NEW 2 PETER • Lines YORK (tuples) 3 JOEL DUBLIN PLANE P# PNAME CAP LOC FLIGHT F# PIL# P# DC AC DT AT 100 A300 300 Nice IT100 1 100 Nice Paris 7 8 101 B707 250 NEWYORK IT101 2 100 Paris Nice 11 12 102 A300 300 DUBLIN IT103 1 103 Lyon Paris 14 15 103 B727 370 LONDON IT104 2 102 Nice Nice 17 18 7 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) SQL mapping with Codd’s relational data model Codd’s model SQL (SEQUEL*) Français English relation relation table domaine domain domain attribut attribute column n-uplet (tuple) tuple line clé primaire primary key primary key foreign key references clé étrangère (primary domain) * SEQUEL : « Structured English as a QUEry Language » 8 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Definition of the relational schema (SQL kernel) Create table [not null ; primary key / references] alter table add [modify, delete] create / drop index unique [create /drop data-space] 9 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Example of schema definition (SQL2) Create schema <Phase1 : Domain creation> <Phase2 : Relation creation> ➢create domain city as char (12) ➢create table PILOT default ‘ PARIS ’ (PIL# decimal (4), check (value in(‘New York ’, ‘ NICE ’, PILNAME char (12), ‘ Dublin ’, ‘Paris’)) ADDR city create domain Hour as time check (Value in (‘ Dublin ’, ‘ NICE ’)), check (Value > 7 and Value < 22) SAL decimal (5), primary key (PIL#)) 10 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Schema definition (Cont’) create table PLANE create table FLIGHT P# decimal (4), (F# char(5), PNAME char (12), PIL# decimal (4) not null, CAP decimal (3) check (Value > 100), P# decimal(4), LOC city, DC city, primary key (P#)) AC city, DT hour, AT hour, primary key (FLIGHT#), foreign key (PIL#) references PILOT, initially deferred, foreign key (P#) references PLANE, on delete cascade, on update set null)) alter domain …. Drop constraint 11 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Schema definition (SQL2) ➢Syntactic Type : ➢Char(n), decimal, integer, bit, float, date (year, month, day), times (hour, minute, second), timestamp, interval ➢VIEW : A view is a virtual table (just its definition is stored) CREATE VIEW vx AS <SQL Query> ➢Relational catalog for the schema ➢(INFORMATION-SCHEMA CATALOG) containing system tables (SYSTABLE) which could be accessed by SQL 12 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Schema definition : relational catalog Example with DB2 relational catalog. SYSTABLES NAME CREATOR COLCOUNT PILOT Serge 4 PLANE Serge 4 FLIGHT Serge 7 SYSCOLUMNS NAME TBNAME COLTYPE SYSINDEX NAME TBNAME CREATOR PIL# PILOT SMALLINT PX PILOT Serge PILNAME PILOT ChaR AX PLANE Serge ADDR PILOT ChaR VX FLIGHT Serge P# PLANE SMALLINT … … … 13 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Schema definition : clusters Cluster : physical JOIN 10 SERGE NICE Example : Cluster PF between PILOT and FLIGHT on PIL# IT 100 10 Paris 100 Nice 7 8 Here cluster block for PIL# = 10 IT 101 10 Nice 101 Paris 11 12 Create cluster PF (PIL# number (4)) IT 105 10 Paris 104 Toulouse 15 16 (Cluster key) 14 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Schema definition : cluster Clustering within CREATE TABLE ➢Example : ➢NOTE : We can create table PILOT (PIL# number(4) primary key, …) ➢ Eliminate a table from a cluster : cluster PF (pil#) alter cluster PF drop table PILOT Clustering an existing table ➢Example : ➢ Delete a cluster : create table FLIGHT2 cluster PF (PIL#) drop cluster PF as select * from FLIGHT drop table FLIGHT rename FLIGHT2 to FLIGHT 15 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) DB manipluation with SQL Data retrieval (SELECT) and storage operators (INSERT, UPDATE, DELETE) 16 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) DB interaction ➢ 1st mode : interactive SQL <Query mapping with SQL> Select <target list or «*»> From <tables (or views) > Where <expression on lines> with in, exists, any, all, and, or, not, between, like, … group by <partitionned attributes> Having <expression on partitions> Order <tri > Union 17 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) SQL manipulation : storage operators UPDATE : DELETE : INSERT : update R delete insert set From R into R where where Values Note : heterogeneous syntax 18 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) DB interaction ➢2nd mode : embedded SQL (in programming language) ➢ impedance mismatch : SAT (Set at-a-time)and RAT (Record at–a-time) ➢Logical pointer : CURSOR (« impedance mismatch ») (exec sql) declare CX cursor for < SQL query > (exec sql) open / fetch / update / delete / close CX (cursor) Current line sqlcode 19 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) SQL2 by example Domain : = {Nice, Paris, London,Dublin } FLIGHT FLIGHT# PIL# AC… IT200 102 Nice IT201 100 Paris PILOT PIL# PILNAME ADDR IT204 102 Paris 100 Serge Nice 101 Jean Paris 102 Paul Paris … … … 20 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Basic SQL mapping : SELECT-FROM-WHERE select - from - where ? PLANE AVN PNAME CAP LOC 100 A300 300 Nice 101 B707 250 Paris 102 A300 300 London 103 B707 200 Nice Select PNAME, CAP PLANE PNAME CAP from PLANE where LOC = ‘ NICE ’ ; A300 300 B707 200 In relational algebra ? 21 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) SQL by example Every line from FLIGHT (complete table) ? SELECT * FROM FLIGHT 22 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) SQL query : interactive and embedded mode What are the flights arriving in Paris in the afternoon ? ➢ interactive mode : select * from FLIGHT where AC = ‘ PARIS ’ and DT > 12 ; ➢ Embedded mode : exec sql declare CX cursor for select FLIGHT#,DT,AT from FLIGHT where AC = ‘ PARIS ’ and DT > 12; declareFLIGHT char(5) ; DT date ; AT date ; exec sql open CX ; do exec sql fetch CX into : FLIGHT, DT, AT ; <variable processing> end exec sql close CX ; 23 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Exercice : On DB2 Catalog use SQL to answer the following queries ➢What are the names of the columns of FLIGHT table ? ➢What are the tables of the schema ? ➢What are the tables which contain PIL# column ? 24 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Solution Relational catalog (in DB2) with eleven tables. Here we ll use : ➢SYSTABLE (TBNAME, NBCOLUMNS, PK,...) ➢SYSCOLUMNS (TBNAME, CNAME, Type,...) 25 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Solution with SQL ➢What are the names of the columns of FLIGHT table ? Select CNAME from SYSCOLUMNS where TBNAME = ‘ FLIGHT ’ ; ➢What are the tables of the schema ? Select TBNAME from SYSTABLES ; ➢What are the tables which contain PIL# column ? Select TBNAME from SYSCOLUMNS where CNAME = ‘ PIL# ’ ; 26 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) JOIN query with SQL ➢What are the names of the pilots which are insuring a flight towards Paris ? 2 basic versions for SQL (which is an hybrid query language) : PREDICATIVE and SET–oriented Predicative version select PILNAME from PILOT, FLIGHT where PILOT. PIL# = FLIGHT. PIL# < join predicate> and FLIGHT. AC = ‘ PARIS ’ ; 27 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) SET-oriented JOIN with SQL (example) SET-oriented version (with subquery) select PILNAME from PILOT, where PIL# IN (Select PIL# From FLIGHT Where FLIGHT. AC = ‘ PARIS ’) ; 28 Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA) Other JOINs in SQL ➢ NATURAL JOIN in the FROM clause (PK and FK implicitly)

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    98 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us