+ All Categories
Home > Documents > S2.DataStructuresandSQL

S2.DataStructuresandSQL

Date post: 04-Apr-2018
Category:
Upload: sargentshriver
View: 212 times
Download: 0 times
Share this document with a friend

of 180

Transcript
  • 7/30/2019 S2.DataStructuresandSQL

    1/180

    S2:DataStructuresandSQL

    ShawndraHill

    Spring2013

    TR1:30-3pmand3-4:30

  • 7/30/2019 S2.DataStructuresandSQL

    2/180

    2

  • 7/30/2019 S2.DataStructuresandSQL

    3/180

    APIs

    3

  • 7/30/2019 S2.DataStructuresandSQL

    4/180

    PhasesintheDMProcess:CRISP-DM

    4

    BusinessUnderstanding

    DataUnderstanding

    DataPreparaon

    Modeling

    Evaluaon

    Deployment

    www.crisp-dm.org

  • 7/30/2019 S2.DataStructuresandSQL

    5/180

    CRossIndustryStandardProcess-DM

    BusinessUnderstanding:Understandingprojectobjecvesanddata

    miningproblemidenficaon

    DataUnderstanding:Capturing,understand,exploreyourdatafor

    qualityissues

    DataPrepara2on:Datacleaning,mergedata,deriveaributesetc.

    Modeling:Selectthedataminingtechniques,buildthemodel

    Evalua2on:Evaluatetheresultsandapprovedmodels

    Deployment:Putmodelsintopracce,monitoringandmaintenance

    5

  • 7/30/2019 S2.DataStructuresandSQL

    6/180

    DataStructures

    Adatastructureisaschemefor

    organizingdatainthememory

    ofacomputer.

    Someofthemorecommonly

    useddatastructuresinclude

    lists,arrays,stacks,queues,

    heaps,trees,andgraphs.Binary Tree

  • 7/30/2019 S2.DataStructuresandSQL

    7/180

    DataStructures

    Thewayinwhichthedatais

    organizedaffectsthe

    performanceofaprogramfor

    differenttasks.

    Computerprogrammersdecide

    whichdatastructurestouse

    basedonthenatureofthedataandtheprocessesthatneedto

    beperformedonthatdata.Binary Tree

  • 7/30/2019 S2.DataStructuresandSQL

    8/180

    Example:AQueue

    Aqueueisanexampleofcommonlyusedsimpledata

    structure.Aqueuehasbeginningandend,calledthefrontand

    backofthequeue.

    Dataentersthequeueatoneendandleavesattheother.

    Becauseofthis,dataexitsthequeueinthesameorderin

    whichitentersthequeue,likepeopleinacheckoutlineata

    supermarket.

  • 7/30/2019 S2.DataStructuresandSQL

    9/180

    Example:ABinaryTree

    Abinarytreeisanother

    commonlyuseddatastructure.

    Itisorganizedlikeanupside

    downtree.

    Eachspotonthetree,calleda

    node,holdsanitemofdata

    alongwithaleYpointerandarightpointer.

    Binary Tree

  • 7/30/2019 S2.DataStructuresandSQL

    10/180

    Example:ABinaryTree

    Thepointersarelinedupso

    thatthestructureformsthe

    upsidedowntree,witha

    singlenodeatthetop,calledtherootnode,andbranches

    increasingontheleYand

    rightasyougodownthe

    tree.Binary Tree

  • 7/30/2019 S2.DataStructuresandSQL

    11/180

    ChoosingDataStructures

    Bycomparingthequeuewiththe

    binarytree,youcanseehowthe

    structureofthedataaffectswhat

    canbedoneefficientlywiththedata.

  • 7/30/2019 S2.DataStructuresandSQL

    12/180

    ChoosingDataStructures

    Aqueueisagooddatastructureto

    useforstoringthingsthatneedtobe

    keptinorder,suchasasetof

    documentswaingtobeprintedon

    anetworkprinter.

    .

  • 7/30/2019 S2.DataStructuresandSQL

    13/180

    ChoosingDataStructures

    Thejobswillbeprintedintheorder

    inwhichtheyarereceived.

    Mostnetworkprintserversmaintain

    suchaprintqueue.

    .

  • 7/30/2019 S2.DataStructuresandSQL

    14/180

    ChoosingDataStructures

    Abinarytreeisagooddatastructure

    touseforsearchingsorteddata.

    Themiddleitemfromthelistis

    storedintherootnode,withlesser

    itemstotheleYandgreateritemsto

    theright.

  • 7/30/2019 S2.DataStructuresandSQL

    15/180

    ChoosingDataStructures

    Asearchbeginsattheroot.The

    computereitherfindthedata,or

    movesleYorright,dependingonthe

    valueforwhichyouaresearching.

    Eachmovedownthetreecutsthe

    remainingdatainhalf.

  • 7/30/2019 S2.DataStructuresandSQL

    16/180

    ChoosingDataStructures

    Itemscanbelocatedveryquicklyinatree.

    Telephonedirectoryassistance

    informaonisstoredinatree,so

    thatanameandphonenumbercan

    befoundquickly.

  • 7/30/2019 S2.DataStructuresandSQL

    17/180

    Lecture Outline

    1

    See the Access Database File (Lab1_Database_BBB.accdb) for this lectureon the course website !

    Relational Databases Tables Columns Rows

    Queries Query Design View Structured Query Language (SQL View)

    Selecting rows meeting a condition Algebraic operations Text matching (with LIKE) Computing summary figures Joining multiple tables

    Inserting, updating, and deleting data

  • 7/30/2019 S2.DataStructuresandSQL

    18/180

    Databases: Overview

    Overview what databases are, and what a databasemanagement system is.

    Present key concepts and vocabulary. Demonstrate Microsoft Access and the BBB database.

    BBB: Bookbinders Book Club We are using a reduced version, of a real database, for class

    discussion

    Full version exists

    18

  • 7/30/2019 S2.DataStructuresandSQL

    19/180

    Rationale

    Why do we have databases? Because data are valuable and essential for conduct of business Because data are created in huge volumes, and need to be

    retained (on disk) and efficiently accessed

    Why do we have DBMSs? To manage the huge amounts of data and to facilitate use of these

    data

    Our focus will be on extracting data from databases inuseful ways. a.k.a. querying.

    19

  • 7/30/2019 S2.DataStructuresandSQL

    20/180

    RelaonalDatabases

    20

    Last Name SS# DOB Major

    Smith 100201122 06/11/84 IS

    Kim 200202222 1/1/85 FIN

    Davis 300201232 12/31/81 MKT

    Pat 999132212 3/3/88 ACC

    Student

    Relational databases

    Databases that use a series of logically related two-dimensionaltables to store their information

    Tables are comprised of fields/records, which in turn contain fieldvalues

    TableField

    Record

    Field value

  • 7/30/2019 S2.DataStructuresandSQL

    21/180

    RelaonalDatabases

    21

    Relational DatabaseTables

    Records Fields

    Field values

    Bytes, bits

    Last Name SS# DOB Major

    Smith 100201122 06/11/84 IS

    Kim 200202222 1/1/85 FIN

    Davis 300201232 12/31/81 MKT

    Pat 999132212 3/3/88 ACC

    StudentTable

    Field

    Record

    Field value

  • 7/30/2019 S2.DataStructuresandSQL

    22/180

    Whydoweneedrelaonaldatabases?

    22

    Problems arise when data is stored in one big file

    Order# Date Customer ID Last Name First Name Address ISBN Book Name Author Price

    1 9/1/03 C1001 Bezos Jeff 1 Amazon Plaza #0465039138 Code and other laws of cyberspace Lessig, Lawrence $25.00

    2 9/2/03 C1004 Sproull LeeDean's Office, SternSchool, New York #1573928895

    Digital Copyright: ProtectingIntel lectual Property on the Internet Litman, Jessica $55.00

    3 9/3/03 C1002 Student Pat Tisch LC-12, New York #0072952849 MIS in the Information Age Haag, Stephen $98.75

    4 9/4/03 C1003 Gates BillMicrosoft Corporation,Redmond #0738206679

    Linked: The New Science ofNetworks

    Barabasi, Albert-Laszlo $34.95

    5 9/5/03 C1003 Gates BillMicrosoft Corporation,Redmond #0738206083

    Smart Mobs: The Next SocialRevolution Rheingold, Howard $29.95

    6 9/6/03 C1001 Bezos Jeff 1 Amazon Plaza #0738206083Smart Mobs: The Next SocialRevolution Rheingold, Howard $29.95

    7 9/7/03 C1002 Student Pat Tisch LC-12, New York #1573928895Digital Copyright: ProtectingIntel lectual Property on the Internet Litman, Jessica $55.00

    8 9/8/03 C1001 Bezos Jeff 1 Amazon Plaza #0738206083Smart Mobs: The Next SocialRevolution Rheingold, Howard $29.95

  • 7/30/2019 S2.DataStructuresandSQL

    23/180

    Anomaliesinun-normalizeddata

    23

    Insertion anomalies

    Inability to insert a piece of information about an object thatcan exist independently of another object without having to

    insert a piece of information about the other object

    Example: Adding a new customer/book before it is orderedDeletion anomalies

    The loss of a piece of information about one object when apiece of information about a different object is deleted

    Example: Deleting an order => deleting a customer/bookUpdate anomalies

    A need to change the same piece of information about an objectmultiple times

    Example: Changing Bill Gates address

  • 7/30/2019 S2.DataStructuresandSQL

    24/180

    AnormalizedversionofAmazonsdata

    24

    Normalization

    The process of assuring that a database can be implementedeffectively as a set of two-dimensional tables

    Prevents insertion, deletion and update anomalies

  • 7/30/2019 S2.DataStructuresandSQL

    25/180

    Benefits of Normalization

    Greateroveralldatabaseorganizaon Minimizedataredundancies Dataconsistencywithinthedatabase Amoreflexibledatabasedesign Datacanbeusedmoreproducvely Abeerhandleondatabasesecurity

    Disadvantage of Normalization

    Reduced database performance because databasemust locate requested tables and join data - requiresadditional processing logic

    A lot of Planning goes in to the design of a database

  • 7/30/2019 S2.DataStructuresandSQL

    26/180

    Connecngtablestogether

    26

    Primary keys

    A field (or group of fields in some cases) that uniquely describeseach record in a table

    Examples: Customer ID, ISBN, Order#Foreign keys

    A field that is a primary key in one table and appears in adifferent table (though not as the primary key)

    Examples: Customer ID in Orders

    Integrity constraints

    rules (most built in) that help ensure the quality of theinformation.

    Not NULL

  • 7/30/2019 S2.DataStructuresandSQL

    27/180

    Concepts & Terminology

    Database: an organized collection of information about entities Sales, business transactions, personnel, inventory, products,

    Entities have attributes. e.g. Person entity has attributes: Last Name, First Name, IDnumber,

    Database management system (DBMS): a computerized record-keeping system.

    DBMS: software for managing (creating, modifying, viewing, retrievinginformation, etc.) from databases.

    Note: We focus on Relational DBMSs (RDBMs), the kind mostwidely used in business

    27

  • 7/30/2019 S2.DataStructuresandSQL

    28/180

    Let s Look at Microsoft Access

    Microsoft Access: A RDBMS, part of the Microsoft OfficeProfessional suite, bundled with Word, Excel,

    PowerPoint,

    NOT an industrial strength DBMS Very useful for small applications, data analysis,

    decision making

    Works well with Excel; easy to use

    28

  • 7/30/2019 S2.DataStructuresandSQL

    29/180

    ACCDB files

    Access 2007/2010 files end in .accdb Previous versions ended in .mdb One database = one .accdb file Access handles one database file at a time. Each database

    consists of a number of tables.

    In other databases, you might find one file per table, rather thanjust one file per database.

    You will notice a temporary .laccdb file created whenever youopen your Access database: this is a lock file that prevents

    problems when there is concurrent access by multiple users.

    29

  • 7/30/2019 S2.DataStructuresandSQL

    30/180

    Industrial Strength Databases

    Different brands of RDBMSs: Commercial providers: Oracle, Microsoft SQL

    Server, DB2, Sybase, Informix,Open Source (free): MySQL, PostgreSql,

    All share a common view of things: tablesAll may be interacted with using SQL Knowledge of one directly transfers to all

    30

  • 7/30/2019 S2.DataStructuresandSQL

    31/180

    Launch Microsoft AccessOpen Lab1_Database_BBB.accdb

    The database window: your portal into the database

    31

    There are 6 tables in this database.

    Double-click a table to open it.

  • 7/30/2019 S2.DataStructuresandSQL

    32/180

    Relationships between tables

    To explore the relationships between tables

    Go to Database Tools tab Click on Relationships You will see an overview of tables and their

    interconnections (see next slide)

    32

  • 7/30/2019 S2.DataStructuresandSQL

    33/180

    Relationships between Tables

    Primary key forProduct table is ProdNum 33

  • 7/30/2019 S2.DataStructuresandSQL

    34/180

    Concepts & TerminologyTables and Relationships

    In an RDBMS, all records (all data) are kept in tables(a.k.a. relations).

    A relation = A tableA relationship = A cross-reference between tables

    The database consists of tables, each table having manyrows (a.k.a. records or tuples).

    The 'Relationships View' of the database shows thetables and their attributes (columns), and the cross-

    references between tables.

    34

  • 7/30/2019 S2.DataStructuresandSQL

    35/180

    Structure vs Contentof a Database Table

    To view or edit the structure of a table: Right-clickit, then choose Design VieworTable Design

    To view or edit the contents of a table: Double-clickit, or Right-clickit, then choose Open

    35

  • 7/30/2019 S2.DataStructuresandSQL

    36/180

    Structure of a TableDesign View of Customers Table (ACCTNUM is Key)

    Primary key forCustomer table isAcctNum

    36

  • 7/30/2019 S2.DataStructuresandSQL

    37/180

    Concepts & TerminologyDesign View of a Single Table (e.g. Customers)

    Every table is made-up of attributes

    Every attribute has a data type Every table has a primary key: one or more attributes

    whose values uniquely identify each table record.

    Primary keys are shown with a key icon in Design View

    37

  • 7/30/2019 S2.DataStructuresandSQL

    38/180

    Contentsof a Database Table

    If in Design View, and you want to view or edit thecontents of the table:

    Choose Datasheet Viewfrom the Design tab

    38

  • 7/30/2019 S2.DataStructuresandSQL

    39/180

    Data in the TableA Portion of the Customer Table

    Records (rows) Attributes (columns) Fields (row elements or "cells"). 39

  • 7/30/2019 S2.DataStructuresandSQL

    40/180

    Design View of Purchase

    A two-attribute primary key: ACCTNUM and CAMPNUM Details of primary key assignment and database design are

    beyond the scope of this course.

    Typically, though, it is best to create a single, system generated(auto-incremented) column, that uniquely identifies each row.

    40

  • 7/30/2019 S2.DataStructuresandSQL

    41/180

    Indexesto improve performance

    To improve the performance of yourdatabase queries, you should create

    indexes on any columns that are

    regularly looked up.

    Creating an index is easy: simplyindicate Indexed asYes in the settings

    for the column in Design view.

    41

  • 7/30/2019 S2.DataStructuresandSQL

    42/180

    Key Points

    Tables: hold all data in a database Rows = records Columns = attributes Attributes: have a data type (number, text, date, etc.) Fields: at the intersection of a row and a column Primary Keys: attributes whose values uniquely refer to any

    given row in a table

    42

  • 7/30/2019 S2.DataStructuresandSQL

    43/180

    Single-Table Queries: Overview

    Querying single tables: Intuitions Microsoft Access Query Design View SQL Microsoft SQL-details

    Database design intuitions(Why split one table into many small ones)

    43

  • 7/30/2019 S2.DataStructuresandSQL

    44/180

    Querying single tables

    Table structureAttributes (columns)

    Records (rows)

    Column order and row order do not matter Queries subdividing a table

    Pick the table you want to look at Pick the column(s) you wish to see

    Pick the row(s) you wish to see

    44

  • 7/30/2019 S2.DataStructuresandSQL

    45/180

    Querying single tables

    BBB Customers:

    What data do we have about customers Name (first, middle, last)Address (street num, street, city, state, zip) Demographics (gender, money non-book purchases)

    What do we want to know

    45

  • 7/30/2019 S2.DataStructuresandSQL

    46/180

    Querying single tables: Intuitions

    Names of all male customers

    ACCTNUM FIRSTNAME

    MIDDLEN

    AME FAMILYNAME

    STREET

    NUMBER STREET CITY STATE ZIP GENDER MONEY

    13015 DENNIS N SKRYPEK 1260 34th AV PRT WASHINGTN NY 11050 M 164

    13016 LOIS T ENGLANDER 105 9th ST WILLINGBORO NJ 8046 F 254

    13017 JANET SIECK 767 3rd ST FOXBORO MA 2035 F 169

    13018 ANN A MARUZZI 1948 10th ST TEWKSBURY MA 1876 F 65

    13971 SAMUEL E HAYES 382 7th ST CENTERVILLE PA 16404 M 81

    14200 EARL J TUCHMAN 1707 29th ST LAKEWOOD NJ 8701 M 135

    17640 AUDREY S KAUFFMANN 1002 RICHMOND HILL AV WEST ORANGE NJ 7052 F 236

    17641 DOROTHY VACCHER 1751 17th AV BROOKLYN NY 11234 M 68

    17642 SANDRA M POSNOCK 1016 MOON AV RIDGWAY PA 15853 F 180

    17643 GERALD C CANNON 1841 2nd AV LEXINGTN MD 20653 M 159

    17644 BETTY K DEL 1794 MARIA ST MULLICA NJ 8062 F 112

    17645 LYNN HUEBBERS 1357 14th AV ANDOVER MA 1810 F 41

    17646 MARY C TALLUTO 1861 2nd ST NEPTUNE NJ 7753 F 102

    19630 CATHERINE R CASANO 965 7th ST SCRANTON PA 18503 F 209

    19631 EDITH E WISCHHUSEN 1520 1st ST PITTSBURGH PA 15243 F 89

    19632 KIMBERLY A SCHOENBERG 1081 18th ST KINGSVILLE MD 21087 F 262

    19633 MARILYN FLAHERTY 33 24th AV WARREN NJ 7059 F 17019634 JOSEPH L FAIELLA 1322 4th AV CLEVERDALE NY 12820 M 200

    19635 ROBERT A SANDLER 316 18th ST PHILADELPHIA PA 19148 M 93

    23088 JAMES M MILLER 409 11th AV SPRING NY 10977 M 63

    23089 PATRICIA WERNER 1535 MARIS ST LONG BEACH NY 11561 F 137

    23090 MARY J MILLER 379 5th ST FRAMINGHAM MA 1701 F 50

    23360 CATHERINE J IMPERATORE 1671 24th ST BROOKLYN NY 11226 F 187

    23361 HARRIET A FAILLA 140 JAMES ST REHOBOTH DE 19971 F 190

    46

  • 7/30/2019 S2.DataStructuresandSQL

    47/180

    Querying single tables: IntuitionsNames of all male customers

    FIRSTNAME

    MIDDLE

    NAME FAMILYNAME

    DENNIS N SKRYPEKSAMUEL E HAYES

    EARL J TUCHMAN

    DOROTHY VACCHER

    GERALD C CANNON

    JOSEPH L FAIELLA

    ROBERT A SANDLER

    JAMES M MILLER

    !

    47

  • 7/30/2019 S2.DataStructuresandSQL

    48/180

    Querying single tables: Intuitions

    Query variations on single tables:1. Pick attributes (columns)2. Pick records (rows) specifying a single condition

    (e.g. Pick all rows where Gender = 'M')

    3.

    Pick rows using two or more conditions4. Selecting rows with attributes you do not display5. Using string matches to pick rows6. Renaming an attribute (column) name7. Arithmetic operations in a query8. Sorting the output (ordering the rows)9. Mathematical aggregations in a query

    48

  • 7/30/2019 S2.DataStructuresandSQL

    49/180

    Querying single tables:Microsoft Access Query Design View

    Go to the Create tab Choose eitherQuery WizardorQuery Design Pick one table (for querying single tables) Pick the columns you want to retrieve, and specify the criteria for the

    rows you want to retrieve. Once in Query Design view, choose Design tab, then Run (! button on

    the toolbar) OR Datasheet Viewto see your query results.

    49

  • 7/30/2019 S2.DataStructuresandSQL

    50/180

    Query Wizard

    50

  • 7/30/2019 S2.DataStructuresandSQL

    51/180

    Query Wizard

    51

    Press > button to select columns one at a time.Press >> button to select all columns.

    Specify which table your data is in.

  • 7/30/2019 S2.DataStructuresandSQL

    52/180

    Query Wizard

    52

    Choose Detailto retrieve individual rows that meet certain criteria.Choose Summaryto compute aggregates (e.g. min, max, average, ..)

  • 7/30/2019 S2.DataStructuresandSQL

    53/180

    Query Wizard

    53

    Name your query, so that you can save it and view / modify it later

  • 7/30/2019 S2.DataStructuresandSQL

    54/180

    Query Design View

    54

    Select the Table you want to query, then pressAddthe button.

    Press Done button when finished.

  • 7/30/2019 S2.DataStructuresandSQL

    55/180

    Query Design ViewSelecting Columns

    55

    Double-click the columns you want to include in your query,or drag-and-drop them from the table into the grid view at the bottom

    Double-click * to include them all

  • 7/30/2019 S2.DataStructuresandSQL

    56/180

    Query Design ViewSelecting Columns

    56

    The grid view at the bottom shows you whether the column is visible (tickShow row). It also lets you specify filter conditions, using the Criteria row.

  • 7/30/2019 S2.DataStructuresandSQL

    57/180

    Query Design ViewSelecting Columns

    57

    View the query results

    Choose whether you want to: View data (SELECT) Insert a table (MAKE) Insert a row (APPEND) Edit a row (UPDATE) Produce aggregate reports (CROSSTAB) Remove rows of data (DELETE)

    Show tables, so you can add morecolumns to your query results.

  • 7/30/2019 S2.DataStructuresandSQL

    58/180

    Viewing Saved Queries

    Click on the down arrow to the right of the Tables heading in thedatabase view Then choose Queries

    58

  • 7/30/2019 S2.DataStructuresandSQL

    59/180

    Query Design ViewQuery 1: Picking attributes (columns)

    Drag and drop

    59

  • 7/30/2019 S2.DataStructuresandSQL

    60/180

    Query Design ViewViewing the query results

    60

  • 7/30/2019 S2.DataStructuresandSQL

    61/180

    Query Design ViewSaving the query

    61

    Right-click in the title bar for the QueryThen choose Save from the pop-up menuChoose an intuitve and descriptive name (e.g. Names of our customers )

  • 7/30/2019 S2.DataStructuresandSQL

    62/180

    Viewing Saved Queries

    After you save the SELECT query, it will

    look like this in the query list on the left

    62

  • 7/30/2019 S2.DataStructuresandSQL

    63/180

    Query Results ViewSwitching back to the query design

    63

  • 7/30/2019 S2.DataStructuresandSQL

    64/180

    Query Design ViewMaking a copy of a query

    64

    Right-click in the Query list on the left Choose Copy Right-click again, and choose Paste Choose an intuitive name for your new query

  • 7/30/2019 S2.DataStructuresandSQL

    65/180

    Query Design ViewQuery 2: Picking records (rows)

    65

    Drag and drop your criterion into the grid Specify the filter condition (e.g. = PA )

  • 7/30/2019 S2.DataStructuresandSQL

    66/180

    Query Design ViewQuery 2: Picking records (rows)

    66

    Now press Run or press DataSheet View to view your results Notice how the results only include customers who live in Pennsylvania (PA)

  • 7/30/2019 S2.DataStructuresandSQL

    67/180

    Query Design ViewQuery 3.1: Picking rows with two conditions - AND

    67

  • 7/30/2019 S2.DataStructuresandSQL

    68/180

    Query Design ViewQuery 3.1: Picking rows with two conditions - AND

    Here there are no records satisfying both conditions.

    68

  • 7/30/2019 S2.DataStructuresandSQL

    69/180

    Query Design ViewQuery 3.2: Picking rows with two conditions - AND

    69

  • 7/30/2019 S2.DataStructuresandSQL

    70/180

    Query Design ViewQuery 3.2: Picking rows with two conditions - AND

    70

    The ACCTNUM column is numeric,so specifying a string condition causes a data type mismatch error..

  • 7/30/2019 S2.DataStructuresandSQL

    71/180

    Query Design ViewQuery 3.3: Picking rows with two conditions - OR

    71

  • 7/30/2019 S2.DataStructuresandSQL

    72/180

    Query Design ViewQuery 3.3: Picking rows with two conditions - OR

    72

  • 7/30/2019 S2.DataStructuresandSQL

    73/180

    Query Design ViewQuery 4: Picking rows using attributes you do not display

    73

    Uncheck the Showbox if you don t want to show the column in your results

  • 7/30/2019 S2.DataStructuresandSQL

    74/180

    Query Design ViewQuery 4: Picking rows using attributes you do not display

    74

    Notice that the customers Last Name is not shown, because we unchecked theShowbox for Last Name in the Query Design View in the previous slide.

    Q D i Vi

  • 7/30/2019 S2.DataStructuresandSQL

    75/180

    Query Design ViewQuery 5: Using string matching to pick rows

    75

    You must use the keyword LIKE if you use a wildcard (e.g. * or ?).If you forget LIKE the database will look for an actual * or ?

    and you won t get the matches you wanted !!

    LIKE means match wildcards The wildcard * means match

    any number of characters

    So LIKE *IL* means find anystrings that have IL anywhere

    inside them. That is, anythingbefore IL and anything after.

    You can also use ? to match asingle character

    In most other databases: use % instead of * use _ instead of ?

  • 7/30/2019 S2.DataStructuresandSQL

    76/180

    Notice here we have all customerswhose familyname has 'IL' in the middle of the string

    Query Design ViewQuery 5: Using string matching to pick rows

    76

  • 7/30/2019 S2.DataStructuresandSQL

    77/180

    Query Design ViewQuery 6: Renaming an attribute (column) name

    77

    [New column name] : [Old column name]

  • 7/30/2019 S2.DataStructuresandSQL

    78/180

    New column name appears in results,in place of old column name.

    Query Design ViewQuery 6: Renaming an attribute (column) name

    78

  • 7/30/2019 S2.DataStructuresandSQL

    79/180

    Query Design ViewQuery 7: Arithmetic operations in a query

    79

    Simply type the formula in the Field row.No loops or VBA code are necessary.

  • 7/30/2019 S2.DataStructuresandSQL

    80/180

    Query Design ViewQuery 7: Arithmetic operations in a query

    80

    SQL is called a declarative language, because we tell thedatabase what we need, rather than how to get it, and the

    database gets the result in the most efficient way possible.There is no need to write loops.

    In contrast, VBA is a procedural language: we have to writeloops to tell VBA exactly how to get the data we want.

    Procedural languages are far more verbose, and require

    more code to get the same job done.

  • 7/30/2019 S2.DataStructuresandSQL

    81/180

    Query Design ViewQuery 8: Sorting the output (ordering the rows)

    81

  • 7/30/2019 S2.DataStructuresandSQL

    82/180

    Query Design ViewQuery 8: Sorting the output (ordering the rows)

    82

    Notice our results are now sorted by Tax,from highest to lowest

    Query Design View

  • 7/30/2019 S2.DataStructuresandSQL

    83/180

    Query Design ViewQuery 9.1: Mathematical aggregations

    Average for all customers

    83

    Other aggregate functions:Min, Max, Count,

    See the Query Design Wizard.

    Q D i Vi

  • 7/30/2019 S2.DataStructuresandSQL

    84/180

    Query Design ViewQuery 9.1: Mathematical aggregations

    Average for all customers

    84

    We can see the average Moneyfor all customers in the data set

    Q D i Vi

  • 7/30/2019 S2.DataStructuresandSQL

    85/180

    Query Design ViewQuery 9.2: Mathematical aggregations

    Average for a subset of customers

    85

    Aggregatefunction returns

    one row.

    Non-Aggregatecriterion returns

    multiple rows.

    Q D i Vi

  • 7/30/2019 S2.DataStructuresandSQL

    86/180

    Query Design ViewQuery 9.2: Mathematical aggregations

    Average for a subset of customers

    86

    Aggregatefunction returns

    one row.

    Non-Aggregatecriterion returns

    multiple rows.

    Q D i Vi

  • 7/30/2019 S2.DataStructuresandSQL

    87/180

    Query Design ViewQuery 9.2: Mathematical aggregations

    Average for a subset of customers

    87

    Solution: Donot show the

    non-aggregatefilter condition

    column. Thatwill fix the error.

    Query Design View

  • 7/30/2019 S2.DataStructuresandSQL

    88/180

    Query Design ViewQuery 9.2: Mathematical aggregations

    Average for a subset of customers

    88

    We can see the average Moneyfor all customers in Pennsylvania

    Query Design View

  • 7/30/2019 S2.DataStructuresandSQL

    89/180

    Query Design ViewQuery 9.3: Mathematical aggregations

    Average for each group of customers

    89

    Groupcustomers by

    state.

    Sort by averagemoney spent for

    the state.

    Query Design View

  • 7/30/2019 S2.DataStructuresandSQL

    90/180

    Query Design ViewQuery 9.3: Mathematical aggregations

    Average for each group of customers

    90

    We can see the average money spent forcustomers in each state,

    and we ve sorted by average money so we caneasily see which are most lucrative states are.

    Query Design View

  • 7/30/2019 S2.DataStructuresandSQL

    91/180

    Query Design ViewBuilding Complex Expressions

    91

    Right-click in the Field row.Then choose Build to build a more complex expression.

  • 7/30/2019 S2.DataStructuresandSQL

    92/180

    SQLGeneral query framework

    SQL is the language underlying Microsoft Access, and

    is the query language standard used by all major RDBMS

    SELECT in the order you want to see them

    FROM

    [WHERE]

    92

  • 7/30/2019 S2.DataStructuresandSQL

    93/180

    Editing SQL

    Just like "Editing a recorded VBA Macro:

    Write a query using Query Design View and then edit using SQL Viewor

    Open the SQL View from a new query and write in SQL from scratch

    93

    SQL View

  • 7/30/2019 S2.DataStructuresandSQL

    94/180

    SQL ViewViewing the SQL underlying

    all your graphical query designs

    94

    For any query, you can view theSQL that Microsoft Access wrote

    for you, by simply choosing Viewand then SQL View.

    Editing the SQL directly is quicker

    and more powerful, so expert

    users tend to use SQL view,whereas novices tend to use the

    Graphical Design view.

  • 7/30/2019 S2.DataStructuresandSQL

    95/180

    SQLQuery 1: Picking attributes (columns)

    SELECT ACCTNUM, FAMILYNAME, STATE

    FROMCUSTOMER

    SELECT STATE, ACCTNUM, FAMILYNAMEFROMCUSTOMER

    Attribute order does not matter

    95

  • 7/30/2019 S2.DataStructuresandSQL

    96/180

    SQLQuery 2: Picking records (rows)

    SELECT ACCTNUM, FAMILYNAME, STATE

    FROMCUSTOMER

    WHERE STATE = 'PA'

    Order of rows in output does not matter, and is not necessarily predictable

    ACCTNUM FAMILYNAME STATE17642POSNOCK PA19630CASANO PA19631WISCHHUSEN PA19635SANDLER PA50670KEYSER PA

    ACCTNUM FAMILYNAME STATE19630CASANO PA50670KEYSER PA17642POSNOCK PA19635SANDLER PA19631WISCHHUSEN PA

    96

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    97/180

    SQLQuery 3.1: Picking rows with two conditions - AND

    SELECT ACCTNUM, FAMILYNAME, STATE

    FROMCUSTOMER

    WHERE STATE = 'PA'

    AND FAMILYNAME = 'MILLER'

    SELECT acctnum, familyname, state

    FROMcustomer

    WHERE state = 'PA'

    and familyname = 'Miller'

    SELECT acctnum, familyname, state

    FROMcustomer

    WHERE state = 'PA'

    AND familyname = 'MILLER'

    SQL is case sensitive within quotes Microsoft Access is NOT

    97

  • 7/30/2019 S2.DataStructuresandSQL

    98/180

    SQLQuery 3.2: Picking rows with two conditions - AND

    Remember that all attributes have a datatype

    SELECT ACCTNUM, FAMILYNAME, STATE

    FROMCUSTOMER

    WHERE STATE = 'PA'

    AND ACCTNUM = 'MILLER'

    ACCTNUM is a NUMBER

    MILLER is a STRING

    98

    SQ

  • 7/30/2019 S2.DataStructuresandSQL

    99/180

    SQLQuery 3.3: Picking rows with two conditions - OR

    SELECT ACCTNUM, FAMILYNAME, STATE

    FROMCUSTOMER

    WHERE STATE = 'PA'

    ORFAMILYNAME = 'MILLER'

    SELECT ACCTNUM, FAMILYNAME, STATE

    FROMCUSTOMER

    WHERE STATE = 'PA'

    AND (FAMILYNAME = 'MILLER'ORFAMILYNAME = 'SMITH')

    99

  • 7/30/2019 S2.DataStructuresandSQL

    100/180

    SQLQuery 4: Picking rows using attributes you do not display

    SELECT ACCTNUM, STATE

    FROMCUSTOMER

    WHERE STATE = 'PA'

    ORFAMILYNAME = 'MILLER'

    100

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    101/180

    SQLQuery 5: Using string matching to pick rows*

    SELECT ACCTNUM, FAMILYNAME, STATE

    FROMCUSTOMER

    WHERE STATE = 'PA'

    AND FAMILYNAME LIKE '%IL%'

    SELECT ACCTNUM, FAMILYNAME, STATEFROMCUSTOMER

    WHERE STATE = 'PA'

    AND FAMILYNAME LIKE 'IL%'

    SELECT ACCTNUM, FAMILYNAME, STATEFROMCUSTOMER

    WHERE STATE = 'PA'

    AND FAMILYNAME LIKE '_IL'

    Standard SQL notation shown above Microsoft Access notation differs as shown

    % matches anynumber of

    characters(Use * in Access)

    _matches any onecharacter

    (Use ? in Access)

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    102/180

    SQLQuery 6: Renaming an attribute (column) name

    SELECT ACCTNUM, FAMILYNAMEAS Last_Name, STATE

    FROMCUSTOMER

    WHERE STATE = 'PA'

    AND FAMILYNAME LIKE '*IL*'

    SELECT ACCTNUM, FAMILYNAMEAS [Last Name], STATE

    FROMCUSTOMER

    WHERE STATE = 'PA'

    AND FAMILYNAME LIKE '*IL*'

    Bracket notation to allow white-space is unique to Microsoft

    102

  • 7/30/2019 S2.DataStructuresandSQL

    103/180

    SQLQuery 7: Arithmetic operations in a query

    SELECT ACCTNUM, FAMILYNAMEAS Last_Name,

    MONEY * 0.10 AS Taxed_Expense

    FROMCUSTOMER

    WHERE FAMILYNAME LIKE '*IL*'

    SELECT ACCTNUM, FAMILYNAMEAS Last_Name,

    (MONEY + ACCTNUM)*1.09 AS Random_nonsense

    FROMCUSTOMER

    WHERE FAMILYNAME LIKE '*IL*'

    103

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    104/180

    SQLQuery 8: Sorting the output (ordering the rows)

    SELECT ACCTNUM, FAMILYNAMEAS Last_Name,

    MONEY * 1.09 AS Taxed_Expense

    FROMCUSTOMER

    WHERE FAMILYNAME LIKE '%IL%'

    ORDER BY FAMILYNAMEASC

    SELECT ACCTNUM, FAMILYNAMEAS Last_Name,

    MONEY * 1.09 AS Taxed_Expense

    FROMCUSTOMER

    WHERE FAMILYNAME LIKE '%IL%'

    ORDERBY Last_Name DESC

    104

    You must use the originalcolumn name in your ORDER BY

    clause.

    Use ASC for Ascending order orDESC for Descending order.

    If you use the new columnname, it won t work.

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    105/180

    SQLQuery 9.1: Mathematical aggregations

    Average for all customers

    SELECTAVG(MONEY)AS Average_Expense

    FROMCUSTOMER

    105

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    106/180

    Query 9.2: Mathematical aggregations

    Average for a subset of customers

    SELECTAVG(MONEY)AS Average_Expense, STATE

    FROMCUSTOMER

    WHERE STATE = "PA"

    SELECTAVG(MONEY) AS Average_Expense, STATE

    FROMCUSTOMER

    Be careful when selecting both aggregations and ordinary attributes

    106

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    107/180

    Query 9.2: Mathematical aggregations

    Average for a subset of customers

    SELECT AVG(MONEY)AS Average_Expense, STATE

    FROMCUSTOMER

    WHERE STATE = "PA"

    Be careful when selecting both aggregations and ordinary attributes

    107

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    108/180

    QQuery 9.3: Mathematical aggregations

    Average for each group of customers

    SELECT

    Avg(CUSTOMER.[Money])AS [Average Money],

    CUSTOMER.State

    FROMCUSTOMER

    GROUP BY CUSTOMER.State

    ORDER BY Avg(CUSTOMER.[Money]) DESC;

    108

  • 7/30/2019 S2.DataStructuresandSQL

    109/180

    Microsoft Flavored SQL

    Attribute names in the SELECT clause are prefaced by table name(e.g. CUSTOMER.ACCTNUM)

    Use of the * versus % in matching multiple characters in LIKEexpression

    Use of ? Versus _ in String matching single character in LIKEexpression

    Use of [ ] in renaming to allow white spacese.g. CUSTOMER.FAMILYNAME AS [Last Name]

    Case sensitivity(Microsoft Access is case insensitive e.g. State = "PA" vs. State = "pa")

    109

    WARNING

  • 7/30/2019 S2.DataStructuresandSQL

    110/180

    Case Sensitivity in SQL

    SELECT * FROMCUSTOMER

    WHERE UPPER(STATE) = PA

    110

    Text in Microsoft Access is not case-sensitive.However, many other databases are case-sensitive.

    To avoid problems with case-sensitivity in SQL

    queries, use the UPPER() function, to convert the

    text to uppercase, before using it !

    K P i t

  • 7/30/2019 S2.DataStructuresandSQL

    111/180

    Key Points

    Querying is simple Pick the table Pick the columns Pick the rows (using some simple or complex criteria)

    Create queries just like VBA Macros Begin in Query Design View; Edit in SQL View Write queries in SQL View from scratch

    Nine (9) simple cases were given as examples. Be wary of exceptions (e.g. SELECT AVG(MONEY), STATE)

    111

    Multi-Table Queries:

  • 7/30/2019 S2.DataStructuresandSQL

    112/180

    Multi Table Queries:

    Overview

    Database design intuitions(Why split one table into many small ones)

    Querying two tables Intuitions SQL Microsoft Access Query Design View

    Microsoft SQL-details

    Querying more than two tables112

    Laying out Data in a Single

  • 7/30/2019 S2.DataStructuresandSQL

    113/180

    y g g

    Spreadsheet TableSort by product

    PRODNAME CATNAME PRICE CAMPDATE CHANNAME

    ACCT

    NUM QTY PURCHDATE

    CAR MAINTANANCE DO-IT-YOURSELF 15 12/1/1987 2 MIN TV SPOT ESPN 13971 1 1/21/1988

    14200 1 1/8/1988

    42763 1 1/13/1988

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 13018 1 1/26/1987

    19635 1 1/9/1987

    23361 1 1/22/198725508 1 1/26/1987

    27028 1 1/20/1987

    27259 1 1/22/1987

    34652 1 1/23/1987

    39403 1 1/18/1987

    50670 1 1/26/1987

    58775 1 1/11/1987

    2/1/1987 2 MIN TV SPOT ESPN 17646 1 3/8/1987

    23088 1 3/20/198732698 1 3/18/1987

    44985 1 3/16/1987

    54690 1 3/20/1987

    113

    Laying out Data in a Single

  • 7/30/2019 S2.DataStructuresandSQL

    114/180

    y g g

    Spreadsheet TableSort by channel

    PRODNAME CATNAME PRICE CAMPDATE CHANNAME

    ACCT

    NUM QTY PURCHDATE

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 13018 1 1/26/1987

    CAR MAINTANANCE DO-IT-YOURSELF 15 12/1/1987 2 MIN TV SPOT ESPN 13971 1 1/21/1988

    2/1/1987 2 MIN TV SPOT ESPN 17646 1 3/8/1987

    14200 1 1/8/1988

    42763 1 1/13/1988

    19635 1 1/9/198723361 1 1/22/1987

    25508 1 1/26/1987

    27028 1 1/20/1987

    27259 1 1/22/1987

    34652 1 1/23/1987

    39403 1 1/18/1987

    50670 1 1/26/1987

    58775 1 1/11/1987

    23088 1 3/20/1987

    32698 1 3/18/1987

    44985 1 3/16/1987

    54690 1 3/20/1987

    114

    Laying out Data in a Single

    S

  • 7/30/2019 S2.DataStructuresandSQL

    115/180

    Spreadsheet TableFilling out the table

    PRODNAME CATNAME PRICE CAMPDATE CHANNAME

    ACCT

    NUM QTY PURCHDATE

    CAR MAINTANANCE DO-IT-YOURSELF 15 12/1/1987 2 MIN TV SPOT ESPN 13971 1 1/21/1988

    CAR MAINTANANCE DO-IT-YOURSELF 15 12/1/1987 2 MIN TV SPOT ESPN 14200 1 1/8/1988

    CAR MAINTANANCE DO-IT-YOURSELF 15 12/1/1987 2 MIN TV SPOT ESPN 42763 1 1/13/1988

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 13018 1 1/26/1987

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 19635 1 1/9/1987

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 23361 1 1/22/1987

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 25508 1 1/26/1987

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 27028 1 1/20/1987

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 27259 1 1/22/1987

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 34652 1 1/23/1987

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 39403 1 1/18/1987

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 50670 1 1/26/1987

    SECRETS OF FRENCH COOKING COOK 15 12/1/1986 "ROCK STARS" - MAG 58775 1 1/11/1987

    SECRETS OF FRENCH COOKING COOK 15 2/1/1987 2 MIN TV SPOT ESPN 17646 1 3/8/1987SECRETS OF FRENCH COOKING COOK 15 2/1/1987 2 MIN TV SPOT ESPN 23088 1 3/20/1987

    SECRETS OF FRENCH COOKING COOK 15 2/1/1987 2 MIN TV SPOT ESPN 32698 1 3/18/1987

    SECRETS OF FRENCH COOKING COOK 15 2/1/1987 2 MIN TV SPOT ESPN 44985 1 3/16/1987

    SECRETS OF FRENCH COOKING COOK 15 2/1/1987 2 MIN TV SPOT ESPN 54690 1 3/20/1987

    115

    Database Design

  • 7/30/2019 S2.DataStructuresandSQL

    116/180

    Database DesignSplitting tables

    Products Campaign

    Category

    CAT

    NUM CATNAME

    1 CHILDREN

    2 YOUTH

    3 COOK

    4 DO-IT-YOURSELF

    5 REFERENCE

    6 ART

    7 GEOGRAPHY

    Channels

    CHAN

    NUM CHANNAME

    3 2 MINUTES SPOT - TV - ESPN (SPORT CHANNEL)

    4 2 MINUTES SPOT - TV - MTV (MUSIC CHANNEL)6 ADVERTISING IN LOCAL NEWSPAPERS

    13 "ROCK STARS" - MAGAZINE

    PRODNUM PRODNAME

    CATNUM

    2PINNOCCHIO 121SECRETS OF FRENCH COOKING 3

    31CAR MAINTANANCE 433GARDENING 4

    1ALICE IN WONDERLAND 1

    CAMPNUM

    PRODNUM PRICE CAMPDATE

    CHANNUM

    21 21 15 12/1/1986 13221 21 15 2/1/1987 3201 1 15 2/1/1987 4

    116

    Database Design

  • 7/30/2019 S2.DataStructuresandSQL

    117/180

    Database DesignSplitting tables (cont)

    ACCT

    NUM

    CAMP

    NUM QTY PURCHDATE

    13018 21 1 1/26/1987

    19635 21 1 1/9/1987

    23361 21 1 1/22/1987

    25508 21 1 1/26/1987

    27028 21 1 1/20/1987

    27259 21 1 1/22/1987

    34652 21 1 1/23/1987

    39403 21 1 1/18/1987

    50670 21 1 1/26/1987

    58775 21 1 1/11/1987

    17646 221 1 3/8/1987

    23088 221 1 3/20/198732698 221 1 3/18/1987

    44985 221 1 3/16/1987

    54690 221 1 3/20/1987

    13971 1231 1 1/21/1988

    14200 1231 1 1/8/1988

    42763 1231 1 1/13/1988

    Purchase

    117

    Database Design

  • 7/30/2019 S2.DataStructuresandSQL

    118/180

    Database DesignSplitting tables

    Go to Database Tools table and click Relationships button to view this118

    Database Design

  • 7/30/2019 S2.DataStructuresandSQL

    119/180

    Database DesignSplitting tables

    Avoid repetition within individual tables Separate attributes that are used independently into

    separate tables

    Introduce (foreign) keys to link tables and keep anyredundancy minimal/simple

    119

    Querying two tables

  • 7/30/2019 S2.DataStructuresandSQL

    120/180

    Querying two tables

    Table structure Attributes (columns) Records (rows)

    Column order and row order do not matter

    Queries combine and then subdivide Pick two tables that you want to look at Combine the two tables (Cartesian product) Pick the row(s) you wish to see Pick the column(s) you wish to see

    120

    Querying two tables

  • 7/30/2019 S2.DataStructuresandSQL

    121/180

    Querying two tables

    BBB Customers and Purchases

    What data do we have about customers

    What data do we have about purchases Who purchased (Acctnum) In response to what advertising campaign In what quantity When did they purchase

    What do we want to know e.g. Find responses by customers to campaigns

    121

    Querying two tables

  • 7/30/2019 S2.DataStructuresandSQL

    122/180

    Customer response (purchases) to campaigns

    ACCT

    NUM

    CAMP

    NUM QTY PURCHDATE

    13018 21 1 1/26/1987

    19635 21 1 1/9/198723361 21 1 1/22/1987

    Customer (truncated to fit on the slide)

    Purchase

    ACCT

    NUM FIRSTNAME STATE ZIP GENDER MONEY

    13015 DENNIS NY 11050 M 164

    19635 ROBERT PA 19148 M 93

    23361 HARRIET DE 19971 F 190

    Step 1.Pick two tables

    122

    Querying two tables

  • 7/30/2019 S2.DataStructuresandSQL

    123/180

    Customer response (purchases) to campaigns

    ACCT

    NUM

    CAMP

    NUM QTY PURCHDATE

    13018 21 1 1/26/1987

    19635 21 1 1/9/198723361 21 1 1/22/1987

    Customer (truncated to fit on the slide)

    Purchase

    ACCT

    NUM FIRSTNAME STATE ZIP GENDER MONEY

    13015 DENNIS NY 11050 M 164

    19635 ROBERT PA 19148 M 93

    23361 HARRIET DE 19971 F 190

    Step 2.Combine the two tables(Cartesian Product i.e. every

    row from first table combined, inturn, with every row from second)

    123

    Querying two tables

  • 7/30/2019 S2.DataStructuresandSQL

    124/180

    ACCT

    NUM

    CAMP

    NUM QTY PURCHDATE

    ACCT

    NUM FIRSTNAME STATE ZIP GENDER MONEY

    13018 21 1 1/26/1987 13015 DENNIS NY 11050 M 164

    y gCustomer response (purchases) to campaigns

    ACCT

    NUM

    CAMP

    NUM QTY PURCHDATE

    13018 21 1 1/26/1987

    19635 21 1 1/9/1987

    23361 21 1 1/22/1987

    CustomerPurchase

    ACCT

    NUM FIRSTNAME STATE ZIP GENDER MONEY

    13015 DENNIS NY 11050 M 164

    19635 ROBERT PA 19148 M 93

    23361 HARRIET DE 19971 F 190

    continue for all permutations of

    Purchase and Customer records

    13018 21 1 1/26/1987 19635 ROBERT PA 19148 M 93

    13018 21 1 1/26/1987 23361 HARRIET DE 19971 F 190

    124

    Querying two tables

  • 7/30/2019 S2.DataStructuresandSQL

    125/180

    y gCustomer response (purchases) to campaigns

    ACCT

    NUM

    CAMP

    NUM QTY PURCHDATE

    13018 21 1 1/26/1987

    19635 21 1 1/9/1987

    23361 21 1 1/22/1987

    CustomerPurchaseACCT

    NUM FIRSTNAME STATE ZIP GENDER MONEY

    13015 DENNIS NY 11050 M 164

    19635 ROBERT PA 19148 M 93

    23361 HARRIET DE 19971 F 190

    PURC.

    ACCT

    NUM

    CAMP

    NUM QTY PURCHDATE

    CUST.

    ACCT

    NUM FIRSTNAME STATE ZIP GENDER MONEY

    13018 21 1 1/26/1987 13015 DENNIS NY 11050 M 164

    13018 21 1 1/26/1987 19635 ROBERT PA 19148 M 93

    13018 21 1 1/26/1987 23361 HARRIET DE 19971 F 190

    19635 21 1 1/9/1987 13015 DENNIS NY 11050 M 164

    19635 21 1 1/9/1987 19635 ROBERT PA 19148 M 93

    19635 21 1 1/9/1987 23361 HARRIET DE 19971 F 190

    23361 21 1 1/22/1987 13015 DENNIS NY 11050 M 164

    23361 21 1 1/22/1987 19635 ROBERT PA 19148 M 93

    23361 21 1 1/22/1987 23361 HARRIET DE 19971 F 190

    Step 3.Pick some rows:Purchase.Acctnum = Customer.Acctnum

    125

    Querying two tables

  • 7/30/2019 S2.DataStructuresandSQL

    126/180

    Que y g t o tab esCustomer response (purchases) to campaigns

    PURC.

    ACCT

    NUM

    CAMP

    NUM QTY PURCHDATE

    CUST.

    ACCT

    NUM FIRSTNAME STATE ZIP G ENDER MO NEY

    13018 21 1 1/26/1987 13015 DENNIS NY 11050 M 164

    13018 21 1 1/26/1987 19635 ROBERT PA 19148 M 93

    13018 21 1 1/26/1987 23361 HARRIET DE 19971 F 190

    19635 21 1 1/9/1987 13015 DENNIS NY 11050 M 164

    19635 21 1 1/9/1987 19635 ROBERT PA 19148 M 93

    19635 21 1 1/9/1987 23361 HARRIET DE 19971 F 190

    23361 21 1 1/22/1987 13015 DENNIS NY 11050 M 164

    23361 21 1 1/22/1987 19635 ROBERT PA 19148 M 93

    23361 21 1 1/22/1987 23361 HARRIET DE 19971 F 190

    PURC.

    ACCT

    NUM

    CAMP

    NUM QTY PURCHDATE

    CUST.

    ACCT

    NUM FIRSTNAME STATE ZIP GENDER MONEY

    19635 21 1 1/9/1987 19635 ROBERT PA 19148 M 93

    23361 21 1 1/22/1987 23361 HARRIET DE 19971 F 190

    Step 4.Pick some columns

    126

    Querying two tables: The final result

  • 7/30/2019 S2.DataStructuresandSQL

    127/180

    Querying two tables: The final resultCustomer response (purchases) to campaigns

    CAMP

    NUM QTY

    ACCT

    NUM GENDER

    21 1 19635 M

    21 1 23361 F

    127

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    128/180

    SQLGeneral JOIN framework: two tables

    SELECT

    in the order you want to see them

    prefix attributes with tablename e.g. table1.attr1

    FROM ,

    WHERE

    You must cross-reference (i.e. join) therelated columns in the tables, using

    table1.columnA = table2.columnA

    128

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    129/180

    Customer responses to campaigns (i.e. customer purchases)

    SELECT CAMPNUM, QUANTITY,

    CUSTOMER.ACCTNUM, GENDER

    FROMPURCHASE, CUSTOMERWHERE PURCHASE.ACCTNUM = CUSTOMER.ACCTNUM

    129

    Notice how, unlike VBA, no line-continuation character is used in SQL

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    130/180

    Customer responses to campaigns (i.e. customer purchases)

    130

    If you make a spelling mistake in your query, you ll get an (error)message like the one shown below

    SQL

  • 7/30/2019 S2.DataStructuresandSQL

    131/180

    SQLA nonsense query for example purposes

    SELECT CAMPNUM, PRICE, ACCTNUM

    FROMCAMPAIGN, CUSTOMER

    WHERE PRICE > MONEY

    ORFIRSTNAME LIKE 'ROB%'

    131

    Query Design View

  • 7/30/2019 S2.DataStructuresandSQL

    132/180

    Query Design ViewQuerying two tables

    Two cases using Microsoft Access Query Design View:

    1. If both tables have a key attribute with the same name(e.g. PURCHASE and CUSTOMER both have anattribute called ACCTNUM), then Microsoft Access willautomatically create the join.

    2. In all other circumstances, you must manually specifythe join:

    the tables have attributes with the same name, but one or bothare not designated as key attributes

    the key attributes in different tables do not have the samename

    the tables have no shared attributes whatsoever132

    Query Design ViewFinding all purchases by customers:

  • 7/30/2019 S2.DataStructuresandSQL

    133/180

    Finding all purchases by customers:

    Picking our tables

    133

    PressShift + Click

    to selectmultiple tables,

    and press theAdd button.

    Press Close

    button when

    done.

    Query Design View

  • 7/30/2019 S2.DataStructuresandSQL

    134/180

    Case 1: Key attribute with same name

    134

    Drag and drop your columns into the grid In this case there is no need to explicitly say:

    Customer.AcctNum = Purchase.Acctnumbecause Microsoft Access is intelligent enough to

    infer the relationship between the tables automatically

    Query Design View: Results of QueryC 1 K tt ib t ith

  • 7/30/2019 S2.DataStructuresandSQL

    135/180

    Case 1: Key attribute with same name

    135

    This result isn t very readable

    Campaign number doesn t mean much:Product name (from the Product table)would be more descriptive

    Account number doesn t mean much:Customer name (from the Customer table)

    would be more descriptive

    SQL ViewCase 1: Key attribute with same name

  • 7/30/2019 S2.DataStructuresandSQL

    136/180

    Case 1: Key attribute with same name

    136

    Microsoft Access generates some pretty nasty looking SQL:

    You can actually simplify it to this:

    Query Design ViewCase 2: No shared attributes

  • 7/30/2019 S2.DataStructuresandSQL

    137/180

    Case 2: No shared attributes

    Finding all products purchased by customers

    137

    The Customer and Product tables are notdirectly related, so Microsoft Access is

    unable to infer the relationship.

    Query Design ViewCase 2: No shared attributes

  • 7/30/2019 S2.DataStructuresandSQL

    138/180

    Case 2: No shared attributes

    Finding all products purchased by customers

    138

    To find the relationship betweenCustomers and Products:

    Go to Show Tables, then Select Purchase and Campaign tables,

    which link Customer to Product PressAddbutton Notice the relationships now visible

  • 7/30/2019 S2.DataStructuresandSQL

    139/180

    Querying more than two tables

    Example 1:

    What products did each customerpurchase?

    139

    Querying more than two tables

  • 7/30/2019 S2.DataStructuresandSQL

    140/180

    Querying more than two tablesWhat products did each customerpurchase?

    140

    Querying more than two tables

  • 7/30/2019 S2.DataStructuresandSQL

    141/180

    y gFollowing the Yellow Brick Road

    141

    Querying more than two tables

  • 7/30/2019 S2.DataStructuresandSQL

    142/180

    Querying more than two tablesWhat products did each customerpurchase?

    142

    Querying more than two tables

  • 7/30/2019 S2.DataStructuresandSQL

    143/180

    Querying more than two tablesWhat products did each customer purchase?

    143

    Microsoft Access generates some pretty nasty looking SQL:

    You can actually simplify it to this:

    Querying more than two tables: ResultsWhat products did each customer purchase?

  • 7/30/2019 S2.DataStructuresandSQL

    144/180

    What products did each customer purchase?

    144

    To confirm you have the correct number of results, check that you have thesame number of rows here as there are in the PURCHASES table.

    There were 19 purchases, so there should be 19 results here !

    Querying more than two tables: ResultsWhat happens if you forget the join / cross-reference?

  • 7/30/2019 S2.DataStructuresandSQL

    145/180

    145

    Notice that you get an impossible number of results if you forget the JOIN inthe query: i.e. if you forget to specify which columns cross-reference !

    There were 19 purchases, so there should be 19 results here !

  • 7/30/2019 S2.DataStructuresandSQL

    146/180

    Querying more than two tables

    Example 2:

    What channels was each product advertised on ?

    146

    Querying more than two tables

  • 7/30/2019 S2.DataStructuresandSQL

    147/180

    Q y gWhat channels was each product advertised in ?

    147

    Querying more than two tables

  • 7/30/2019 S2.DataStructuresandSQL

    148/180

    y gWhat channels was each product advertised in ?

    148

    Querying more than two tablesWhat channels was each product advertised in ?

  • 7/30/2019 S2.DataStructuresandSQL

    149/180

    What channels was each product advertised in ?

    149

    Querying more than two tablesWhat channels was each product advertised in ?

  • 7/30/2019 S2.DataStructuresandSQL

    150/180

    What channels was each product advertised in ?

    150

    Querying more than two tablesWhat channels was each product advertised in ?

  • 7/30/2019 S2.DataStructuresandSQL

    151/180

    What channels was each product advertised in ?

    151

    Microsoft Access generates some pretty nasty looking SQL:

    You can actually simplify it to this:

    Querying more than two tablesWhat channels was each product advertised in ?

  • 7/30/2019 S2.DataStructuresandSQL

    152/180

    What channels was each product advertised in ?

    152

    The results of the query :

    To confirm you have the correct number ofresults, check that you have the same

    number of rows here as there are in theCAMPAIGN table. There were 3 campaigns,

    so there should be 3 results here !

    Querying more than two tables

  • 7/30/2019 S2.DataStructuresandSQL

    153/180

    y g

    Option 1: Create one giant table

    Create one giant table (Cartesian Product)A B C D

    Option 2: Combine tables pair-wise

    Associative: (A B) C = A (B C) Combine in stages. For three or more

    A B (A B) C ((A B) C ) D

    153

    Key Points

  • 7/30/2019 S2.DataStructuresandSQL

    154/180

    y

    Divide tables for ease in managing data Query two (or more) tables like one big table

    Combine tables: Cartesian Product Put tables side-by-side: every row permutation Look for attributes that link tables

    (e.g. Acctnum in CUSTOMER and PURCHASES)

    Create queries just like VBA Macros Begin in Query Design View; Edit in SQL View, or Write queries in SQL View from scratch

    154

    Inserting, Updating, and Deleting Data

  • 7/30/2019 S2.DataStructuresandSQL

    155/180

    When you open Access, you must click Options

    on the message bar, and choose Enable this content, if you want

    SQL INSERTs, UPDATES, and DELETES to work!

    155

    Inserting Data

  • 7/30/2019 S2.DataStructuresandSQL

    156/180

    Use the INSERT statement to append new data.

    156

    Here are the various parts of the INSERT statement:

    INSERT INTO [table-name] ( [ column1, column2,

    ] )VALUES ( [value for column1], [value for column2],);

    Inserting Data

  • 7/30/2019 S2.DataStructuresandSQL

    157/180

    Use the INSERT statement to append new data.

    157

    Inserting Data

  • 7/30/2019 S2.DataStructuresandSQL

    158/180

    After you save the INSERT query, it willlook like this in the query list on the left

    158

    Updating Data

  • 7/30/2019 S2.DataStructuresandSQL

    159/180

    Use the UPDATE statement to edit existing data.

    159

    Here are the various parts of the UPDATE statement:

    UPDATE [table-name]

    SET [column1] = [value], [column2] = [value],

    WHERE [column] = [value]

    DON T FORGET YOUR WHERE CLAUSE !! IF YOU FORGET IT YOU COULD

    END UP MISTAKENLY UPDATING ALL DATA IN THE TABLE ! Access 2007will give you a warning if you inadvertently update multiple rows, but, if you

    execute the UPDATE statement from Excel 2007, you ll get no such

    Updating Data

  • 7/30/2019 S2.DataStructuresandSQL

    160/180

    Use the UPDATE statement to edit existing data.

    160

    Updating Data

  • 7/30/2019 S2.DataStructuresandSQL

    161/180

    After you save the UPDATE query, it willlook like this in the query list on the left

    161

    NOTE:It is often bad practice to update data in a database.

    For example, rather than update a customer s address, you can add an

    event table e.g. a table called MOVE with a date the customer moved.That way you always have an audit log of when the customer moved,

    and you have not lost any data.

    You might, for instance, later want to count how many times the customer

    moved in the past 3 years, so you can compute a credit score for them.

    Deleting Data

  • 7/30/2019 S2.DataStructuresandSQL

    162/180

    Here are the various parts of the DELETE statement:

    DELETE FROM [table-name]

    WHERE [column] = [value]

    DON T FORGET YOUR WHERE CLAUSE !! IF YOU FORGET IT YOU COULD

    END UP MISTAKENLY DELETING ALL DATA IN THE TABLE ! Access 2007 willgive you a warning if you inadvertently delete multiple rows, but, if you

    execute the DELETE statement from Excel 2007, you ll get no such warning,

    and it will be impossible to recover the data you have deleted !!

    Use the DELETE statement to permanently remove a row of data.

    162

    Deleting Data

  • 7/30/2019 S2.DataStructuresandSQL

    163/180

    Use the DELETE statement to permanently remove a row of data.

    163

    Deleting Data

  • 7/30/2019 S2.DataStructuresandSQL

    164/180

    After you save the DELETE query, it willlook like this in the query list on the left

    164

    NOTE:It is generally bad practice to delete data from a database.

    Rather, you want to add a column, and mark the data as inactive

    (include a column with the date it was marked inactive and

    a column with the user-ID of the user who marked it as inactive, if necessary).

    That way you always have an audit log of when data was deleted ,

    and you can always recover the data later.

    Obtaining Data via Email

  • 7/30/2019 S2.DataStructuresandSQL

    165/180

    165

    Microsoft Access 2007 contains great new features

    to allow you to collect data via email.You need to have Microsoft Outlook 2007 installed and working.

    Click on External Data tab in Access 2007.

    Then click on Create Email.

    Obtaining Data via Email

    A wizard will help you through the process of composing your

  • 7/30/2019 S2.DataStructuresandSQL

    166/180

    166

    p y g p p g y

    electronic data collection form.

    Obtaining Data via Email

    Recipients will need to have an HTML-capable email client, to be able to

  • 7/30/2019 S2.DataStructuresandSQL

    167/180

    167

    respond to your emails. Luckily, most people have one nowadays.

    Obtaining Data via Email

    Specify what you would like to do with the data.

  • 7/30/2019 S2.DataStructuresandSQL

    168/180

    168

    p y y

    Obtaining Data via EmailSpecify which fields (database columns)

    t t i l d i il

  • 7/30/2019 S2.DataStructuresandSQL

    169/180

    169

    you want to include in your email.

    Obtaining Data via Email

    Specify how you want to hand replies.

  • 7/30/2019 S2.DataStructuresandSQL

    170/180

    170

    Obtaining Data via EmailSet automatic processing options.

    Y h t li t ti ll t ll dd li t

  • 7/30/2019 S2.DataStructuresandSQL

    171/180

    171

    You can choose to process replies automatically, or to manually add replies to

    the database after you have checked each individually in Outlook.

    Obtaining Data via Email

    Select how you want to specify the email addresses of recipients.

  • 7/30/2019 S2.DataStructuresandSQL

    172/180

    172

    Obtaining Data via Email

    If your email addresses are in your database, specify whether the

    dd i th t t bl i li k d t bl

  • 7/30/2019 S2.DataStructuresandSQL

    173/180

    173

    addresses are in the current table, or in a linked table.

    Obtaining Data via Email

    Specify the contents of the email you are sending.

  • 7/30/2019 S2.DataStructuresandSQL

    174/180

    174

    Obtaining Data via Email

    Confirm the email addresses of the message recipients.

  • 7/30/2019 S2.DataStructuresandSQL

    175/180

    175

    Obtaining Data via Email

    After sending your email wait for some responses then click

  • 7/30/2019 S2.DataStructuresandSQL

    176/180

    176

    After sending your email, wait for some responses, then click

    Manage Replies to manage your responses.

    Obtaining Data via Email

    Under Manage Replies, you can choose to resend the email, if it wasnt

    sent successfully the first time

  • 7/30/2019 S2.DataStructuresandSQL

    177/180

    177

    sent successfully the first time.

    Lab Exercise

  • 7/30/2019 S2.DataStructuresandSQL

    178/180

    17

    Our next session will be a

    Databaseslab exercise

    ROOM JMHH 380

  • 7/30/2019 S2.DataStructuresandSQL

    179/180

    17

    CREDITS: Alan Abrahams, VA Tech

    S2:DataStructuresandSQL

  • 7/30/2019 S2.DataStructuresandSQL

    180/180

    ShawndraHillS i 2013


Recommended