CSI 1306 Databases – Part 4
Upcoming SlideShare
Loading in...5
×
 

CSI 1306 Databases – Part 4

on

  • 689 views

 

Statistics

Views

Total Views
689
Views on SlideShare
689
Embed Views
0

Actions

Likes
0
Downloads
7
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

CSI 1306 Databases – Part 4 CSI 1306 Databases – Part 4 Presentation Transcript

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