Download presentation
Presentation is loading. Please wait.
Published byArabella Gilbert Modified over 6 years ago
1
Using SQL with Access I am adding queries to the stu table in SecondDB.accdb – a database that I created in class. SQL stands for structured query language and is also called sequel. It is behind the queries we create with the user interface and we can create these queries without using the user interface.
2
This is the database we created and below is the data in the stu table.
3
Select create and then query design
Select create and then query design. When the Show Table comes up you will close it. Notice the SQL that has appeared to the left of the screen. After you close, you should click on SQL
4
I just entered the code to select all the fields from the records in the stu table. Note the * which means all fields, the from clause that identifies the table as stu and the semi-colon at the end of the line. SELECT * FROM stu; You now see SELECT; This is the place you will enter your SQL. The results of the query.
5
Hard to read, but on the previous slide there was an arrow below view in the left cornor. Click on that and the word SQL comes up. Click on that to get back to the code. Then I saved the code just like I save any query.
6
Listing the field names means only those fields will show
Listing the field names means only those fields will show. Here is the query and the records showing stuid, stuname, gpa and major.
7
Now I am setting a criteria where I only want to see records where the major field contains CI.
I write this as WHERE major = “CI”; Note that CI is a non numeric literal and it needs to be in quotes.
8
I now have criteria that consists of two fields in an AND relationship
I now have criteria that consists of two fields in an AND relationship. Both of them have to be true before the record is displayed. As you can see both records have major of CI and gpa > 3.4.
9
Now we have two things in an OR relationship
Now we have two things in an OR relationship. Either one can be true to get the record displayed. Note that the records that do not have BU do not need any particular number of credits, but the ones that have CI have numCredits > 25 so that they can meet the criteria.
10
Remember that ANDs are resolved before ORs so the two things around the and are grouped and we are looking for one group or another. We want CI majors with gpa > 3.4 and that worked and we also want BU majors with a gpa > 3 and that worked.
11
In this one I want major CI with gpa > 3
In this one I want major CI with gpa > 3.4 or students students with numcredits > 20. This part stands alone. If the have more than 20 credits, I do not care about the other criteria. Note that John Doe is CI but is gpa is too low, so he made it to the list because his numCredits is > 20. Susan Ash made it to the list because she was CI with credits > 3.4 David Costa made it to the list because he had credits creater than 20. He could not have been here based on major and gpa. Bill Smith could have made it because of CI and gpa > 3.4 or he could have made it because his numCredits was > 20,
12
This is the situation where one thing has to be true and either of two other things has to be true. So the major has to be CI and then in addition, either gpa has to be greater than 3.4 OR numCredits has to be greater than 30. I have to have the parenthesis here so it is clear that it is major CI and either of the other two criteria. Since ANDs are resolved before ORs without the parenthesis we would have CI grouped with the AND gpa > 3.4 and the numCredits > 30 would have stood alone and anyone meeting that criteria would show. We do not want this. We absolutely want the CI to have to be true so we need the parenthesis. Note all the majors are CI and everyone either has gpa > 3.4 OR numCredits > 30.
13
Here are the SQL queries and some left over from the user interface
Here are the SQL queries and some left over from the user interface. I will put the database on line so you can see them.
14
Now I went back to the interface and made the same query.
15
I then opened the code that I wrote in the User Interface to see what it looked like in SQL.
Notice the parenthesis. There are layers of them that make it difficult to read. Also notice that every field now has the table name in front of the field so it is tableName.fieldName. This is required if you have multiple tables with fields that have the same name but it is not required when you are dealing with one table. When you do the assignment on SQL, I want you to send me the access database and in addition I want you to send me the code you wrote for each of the SQLs. Sometimes Microsoft decides to fix things if you go back and forth between the user interface and the SQL too often and the way things are set up, that is easy to do.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.