Temple University – CIS Dept. CIS331– Principles of Database Systems

47
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model – SQL Part II (based on notes by Silberchatz,Korth, and Sudarshan and

description

Temple University – CIS Dept. CIS331– Principles of Database Systems. V. Megalooikonomou Relational Model – SQL Part II (based on notes by Silberchatz,Korth, and Sudarshan and notes by C. Faloutsos at CMU). General Overview - rel. model. Formal query languages rel algebra and calculi - PowerPoint PPT Presentation

Transcript of Temple University – CIS Dept. CIS331– Principles of Database Systems

Page 1: Temple University – CIS Dept. CIS331– Principles of Database Systems

Temple University – CIS Dept.CIS331– Principles of Database Systems

V. Megalooikonomou

Relational Model – SQL Part II

(based on notes by Silberchatz,Korth, and Sudarshan and notes by C. Faloutsos at CMU)

Page 2: Temple University – CIS Dept. CIS331– Principles of Database Systems

General Overview - rel. model

Formal query languages rel algebra and calculi

Commercial query languages SQL QBE, (QUEL)

Page 3: Temple University – CIS Dept. CIS331– Principles of Database Systems

Overview - detailed - SQL

DML select, from, where, renaming,

ordering, aggregate functions, nested

subqueries insertion, deletion, update

other parts: DDL, embedded SQL, auth etc

Page 4: Temple University – CIS Dept. CIS331– Principles of Database Systems

Reminder: our Mini-U db

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

TAKESSSN c-id grade

123cis331 A234cis331 B

Page 5: Temple University – CIS Dept. CIS331– Principles of Database Systems

DML - insertions etc

insert into studentvalues (“123”, “smith”, “main”)

insert into student(ssn, name, address)

values (“123”, “smith”, “main”)

Page 6: Temple University – CIS Dept. CIS331– Principles of Database Systems

DML - insertions etc

bulk insertion: how to insert, say, a table of ‘foreign-student’s, in bulk?

Page 7: Temple University – CIS Dept. CIS331– Principles of Database Systems

DML - insertions etc

bulk insertion:

insert into studentselect ssn, name, address

from foreign-student

Page 8: Temple University – CIS Dept. CIS331– Principles of Database Systems

DML - deletion etc

delete the record of ‘smith’

Page 9: Temple University – CIS Dept. CIS331– Principles of Database Systems

DML - deletion etc

delete the record of ‘smith’:

delete from studentwhere name=‘smith’

(careful - it deletes ALL the ‘smith’s!)

Page 10: Temple University – CIS Dept. CIS331– Principles of Database Systems

DML - update etc

record the grade ‘A’ for ssn=123 and course cis351

update takesset grade=“A”where ssn=“123” and c-id=“cis351”

(will set to “A” ALL such records)

Page 11: Temple University – CIS Dept. CIS331– Principles of Database Systems

DML - view update

consider the db-takes view:create view db-takes as (select * from takes where c-id=“cis351”)

view updates are tricky - typically, we can only update views that have no joins, nor aggregates

even so, consider changing a c-id to cis333....

Page 12: Temple University – CIS Dept. CIS331– Principles of Database Systems

DML - joins

so far: ‘INNER’ joins, eg:

select ssn, c-namefrom takes, classwhere takes.c-id = class.c-id

Page 13: Temple University – CIS Dept. CIS331– Principles of Database Systems

Reminder: our Mini-U db

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

TAKESSSN c-id grade

123 cis331 A234 cis331 B

Page 14: Temple University – CIS Dept. CIS331– Principles of Database Systems

inner join

SSN c-name123d.b.234d.b. o.s.: gone!

TAKESSSN c-id grade

123 cis331 A234 cis331 B

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

Page 15: Temple University – CIS Dept. CIS331– Principles of Database Systems

outer join

SSN c-name123d.b.234d.b.null o.s.

TAKESSSN c-id grade

123 cis331 A234 cis331 B

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

Page 16: Temple University – CIS Dept. CIS331– Principles of Database Systems

outer join

SSN c-name123d.b.234d.b.null o.s.

select ssn, c-namefrom takes outer join class on

takes.c-id=class.c-id

Page 17: Temple University – CIS Dept. CIS331– Principles of Database Systems

outer join

left outer join right outer join full outer join natural join

Page 18: Temple University – CIS Dept. CIS331– Principles of Database Systems

Overview - detailed - SQL

DML select, from, where, renaming,

ordering, aggregate functions, nested

subqueries insertion, deletion, update

other parts: DDL, embedded SQL, auth etc

Page 19: Temple University – CIS Dept. CIS331– Principles of Database Systems

Data Definition Language

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

Page 20: Temple University – CIS Dept. CIS331– Principles of Database Systems

Data Definition Language

create table r( A1 D1, …, An Dn, integrity-constraint1, … integrity-constraint-n)

Page 21: Temple University – CIS Dept. CIS331– Principles of Database Systems

Data Definition Language

Domains: char(n), varchar(n) int, numeric(p,d), real, double

precision float, smallint date, time

Page 22: Temple University – CIS Dept. CIS331– Principles of Database Systems

Data Definition Language

integrity constraints: primary key foreign key check(P)

Page 23: Temple University – CIS Dept. CIS331– Principles of Database Systems

Data Definition Language

create 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 24: Temple University – CIS Dept. CIS331– Principles of Database Systems

Data Definition Language

delete a table: difference between drop table student

delete from student

Page 25: Temple University – CIS Dept. CIS331– Principles of Database Systems

Data Definition Language

modify a table: alter table student drop address

alter table student add major char(10)

Page 26: Temple University – CIS Dept. CIS331– Principles of Database Systems

Overview - detailed - SQL

DML select, from, where, renaming,

ordering, aggregate functions, nested

subqueries insertion, deletion, update

other parts: DDL, embedded SQL, auth etc

Page 27: Temple University – CIS Dept. CIS331– Principles of Database Systems

Embedded SQL

from within a ‘host’ language (eg., ‘C’, ‘VB’)

EXEC SQL <emb. SQL stmnt> END-EXEC

Q: why do we need embedded SQL??

Page 28: Temple University – CIS Dept. CIS331– Principles of Database Systems

Embedded SQL

SQL returns sets; host language expects a tuple - impedance mismatch!

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

example:

Page 29: Temple University – CIS Dept. CIS331– Principles of Database Systems

Embedded SQL

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

Page 30: Temple University – CIS Dept. CIS331– Principles of Database Systems

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 31: Temple University – CIS Dept. CIS331– Principles of Database Systems

Embedded SQL - ctn’d

…EXEC SQL close c END-EXEC…} /* end main() */

Page 32: Temple University – CIS Dept. CIS331– Principles of Database Systems

dynamic SQL Construct and submit SQL queries at run

time “?”: a place holder for a value provided when it is executed

main(){ /* 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 33: Temple University – CIS Dept. CIS331– Principles of Database Systems

Overview - detailed - SQL

DML select, from, where, renaming,

ordering, aggregate functions, nested

subqueries insertion, deletion, update

other parts: DDL, embedded SQL, authorization, etc

Page 34: Temple University – CIS Dept. CIS331– Principles of Database Systems

SQL - misc

Later, we’ll see authorization: grant select on student to

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

etc)

Page 35: Temple University – CIS Dept. CIS331– Principles of Database Systems

General Overview - rel. model

Formal query languages rel algebra and calculi

Commercial query languages SQL QBE, (QUEL)

Page 36: Temple University – CIS Dept. CIS331– Principles of Database Systems

Rel. model – Introduction to 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 cis351

Page 37: Temple University – CIS Dept. CIS331– Principles of Database Systems

Rel. model - QBE

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

TAKESSSN c-id grade

123cis331 A234cis331 B

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

Page 38: Temple University – CIS Dept. CIS331– Principles of Database Systems

Rel. model - QBE

CLASSc-id c-name units

TAKESSSN c-id grade

STUDENTSsn Name Address

Page 39: Temple University – CIS Dept. CIS331– Principles of Database Systems

Rel. model - QBE

CLASSc-id c-name units

SSN c-id grade_x cis351

STUDENTSsn Name Address_x P.

names of students taking cis351

Page 40: Temple University – CIS Dept. CIS331– Principles of Database Systems

Rel. model - QBE

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

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

Page 41: Temple University – CIS Dept. CIS331– Principles of Database Systems

Rel. model - QBE

CLASSc-id c-name units

SSN c-id gradeP.AVG.ALL.

STUDENTSsn Name Address

aggregate: avg grade overall:

Page 42: Temple University – CIS Dept. CIS331– Principles of Database Systems

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 43: Temple University – CIS Dept. CIS331– Principles of Database Systems

General Overview - rel. model

Formal query languages rel algebra and calculi

Commercial query languages SQL QBE, (QUEL)

Page 44: Temple University – CIS Dept. CIS331– Principles of Database Systems

Rel. model - QUEL

Used in INGRES only - of historical interest.

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

]}[][|{ ssntssnsstudentst

Page 45: Temple University – CIS Dept. CIS331– Principles of Database Systems

Rel. model - QUEL

general syntax:range of …. is t-

name

retrieve (attribute list)

where condition

SQL

select attr. list

from t-name

where condition

Page 46: Temple University – CIS Dept. CIS331– Principles of Database Systems

Rel. model - QUEL

very similar to SQL also supports aggregates,

ordering etc

Page 47: Temple University – CIS Dept. CIS331– Principles of Database Systems

General Overview

Formal query languages rel algebra and calculi

Commercial query languages SQL QBE, (QUEL)

Integrity constraints Functional Dependencies Normalization - ‘good’ DB design