Action Queries |
|
Introduction to Action Queries |
We have used the concept of a query so far only to create a list of fields that would be considered for a set of records. We also mentioned that a query in Microsoft Access is simply a means of visually representing data. Indeed, a query is based on a SQL statement. As SQL is its own, fully functional language, we can use it to perform far more operations than to only select columns for a query. The SQL can be used to create tables, modify tables, perform data entry, modify records, etc. Some of these operations can be performed visually in the query Design View. They can also be performed using SQL statements. To know what is going on behind the scenes, you can write your own code (SQL statement). You can also use the Design View to start a query, and then open the code to customize the statement. |
You will hardly perform some of the operations we will review here, but you should know that they are available. It is very important to know that, when using an action query to perform a specific action such as creating a table, you are not creating a regular query in the strict sense. Most of these queries are usually useful only once. For this reason, you will hardly need to save them, as the action may be needed only once. If you perform the action of the query, you do not need to save it but once you apply its intended action, the related action is executed and stays with the database even if either you do not save the query or save it but later on delete the query. In practicality, this is also how queries are used in many other environments. For example, in Microsoft SQL Server, the statement that makes up a query is created as its own file, also called a view, then it can be called any time. |
Practical Learning: Introducing Action Queries
|
Queries and Table Creation |
Creating a database is usually the first action you perform in order to have one. Of course, you can also open an existing database. Creating a Microsoft Access database is done visually as you must create its file without using SQL code, contrary to many other database environments. The second action to
take is probably to create a table.
Using queries, there are two main techniques you can use to create a new table for your database. Microsoft Access provides a technique called Make-Table Query. This type of query is used to retrieve all or some fields of an existing table and, instead of creating a new query that depends on an existing table, you would get a brand new table, independent of any existing table. This type of action can let you create a new table based on a rule applied on an existing table. For example, suppose the Cars table of your car rental database contains cars that should not be rented to customers
anymore, perhaps because of their age. You can create a query that would make a list of cars that follow
a rule, apply it, and then create a new table made of only such cars.
To create a table using a Microsoft Access query, start a new query in Design View. Then, on the main menu, you can click Query -> Make Table Query, this would present you with a Make Table dialog box that allows you to specify a new name for the query or to select an existing table that would receive the new data.
Practical Learning: Using Make-Table Queries
SQL and Table Creation |
As a computer language, the SQL is equipped to perform all basic and necessary operations of a database. As such, it can be used to create a table. Although you will usually use the visual means of Microsoft Access, you can still use a SQL statement to create a table. CREATE TABLE TableName As you have probably realized by now, every table must have at least one column. The list of columns of a table starts with an opening parenthesis “(“ and ends with a closing parenthesis with an optional semi-colon “);”. If the table will be made of more than one column, you can separate them with a comma. The syntax used would be:
|
CREATE TABLE TableName (Column1, Column2, Column_n); To make the statement easier to read, and because some columns can be long, you can create each on its own line. The syntax would become: CREATE TABLE TableName ( Column1, Column2, Column_n ) To create a column, you specify its name, followed by its data type, and some possible options. Therefore, the syntax of creating a (one) column is: ColumnName DataType Options Notice that there is only space that separates the sections of the syntax. For this reason, the name of a column should be in one word because, if you put space after the first word, the SQL engine would treat the next word as a data type. For example, the following statement would produce an error: CREATE TABLE Test ( Marital Status ) If you want to use space in a column name, include the name between an opening square bracket “[“ and a closing square bracket “]”. The above statement would be changed to: CREATE TABLE Test ( [Marital Status] ) After (or on the right side of) the column name, you must specify the type of information, also called the data type, that will be stored in the cells under that column. Here are the SQL data types supported in Microsoft Access:
CREATE TABLE Persons ( FirstName VARCHAR, [Last Name] Text, Gender int, [Date Of Birth] DateTime, MaritalStatus Integer, IsMarried Bit, PersonalIncome Money, HouseholdIncome Currency );
When studying techniques of creating fields in a table’s Design View, we saw that a property called
Field Size can be used to specify the size of value used on a field. In the SQL also, this property is fixed for most fields expect those that are text-based. Therefore, when creating a field whose data type is
char, text or varchar, you can specify the desired number of characters that the field should allow.
If you are creating a char or a varchar column, you should
(strongly) specify the number of characters instead of letting Microsoft
Access assign the default. The text usually doesn't use a number of
characters. CREATE TABLE Persons ( PeronID long Primary Key, FirstName VARCHAR NULL, [Last Name] Text, Gender int NULL, [Date Of Birth] DateTime, MaritalStatus Integer, IsMarried Bit NULL, PersonalIncome Money, HouseholdIncome Currency ); If you don't specify the NULL attribute, the nullity is implied. On the other hand, if you want to require the values of a field so the user cannot move to the next record unless the field is filled, type
not null to its right. This would indicate to Microsoft Access that the field does not allow a null value. CREATE TABLE Genders ( GenderID int CONSTRAINT PKGender Primary Key, Gender char(20) ); To establish a relationship from this table when creating another table, in that other table, create a field. By tradition, this would have the same name as we have mentioned already but this is not a requirement. The requirement is that both fields (the new field and the field it refers to in the parent table) must have the same data type. When creating the relationship, after specifying the name of the field and its data type, type the references keyword followed by the name of the table it is linked to and its primary key as argument. For example, to create a Persons table that is related to the above Genders table, you would type: CREATE TABLE Persons ( PeronID long Primary Key, FirstName VARCHAR NULL, LastName Text Not Null, GenderID int REFERENCES Genders(GenderID), Notes char ); |
Practical Learning: Creating Tables With SQL
|
SQL and Data Entry |
When using the Make-Table Query of Microsoft Access, you actually perform two actions at once. First you create a table, which is the primary requirement of the Make-Table Query, then, when you execute the query, you fill the new table’s fields with the existing values of the originating table. |
The SQL allows you to actually perform data entry on a table. In the language’s standard, you can create a table and fill it with values. Alternatively, you can add various new records to the fields of an existing table. This is also referred to as inserting records. In the SQL, data entry is performed using the INSERT INTO expression, followed by the table name, followed by the VALUES keyword, and followed by the values in parentheses. If the table is made of only one column, you can include the desired value in the parentheses. If the table is made of more than one column, you must separate the values with commas. The syntax to use is: INSERT INTO TableName VALUES(Column1, Column2, Column_n) The TableName attribute must be the name of an existing table of the current database. If the name is wrong, that is, if the name is not found in the database, the SQL engine would consider that the table you are referring to does not exist. Consequently, you would receive an error. The VALUES keyword indicates that you are ready to list the values of each field of the record. The values of the columns must be included in parentheses. |
Practical Learning: Performing Data Entry
|
Queries and Record Appending |
When working in a sensitive database, if you were the one in charge of data entry, you may prefer to use a temporary table to create records to make sure they are validated before actually adding them to the system. If you had created a significant number of these records and need to add them to
the main table, the work can become overwhelming. Fortunately, Microsoft Access allows you to create a special query that can be used to add many records to a table in one step. |
Practical Learning: Appending Records
|
Queries and Records Updating |
If you have a large database and all of a sudden, there are many records that need to receive a common change, you may face an arduous task.
For example, you may have to check every record and make the change where
needed. Fortunately, to apply this type of impact to a set of records, you can create a special query. |
Practical Learning: Updating Records
|
Queries and Table Deletion |
Microsoft Access provides various techniques to delete tables. In the next lesson, we will see how this can be done in the Database window. Meanwhile, the SQL provides its own mechanism to delete a table. This is easily done using the DROP command. The syntax is: DROP TABLE TableName; The DROP TABLE command is used only to delete tables (and indexes), not forms or reports. The TableName parameter must be a valid name of a table of the current database. There are two big issues with the DROP TABLE command: it does not warn you and it is not reversible. |
Practical Learning: Deleting Database Tables
|
Queries and Record Deletion |
Besides deleting tables, you can also delete individual records of a table. Microsoft Access provides an easy mechanism of performing such an operation. To delete a group of records in one action, you can create a special query called a Delete Query. |
Practical Learning: Deleting Database Records
|
|
||
Previous | Copyright © 2002-2019, FunctionX | Next |
|