1 dept. of cis, temple univ. cis616/661 – principles of data management v. megalooikonomou sql...

31
1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Upload: tyler-sullivan

Post on 18-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

Data Definition Language create table student (ssn char(9) not null, name char(30), address char(50), primary key (ssn) )

TRANSCRIPT

Page 1: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

1

Dept. of CIS, Temple Univ.CIS616/661 – Principles of Data Management

V. MegalooikonomouSQL (part 2)

(based on slides by C. Faloutsos at CMU)

Page 2: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Overview - detailed - SQL DML

select, from, where, renaming, ordering,

aggregate functions, nested subqueries

insertion, deletion, update other parts: DDL, embedded SQL,

auth etc

Page 3: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Data Definition Languagecreate table student(ssn char(9) not null, name char(30), address char(50),primary key (ssn) )

Page 4: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Data Definition Languagecreate table r( A1 D1, …, An Dn, integrity-constraint1, … integrity-constraint-n)

Page 5: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Data Definition LanguageDomains: char(n), varchar(n) int, numeric(p,d), real, double

precision float, smallint date, time

Page 6: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Data Definition Languageintegrity constraints: primary key foreign key check(P)

Page 7: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Data Definition Languagecreate table takes(ssn char(9) not null, c-id char(5) not null, grade char(1),primary key (ssn, c-id),check grade in (“A”, “B”, “C”, “D”,

“F”))

Page 8: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Data Definition Languagedelete a table: difference between drop table student

delete from student

Page 9: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Data Definition Languagemodify a table: alter table student drop address

alter table student add major char(10)

Page 10: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Overview - detailed - SQL DML

select, from, where, renaming, ordering,

aggregate functions, nested subqueries

insertion, deletion, update other parts: DDL, embedded SQL,

auth etc

Page 11: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Embedded SQLfrom within a ‘host’ language (eg.,

‘C’, ‘VB’) EXEC SQL <emb. SQL stmnt> END-

EXEC

Q: why do we need embedded SQL??

Page 12: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Embedded SQLSQL returns sets; host language

expects a tuple - impedance mismatch!

solution: ‘cursor’, ie., a ‘pointer’ over the set of tuples.

example:

Page 13: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Embedded SQLmain(){…EXEC SQL declare c cursor for select * from studentEND-EXEC…

Page 14: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Embedded SQL - ctn’d…EXEC SQL open c END-EXEC… while( !sqlerror ){ EXEC SQL fetch c into :cssn, :cname, :cad END-EXEC fprintf( … , cssn, cname, cad);}

Page 15: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Embedded SQL - ctn’d…EXEC SQL close c END-EXEC…} /* end main() */

Page 16: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

dynamic SQLmain(){ /* set all grades to user’s input */…char *sqlcmd=“ update takes set grade = ?”;EXEC SQL prepare dynsql from :sqlcmd ;char inputgrade[5]=“a”;EXEC SQL execute dynsql using :inputgrade;…} /* end main() */

Page 17: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Overview - detailed - SQL DML

select, from, where, renaming, ordering,

aggregate functions, nested subqueries

insertion, deletion, update other parts: DDL, embedded SQL,

auth etc

Page 18: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

SQL - miscLater, we’ll see authorization: grant select on student to

<user-id> transactions other features (triggers, assertions

etc)

Page 19: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

General Overview - rel. model Formal query languages

rel algebra and calculi Commercial query languages

SQL QBE, (QUEL)

Page 20: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Rel. model - QBE Inspired by the domain relational

calculus “P.” -> print (ie., ‘select’ of SQL) _x, _y: domain variables (ie.,

attribute names) Example: find names of students

taking 15-415

Page 21: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Rel. model - QBECLASSc-id c-name units15-413 s.e. 215-412 o.s. 2

TAKESSSN c-id grade

123 15-413 A234 15-413 B

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

Page 22: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Rel. model - QBECLASSc-id c-name units

TAKESSSN c-id grade

STUDENTSsn Name Address

Page 23: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Rel. model - QBE

CLASSc-id c-name units

SSN c-id grade_x 15-415

STUDENTSsn Name Address_x P.

names of students taking 15-415

Page 24: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Rel. model - QBE condition box self-joins (Tom’s grandparents) ordering (AO., DO.) aggregation (SUM.ALL.,

COUNT.UNIQUE. , …) group-by (G.)

Page 25: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Rel. model - QBE

CLASSc-id c-name units

SSN c-id gradeP.AVG.ALL.

STUDENTSsn Name Address

aggregate: avg grade overall:

Page 26: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Rel. model - QBE

CLASSc-id c-name units

SSN c-id gradeP.G. P.AVG.ALL.

STUDENTSsn Name Address

aggregate: avg grade per student:

Page 27: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

General Overview - rel. model Formal query languages

rel algebra and calculi Commercial query languages

SQL QBE, (QUEL)

Page 28: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Rel. model - QUELUsed in INGRES only - of historical

interest.Eg.: find all ssn’s in mini-U: range of s is student; retrieve (s.ssn);

]}[][|{ ssntssnsstudentst

Page 29: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Rel. model - QUELgeneral syntax:range of …. is t-

name

retrieve (attribute list)

where condition

SQL

select attr. list

from t-name

where condition

Page 30: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

Rel. model - QUEL very similar to SQL also supports aggregates,

ordering etc

Page 31: 1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)

General Overview Formal query languages

rel algebra and calculi Commercial query languages

SQL QBE, (QUEL)

Integrity constraints Functional Dependencies Normalization - ‘good’ DB design