470 likes | 587 Views
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
E N D
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 • Commercial query languages • SQL • QBE, (QUEL)
Overview - detailed - SQL • DML • select, from, where, renaming, ordering, • aggregate functions, nested subqueries • insertion, deletion, update • other parts: DDL, embedded SQL, auth etc
CLASS c-id c-name units cis331 d.b. 2 cis321 o.s. 2 TAKES SSN c-id grade 123 cis331 A 234 cis331 B Reminder: our Mini-U db
DML - insertions etc insert into student values (“123”, “smith”, “main”) insert into student(ssn, name, address) values (“123”, “smith”, “main”)
DML - insertions etc bulk insertion: how to insert, say, a table of ‘foreign-student’s, in bulk?
DML - insertions etc bulk insertion: insert into student select ssn, name, address from foreign-student
DML - deletion etc delete the record of ‘smith’
DML - deletion etc delete the record of ‘smith’: delete from student where name=‘smith’ (careful - it deletes ALL the ‘smith’s!)
DML - update etc record the grade ‘A’ for ssn=123 and course cis351 update takes set grade=“A” where ssn=“123” and c-id=“cis351” (will set to “A” ALL such records)
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....
DML - joins so far: ‘INNER’ joins, eg: select ssn, c-name from takes, class where takes.c-id = class.c-id
CLASS c-id c-name units cis331 d.b. 2 cis321 o.s. 2 TAKES SSN c-id grade 123 cis331 A 234 cis331 B Reminder: our Mini-U db
CLASS c-id c-name units cis331 d.b. 2 cis321 o.s. 2 TAKES SSN c-id grade 123 cis331 A 234 cis331 B inner join SSN c-name 123 d.b. o.s.: gone! 234 d.b.
CLASS c-id c-name units cis331 d.b. 2 cis321 o.s. 2 TAKES SSN c-id grade 123 cis331 A 234 cis331 B outer join SSN c-name 123 d.b. 234 d.b. null o.s.
outer join select ssn, c-name from takes outer join class on takes.c-id=class.c-id SSN c-name 123 d.b. 234 d.b. null o.s.
outer join • left outer join • right outer join • full outer join • natural join
Overview - detailed - SQL • DML • select, from, where, renaming, ordering, • aggregate functions, nested subqueries • insertion, deletion, update • other parts: DDL, embedded SQL, auth etc
Data Definition Language createtable student (ssn char(9) not null, name char(30), address char(50), primarykey (ssn) )
Data Definition Language createtable r( A1 D1, …, An Dn, integrity-constraint1, … integrity-constraint-n)
Data Definition Language Domains: • char(n), varchar(n) • int, numeric(p,d), real, double precision • float, smallint • date, time
Data Definition Language integrity constraints: • primary key • foreign key • check(P)
Data Definition Language createtable takes (ssn char(9) not null, c-id char(5) not null, grade char(1), primarykey (ssn, c-id), check grade in (“A”, “B”, “C”, “D”, “F”))
Data Definition Language delete a table: difference between droptable student deletefrom student
Data Definition Language modify a table: altertable student drop address altertable student add major char(10)
Overview - detailed - SQL • DML • select, from, where, renaming, ordering, • aggregate functions, nested subqueries • insertion, deletion, update • other parts: DDL, embedded SQL, auth etc
Embedded SQL from within a ‘host’ language (eg., ‘C’, ‘VB’) EXEC SQL <emb. SQL stmnt> END-EXEC Q: why do we need embedded SQL??
Embedded SQL SQL returns sets; host language expects a tuple - impedance mismatch! solution: ‘cursor’, i.e., a ‘pointer’ over the set of tuples example:
Embedded SQL main(){ … EXEC SQL declare c cursorfor select * from student END-EXEC …
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); }
Embedded SQL - ctn’d … EXEC SQL close c END-EXEC … } /* end main() */
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() */
Overview - detailed - SQL • DML • select, from, where, renaming, ordering, • aggregate functions, nested subqueries • insertion, deletion, update • other parts: DDL, embedded SQL, authorization, etc
SQL - misc Later, we’ll see • authorization: grant select on student to <user-id> • transactions • other features (triggers, assertions etc)
General Overview - rel. model • Formal query languages • rel algebra and calculi • Commercial query languages • SQL • QBE, (QUEL)
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
Rel. model - QBE CLASS c-id c-name units cis331 d.b. 2 cis321 o.s. 2 TAKES SSN c-id grade 123 cis331 A 234 cis331 B
Rel. model - QBE names of students taking cis351 SSN c-id grade _x cis351
Rel. model - QBE • condition box • self-joins (Tom’s grandparents) • ordering (AO., DO.) • aggregation (SUM.ALL., COUNT.UNIQUE. , …) • group-by (G.)
Rel. model - QBE aggregate: avg grade overall:
Rel. model - QBE aggregate: avg. grade per student:
General Overview - rel. model • Formal query languages • rel algebra and calculi • Commercial query languages • SQL • QBE, (QUEL)
Rel. model - QUEL Used in INGRES only - of historical interest. Eg.: find all ssn’s in mini-U: rangeof s is student; retrieve (s.ssn);
Rel. model - QUEL general syntax: range of …. ist-name retrieve (attribute list) wherecondition SQL select attr. list from t-name where condition
Rel. model - QUEL • very similar to SQL • also supports aggregates, ordering etc
General Overview • Formal query languages • rel algebra and calculi • Commercial query languages • SQL • QBE, (QUEL) • Integrity constraints • Functional Dependencies • Normalization - ‘good’ DB design