Robert Austin | November 18, 2018
In this post, we will be looking at how to use Structured Query Language (SQL for short) to create, read, upate and delete data in our MS Access applications.
Note: SQL is usually pronounced SeQueL.
The fundamental job of an MS Access database (or indeed any database) is to provide a home (base) for data (data). Hence database. Data is stored in entities which are referred to in MS Access as tables and this ability to store data provides us with an opportunity to read, update, insert and delete said data at a later date. In order to work with the data we have stored, we need to be able to communicate with the database. And in order to communicate with the database, we need to use Structured Query Language (SQL).
SQL provides us with the opportunity to read data from single or even multiple tables. We can use SQL to sum, count or average values stored in the database. SQL can also be used to insert new records, update existing ones and delete unwanted ones. Let’s examine what an SQL statement might look like.
The most basic statement you can write with SQL is called a SELECT statement.
SELECT Invoices.ID, Invoices.InvoiceNo, Invoices.Date,
Invoices.OrganisationID FROM Invoices
In the above SQL statement we are retrieving the fields ID, InvoiceNo, Date and OrganisationID from an Invoices table.
In MS Access, SQL can be found in queries (view a query in SQL View). It can also be used in VBA to construct custom queries to manipulate data (this will be covered later in the post).
Note: In MS Access, every communication with the database uses SQL to some degree although this is not always obvious. When you create a form based off a table, the form is using SQL to communicate with the table but these interactions are buried inside MS Access.
When you use SQL to retrieve data, you are creating a query. If you take another look at the Select statement above, you will see that some of the words are in UPPER case (SELECT and FROM). These are called keywords and are fundamental to creating SQL queries.
Note: When writing SQL, it doesn’t matter if you use UPPER or lower case for the keywords. SELECT and select are treated exactly the same. There is, however, a convention to use UPPER case as it is easier to read.
There are many keywords in Structured Query Language. Here is a brief description of some of the more popular ones:
Keyword |
Description |
SELECT |
Used to retrieve data from a table. |
INSERT |
Used to add new rows of data to a table. |
UPDATE |
Used to update existing data in a table. |
DELETE |
Used to remove data from a table. |
WHERE |
Used to filter out certain rows based on some criteria. |
ORDER BY |
Used to return the data rows in a particular order based on one or more columns (fields). |
JOIN, INNER JOIN & OUTER JOIN |
Used to join tables together on primary and foreign key fields. |
DISTINCT |
Used to reduce the number of rows returned based on the uniqueness of a given column (field). |
UNION |
Used to join tables together that have an identical number of columns with identical data types. |
Table 1
It is worth remembering that, the fundamental purpose of a database, is to store and manipulate data. At the very basic level there are four actions that a database is required to perform. These activities are often referred to as CRUD operations. CRUD stands for Create, Read, Update and Delete. In SQL terms, these 4 operations translate to:
Let’s expand on the SELECT statement.
In MS Access, you can create a SQL query by either writing the code directly (SELECT field FROM table) or you can use a special interface to help construct the code for you. This special interface method is known as Query-By-Example. It can be an excellent way to generate SQL code without having to be too adept at writing it. We will begin by showing you how to use the QBE editor.
Open the Teaching Institute Software System Database if you wish to practice creating and modifying queries.
TeachingInstituteSoftwareSystem
In the ribbon, click Create and then select Query Design, the Query By Example editor will open (double-click on the image to enlarge it).
Figure 1
The three areas that have been highlighted are:
We are going to create a simple query that will retrieve the StudentID, First Name, Last Name and Email Address from the tblStudents table.
Your screen should now look something like this:
Figure 2
The results of the query are displayed in tabular form.
Figure 3
Скачано с www.znanio.ru
Материалы на данной страницы взяты из открытых источников либо размещены пользователем в соответствии с договором-офертой сайта. Вы можете сообщить о нарушении.