Bab3 -9 Create Queries

Embed Size (px)

Citation preview

  • 8/8/2019 Bab3 -9 Create Queries

    1/13

    General Information

    Database normalizationRelationships in Access 2007

    Create queriesOpen a recently used database

    Open a database through Microsoft Office Access buttonClose a database

    Database normalization

    Database normalization is the process to organize the data of a data base. It includes the creation oftables and the establishment of relations among them according to designed rules by eliminating the

    incoherent redundancy and dependencies.

    Redundancy data waste disk space and create maintenance problems. If there is to change data thatexist in more of a place, they are due to change exactly of the same form in all his locations. A change

    in the direction of a client is much easier to implement if the data only store in the table Clients and notin some other place of the data base.

    Microsoft Access 2007 uses the relational model for the database management which is based on thelogic of predicates where the data is represented by mathematical n-ary relations. Each relation isrepresented by the composition of fields stored in tables that represent these n-ary relations.

    To transform a database in a relational database there are several normalization forms to apply:

    First normal form

    1) Eliminate the repeated groups of the individual tables.

    2) Create an independent table for each related data set.

    3) Identify each data set related with a primary key.

    Second normal form

    4) Create independent tables for sets of values that are applied to several records.

    5) Relate these tables to an external key.

    Third Normal form

    6) Eliminate the fields that do not depend on the key.

  • 8/8/2019 Bab3 -9 Create Queries

    2/13

    Record values that are not part of the key of that record do not belong to the table. In general,whenever the content of a group of fields can be applied to more of an only registry of the table, it

    considers placing these fields in an independent table.

    There are two additional normal forms (fourth and fifth) but both are rarely used.

    Relationships in Access 2007

    7) Open TutorialDB.

    8) Click on Database Tools and select Relationships. Remember that the database must benormalized before.

    Illustration 1: Normalize the database and create relationships between tables and data.

    A blank screen will appear. You must add tables.

    9) Right click on the empty screen and select the Show table menu item.

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure2.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure1.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    3/13

    Illustration 2: Normalize the database and create relationships between tables and data.

    10) Add all tables in the database.

    Illustration 3: All tables must be added.

    11) Arrange tables in the diagram. Then click and holds the AreaId field in Areas table and drag it overAreaId fields in Fields tables.

    12) Release the mouse button to create the relationship.

    Illustration 4: Edit relationships properties.

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure4.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure3.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    4/13

    13) Edit Relationship properties and press the Create properties.

    14) Repeat steps 12 and 13 to create all needed relationships.

    Illustration 5: Normalized database.

    Once normalized the data base, it is possible to design queries that take advantage of the relational

    model.

    Create Queries

    Queries are those that truly make the work in a data base. They can make several different functions.

    Its function commonest is to recover specific data of the tables. The data that wish to see usually aredistributed by several tables and, thanks to the queries; you can see them in a single data sheet. Inaddition, since normally you do not wish to see all the records simultaneously, the queries allow you to

    add criteria for "filtering" the data until obtaining the whished records. The queries often serve as originof registries for forms and reports.

    Some queries are "updatable", which means that it is possible to edit the data of the tables throughquerys datasheet. If it works with an updateable query, remember that the changes also take place inthe tables, no only in the querys datasheet. There are two basic types of queries: those ofselectionand those ofaction. Aselection query simply recovers the data and causes that it become available

    for his use. The results of the query can be seen in the screen, be printed or be copied to the paperholder. Or can be used like data source for forms or reports.

    An action query, as its name indicates, makes a task with the data. The action queries can serve to

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure5.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    5/13

    create new tables, to add data to existing tables, to update data or to eliminate data.

    15) Open TutorialDb.

    16) Select the Create tab on Options Tape and select the Query Wizardcommand.

    Illustration 6: Create a query.

    17) Select the Simple Query wizardOption and press OK button.

    Illustration 7: Simple query wizard.

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure7.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure6.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    6/13

    Open a database through Microsoft Office Access button

    18) Select the Files table from Table / Queries list. Then select Filename, Comments andDirectoryfields from theAvailable Fields list.

    Illustration 8: Configure the querys fields.

    19) Press the NEXTbutton.

    20) Assign a name to the query.

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure9.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure8.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    7/13

    Illustration 9: Set the querys name. You can see the data or modify the query design.

    21) Press the FINISHbutton. You will see the data.

    Illustration 10: Querys results.

    22) Now, you will create a more refined query. Select the Create option in the Options Tape, andselect the Query design command.

    Illustration 11: Query design view.

    23) Double click on each table name to add it to the query designer.

    Illustration 12: Query design view.

    Notice that the relations between tables are added at time of there is added to the query designer

    24) Double click on each Field on File table that you want to show. Skip the TypeId,AreaIdand

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure12.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure11.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure10.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    8/13

    ThemeIdfields.

    25) The fields Filename, Comments and Directorywill appear at bottom grid. You can deselect theshow option. This option will hide the field but it will still remain in the query for other purposes (for

    example, you can count the distinct instances of the file name).

    Illustration 13: You added the main query fields.

    26) Now, add the additional fields (those related to the TypeId,AreaIdand ThemeIdkeys) by

    clicking inAreaName fromAreas table, TypeName from FileTypes table and Themename fromThemestable.

    Illustration 14: You added the related tables fields.

    27) Right click on Query1 name and select the SAVEmenu item.

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure15.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure14.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure13.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    9/13

    Illustration 15: Save the query.

    28) Now set a name to the query and save.

    Illustration 16: Set the query name.

    29) Select the Run command to view the querys results.

    Illustration 17: Run the query.

    30) Access 2007 will take each related field from related tables using the relational model data retrievaltheory.

    Illustration 18: Query Results.

    If you try to change any related field (likeAreaName, ThemeName or TypeName) all instances ofthese fields will be changed. For example if you change the value SCM at the ThemeName in the first

    row, all instances of this field will be changed automatically in the query and the base table (Themes)will be changed too.

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure18.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure17.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure16.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    10/13

    Illustration 19: Changing a related field will affect all instances of this field and the base table.

    31) Now you will create a crosstab query. Select the Simple Query wizardOption and press OK

    button.

    32) Select the Crosstab Query wizardOption and press OK button.

    Illustration 20: Crosstab query wizard.

    33) Select the List Files table from Table / Queries list. The Both option must be selected first.

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure20.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure19.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    11/13

    Illustration 21: You can see a design preview.

    34) Press the NEXTbutton.

    35) Select theAreaName as row to summarize and press the NEXTbutton.

    Illustration 22: You can see a design preview.

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure22.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure21.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    12/13

    36) Select the TypeName as column to summarize and press the NEXTbutton.

    Illustration 23: You can see a design preview.

    37) Select the Countas summarize operation and press the NEXTbutton.

    Illustration 24: You can see a design preview.

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure24.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure23.jpg
  • 8/8/2019 Bab3 -9 Create Queries

    13/13

    38) Set the query name to FilesQtyByType and select the viewthe query Options. Press the FINISHbutton.

    Illustration 25: Now, you can see the query results.

    Illustration 26: Query results.

    http://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure26.jpghttp://www.mstipsandtricks.com/tips-and-tricks/ms-office-tips-and-tricks/ms-access/creating-queries/figure25.jpg