SQL and AccessA Guide to Understanding SQL and its application inMicrosoft Access by Maggie McClelland
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!
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
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.
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
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
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])
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”
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.
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.
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…
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.
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.
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
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.
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.
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.
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.
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!