Presentation is loading. Please wait.

Presentation is loading. Please wait.

Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Rel. model - SQL part3.

Similar presentations


Presentation on theme: "Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Rel. model - SQL part3."— Presentation transcript:

1 Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Rel. model - SQL part3

2 Carnegie Mellon 15-415 - C. Faloutsos2 General Overview - rel. model Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL)

3 Carnegie Mellon 15-415 - C. Faloutsos3 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, embedded SQL, auth etc

4 Carnegie Mellon 15-415 - C. Faloutsos4 Reminder: our Mini-U db

5 Carnegie Mellon 15-415 - C. Faloutsos5 DML - insertions etc insert into student values (“123”, “smith”, “main”) insert into student(ssn, name, address) values (“123”, “smith”, “main”)

6 Carnegie Mellon 15-415 - C. Faloutsos6 DML - insertions etc bulk insertion: how to insert, say, a table of ‘foreign-student’s, in bulk?

7 Carnegie Mellon 15-415 - C. Faloutsos7 DML - insertions etc bulk insertion: insert into student select ssn, name, address from foreign-student

8 Carnegie Mellon 15-415 - C. Faloutsos8 DML - deletion etc delete the record of ‘smith’

9 Carnegie Mellon 15-415 - C. Faloutsos9 DML - deletion etc delete the record of ‘smith’: delete from student where name=‘smith’ (careful - it deletes ALL the ‘smith’s!)

10 Carnegie Mellon 15-415 - C. Faloutsos10 DML - update etc record the grade ‘A’ for ssn=123 and course 15- 415 update takes set grade=“A” where ssn=“123” and c-id=“15-415” (will set to “A” ALL such records)

11 Carnegie Mellon 15-415 - C. Faloutsos11 DML - view update consider the db-takes view: create view db-takes as (select * from takes where c-id=“15-415”) view updates are tricky - typically, we can only update views that have no joins, nor aggregates even so, consider changing a c-id to 15-222...

12 Carnegie Mellon 15-415 - C. Faloutsos12 DML - joins so far: ‘INNER’ joins, eg: select ssn, c-name from takes, class where takes.c-id = class.c-id

13 Carnegie Mellon 15-415 - C. Faloutsos13 Reminder: our Mini-U db

14 Carnegie Mellon 15-415 - C. Faloutsos14 inner join o.s.: gone!

15 Carnegie Mellon 15-415 - C. Faloutsos15 outer join

16 Carnegie Mellon 15-415 - C. Faloutsos16 outer join select ssn, c-name from takes outer join class on takes.c- id=class.c-id

17 Carnegie Mellon 15-415 - C. Faloutsos17 outer join left outer join right outer join full outer join natural join

18 Carnegie Mellon 15-415 - C. Faloutsos18 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, embedded SQL, auth etc

19 Carnegie Mellon 15-415 - C. Faloutsos19 Data Definition Language create table student (ssn char(9) not null, name char(30), address char(50), primary key (ssn) )

20 Carnegie Mellon 15-415 - C. Faloutsos20 Data Definition Language create table r( A1 D1, …, An Dn, integrity-constraint1, … integrity-constraint-n)

21 Carnegie Mellon 15-415 - C. Faloutsos21 Data Definition Language Domains: char(n), varchar(n) int, numeric(p,d), real, double precision float, smallint date, time

22 Carnegie Mellon 15-415 - C. Faloutsos22 Data Definition Language integrity constraints: primary key foreign key check(P)

23 Carnegie Mellon 15-415 - C. Faloutsos23 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”))

24 Carnegie Mellon 15-415 - C. Faloutsos24 Data Definition Language delete a table: difference between drop table student delete from student

25 Carnegie Mellon 15-415 - C. Faloutsos25 Data Definition Language modify a table: alter table student drop address alter table student add major char(10)

26 Carnegie Mellon 15-415 - C. Faloutsos26 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, embedded SQL, auth etc

27 Carnegie Mellon 15-415 - C. Faloutsos27 Embedded SQL from within a ‘host’ language (eg., ‘C’, ‘VB’) EXEC SQL END-EXEC Q: why do we need embedded SQL??

28 Carnegie Mellon 15-415 - C. Faloutsos28 Embedded SQL SQL returns sets; host language expects a tuple - impedance mismatch! solution: ‘cursor’, ie., a ‘pointer’ over the set of tuples. example:

29 Carnegie Mellon 15-415 - C. Faloutsos29 Embedded SQL main(){ … EXEC SQL declare c cursor for select * from student END-EXEC …

30 Carnegie Mellon 15-415 - C. Faloutsos30 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); }

31 Carnegie Mellon 15-415 - C. Faloutsos31 Embedded SQL - ctn’d … EXEC SQL close c END-EXEC … } /* end main() */

32 Carnegie Mellon 15-415 - C. Faloutsos32 dynamic SQL 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() */

33 Carnegie Mellon 15-415 - C. Faloutsos33 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, embedded SQL, auth etc

34 Carnegie Mellon 15-415 - C. Faloutsos34 SQL - misc Later, we’ll see authorization: grant select on student to transactions other features (triggers, assertions etc) see, e.g.: http://www.contrib.andrew.cmu.edu/~shadow/sql.html

35 Carnegie Mellon 15-415 - C. Faloutsos35 General Overview - rel. model Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL)

36 Carnegie Mellon 15-415 - C. Faloutsos36 Rel. model - QBE Inspired by the R.D.C. “P.” -> print (ie., ‘select’ of SQL) _x, _y: domain variables (ie., attribute names) Example: find names of students taking 15- 415

37 Carnegie Mellon 15-415 - C. Faloutsos37 Rel. model - QBE

38 Carnegie Mellon 15-415 - C. Faloutsos38 Rel. model - QBE

39 Carnegie Mellon 15-415 - C. Faloutsos39 Rel. model - QBE names of students taking 15-415

40 Carnegie Mellon 15-415 - C. Faloutsos40 Rel. model - QBE condition box self-joins (Tom’s grandparents) ordering (AO., DO.) aggregation (SUM.ALL., COUNT.UNIQUE., …) group-by (G.)

41 Carnegie Mellon 15-415 - C. Faloutsos41 Rel. model - QBE aggregate: avg grade overall:

42 Carnegie Mellon 15-415 - C. Faloutsos42 Rel. model - QBE aggregate: avg grade per student:

43 Carnegie Mellon 15-415 - C. Faloutsos43 General Overview - rel. model Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL)

44 Carnegie Mellon 15-415 - C. Faloutsos44 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);

45 Carnegie Mellon 15-415 - C. Faloutsos45 Rel. model - QUEL general syntax: range of …. is t-name retrieve (attribute list) where condition SQL select attr. list from t-name where condition

46 Carnegie Mellon 15-415 - C. Faloutsos46 Rel. model - QUEL very similar to SQL also supports aggregates, ordering etc

47 Carnegie Mellon 15-415 - C. Faloutsos47 General Overview Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL) Integrity constraints Functional Dependencies Normalization - ‘good’ DB design


Download ppt "Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Rel. model - SQL part3."

Similar presentations


Ads by Google