1 / 31

SQL Principles of Data Management: DML, Integrity Constraints, and Embedded SQL

Learn the principles of data management using SQL, including DML commands, integrity constraints, and embedded SQL within a host language. Understand the importance of integrity constraints and how to modify tables using SQL. Explore the concepts of embedded SQL and how it solves the mismatch between SQL returning sets and the host language expecting tuples. Also, gain an overview of other features of SQL such as authorization, transactions, triggers, and assertions.

Download Presentation

SQL Principles of Data Management: DML, Integrity Constraints, and Embedded SQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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)

  2. Overview - detailed - SQL • DML • select, from, where, renaming, ordering, • aggregate functions, nested subqueries • insertion, deletion, update • other parts: DDL, embedded SQL, auth etc

  3. Data Definition Language createtable student (ssn char(9) not null, name char(30), address char(50), primarykey (ssn) )

  4. Data Definition Language createtable r( A1 D1, …, An Dn, integrity-constraint1, … integrity-constraint-n)

  5. Data Definition Language Domains: • char(n), varchar(n) • int, numeric(p,d), real, double precision • float, smallint • date, time

  6. Data Definition Language integrity constraints: • primary key • foreign key • check(P)

  7. 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”))

  8. Data Definition Language delete a table: difference between droptable student deletefrom student

  9. Data Definition Language modify a table: altertable student drop address altertable student add major char(10)

  10. Overview - detailed - SQL • DML • select, from, where, renaming, ordering, • aggregate functions, nested subqueries • insertion, deletion, update • other parts: DDL, embedded SQL, auth etc

  11. Embedded SQL from within a ‘host’ language (eg., ‘C’, ‘VB’) EXEC SQL <emb. SQL stmnt> END-EXEC Q: why do we need embedded SQL??

  12. Embedded SQL SQL returns sets; host language expects a tuple - impedance mismatch! solution: ‘cursor’, ie., a ‘pointer’ over the set of tuples. example:

  13. Embedded SQL main(){ … EXEC SQL declare c cursorfor select * from student END-EXEC …

  14. 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); }

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

  16. 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() */

  17. Overview - detailed - SQL • DML • select, from, where, renaming, ordering, • aggregate functions, nested subqueries • insertion, deletion, update • other parts: DDL, embedded SQL, auth etc

  18. SQL - misc Later, we’ll see • authorization: grant select on student to <user-id> • transactions • other features (triggers, assertions etc)

  19. General Overview - rel. model • Formal query languages • rel algebra and calculi • Commercial query languages • SQL • QBE, (QUEL)

  20. 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

  21. Rel. model - QBE

  22. Rel. model - QBE

  23. Rel. model - QBE names of students taking 15-415

  24. Rel. model - QBE • condition box • self-joins (Tom’s grandparents) • ordering (AO., DO.) • aggregation (SUM.ALL., COUNT.UNIQUE. , …) • group-by (G.)

  25. Rel. model - QBE aggregate: avg grade overall:

  26. Rel. model - QBE aggregate: avg grade per student:

  27. General Overview - rel. model • Formal query languages • rel algebra and calculi • Commercial query languages • SQL • QBE, (QUEL)

  28. 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);

  29. Rel. model - QUEL general syntax: range of …. ist-name retrieve (attribute list) wherecondition SQL select attr. list from t-name where condition

  30. Rel. model - QUEL • very similar to SQL • also supports aggregates, ordering etc

  31. General Overview • Formal query languages • rel algebra and calculi • Commercial query languages • SQL • QBE, (QUEL) • Integrity constraints • Functional Dependencies • Normalization - ‘good’ DB design

More Related