CSI 1306 Databases – Part 4 SQL continued and  Database Management Systems
Union <ul><li>Union  allows the merging of two Selects </li></ul><ul><ul><li>The number of fields must be the same in both...
Query 16 <ul><li>Write Query 15 as a Union </li></ul>
Query 17 <ul><li>Find all students, and employees whose names begin with Z </li></ul><ul><ul><li>the union operation is th...
Where - In <ul><li>In  will make the comparison to a list of known values.  It is a Set operator. </li></ul>
Query 18 <ul><li>Use the Set operator to find information about a series of students </li></ul>
Query 19 <ul><li>Students from Guelph who take the philosophy course </li></ul>
Query 20 <ul><li>Students from Guelph who do not take the psychology course </li></ul>
Aggregate Functions <ul><li>Select Aggregate Function   </li></ul><ul><ul><li>Will return a single value from a table </li...
Query 21 <ul><li>Students registered in 7 or fewer courses </li></ul><ul><ul><li>must use the aggregate COUNT() </li></ul>...
Query 22 <ul><li>Average mark in the CS course </li></ul>
Query 23 <ul><li>The oldest student's birthday </li></ul>
Query 24 <ul><li>The youngest student's birthday </li></ul>
Query 25 <ul><li>The name of the youngest student </li></ul>
Query 26 <ul><li>Find the students who have handed in an assignment that is 25% greater than the average assignment mark <...
SQL <ul><li>Data Definition Language </li></ul><ul><ul><li>(create table, drop table, alter table) </li></ul></ul><ul><li>...
Data Definition Language <ul><li>Create Table   </li></ul><ul><ul><li>To create an empty database </li></ul></ul><ul><ul><...
Data Manipulation Language <ul><li>Insert </li></ul><ul><ul><li>To add records to the end of a table (append) </li></ul></...
Data Manipulation Language <ul><li>Delete </li></ul><ul><ul><li>To remove records from a table </li></ul></ul><ul><ul><li>...
Databases <ul><li>Data Files  contain the data (records) </li></ul><ul><li>Data Definitions  describe the different pieces...
Hierarchy of Data <ul><li>Bit   Byte   Field   Record   Data file   Database </li></ul><ul><li>Physical  Representati...
Hierarchy of Data <ul><li>Bit  – 0 or 1 (i.e. a location on a disk which is magnetized in one direction or the other) </li...
Databases <ul><li>Databases are used in most business applications </li></ul><ul><ul><li>Sales, accounts receivable, inven...
Databases <ul><li>Building a database is accomplished in several steps using the capabilities of the database management s...
Database Management Software <ul><li>There are 3 types of data models </li></ul><ul><ul><li>Hierarchical </li></ul></ul><u...
Database Management Software <ul><li>However, all maintain data independence </li></ul><ul><ul><ul><li>the format of the d...
Database Management Software <ul><li>There are 4 classes of users </li></ul><ul><ul><ul><li>A  database administrator  use...
General organization of a DBMS
Other Capabilities of Database Management Software <ul><li>Efficient access to data </li></ul><ul><ul><li>Databases typica...
Other Capabilities of Database Management Software <ul><li>Support for different user views </li></ul><ul><ul><li>Cross se...
Database Management Software <ul><li>Relational is the most recent model and the one most widely used in the business worl...
Excel vs Access <ul><li>Where does Excel break down for database modeling? </li></ul><ul><ul><ul><li>Ability to search mul...
Additional Material Additional SQL Examples
Query 27 <ul><li>Is there a faster query than Query 11? </li></ul><ul><ul><li>This  very  slow query accesses three tables...
Query 27 <ul><li>Create a new table with the two attributes needed for further selection </li></ul>
Query 27 <ul><li>Insert  only  data about 86030 into the new table </li></ul>
Query 27 <ul><li>Since there are 9 rows in tmp,  </li></ul><ul><ul><li>the overall size of the product drops from 1371648 ...
Query 27 <ul><li>Clean up: remove the table </li></ul>
Query 28 <ul><li>Use the method in Query 27 to find out who tutors SCOTT </li></ul>
 
Query 29 <ul><li>Query 26, recalculated the average several times. Find a faster system . </li></ul>
Query 30 <ul><li>Calculate the assignment mark for the CS course </li></ul><ul><ul><li>there are five marks and their weig...
Query 30 <ul><li>Build a temporary table of marks in CS assignments  with weights </li></ul>
Query 30 <ul><li>Check to see that all students have handed in all assignments  </li></ul><ul><ul><li>0 should be put in f...
Query 30 <ul><li>Calculate the mark </li></ul>
Query 30 <ul><li>Now sort the results </li></ul>
Query 30 <ul><li>Now sort the results by mark </li></ul>
Query 30 <ul><li>Clean up </li></ul>
Query 31 <ul><li>Modify the ROOM table by adding a Building description </li></ul>19 More records
Query 32  <ul><li>Create a new course ECON with Lab </li></ul><ul><li>Add a record to the LAB table </li></ul>
Query 32 <ul><li>Add a record to the COURSE table </li></ul>
Query 33 <ul><li>Remove all the records from the LAB table </li></ul>
Query 34 <ul><li>Delete the Physics Lab </li></ul>
Query 35 <ul><li>Add 1% to  every  mark in the table of marks  </li></ul><ul><ul><li>the attribute name denotes the old va...
Query 36 <ul><li>Add 2.2 to a specific mark </li></ul><ul><ul><li>note that the primary key for the table MARK has three a...
Query 37 <ul><li>Add 5% to the lab pay of every tutor who is paid below the average   </li></ul>
Homework
<ul><li>For the people/picture database develop the SQL queries to </li></ul><ul><ul><li>list all pictures between two dat...
Upcoming SlideShare
Loading in …5
×

CSI 1306 Databases – Part 4

712 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
712
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

CSI 1306 Databases – Part 4

  1. 1. CSI 1306 Databases – Part 4 SQL continued and Database Management Systems
  2. 2. Union <ul><li>Union allows the merging of two Selects </li></ul><ul><ul><li>The number of fields must be the same in both Select statements </li></ul></ul><ul><ul><ul><li>Field types do not matter </li></ul></ul></ul>
  3. 3. Query 16 <ul><li>Write Query 15 as a Union </li></ul>
  4. 4. Query 17 <ul><li>Find all students, and employees whose names begin with Z </li></ul><ul><ul><li>the union operation is the only way if the partial results come from different tables </li></ul></ul>
  5. 5. Where - In <ul><li>In will make the comparison to a list of known values. It is a Set operator. </li></ul>
  6. 6. Query 18 <ul><li>Use the Set operator to find information about a series of students </li></ul>
  7. 7. Query 19 <ul><li>Students from Guelph who take the philosophy course </li></ul>
  8. 8. Query 20 <ul><li>Students from Guelph who do not take the psychology course </li></ul>
  9. 9. Aggregate Functions <ul><li>Select Aggregate Function </li></ul><ul><ul><li>Will return a single value from a table </li></ul></ul><ul><ul><li>Aggregate Functions are performed on a field </li></ul></ul><ul><ul><li>Standard Functions are: </li></ul></ul><ul><ul><ul><li>Avg() - Arithmetic average of the values </li></ul></ul></ul><ul><ul><ul><li>Count() - Number of records </li></ul></ul></ul><ul><ul><ul><li>First() - Value of the first record in the set </li></ul></ul></ul><ul><ul><ul><li>Last() - Value of the last record in the set </li></ul></ul></ul><ul><ul><ul><li>Min() - Smallest value in the set </li></ul></ul></ul><ul><ul><ul><li>Max() - Largest value in the set </li></ul></ul></ul><ul><ul><ul><li>Sum() - Total of all records </li></ul></ul></ul><ul><ul><ul><li>StDev() - Standard deviation (nonbiased) </li></ul></ul></ul><ul><ul><ul><li>StDevP - Standard deviation (biased) </li></ul></ul></ul><ul><ul><ul><li>Var() - Variance (nonbiased) </li></ul></ul></ul><ul><ul><ul><li>VarP() - Variance deviation (biased) </li></ul></ul></ul>
  10. 10. Query 21 <ul><li>Students registered in 7 or fewer courses </li></ul><ul><ul><li>must use the aggregate COUNT() </li></ul></ul>
  11. 11. Query 22 <ul><li>Average mark in the CS course </li></ul>
  12. 12. Query 23 <ul><li>The oldest student's birthday </li></ul>
  13. 13. Query 24 <ul><li>The youngest student's birthday </li></ul>
  14. 14. Query 25 <ul><li>The name of the youngest student </li></ul>
  15. 15. Query 26 <ul><li>Find the students who have handed in an assignment that is 25% greater than the average assignment mark </li></ul>
  16. 16. SQL <ul><li>Data Definition Language </li></ul><ul><ul><li>(create table, drop table, alter table) </li></ul></ul><ul><li>Data Manipulation Language </li></ul><ul><ul><li>(insert, delete, update) </li></ul></ul><ul><li>Query Language. </li></ul><ul><ul><li>(select) </li></ul></ul>
  17. 17. Data Definition Language <ul><li>Create Table </li></ul><ul><ul><li>To create an empty database </li></ul></ul><ul><ul><li>Create Table Name ( Field Type, Field Type… ) </li></ul></ul><ul><li>Drop Table </li></ul><ul><ul><li>To delete a table </li></ul></ul><ul><ul><li>Drop Table Name </li></ul></ul><ul><li>Alter Table </li></ul><ul><ul><li>To add/remove a field from the database </li></ul></ul><ul><ul><li>Alter table Name add/drop Field Type </li></ul></ul>
  18. 18. Data Manipulation Language <ul><li>Insert </li></ul><ul><ul><li>To add records to the end of a table (append) </li></ul></ul><ul><ul><li>Insert Into Name values ( Field1, Field2 … ) </li></ul></ul><ul><ul><ul><li>Strings have single quotes ‘’ </li></ul></ul></ul><ul><ul><ul><li>Works with the Select statement to add records from another table </li></ul></ul></ul><ul><li>Update </li></ul><ul><ul><li>To modify records in a database </li></ul></ul><ul><ul><li>Update Name set Field = Expression </li></ul></ul><ul><ul><ul><li>Also works with the Where clause in the Select statement </li></ul></ul></ul>
  19. 19. Data Manipulation Language <ul><li>Delete </li></ul><ul><ul><li>To remove records from a table </li></ul></ul><ul><ul><li>Delete * from Name </li></ul></ul><ul><ul><ul><li>Deletes all records </li></ul></ul></ul><ul><ul><ul><li>Might help if someone made a mistake </li></ul></ul></ul><ul><ul><ul><li>Note the Where clause in the Select statement works with Delete </li></ul></ul></ul>
  20. 20. Databases <ul><li>Data Files contain the data (records) </li></ul><ul><li>Data Definitions describe the different pieces of information (fields) in the data files </li></ul><ul><li>Data Schema describes how the information is related and how one is to interpret such relations </li></ul><ul><li>A Database is an organized collection of related data files </li></ul><ul><li>A Database Management System is software that allows us to create and work with databases </li></ul>
  21. 21. Hierarchy of Data <ul><li>Bit  Byte  Field  Record  Data file  Database </li></ul><ul><li>Physical Representation (how the data is actually stored) </li></ul><ul><ul><li>Bits </li></ul></ul><ul><ul><li>Bytes </li></ul></ul><ul><li>Logical Representation (how we, through software, organize, process and interpret the stored data) </li></ul><ul><ul><li>Fields, Records, Data files, Databases </li></ul></ul>
  22. 22. Hierarchy of Data <ul><li>Bit – 0 or 1 (i.e. a location on a disk which is magnetized in one direction or the other) </li></ul><ul><li>Byte – 8 bits (a character) </li></ul><ul><li>Field – a collection of bytes (e.g. student number) </li></ul><ul><li>Record – a collection of fields (e.g. student number, name, address) </li></ul><ul><li>Data file – a collection of records (e.g. all students in a class) </li></ul><ul><li>Database – a collection of related data files (e.g. students, professors, classrooms) </li></ul>
  23. 23. Databases <ul><li>Databases are used in most business applications </li></ul><ul><ul><li>Sales, accounts receivable, inventory </li></ul></ul><ul><ul><li>Purchasing, accounts payable </li></ul></ul><ul><ul><li>Budgeting, payroll, general ledger </li></ul></ul><ul><ul><li>On-line banking </li></ul></ul><ul><ul><li>Mutual fund administration </li></ul></ul><ul><ul><li>Library catalogues </li></ul></ul>
  24. 24. Databases <ul><li>Building a database is accomplished in several steps using the capabilities of the database management system. </li></ul><ul><ul><li>Define the information to be stored and the relationships between the information </li></ul></ul><ul><ul><li>Build a database schema </li></ul></ul><ul><ul><li>Create an empty database </li></ul></ul><ul><ul><li>Fill (or populate) the database with data </li></ul></ul>
  25. 25. Database Management Software <ul><li>There are 3 types of data models </li></ul><ul><ul><li>Hierarchical </li></ul></ul><ul><ul><li>Network </li></ul></ul><ul><ul><li>Relational </li></ul></ul><ul><li>Each physically structures and records the relationships among records in the database in a different manner </li></ul><ul><li>Whereas, relational uses common fields, hierarchical and network use pointers (fields containing addresses of related records) </li></ul>
  26. 26. Database Management Software <ul><li>However, all maintain data independence </li></ul><ul><ul><ul><li>the format of the database is recorded within the database itself, on disk with the data (and not in the application programs) </li></ul></ul></ul><ul><li>Since many application may process data in the same database, this is important because, </li></ul><ul><ul><ul><li>Each application program does not have to define the format of the database </li></ul></ul></ul><ul><ul><ul><li>When the format of the database is changed, application programs do not have to be changed, for example to accommodate the addition of a new field to a record or a new data file to the database </li></ul></ul></ul>
  27. 27. Database Management Software <ul><li>There are 4 classes of users </li></ul><ul><ul><ul><li>A database administrator uses the data definition language supplied by the DBMS to define the data types and definitions for the data files in the database. He develops a data schema to describe how the information is related and how to interpret such relations </li></ul></ul></ul><ul><ul><ul><li>An application programmer uses the data manipulation language supplied by the DBMS to access, update and modify the data in the database </li></ul></ul></ul><ul><ul><ul><li>An end user uses an application program that uses the DBMS software to access data for processing </li></ul></ul></ul><ul><ul><ul><li>An advanced user uses a query language to write queries which are processed by the DBMS software to access the database and provide answers to the queries </li></ul></ul></ul>
  28. 28. General organization of a DBMS
  29. 29. Other Capabilities of Database Management Software <ul><li>Efficient access to data </li></ul><ul><ul><li>Databases typically contain millions of records </li></ul></ul><ul><li>Controlled access to data </li></ul><ul><ul><li>Who can see what </li></ul></ul><ul><ul><li>Read and/or write access </li></ul></ul><ul><li>Concurrent access to data </li></ul><ul><ul><li>Simultaneous access by multiple users / applications </li></ul></ul><ul><ul><li>Locking a record during update </li></ul></ul>
  30. 30. Other Capabilities of Database Management Software <ul><li>Support for different user views </li></ul><ul><ul><li>Cross section through a database, focusing on a specific task </li></ul></ul><ul><ul><li>End users will see the data according their individual needs </li></ul></ul><ul><li>Support for database recovery </li></ul><ul><ul><li>Techniques for reconstruction of a database after a failure (disk crash, program error, power outage, fire) </li></ul></ul>
  31. 31. Database Management Software <ul><li>Relational is the most recent model and the one most widely used in the business world </li></ul><ul><ul><li>It is also the most intuitive </li></ul></ul><ul><ul><li>Examples of relational database software include </li></ul></ul><ul><ul><ul><li>Access, Oracle, Progress, Sybase, DB2, Ingres </li></ul></ul></ul>
  32. 32. Excel vs Access <ul><li>Where does Excel break down for database modeling? </li></ul><ul><ul><ul><li>Ability to search multiple data files </li></ul></ul></ul><ul><ul><ul><ul><li>Searches are typically limited to one worksheet </li></ul></ul></ul></ul><ul><ul><ul><li>Size of database </li></ul></ul></ul><ul><ul><ul><ul><li>Excel is limited in the number of rows in a worksheet </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>65,000 records vs millions in Access </li></ul></ul></ul></ul></ul><ul><ul><ul><li>Amount of memory required </li></ul></ul></ul><ul><ul><ul><ul><li>A complete Excel worksheet must be loaded into memory </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Access does not load every record into memory at one time </li></ul></ul></ul></ul><ul><ul><ul><li>Speed of execution </li></ul></ul></ul><ul><ul><ul><ul><li>Excel is optimized for data calculations not data retrieval </li></ul></ul></ul></ul><ul><ul><ul><li>Concurrency </li></ul></ul></ul><ul><ul><ul><ul><li>Excel allows only one user to change data at a time </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Opens as “Read Only” for a second user </li></ul></ul></ul></ul>
  33. 33. Additional Material Additional SQL Examples
  34. 34. Query 27 <ul><li>Is there a faster query than Query 11? </li></ul><ul><ul><li>This very slow query accesses three tables with 1371648 rows! </li></ul></ul><ul><ul><li>It's better to help the unoptimized SQL by using a temporary table. </li></ul></ul>
  35. 35. Query 27 <ul><li>Create a new table with the two attributes needed for further selection </li></ul>
  36. 36. Query 27 <ul><li>Insert only data about 86030 into the new table </li></ul>
  37. 37. Query 27 <ul><li>Since there are 9 rows in tmp, </li></ul><ul><ul><li>the overall size of the product drops from 1371648 to 13824 </li></ul></ul>
  38. 38. Query 27 <ul><li>Clean up: remove the table </li></ul>
  39. 39. Query 28 <ul><li>Use the method in Query 27 to find out who tutors SCOTT </li></ul>
  40. 41. Query 29 <ul><li>Query 26, recalculated the average several times. Find a faster system . </li></ul>
  41. 42. Query 30 <ul><li>Calculate the assignment mark for the CS course </li></ul><ul><ul><li>there are five marks and their weight are stored in a separate table </li></ul></ul><ul><ul><li>this problem is best solved in two steps </li></ul></ul>
  42. 43. Query 30 <ul><li>Build a temporary table of marks in CS assignments with weights </li></ul>
  43. 44. Query 30 <ul><li>Check to see that all students have handed in all assignments </li></ul><ul><ul><li>0 should be put in for an incomplete </li></ul></ul><ul><ul><li>empty result is </li></ul></ul>
  44. 45. Query 30 <ul><li>Calculate the mark </li></ul>
  45. 46. Query 30 <ul><li>Now sort the results </li></ul>
  46. 47. Query 30 <ul><li>Now sort the results by mark </li></ul>
  47. 48. Query 30 <ul><li>Clean up </li></ul>
  48. 49. Query 31 <ul><li>Modify the ROOM table by adding a Building description </li></ul>19 More records
  49. 50. Query 32 <ul><li>Create a new course ECON with Lab </li></ul><ul><li>Add a record to the LAB table </li></ul>
  50. 51. Query 32 <ul><li>Add a record to the COURSE table </li></ul>
  51. 52. Query 33 <ul><li>Remove all the records from the LAB table </li></ul>
  52. 53. Query 34 <ul><li>Delete the Physics Lab </li></ul>
  53. 54. Query 35 <ul><li>Add 1% to every mark in the table of marks </li></ul><ul><ul><li>the attribute name denotes the old value; the expression after “=” defines the new value </li></ul></ul>
  54. 55. Query 36 <ul><li>Add 2.2 to a specific mark </li></ul><ul><ul><li>note that the primary key for the table MARK has three attributes, all of them necessary to identify the mark </li></ul></ul>
  55. 56. Query 37 <ul><li>Add 5% to the lab pay of every tutor who is paid below the average </li></ul>
  56. 57. Homework
  57. 58. <ul><li>For the people/picture database develop the SQL queries to </li></ul><ul><ul><li>list all pictures between two dates </li></ul></ul><ul><ul><li>list all pictures in which a specific person appears </li></ul></ul><ul><ul><li>list all pictures between two dates in which a specific person appears </li></ul></ul><ul><li>Write SQL queries for queries 1 to 4 from slide 29 of the Databases – Part 2 lecture </li></ul>

×