36
When I want to work with SQL, I start off as if I am doing a regular query.

When I want to work with SQL, I start off as if I am doing a regular query

Embed Size (px)

Citation preview

Page 1: When I want to work with SQL, I start off as if I am doing a regular query

When I want to work with SQL, I start off as if I am doing a regular query.

Page 2: When I want to work with SQL, I start off as if I am doing a regular query

When I get to the user interface, I add the table and then I go to the icon that lets me look at how to view what I did. From there, I select SQL. In this example, it brought up SELECT FROM books;I wanted to see all fields, so I put in the * after SELECT. Therefore, I am selecting all fields from the table named books.

Page 3: When I want to work with SQL, I start off as if I am doing a regular query
Page 4: When I want to work with SQL, I start off as if I am doing a regular query

Now, I want to see specific fields. I am going to change the * and list the fields that I want to see.I am going to copy and paste over the query that now says SELECT * FROM books; Please note the semi-colon at the end of the SQL command.Note: What your seeing displayed on top of the Access view is a separate Notepad file that was resized to make it easier for you to see the SQL statement.

Page 5: When I want to work with SQL, I start off as if I am doing a regular query

My final SQL SELECT statement has been changed to:SELECT ISBN, title, publisherNote that this is still searching within the “books” table.To execute this, I must click on the icon to display the results.

Page 6: When I want to work with SQL, I start off as if I am doing a regular query

If I put a field that does not exist in the field list, I get this response. There is no field called yr on the table.

Page 7: When I want to work with SQL, I start off as if I am doing a regular query

I have now corrected it and replaced yr with the accurate field name of yearPub.

Page 8: When I want to work with SQL, I start off as if I am doing a regular query
Page 9: When I want to work with SQL, I start off as if I am doing a regular query

This is WRONG. The order of the clauses are important! The FROM clause should be on the second line and the WHERE clause should be on the third line.

Page 10: When I want to work with SQL, I start off as if I am doing a regular query

Now, it is correct!Please note again that there is a semi-colon only after all of the clauses.Also remember, this is just a copy in notepad so you can see the code better.

This is Query3 thatI will run.

Page 11: When I want to work with SQL, I start off as if I am doing a regular query

I showed the result and I showed the separate copy in notepad since I cannot show the query code and the result at the same time.Again, this is just a copy in notepad.

Page 12: When I want to work with SQL, I start off as if I am doing a regular query

It is important that when I compare fields, I reference fields of the same type. I put the ABC in quotes because it is a text/string field and therefore must be enclosed in quotes. If I did not use quotes, it would assume that ABC was the name of a field. Numerical data should not be surrounded by quotes if it is being compared to a numeric field.

Page 13: When I want to work with SQL, I start off as if I am doing a regular query

I made some changes in the structure and added an “Edition” filed.

Page 14: When I want to work with SQL, I start off as if I am doing a regular query

Here I am checking two things using an AND relationship.The second thing is comparing “edition” (which I made numeric) to “2”, which is testing a numeric to a numeric reference.

Page 15: When I want to work with SQL, I start off as if I am doing a regular query

The text fields are pushed against the left wall and the numeric fields are pushed against the right wall.

Page 16: When I want to work with SQL, I start off as if I am doing a regular query

This shows the SQL and the user interface.Please note that sometimes when you bring up the user interface it changes the code in the SQL by putting in a lot of parenthesis, etc. that are not necessary.

Page 17: When I want to work with SQL, I start off as if I am doing a regular query
Page 18: When I want to work with SQL, I start off as if I am doing a regular query
Page 19: When I want to work with SQL, I start off as if I am doing a regular query

This shows the SQL generated by Access. This is NOT the way I want you to write it.Note that books.ISBN means the ISBN field within the books table, but since I am only using one table, these are not necessary.Also note the parenthesis, none of which are needed in this example.

Page 20: When I want to work with SQL, I start off as if I am doing a regular query
Page 21: When I want to work with SQL, I start off as if I am doing a regular query

I changed the AND to an OR so I get records that either have ABC or have edition 2.

Page 22: When I want to work with SQL, I start off as if I am doing a regular query

This is an OR relationship.

Page 23: When I want to work with SQL, I start off as if I am doing a regular query

Now I want to do this problem - see the next few pages.

Page 24: When I want to work with SQL, I start off as if I am doing a regular query

This is not what I wanted. It reads publisher ABC and edition 2 OR just yearPub > 2003.

Page 25: When I want to work with SQL, I start off as if I am doing a regular query

When I put it within parenthesis, it renders correctly.This reads publisher ABC AND either edition 2 OR yearPub > 2003.

Page 26: When I want to work with SQL, I start off as if I am doing a regular query
Page 27: When I want to work with SQL, I start off as if I am doing a regular query
Page 28: When I want to work with SQL, I start off as if I am doing a regular query
Page 29: When I want to work with SQL, I start off as if I am doing a regular query
Page 30: When I want to work with SQL, I start off as if I am doing a regular query

This is how the SQL shows up on the query user interface. Note that ABC is repeated because it has to be true.

Page 31: When I want to work with SQL, I start off as if I am doing a regular query

This is the way that it is generated by Access.

Page 32: When I want to work with SQL, I start off as if I am doing a regular query

This does not have the parenthesis so it will not solve my problem - it will render incorrectly.

Page 33: When I want to work with SQL, I start off as if I am doing a regular query
Page 34: When I want to work with SQL, I start off as if I am doing a regular query

This shows the fact that I get the wrong output - one of the records has CDE. Note that I went back and changed the data for better results. See list of data on one of the previous slides.

Page 35: When I want to work with SQL, I start off as if I am doing a regular query

This shows the wrong code in the user interface.

Page 36: When I want to work with SQL, I start off as if I am doing a regular query

When I want to copy a Please keep in mind that the 2007 version of Access uses a different filename extension.database, I go into windowsexplorer and drag it to another drive.This mdb is also what I would attach to an email for submission.