Tutorial for using SQL in Microsoft Access


Published on

Published in: Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Tutorial for using SQL in Microsoft Access

  1. 1. SQL and AccessA Guide to Understanding SQL and its application inMicrosoft Access by Maggie McClelland
  2. 2. Why should I learn SQL? SQL is a common programming language used in many relational databases to manage data and records by performing tasks that would be time consuming to do by searching the records individually. Such relational databases are commonly used in the field of library and information science, which means that in addition to being useful in managing data…. Means employers may want you to know it!
  3. 3. What Is SQL? SQL (Structured Query Language) is a programming language used for manipulating and managing data in relational databases such as Microsoft Access, MySQL, or Oracle.What does SQL do?  SQL interacts with data in tables by:  inserting data  querying data  updating data  deleting data  creating schemas  controlling access
  4. 4. What do I need to learn it?You: In using it, its helpful to understand how data may already interact within databases, but not necessary to learning the fundamentals.Your Computer: Relational database software such as Microsoft Access, mySQL, or Oracle.
  5. 5. A breakdown of SQL(anguage)  Two major features  Statements: affects data and structure (what data is in)  Queries: retrieve data based on programming  Clauses are the parts of these  Within clauses may be:  Expressions: produce the fields of a table  Predicates: specifies under what conditions action is to occur Image retrieved from Wikipedia.org
  6. 6. TWO TYPES OFSTATEMENTS Data Definition Language  Data Manipulation Language Manages structure of data  Manages the data in tables and indices (where structures data goes) ……………………….. Some Basic Elements:  Some Basic Elements: Drop/Create, Select, Select…Into, Update, Grant/Revoke, Add, and Delete, Insert…Into Alter
  7. 7. TWO TYPES OFSTATEMENTSData Definition Language Syntax is similar to computer programming Basic Statements are Composed of a Element part, Object part, and Name part,  Ex: To create a Table Named „tblBooks‟ it would look like this: CREATE TABLE Books To include an expression to the CREATE TABLE to add Book ID and Title fields, insert them in brackets and separate out by a comma, all within parentheses. This is a common syntax.  Ex: To add the fields BookID and Title: CREATE TABLE tblBooks ([BookID], [Title])
  8. 8. TWO TYPES OFSTATEMENTSData Manipulation Language Composed of dependent clauses Common features of syntax:  “ “ to contain the data specified  = to define the data to field relationship It is defined by a change to the data, in the below case, deletion from tblBooks all records which have 1999 in their Year field  ex. DELETE FROM tblBooks WHERE Year = “1999”
  9. 9. Queries Queries themselves do not make any changes to the data or to the structure. Allows user to retrieve data from one or many tables. Performed with SELECT statement. Returning to our example to display all books published in 1999:  Ex: Select From tblBooks Where Year = “1999” Note: SELECT is nominally said to be a statement but it does not ‘affect data and/or structure’. It just retrieves.HOWEVER Queries are what make statementshappen. When combined in access with statements,they make the changes to data that we desire.
  10. 10. What about MicrosoftAccess?All of these SQL aspects manage and manipulateyour data in be performed in Microsoft Access.Microsoft Access is usually available with yourbasic Microsoft Office package, as such it is widelyused. It is mostly suitable for any database under 2 GB.In the next half, I will show you how to execute inMicrosoft Access common SQL commands.
  11. 11. Proceeding… Here is an access database to download if you wish to follow along with the tutorial:  Upon opening it up, look on the left side of your screen. Double click on the first table selected, Books. This is where we will start from. What follows will be a slide of explanations and then a video. You can pause or stop the videos at any time and may jump around using the progress bar at the bottom of the video. These videos have no sound. Continuing on…
  12. 12. QueriesSimple SELECT query, designed to  What follows is a simple „select‟ filter records from a table. From the menu, next to Home, select Create. Go to the last section of selections, marked other. Select Query Design. Once you reach the Query Design screen you will be prompted by a window. Cancel this out. In the upper left corner under the round Windows Button, is a button that says SQL view. Select this. For a simple search calling all records from the Books table, enter: Select * FROM Books Going back to the upper left corner next to SQL view is another button that is an exclamation mark and says Run. Select this. Play video here.
  13. 13. QueriesMore complex SELECT  To execute a query that pulls out specific information, we‟ll have to add a WHERE clause.  Lets look for all books published in the year 1982. To do this we will be looking at all records in the Books table that have 1982 in the Year field. To go back to the screen where you can edit your SQL query, simply go to the button under your round windows button. There should be a down arrow to select other options. Click this and select your SQL view. Now that you are in SQL view, add to what you have so it reads: Select * FROM Books Where Year = 1982 Again, select Run when done. Play video here.
  14. 14. QueriesEven More complexSELECT  Lets say that we want to combine two tables. Maybe we want to find all books published in 1982 that were sent away for rebinding.  Table named Actions this. In this table, Object specified in each record is linked to the BookID in the Books table. To draw these two together in our search we will use an INNER JOIN, specifying with ON which two of those records are linked.  Because we have two tables now, we have to refer to the fields we are interested in as table.field
  15. 15. QueriesEven More complexSELECT cont…  To do this, return to your SQL query edit screen and enter: SELECT * FROM Books INNER JOIN Actions ON Actions.Object=Books.BookID WHERE Year = 1982 Run this. Play video here.
  16. 16. Changing Data Now we may want to change the data. In Microsoft Access, this is still done through the same Screen where we were entering SQL before. The most useful may be the UPDATE and the DELETE statements, which do exactly what they say. These are what we will execute.
  17. 17. Update Statements Say that you want to update the Authors field in the Books table with (LOC) following the names to show that they follow LOC name authority. We will use the UPDATE statement and following SET establish an expression to update the field. To do this return to your SQL query edit screen and enter: UPDATE Books SET Author=Author + " (LOC)“ Note: the + , this means add the following onto what is existing; we use “ “ because what is inside these is text entered into the table‟s field. Play video here.
  18. 18. Delete Statements Lets say that our collection deacessioned all books made before 1970 and we want to delete these from our files. To do this return to your SQL query edit screen and enter: DELETE * FROM Books WHERE Year <1970 Notice how we used < instead of = to find entries with values smaller than the number 1970. Play video here.
  19. 19. Congratulations!By now, you should have an understanding of SQL and a basic knowledge of how to use SQL in Microsoft AccessThe best way to learn a new technology is to play with it, I encourage you to do so. Before you know it, you will be a pro!
  20. 20. Helpful Sites http://msdn.microsoft.com/en- us/library/bb177893%28v=office.12%29.aspx http://download.oracle.com/docs/cd/B28359_0 1/appdev.111/b28370/static.htm http://w3schools.com/sql/sql_syntax.asp