Using Action Queries, and Defining Table Relationships

27
Using Action Queries, and Defining Table Relationships

description

Using Action Queries, and Defining Table Relationships. Create action queries. You can create other types of queries that make changes to the data in the underlying table(s). Action queries can: Create a new table (make-table query) Add records to a table (append query) - PowerPoint PPT Presentation

Transcript of Using Action Queries, and Defining Table Relationships

Page 1: Using Action Queries, and Defining Table Relationships

Using Action Queries, and Defining Table Relationships

Page 2: Using Action Queries, and Defining Table Relationships

2

Create action queries

• You can create other types of queries that make changes to the data in the underlying table(s).

• Action queries can:– Create a new table (make-table query)– Add records to a table (append query)– Delete records from a table (delete query)– Update the data in a table (update query)

Page 3: Using Action Queries, and Defining Table Relationships

3

Action query considerations

• When creating a query that actually makes changes to your database, take extreme care that you do not create a query that performs unintended actions.

• It is a good idea to create your action query first as a select query.

• You can then view the results and determine if these are the records you want to alter.

• When you are sure the query will be applied to the records you really want, you can then run the query as an action query.

Page 4: Using Action Queries, and Defining Table Relationships

4

Use make-table and append query queries

• To change a select query to a Make Table query, choose Make-Table Query from the Query Type button's list.

• This will open the Make Table dialog box, in which you will specify the name of the new table.

• To change a select query to an append query, choose Append Query from the Query type button's list.

• In the Append Query dialog box, you will specify to which table you want to append records.

• The Append To row will be displayed in the design grid indicating the fields that the query will append to the table.

Page 5: Using Action Queries, and Defining Table Relationships

5

The Make Table dialog box

Page 6: Using Action Queries, and Defining Table Relationships

6

The query window for an append query

Page 7: Using Action Queries, and Defining Table Relationships

7

Creating a Delete Query

• A delete query is used to delete records you no longer need in your database.

• To create a delete query, first create and run a select query in Design View, selecting the items you wish to delete.

• If the select query displays only the records you wish to delete, click the list arrow for the Query Type button in design view and then click Delete Query.

• Run the query to delete the records you previously selected.

Page 8: Using Action Queries, and Defining Table Relationships

8

Delete Query Dialog Box

Page 9: Using Action Queries, and Defining Table Relationships

9

Creating an Update Query• Create a select query with the necessary fields

and selection criteria and click the Run button to preview the results.

• Click the list arrow for the Query Type button and select Update Query.

• Type the update values for the fields you need to update in their Update To text boxes.

• Click the Run button on the Query Design toolbar.• Click the Yes button to confirm updating the

records.

Page 10: Using Action Queries, and Defining Table Relationships

10

Update Query Dialog Box

Page 11: Using Action Queries, and Defining Table Relationships

11

One-to-Many Relationships

• A one-to-many (1:M) exists between two tables when each record in the primary table matches zero, one, or many records in the related table, and when each record in the related table matches at most one record in the primary table.

• Referential integrity is the rule that requires each nonnull foreign key value to match a primary key value

Page 12: Using Action Queries, and Defining Table Relationships

12

A one-to-many relationship

Page 13: Using Action Queries, and Defining Table Relationships

13

A query datasheet for a one-to-many relationship

Page 14: Using Action Queries, and Defining Table Relationships

14

Define many-to-many and one-to-one relationships between tables

• A many-to-many relationship exists between tables when the tables involved have multiple matches in each of the tables. – For example, if you have a table containing

student data and another table containing course data, you could say that this is a (M:N) relationship because a student can take many courses and a course can have many students

Page 15: Using Action Queries, and Defining Table Relationships

15

Define many-to-many and one-to-one relationships between tables

• Whenever there is a many-to-many relationship, you must provide a third table that will “link” the two tables together in a one-to-many relationship.

• You can also have a one-to-one relationship between tables, which means that there is exactly one record in the primary table that matches exactly one record in the related table.

Page 16: Using Action Queries, and Defining Table Relationships

16

A many-to-many relationship

Page 17: Using Action Queries, and Defining Table Relationships

17

A query datasheet for a many-to-many relationship

Page 18: Using Action Queries, and Defining Table Relationships

18

The Relationships Window

Page 19: Using Action Queries, and Defining Table Relationships

19

View and create indexes to tables

• When you establish a primary key for a table, you are actually creating an index for the table.

• An index is a list that relates field values to the records that contain those field values.

• The index makes it faster to search for records and to sort the records in a table.

• You can view existing indexes in table design view by clicking on the Indexes button on the Table Design toolbar.

Page 20: Using Action Queries, and Defining Table Relationships

20

How indexing works• You can create an index on any field in a

table. – However, each index actually is a new file and

takes up space

• When records are entered into a table they are assigned an internal record number in the order they were entered.

• The index created a different ordering of the records but keeps track of the number originally assigned to the record.

Page 21: Using Action Queries, and Defining Table Relationships

21

An illustration of an index

Page 22: Using Action Queries, and Defining Table Relationships

22

The Indexes window

Page 23: Using Action Queries, and Defining Table Relationships

23

Join a table using a self-join• There are three types of joins:

– The inner join selects records from two tables only when the records have the same value in the common field that links the tables.

– A left outer join selects all records from the first, or left, table and only those records from the second, or right, table that have matching common field values.

– A right outer join selects all records from the second, or right, table and only those records from the first, or left, table that have matching common field values.

• A self-join can be either an inner or outer join and is used to join a table to itself.

Page 24: Using Action Queries, and Defining Table Relationships

24

An example of an inner join

Page 25: Using Action Queries, and Defining Table Relationships

25

An example of a left outer join

Page 26: Using Action Queries, and Defining Table Relationships

26

An example of a right outer join

Page 27: Using Action Queries, and Defining Table Relationships

27

An example of a self-join