You are on page 1of 43

[Please insert any This chapter is two folds.

The earlier
relevant photo section focuses the learning at two key
features. First, the creation of the key of a
around this box]
table, of which is the fundamental
dependent of a relationship. Second, it is
the study of the fundamental concept of
relationships and the approaches for
CHAPTER setting up the relationship between two
tables at a time.
In order to set up the relationships for the

5
entire database, the relationship for
setting upbetween two tables at a time is
needed to be repeatedly used for
sufficient number of times.
The latter section focuses the learning on
the creation and manipulation of query.
With therelationships of tables, query can
be created with fields obtained from more
than one tables.
Finally, the manipulations of query are
the hide/show, delete, sort and setting
criteria to filterfields in a query.

Table Relationship And


Creating Query
Subtopics:-
5.1 Establishing Relationship.........................................1
5.1.1 How to Set Primary Key?......................................2 - 4
5.1.2 Creating Relationship between Tables .............5 - 8
5.1.3 Enforce Referential Integrity................................9
5.2 Creating Queries......................................................10
5.2.1 Creating Query in Design View............................10 - 13
5.2.2 Creating Query by Using Wizard..........................13 - 16
5.3 Opening a Query......................................................16
5.4 Changing a Query....................................................17
5.5 Re-arranging Fields....................................................17 - 18
5.6 Deleting a Field .........................................................18 - 19
5.7 Hiding a Field..............................................................20 - 21
5.8 Selecting all Fields .....................................................22
5.9 Sorting Query Results ................................................22 - 23
5.10 Using the Criteria .....................................................24 - 25
5.11 Renaming a Query..................................................25 - 26

1|P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
5.12 Deleting a Query ....................................................27
5.13 Summary...................................................................28 - 36
5.14 Exercises....................................................................37

5.1 Establishing Relationship

In this topic we will learn on how to create a relationship between tables by using MicrosoftAccess.
Before creating a relation, we have to make sure that all created tables have a primary key oneach table.

5.1.1 How to Set Primary Key?


Primary key is a candidate key selected uniquely to identify the record and it is also named asthe Unique
Entity Identifier. Now we are going to set a primary key for the Author table. Inorder to that, open E-Book
database and open the Author table in design view. Then, followsthe steps described as follow to set the
primary key.
· To set the primary key:
1. Click the field you want to set as the primary key. Primary key for the Author table isAuthorID.

2. Click to set the field as the primary key.

2|P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-1 Set the Primary Key

3. Click on to save your changes.

Figure 5-2 Indexed Properties

4. Set the Indexed property of the field to Yes (No Duplicates). This will allow you to
sort data quickly and search data in the field. No Duplicates mean that you are not
3|P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
allowed to enter the same value in the field more than once.

Figure 5-3 Set more than one field as primary key

You can set more than one field as the primary key.
· To set for more than one primary key:
1. Press and hold down the CTRL key, click on the fields selected for the primary key.
2. Click on to set the selected field as the primary key.
3. A key symbol appears beside each field, indicating that Book_Code and AuthorID arecombined to
become one primary key.
Repeat step 1 to 3 for setting a primary key for the following tables:
· Book,
· Book_Author and
· Publisher.
The primary key of the tables are described in Table 5-1.

5.1.2 Creating Relationship between Tables

4|P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
You can create relationship between tables. Relationships allow you to bring together relatedinformation in
your database.
In a relational database, relationships enable you to prevent redundant data. Referential
integrity relationships help to ensure that information in one table matches information in
another.
Types of Table Relationships
A relationship works by matching data in key columns, usually columns with the same namein both tables.
In most cases, the relationship matches the primary key from one table, which provides a
unique identifier for each row, to an entry in the foreign key of another table.
There are three types of relationships between tables. The types of relationship created
depend on how the related columns were defined.
Below are the three types of relationship in use:
1. One-To-Many (1:M) Relationships
A one-to-many relationship is the most common type of relationship. In this type of
relationship, a row in table 1 can have many matching rows in table 2, but a row in table 2can have only one
matching row in table 1. A one-to-many relationship is created if only
one of the related columns is a primary key or has a unique constraint.
2. Many-To-Many (M:N) Relationships
In a many-to-many relationship, a row in table 1 can have many matching rows in table 2,
and vice versa. You can create such relationship by defining a third table, called a
junction table with the primary key consists of the foreign keys from both table 1 and
table 2.
3. One-To-One (1:1) Relationships
In a one-to-one relationship, a row in table 1 can have no more than one matching row in
table 2, and vice versa. A one-to-one relationship is created if both of the related columns
are primary keys or have unique constraints.
· To view the relationships of tables:

1. Click on to display the Relationships Window.

5|P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-4 Create Relationships

2. The Relationships Window appears. If any relationships exist between the tables in
your database, a box for each table appears in the window.
3. In this case, no tables are displayed for this relationship. (As shown in Figure 5-5.)

Figure 5-5 Show Table dialog box

6|P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
4. Click on Show Table button to add tables to the Relationships Window.
To add table to the relationship:
5. Click on Add button to add the table to the window.
6. Click Close button to close the Show Table dialog.
7. Otherwise, repeat the steps to add more tables to the relationships.

Figure 5-6 Tables in the Relationship

So far, three tables, which are the Book_Author, Book and Publisher are added to
the relationship, as shown in Figure 5-6.
Each table is displayed as a box in the Relationship Window, where each box in the
relationships window displays the field for one table. The primary key for each table
appears in bold. The primary key uniquely identifies each record in the table.
You create a relationship between tables by identifying the matching fields in the tables.
Now we are going to create a relationship between table Book and Book_Author, based
on their same primary used – Book_Code.
· To create a relationship between two tables:
1. Position the mouse over the field you want to use to create a relationship with anothertable. Drag
the field over the other table until a small box appears over the matchingfield. In this case, click on
Book_Code of Book_Author table and drag it to the Booktable.
2. Double click on the relationship line appeared.
3. The Edit Relationships dialog box appears. (As shown in Figure 5-7)
4. The relationship of the selected tables is displayed here.

7|P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-7 Edit Relationship

5. Check on one of these check boxes, that is, Enforce Referential Integrity, CascadeUpdate
Related Fields and Cascade Delete Related Fields.
6. Click on Create button to create this relationship.

Below are the descriptions of the three check-box options:


Enforce Referential Integrity
Referential integrity is a set of rules that prevents you from changing or deleting a
record if matching records exist in a related table.
Cascade Update Related Fields
If you click to select the Cascade Update Related Fields check box when you
define a relationship, any time that you change the primary key of a record in the
primary table, Microsoft Access automatically updates the primary key to the
new value in all related records.
Cascade Delete Related Records
If you select the Cascade Delete Related Records check box when you define a
relationship, any time that you delete records in the primary table, Microsoft
Access automatically deletes related records in the related table.

7. Click on Create button to create the relationship.

8|P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
8. The connections appear.

Figure 5-8 Connection of Relationship

9. Click on to save the relationship.


10. The symbols above the line indicate the cardinality for the relationship. In this
example, each record in the Book table (1) relates to one or more records in the
Book_Author.
11. Repeat step 1 to 7 to create a relationship for the rest of the tables shown in
Relationships.

5.1.3 Enforce Referential Integrity


Referential integrity between tables is enforced by default when you create a relationship inyour database
diagram. An enforced relationship ensures each value entered in a foreign keycolumn matches an existing
value in the related primary key column.

9|P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
5.2 Creating Queries

Queries are used to view, change, and analyze data in different ways. You can also use themas the source of
records for forms, reports, and data access pages. You can create a query tofind information of your interest
from the database.

Figure 5-9 Enquiries


Now we are going to create a query to make a list of books that is written by the author and the book title.
• To create a query (in Design View):
1. Click on Queries in the Database window.
2. Double click on Create query in Design view.
3. The Select Query window and the Show Table dialog box appear, as in
Figure 5-10.

10 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
11 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-10 Create Query in the Design view

4. Click on a table that contains information you want to use in your query.
5. Click on Add to add the table to your query. The selected table appears in the display area.
6. Click on Close button to hide the Show Table dialog box.
The Select Query window will appears as in the figure below.

Figure 5-11 Select Query window

7. If you want to delete the table inserted in your Select Query window, click on the table and then
press the REMOVE TABLE key.
This removes the table from the query, but not from the database.

12 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-12 Select the table to be deleted

8. Double click on a field you want to include in your query.


9. Press the button when you are ready to run the query.
10. The result of the query appears as in Figure 5-13.

Figure 5-13 Result of the Query

11. Click on to save your query so you can run the query again later.
12. The Save dialog box appears.

13 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-14 Save as dialog box

13. Type a name for your query.


14. Click on OK.

15. You are back in query results, so, when you finish viewing the results of your query, click
to close the query.

5.2.2 Creating Query by Using Wizard


• To create query (by using Wizard):
1. Click on Query Wizard in the Database window.

Figure 5-15 Using Query Wizard

2. Click on the Simple Query Wizard from the New Query Dialog Box.

14 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-16 Using Simple Query Wizard

3. Select the table containing the fields you want to include in your query.

Figure 5-17 Selecting fields

3. Double click on each field you want to include or use the  button to select a field at a time or use
the  button to select all fields.
4. Select the table containing the field you want to include in your query.
5.In this case, click on  to select all fields of Show_Query table.

15 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-18 Selected fields

6. Click on Next to go to the next page of Query Wizard.

Figure 5-19 Detail / Summary Query

7. Click on Next to go to the next page of Query Wizard.

16 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-20 Naming Query

8. Type Publisher Query as the title of the query name.


9. Click Finish to complete the query creation.

5.3 Opening a Query

To open a query:
1. Click on Queries in the Database window.
2. Double click on the query you want to open, such as the ShowQuery query.

Figure 5-21 Query

3. The query opens up. You can now review the result of the query.

4. When you finish working with the query, click on to close the query.

17 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
5. A dialog box will appear if you have not saved the changes for the query yet. Click on Yes if you
want to save the changes.

5.4 Changing a Query

You can change a query by reopen the desire query and change the fields of the query.
• To change a query:
1. Double click on the selected query to open it. The Query is displayed in Datasheet View.

2. Click on this symbol to change the view to Design View.

Figure 5-22 Change the query view

2. The query appears in the Design View.

5.5 Re-arranging Fields

You can change the order of fields in a query by rearranging fields in a query. However, it will affect the
order that the fields appear in the query results.
• To rearrange fields:
1. Position the mouse directly above the field you want to move, that is Book_Code.

18 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
2.
3.
4.
5.

Figure 5-23 Rearrange query field

19 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Dragged
field

Figure 5-24 Dragged query field

2. Drag the field to a new location.

5.6 Deleting a Field

You can delete a field that you no longer need from your query.

• To delete the field:


20 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
1. Position the mouse directly above the field that you want to delete.

2. Click and select the field.

3. Press the DELETE key

Figure 5-25 Delete a field

4. The field disappears from your query.

5. Click on to save the changes.

6. Click on this datasheet symbol to display data in Datasheet View.

7. The query displays the result without the field the deleted field now.

21 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-26 New query

5.7 Hiding a Field

You can hide a field used in a query. Hiding a field is useful when you need a field to find
information in your database, but do not want the field to appear in the result of your query.

• To hide a field:

1. If you do not want a field to appear in the results of your query, click on the Show box for the
field and it is displayed as . Otherwise, keep the show box as .

22 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-27 Hide a field

2. The unselected field does not appear in the result of the query.

3. To return to the Design view, click on .

Figure 5-28 Hidden field

5.8 Selecting all Fields

23 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
• To select fields for a query:
1. Double click on the heading of the table that contains the field you want to add to your query.
2. Position the mouse over any field and then drag the fields to the first empty column.
3. Below (as shown in Figure 5-29) is the query box with all the selected fields.

Figure 5-29Select all field in the table

5.9 Sorting Query Results

You can sort the results of a query to better organize the results. This can help you quickly
find the information of your interest.

Figure 5-30 Unsorted Publisher_ID

• To sort on fields in a query:

1. Click on the Sort area for the field you wanted sort its results.
24 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-31 To sort the publisher_id

2. Click on an order you wanted to sort data with, that is, Ascending, Descending or (not sorted).

3. Click on to run the query.

Figure 5-32 Sorted publisher_id

5.10 Using the Criteria

You can use criteria to find specific records in your database. Criteria are conditions that

25 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
identify which records you want to find.
For example, you can specify to view the year of publishing that arewritten by one publisher.
• To set criteria for records:
1. Click on year field and set the criteria.
2. Type the criteria and then press ENTER. Access may add quotation marks (“ “) or number sign
(#) to the criteria you type.

Figure 5-33 Using Criteria

3. Click on to save the changes and click on to run the query.


4. The results of the query appear.

26 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
Figure 5-34 Results of the query using criteria

Example of criteria that you can use to find records in your database:
Exact matches = 200 (Finds the number 200 )
= Kuala Lumpur (Finds the word Kuala Lumpur )
= 1/11/2000 (Finds the date 11 Jan 2000 )
Less than < 200 (Finds the number less than 200 )
< N (Finds text starting with the letters A to M )
< 1/11/200 (Finds dates before 11 Jan 2000 )
Less than or equal to <= 200 (Finds numbers less than or equal to 200 )
<= N (Finds the letter N and the text starting with the letters A to
M)
<= 1/11/2000 (Finds dates on and before 11 Jan 2000 )
Greater than > 200 (Finds number greater than 200 )
> N (Finds text starting with the letters N to Z )
> 1/11/2000 (Finds dates after 11 Jan 2000 )
Greater than or equalto
>= 200 (Finds number greater than or equal to 200 )
>= N (Finds the letter N and text starting with the letters N to Z)
>= 1/11/2000 (Finds dates on and after 11 Jan 2000 )
Not equal to <> 200 (Finds numbers not equal to 200 )
<> Kuala Lumpur (Finds text not equal to Kuala Lumpur )
<> 1/11/2000 (Finds dates not on 11 Jan 2000 )
Empty Fields Is Null (Finds records that do not contain data in the field )
Is Not Null (Finds records that contain data in the field )

Find list of items In ( 100, 101 ) (Finds the number 100 and 101 )

27 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
In ( Kuala Lumpur , KL ) (Finds Kuala Lumpur and KL )
Between …. And…. Between 100 And 200 (Finds numbers from 100 and 200 )
Between A And F (Finds the letter F and text starting with the
letter A to E)

Between 1/11/2000 And 11/20/2000 (Finds dates on and between


11 Jan 2000 and 20 Jan 2000 )
Wildcards The asterisks wildcards represent one or more characters. The
question marks (?) wildcards represent a single character.
Like Br*(Finds text starting with Br, such as Brenda and Brown )
Like *ar* (Finds text containing ar, such as Arnold and Marc )

5.11 Renaming a Query

You can change the name of a query to make it more meaningful to the information of thequery displayed.
• To rename a query:
1. Click on Queries in the Database window.
2. Click on the name of the query you want to rename.
3. Click on the name of the query again. A black border appears around the name of the query.

Figure 5-35 Rename a query

4. Type the new name for the query and then press ENTER.
5. The new query name displayed.

5.12 Deleting a Query

28 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
If you no longer need a query, you can permanently delete the query from your database.
• To delete a query:
1. Click on the Queries in the Database Window.
2. Click on the name of the query you wanted to delete.
3. Click on the delete option to delete the query.

Figure 5-36 Delete a query

5.13 Summary

· To set the primary key:


1. Click the field you want to set as the primary key. Primary key for the Author table isAuthorID.

2. Click to set the field as the primary key.


29 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
3. Click on to save your changes.
4. Set the Indexed property of the field to Yes (No Duplicates). This will allow you to
sort data quickly and search data in the field. No Duplicates mean that you are notallowed to
enter the same value in the field more than once.

5.1.2 Creating Relationship between Tables


You can create relationship between tables. Relationships allow you to bring together relatedinformation in
your database.
In a relational database, relationships enable you to prevent redundant data. Referential
integrity relationships help to ensure that information in one table matches information in
another.
Types of Table Relationships
A relationship works by matching data in key columns, usually columns with the same namein both tables.
In most cases, the relationship matches the primary key from one table, which provides a
unique identifier for each row, to an entry in the foreign key of another table.
There are three types of relationships between tables. The types of relationship created
depend on how the related columns were defined.
Below are the three types of relationship in use:
1. One-To-Many (1:M) Relationships
2. Many-To-Many (M:N) Relationships
3. One-To-One (1:1) Relationships
· To view the relationships of tables:

1. Click on to display the Relationships Window.


2. The Relationships Window appears. If any relationships exist between the tables inyour
database, a box for each table appears in the window.
3. In this case, no tables are displayed for this relationship. (As shown in Figure 5-5.)

4. Click on Show Table button to add tables to the Relationships Window.


To add table to the relationship:
5. Click on Add button to add the table to the window.
6. Click Close button to close the Show Table dialog.
7. Otherwise, repeat the steps to add more tables to the relationships.

So far, three tables, which are the Book_Author, Book and Publisher are added to
the relationship, as shown in Figure 5-6.
Each table is displayed as a box in the Relationship Window, where each box in the
relationships window displays the field for one table. The primary key for each table
30 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
appears in bold. The primary key uniquely identifies each record in the table.
You create a relationship between tables by identifying the matching fields in the tables.
Now we are going to create a relationship between table Book and Book_Author, based
on their same primary used – Book_Code.
· To create a relationship between two tables:
1. Position the mouse over the field you want to use to create a relationship with anothertable. Drag
the field over the other table until a small box appears over the matchingfield. In this case, click on
Book_Code of Book_Author table and drag it to the Booktable.
2. Double click on the relationship line appeared.
3. The Edit Relationships dialog box appears. (As shown in Figure 5-7)
4. The relationship of the selected tables is displayed here.
5. Check on one of these check boxes, that is, Enforce Referential Integrity, CascadeUpdate
Related Fields and Cascade Delete Related Fields.
6. Click on Create button to create this relationship.

Below are the descriptions of the three check-box options:


Enforce Referential Integrity
Referential integrity is a set of rules that prevents you from changing or deleting a
record if matching records exist in a related table.
Cascade Update Related Fields
If you click to select the Cascade Update Related Fields check box when you
define a relationship, any time that you change the primary key of a record in the
primary table, Microsoft Access automatically updates the primary key to the

new value in all related records.


Cascade Delete Related Records
If you select the Cascade Delete Related Records check box when you define a
relationship, any time that you delete records in the primary table, Microsoft
Access automatically deletes related records in the related table.
7. Click on Create button to create the relationship.
8. The connections appear.

9. Click on to save the relationship.


10. The symbols above the line indicate the cardinality for the relationship. In this
example, each record in the Book table (1) relates to one or more records in the
Book_Author.
11. Repeat step 1 to 7 to create a relationship for the rest of the tables shown in
Relationships.
31 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
5.1.3 Enforce Referential Integrity
Referential integrity between tables is enforced by default when you create a relationship inyour database
diagram. An enforced relationship ensures each value entered in a foreign keycolumn matches an existing
value in the related primary key column.

5.2 Creating Queries

Queries are used to view, change, and analyze data in different ways. You can also use themas the source of
records for forms, reports, and data access pages. You can create a query tofind information of your interest
from the database.

Now we are going to create a query to make a list of books that is written by the author and the book title.
• To create a query (in Design View):
1. Click on Queries in the Database window.
2. Double click on Create query in Design view.
3. The Select Query window and the Show Table dialog box appear, as in
Figure 5-10.
4. Click on a table that contains information you want to use in your query.
5. Click on Add to add the table to your query. The selected table appears in the display area.

6. Click on Close button to hide the Show Table dialog box.


The Select Query window will appears as in the figure below.
7. If you want to delete the table inserted in your Select Query window, click on the table and then
press the REMOVE TABLE key.
This removes the table from the query, but not from the database.
8. Double click on a field you want to include in your query.
9. Press the button when you are ready to run the query.
10. The result of the query appears as in Figure 5-13.

11. Click on to save your query so you can run the query again later.
12. The Save dialog box appears.
13. Type a name for your query.
14. Click on OK.

15. You are back in query results, so, when you finish viewing the results of your query, click
to close the query.

5.2.2 Creating Query by Using Wizard


32 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
• To create query (by using Wizard):
1. Click on Query Wizard in the Database window.
2. Click on the Simple Query Wizard from the New Query Dialog Box.
3. Select the table containing the fields you want to include in your query.
4. Double click on each field you want to include or use the  button to select a field at a time or use
the  button to select all fields.
5. Select the table containing the field you want to include in your query.
6.In this case, click on  to select all fields of Show_Query table.
7. Click on Next to go to the next page of Query Wizard.
8. Click on Next to go to the next page of Query Wizard.
9. Type Publisher Query as the title of the query name.
10. Click Finish to complete the query creation.

5.3 Opening a Query

To open a query:
1. Click on Queries in the Database window.
2. Double click on the query you want to open, such as the ShowQuery query.
3. The query opens up. You can now review the result of the query.

4. When you finish working with the query, click on to close the query.
5. A dialog box will appear if you have not saved the changes for the query yet. Click on Yes if you
want to save the changes.

5.4 Changing a Query

You can change a query by reopen the desire query and change the fields of the query.
• To change a query:
1. Double click on the selected query to open it. The Query is displayed in Datasheet View.

2. Click on this symbol to change the view to Design View.

3. The query appears in the Design View.

33 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
5.5 Re-arranging Fields

You can change the order of fields in a query by rearranging fields in a query. However, it will affect the
order that the fields appear in the query results.
• To rearrange fields:
1. Position the mouse directly above the field you want to move, that is Book_Code.
2. Drag the field to a new location.

5.6 Deleting a Field

You can delete a field that you no longer need from your query.

• To delete the field:

1. Position the mouse directly above the field that you want to delete.

2. Click and select the field.

3. Press the DELETE key

4. The field disappears from your query.

5. Click on to save the changes.

6. Click on this datasheet symbol to display data in Datasheet View.

7. The query displays the result without the field the deleted field now.

5.7 Hiding a Field

You can hide a field used in a query. Hiding a field is useful when you need a field to find
information in your database, but do not want the field to appear in the result of your query.

• To hide a field:

34 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
1. If you do not want a field to appear in the results of your query, click on the Show box for the
field and it is displayed as . Otherwise, keep the show box as .
2. The unselected field does not appear in the result of the query.

3. To return to the Design view, click on .

5.8 Selecting all Fields

• To select fields for a query:


1. Double click on the heading of the table that contains the field you want to add to your query.
2. Position the mouse over any field and then drag the fields to the first empty column.
3. Below (as shown in Figure 5-37) is the query box with all the selected fields.

5.9 Sorting Query Results

You can sort the results of a query to better organize the results. This can help you quickly
find the information of your interest.

• To sort on fields in a query:

1. Click on the Sort area for the field you wanted sort its results.

2. Click on an order you wanted to sort data with, that is, Ascending, Descending or (not sorted).

3. Click on to run the query.

5.10 Using the Criteria

You can use criteria to find specific records in your database. Criteria are conditions that
identify which records you want to find.
For example, you can specify to view the year of publishing that are written by one publisher.
• To set criteria for records:
1. Click on year field and set the criteria.

35 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
2. Type the criteria and then press ENTER. Access may add quotation marks (“ “) or number
sign (#) to the criteria you type.

3. Click on to save the changes and click on to run the query.


4. The results of the query appear.

Example of criteria that you can use to find records in your database:
Exact matches = 200 (Finds the number 200 )
= Kuala Lumpur (Finds the word Kuala Lumpur )
= 1/11/2000 (Finds the date 11 Jan 2000 )
Less than < 200 (Finds the number less than 200 )
< N (Finds text starting with the letters A to M )
< 1/11/200 (Finds dates before 11 Jan 2000 )
Less than or equal to <= 200 (Finds numbers less than or equal to 200 )
<= N (Finds the letter N and the text starting with the letters A to
M)
<= 1/11/2000 (Finds dates on and before 11 Jan 2000 )
Greater than > 200 (Finds number greater than 200 )
> N (Finds text starting with the letters N to Z )
> 1/11/2000 (Finds dates after 11 Jan 2000 )
Greater than or equalto
>= 200 (Finds number greater than or equal to 200 )
>= N (Finds the letter N and text starting with the letters N to Z)
>= 1/11/2000 (Finds dates on and after 11 Jan 2000 )
Not equal to <> 200 (Finds numbers not equal to 200 )
<> Kuala Lumpur (Finds text not equal to Kuala Lumpur )
<> 1/11/2000 (Finds dates not on 11 Jan 2000 )
Empty Fields Is Null (Finds records that do not contain data in the field )
Is Not Null (Finds records that contain data in the field )
Find list of items In ( 100, 101 ) (Finds the number 100 and 101 )
In ( Kuala Lumpur , KL ) (Finds Kuala Lumpur and KL )
Between …. And…. Between 100 And 200 (Finds numbers from 100 and 200 )
Between A And F (Finds the letter F and text starting with the
letter A to E)

Between 1/11/2000 And 11/20/2000 (Finds dates on and between


36 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
11 Jan 2000 and 20 Jan 2000 )
Wildcards The asterisks wildcards represent one or more characters. The
question marks (?) wildcards represent a single character.
Like Br*(Finds text starting with Br, such as Brenda and Brown )
Like *ar* (Finds text containing ar, such as Arnold and Marc )

5.11 Renaming a Query

You can change the name of a query to make it more meaningful to the information of thequery displayed.
• To rename a query:
1. Click on Queries in the Database window.
2. Click on the name of the query you want to rename.
3. Click on the name of the query again. A black border appears around the name of the query.
4. Type the new name for the query and then press ENTER.
5. The new query name displayed.

5.12 Deleting a Query

If you no longer need a query, you can permanently delete the query from your database.
• To delete a query:
1. Click on the Queries in the Database Window.
2. Click on the name of the query you wanted to delete.
3. Click on the delete option to delete the query.

37 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
5.17 Exercises

38 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
39 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
40 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
41 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
42 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l
43 | P a g e C h a p t e r 5 : D a t a b a s e F u n d a m e n t a l

You might also like