Você está na página 1de 20

SQL PRIMER FOR TECHWRITING ENG 307 -- pg.

<SELECT An SQL PRIMER for the English Major in all of us.>

by: Will Kotheimer

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 2

TABLE of CONTENTS:
Part I: Beginnings 1.Introduction 1.1 What is SQL? 1.2 Why Study SQL? 1.3 Audience and Scope:? Part II: The software program 2.1 phpmyadmin Graphical User interface. 2.2 How to set up XAMPP, localhost, and phpmyadmin. Part III : A glossary of terms Part IV: Visual Approaches Databases: A visual approach. Part V: Instructions for creating a database in phpmyadmin Part VI: Some SQL Querying in action. 5.1 The Generic SELECT statement 5.2 SELECT specific categories, GROUP BY, ASC, DESC 5.3 JOINS and aliases: helps you make meaningful connections 5.4 Further explanation of JOIN example 5.5 Aggregate Functions ` 5.5 LIKE clause Part VII Conclusion. Part VIII Appendix.

pg. 3

pg. 4 pg. 5 pg. 6 pg. 7 pg. 11 pg. 11 pg. 12 pg. 13 pg. 14 pg. 15 pg. 16

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 3

Part I: Beginnings
INTRODUCTION:
WHY STUDY SQL?
You would want to study SQL if you are working in an office environoment and your company used databases. An example of some of the environments where SQL would be used include a doctors office, a university office, a corporate website, and an auto parts supplier. There are many places where databases can be used.

WHAT IS SQL?
SQL is Structured Query Language, a language developed during the 1970s to be used in databases.

WHO IS THIS MANUAL INTENDED FOR, AND JUST HOW GOOD IS IT? (AUDIENCE AND SCOPE)
This manual is primarily for people without technical saavy, such as those gifted with understanding the world through a lense of metaphor, art, and stories. My hope is that it will be user friendly for those who normally find technical manuals challenging. This manual is not for advanced users but I do hope that technically minded people could enjoy it as well.

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 4

PART II: The Software program


The phpmyadmin GUI
PHPMYADMIN is a graphical user interface that allows you to create tables and do queries with relative ease. It is also the official database creator for mysql, a popular open source version of SQL.

Setting up XAMPP on your computer


STEP 1: go to google and type in xammp. STEP 2: it is the first entry- Apachefiends-xampp, click on it. STEP 3: Click on the one for your operating system. STEP 4: Wait while it downloads. STEP 5: Use the install procedures provided by the Apache company.

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 5

PART III: A Glossary of SQL Terms

DATABASE: A collection of values organized by categories usually connectd to id numbers referring to specific people, items, or locations; larger databases are further organized by tables. QUERY: A search for information in a database. SELECT: A statement in SQL used at the begining of a query that allows the computer to know to query and display the information that follows it. FROM: A statement in SQL that tells the computer to make the query with the table categories following FROM. WHERE: A statement in SQL that liits the result of a query to a specific request. INNER JOIN . . . ON: A statement in SQL that allows two or more tables to be connected and displayed. GROUP BY: A statement in SQL that specifies what category will be used to order the display. ALIASES: one or two letters that stand in for a table name in a JOIN or WHERE clause, to keep the syntax more readable. After the one or two letter aliase, there is a period then a category name that comes from that field,( e.g. p.description). LIKE: A statement in SQL that allows you to do a search query without actually knowing exactly the name of what you are looking for.

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 6

Part IV: Visual Approaches


Databases: a visual approach

Row1: Person information

Row2: Product information

Row3: Invoice information

quantity

Row4: Personal information Figure 1: A visual approach to the order database structure.

Explanation:
Each of these icons represent some piece of information. Row 1 has a persons name, address, and personal id number. Row 2 carries the product number, category, description, and price. In row 3, the cash register represents when the order was placed, the calendar with the $ by it represents when the order was paid, the rocket ship represents when the order was sent, and the shopping cart represents an item. There would be more icons than this in a real database, but this is just to show how these are structured. Row 4 has all the personal information: the credit card number, password, username, and the personal id number. It is good to keep the shoppers name and address apart from this for obvious reasons.

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 7

PART V: Instructions for creating a database in phpmyadmin


1) Press the buttons to turn on apache and mysql. 2) Open your browser 3)In Browser url type: http://localhost

We are now going to delve into creating these databases. If you set up XAMPP on your computer, then you can follow along below.

Figure 2 4)Go to the left side of the screen and click on phpmyadmin.

Behind the scenes: More than you want to know about databases. Yeah, I know, you dont understand much about that either.

Figure 3 5)You will see a bar that says CREATE DATABASE, type the name of the database. Figure 2. Since this is an ecommerce site, I will choose the name Ecommerce_site. Press Enter.

Figure 4

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 8 6) It will take you to a screen that will prompt you to create a new table for your database. Here it will also prompt you to enter the number of fields. Here I will do a simple products table. Below is a graphical display of the products table with the relevant categories: Figure 5: PRODUCTS TABLE Description Umbrella Jacket Blouse Black t-shirt Diamond Pendant Item# 9999 9296 8918 7902 9268 Category Accessories Outerwear Womens Shirts Jewelry Price 12.99 20.00 25.00 15.00 999.00 Figure 5: For this products table, the fields are description, item#, category, and price.

Okay, they are random categories, and the items wouldnt likely be grouped together like this (but this is just for an example.) In the next step I am going to show you how you would create this database.

Figure 6: In order to enter these into your database, you must first enter the name of your table and the number of fields. In the case above, you would enter Products as the table name and 4 as the number of fields. And press Go. Figure 6

Filling out the table categories

Now it takes you to a screen where you will type the categories of each of the fields.

Figure 7: This is where you type the catgories. you will use.

Figure 7

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 9

Figure 8: You can fill out these fields however you would like to, but I filled them out for the products table, and included a heading for each category.

Figure 8 We are not typing in vaues yet. We re creating the the homes, or boxes the values willl live in. It is important to realize that each field can only hold one type of value. An integer (INT) would hold one type, a variable character (VARCHAR) i.e. or word, or phrase, would hold another different type. A FLOAT (decimal) would hold a third type such as a price. Also, each value must have a length, or how many characters the database will allow it to be (something that would hold the number 1000 would have a length of 4). It also has a value that tells the database whether it will accept NULL values, which are not values at all, but are place holders. 8) As you can see, I have filled in the attributes field, type, length, values, and collation. For English speakers, latin1_swedish_ci is the correct collation, because English has no special characters. 9) Phpmyadmin will prompt you as to whether you want to save, or add more fields. In this case you will save. After you do this, it will take you to a screen that shows you these fields: Figure 9: Completed fields.

Figure 9

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 10 11) Now its time to enter the values. Goto the INSERT tab at the top of the screen. FIGURE A, Figure B Figure C Figure 10: Before what you were doing was creating the boxes that the values would go in; now you are actually

After you press enter, you will see this message:

Figure 10

Figure 11 12) As you can see in figure 11, once you have entered your rows, you are sent to another screen, where there is some code that looks like this: INSERT INTO `Ecommerce_site`.`Products` (`Description`, `Item_num`, `Category`, `Price`) VALUES (Umbrella, 9999, Accessories, 12.99), (Jacket, 9296, Outerwear, 20.00), (Blouse, 8918, Womens clothes, 25.00), (Black T-shirt, 7902, Shirts, 15.00), (Diamond Pendant, 9268, Jewelry, 999.00); That is the data manipulation language of SQL, and you have seen the first kind of actual SQL that you will

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 11 see. This is what is actually going on under the hood of SQL but you can learn how to do this as well, and then enter into the database directly. Basically, this may require a little explanation, but after you understand the concept, it will be easier. phpmyadmin is automatically inserting records into the site you set up. First its inserting into ecommerce database, products table then in parentheses its telling the phpmyadmin which fields (or boxes, as it were) to put the Values. After this, there is another set of parentheses that give the actual values that it has inserted. This is very useful to know, because it can help you set up a database in SQL from the beginning. If you wanted to do that, feel free to look at the appendix, you can also get a book on SQL syntax. Next I will show how this current database can be used in a useful way to learn to make queries with databases.

Part VI: SQL Queries in Action


If youve made it this far, congratulations! You are on your way to learning Mysql. You have at least become familiar with how to set up a database in phpmyadmin, and have become a little familiar with the code. Now you will learn some of the basics of queries, and learn the meanings of many of the common syntax types of Mysql (and SQL, by extension). If you have created the databases, then you will be able to do the queries as well. If not, dont fret, there are screen shots included so you can still follow along. If you have created the tables, you should open phpmyadmin and go to the SQL tab at the top, where you will be able to enter the queries as stated in the text.

6.1 The Generic SELECT statement


The SELECT is the cornerstone of Structured Query Language. This is because it is what allows you to query in the first place. Here is an example of a simple select statement: SELECT * FROM products WHERE price > 20; Lets break this down a little. SELECT * means to select everything, meaning, all categories in the products table. It wouldnt necessarily useful to ask for everything though. There is another term in the syntax that gives a limit to this query, making it more useful. The clause WHERE price > 20 indicates that the search should be limited to those products that have a price greater than $20.00.

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 12 Here is the result of the query: SELECT * FROM products WHERE price > 20;

6.2 SELECT specific categories: GROUP BY, desc, and asc


The SELECT * statement in action.

Figure 12 Sometimes you may not want to select everything, at these times you most likely want to select only specific categories. Heres a way you can do that with the preceding database: SELECT firstname, lastname FROM person GROUP BY lastname desc; This code will do two things both selects only the firstname and lastname, and also puts the categories in reverse alphabetical order, and the GROUP BY statement makes the result ordered by last name. An alternative is the asc (or ascending) statement.

Figure 13

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 13

6.3 JOINS and aliases: helps you make meaningful connections


Joins are a very useful part of SQL. Consider the different tables weve been working with: Products table: fields- Description, Item_num, Category, price. Person table: fields firstname, lastname, streetaddress,city,state, zip, person_num. Purchases table: fields- Item_num, Invoice_num, person_num, quanitity, purchase_date, ship_date, paid_ date. Personal Info table: fields- creditcardnumber, threedigitcode, username, password, personnumber. First of all, you might be asking why we would split them up this way. Its a good question, requiring a bit of explanation. First of all, the products table is not something that is connected to any particular person, so it must be independent. Likewise personal info could be stored in a protected table, where only certain people could access the credit card numbers. These are just a few reasons its useful to put these in separate tables. However, sometimes we want to see these tables side by side. Thats when joins are useful, and joins also can use aliases. Aliases are names we give tables to make them more manageable. A products table could be aliased as p, then to refer to the description category in the products table, we could say something like p.description. Suppose we wanted to merge three tables: we wanted the lastname description and price, but to do this, remember that the common link between the products table and person table is a purchase. Then depending upon the aliases used (in this case I used c for person (customer) since theres more than one p) the join would look something like this:

SELECT lastname, pr.description, pr.price FROM person c inner join purchases p on c.person_num = p.person_num inner join products pr on p.item_num = pr.item_num ORDER BY c.lastname;

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 14

6.4 Further Explanation of Join Example

The result of the query:

SELECT lastname, pr.description, pr.price FROM person c inner join purchases p on c.person_num = p.person_num inner join products pr on p.item_ num = pr.item_num ORDER BY c.lastname; You will noticethat this statement merges two tables that are connected by a single table purchases. Figure 14 A key for this statement: pr.description: this is taking description category from the table products. The aliases in the statement are: pr for products, c for person, and p for purchases. For the tables person and purchases and the inner join brings them together when the person id is the same. For the tables purchases and products the inner join brings them together when the item number is the same. Therefore, the person is connected to their products by way of their purchases. No information from the purchases table is displayed though. This is accomplished because the tables are joined, however only the three categories lastname, pr.description and pr.price are selected for display.

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 15 GOOD JOB! Joins are hard. Now that you have some idea of what JOINS are, I would like to introduce you to a few more useful tools to use in database queries. AGGREGATE FUNCTIONS and the LIKE statement.

6.5 Aggregate Functions:

(adapted from Murachs PHP and Mysql pg. 595) AVG (category) . . . Gets the avg of all of a specific category. SUM (category) . . .Gets the sum of all of a specific category. MIN (category) . . .Gets the minimum of all of a specific category. MAX (category) . . .Gets the maximum of all of a specific category. COUNT (category) . . .Gets the Count of all of a specific category. COUNT (*) . . .Gets the count of all values. For instance: SELECT MIN(price) AS lowestprice; MAX(price) AS highestprice; AVG(price) AS averageprice; from products; This would return the following table:

Figure 15 Notice the AS clause. This essentially is a way to rename the price, since its no longer applicable to just call it price. However, you could use the AS clause to rename any field you were selecting such as saying SELECT lastname AS name; This would allow you to rename any field into another name.

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 16

6.6 The Like Clause:


The following should help you understand the LIKE clause (and perhaps even like it!) . . .suppose you were trying to remember the name of a band to tell your friend but you could not remember the name. It sounds like Melana. With sql you could use the LIKE clause and say SELECT * FROM bands WHERE name LIKE %ana; Amongst those entries you would find nirvana because the wildcard % was placed at the beginning. Likewise, if you wanted to find anything related to Pink you could use SELECT * FROM bands WHERE name LIKE PINK%; This would get you a bunch of entries such as Pink Floyd, Pink Fairies, and Pink. For the person database, we could use: SELECT * FROM person WHERE lastname LIKE Sing%; would get you John Singleton Likewise, by extension: SELECT *FROM person WHERE lastname LIKE %ENT%; would get you the result:

Figure 16

result: Amy Benton

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 17

Part VII: Conclusion


Well, I hope youve enjoyed this lesson on SQL and phpmyadmin. If you have questions there is a very large online community who support Mysql and who definitely desire to help others to learn it. Also, if youve gotten this far, and you still think SQL is cool, congratulations, you should know that theres some decent money to be made in database administration.

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 1

APPENDIX
SETTING UP A DATABASE USING SQL
Remember the part where you entered everything into tables? We are going back so we can set up the database with SQL. If you wanted to do that, you would go to the top tab on phpmyadmin and you would select SQL. Into the command prompt box you would enter For the sake of this illustration, since the table Products already exists, I will use products_test to show how this works. CREATE TABLE Products_test ( Description VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL , Item_Num INT(6) NOT NULL, Category VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL , PRICE FLOAT(6) NOT NULL ) ENGINE = MYISAM Successful test graphic NOW THAT YOU HAVE SEEN A LITTLE OF DATA MANIPULATION LANGUAGE, WE SHOULD BE ABLE TO FINISH THE TABLES. The table we already set up is Products table: fields- Description, Item_num, Category, price. The other tables are : Person table: fields firstname, lastname, streetaddress,city,state, zip, person_num. Purchases table: fields- Item_num, Invoice_num, person_num, quanitity, purchase_date, ship_date, paid_ date. Personal Info table: fields- creditcardnumber, threedigitcode, username, password, personnumber.

SQL PRIMER FOR TECHWRITING ENG 307 -- pg. 1 These are set up the same way, through the two step process of Creating a table, then INSERTING values into that table. To save time I will give an example of doing one table with SQL, then I will do the rest of the tables with the phpmyadmin. Feel free to try them yourself, if you want to, or you can just follow along. CREATING THE PERSON TABLE 1) First we want to open the SQL tab on phpmyadmin (make sure ECOMMERCE-SITE is selected on the side tab, or you might be creating a table in another database.) 2) Then well put in the SQL to create the table: CREATE TABLE person ( firstname VARCHAR(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL , lastname VARCHAR(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL , address VARCHAR(30) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL , city VARCHAR(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL , state VARCHAR(2) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL , zipcode INT(5) NOT NULL , person_num INT(7) NOT NULL ) ENGINE = MYISAM This looks like a lot, but really its just the same thing over and over. We needed several of the same types of entered values. Firstname, Lastname, Address and City are all the same type. State was limited to two letters, so there can be consistency, and zipcode were limiting to 5. Now if we complete the SQL for values of person, this is the result: INSERT INTO `Ecommerce_site`.`Person` (`firstname`, `lastname`, `address`, `city`, `state`, `zipcode`, `person_num`) VALUES (Abbey, Smith, 315 State Street,Louisville,KY, 40220, 100112), (John, Apple, 4000 Dixie Hwy, Louisville,KY,40312,101113), (Lee, Singleton, 15 Jesuit Way, Boulder, CO, 80301, 101114), (Amy, Benton, 143 Mary Lane, Bowling Green, KY, 42101, 101115), (Polly, Harvey, 190 Kensington Ct, London, England, 45654, 10799) In this instance, there is no error, however, if you encounter an error, it might be in the quotes. Notice the first quotes must be backticks (this is located on the top left of your keyboard), and the second quotes must be single quotes . If they are apostrophes, slightly slanted, this will not work. This can happen if you simply cut and paste your data and replace it with new data from a previous SQL entry. Sometimes it doesnt enter correctly.

SOURCES: Book Sources: 1) Harris, Ray, and Mike Murach. Murachs PHP and Mysql. United States: Mike Murach and Associates: 2010. 2) Markel, Mike. Technical Communication. New York: Bedford St. Martins: 2010. Other sources: 1) Lindsey, Jim. Mysql CIT 436 Blackboard Lecture. Bowling Green 2011. Photocredits:
Nerdy front cover image: inspiredeater.com Under the hood image: rides.musclemustangfastfords.com Ladies restroom icon: http://www.mricons.com/show/ladies-icons City icon: iconarchive.com Street sign icon: iconarchive.com Ky state icon: istockphoto.com Number sign icon: hash sign from twitter. Drawer icon: iconarchive.com Shopping cart icon: id18536.securedata.net Description icon: stockphotos.it Money symbol icon: history.nd.gov Cash register icon: moi-meme.com Calendar icon: iconarchive.com Rocketship icon: easyvectors.com Credit card icon: iconarchive.com Username icon: savoot.com Password icon: ratedapartments.de exercising man: scientificamerican.com yawning women: psychologytoday.com Different height kids: almightydad.com Wedding: styleityou.com); Villain: netpowersystem.com Like OMG Hailey Williams: Fanpop.com

Você também pode gostar