SQL Tutorial Version: 3.0 By: Josh Grant (jrgrant@ciusa.net) TOC === 1. Introduction 2. Revision History 3. Notes 4. Getting Started 4.1 How database are organized 5. The SQL Tutorial 5.1 Creating a table 5.2 Adding records 5.3 Retrieving values 5.4 Updating values 5.5 Deleting records 6. Misc. 6.1 Definitions 6.2 Examples 6.3 More Resources 7. Closure ################# | 1. Introduction ################# I hope to make learning SQL very simple and easy with this tutorial. This is the first real tutorial I have wrote so I am not that experienced with how to layout the document, how to update it and such, but I will learn as I go. In this tutorial I am going to go step-by-step. I am going to show you what information the database holds, and when I show a command I will show you what will be returned. This tutorial refers to using MySQL database software, which can be obtained for different platforms at no cost from http://www.mysql.com. ##################### | 2. Revision History ##################### This is a record of all the changes I made to the document throughout the different versions. Read this only if you are interested in what has changed since the previous versions. Version 2.0 - I added a TOC, fixed alot of errors. Formatted the document so it looks better in explorer. I also explain what a table is. Version 3.0 - Fixed a couple spelling errors. Beefed up the document with the 'Getting Started' section. Which will show you, well, how to get started. I also added a little introduction to each section sort of like books do, I don't know why, it just seemed right. I did not add an introduction to the introduction, that wouldn't be right... ########## | 3. Notes ########## These are some general notes to keep in mind throughout the tutorial. - For SQL commands I use all capital letters - I use all capital letters for the column names, so they can be distinguished from database entries - Email me at jrgrant@ciusa.net with any comments, correction or questions - I am still learning SQL, so there are bound to be errors ################################### | 4. Getting Started ################################### This section will get you started with using the SQL commands that you will learn in the next section. It will show you where to enter the commands, and give you a brief introduction to the layout of a database. You could skip this section if you are already somewhat familiar with databases. // This section will be completed shortly ##################### | 5. The SQL Tutorial ##################### Ahh, onto the meat and potatoes of the tutorial. This is what you actualy came for. Sorry to say right now it does not look like much more than a reference. Somewhere around version 4 this will be more of a 'guide' to SQL. # 5.1 Creating a table CREATE TABLE tblUsers ( USERNAME char(20), PASSWORD char(20) ); This creates a table named 'tblUsers' and creates two columns, Username and Password. Now here is what are database looks like: +===============================================+ | tblUsers | +===============================================+ | USERNAME | PASSWORD | +-----------------------+-----------------------+ # 5.2 Adding records INSERT INTO tblUsers (USERNAME, PASSWORD) VALUES ('JRGrant', 'secret'); Now here is what are database looks like: +===============================================+ | tblUsers | +===============================================+ | USERNAME | PASSWORD | +-----------------------+-----------------------+ | JRGrant | secret | +-----------------------+-----------------------+ Suppose you want to add more than one record at a time. INSERT INTO tblUsers (USERNAME, PASSWORD)(USERNAME, PASSWORD) VALUES ('BClinton', 'cigar')('BillG', 'monopoly'); Here is the updated database: +===============================================+ | tblUsers | +===============================================+ | USERNAME | PASSWORD | +-----------------------+-----------------------+ | JRGrant | secret | +-----------------------+-----------------------+ | BClinton | cigar | +-----------------------+-----------------------+ | BillG | monopoly | +-----------------------+-----------------------+ # 5.3 Retrieving values Let's say user JRGrant wants to log in. How do we retrieve his password to verify it is the same as the one he enters? SELECT PASSWORD FROM tblUsers WHERE USERNAME = 'JRGrant'; This would return 'secret'. # 5.4 Updating values Secret is not a very good password. What if we want to update it? UPDATE tblUsers SET PASSWORD = 'bettersecret' WHERE USERNAME = 'JRGrant'; Now our database looks like this: +===============================================+ | tblUsers | +===============================================+ | USERNAME | PASSWORD | +-----------------------+-----------------------+ | JRGrant | bettersecret | +-----------------------+-----------------------+ | BClinton | cigar | +-----------------------+-----------------------+ | BillG | monopoly | +-----------------------+-----------------------+ # 5.5 Deleting records Looks like BillG wants his account removed. This isn't hard DELETE FROM tblUsers WHERE USERNAME = 'BillG'; This is our database: +===============================================+ | tblUsers | +===============================================+ | USERNAME | PASSWORD | +-----------------------+-----------------------+ | JRGrant | secret | +-----------------------+-----------------------+ | BClinton | cigar | +-----------------------+-----------------------+ ########## | 6. Misc. ########## # 6.1 Definitions record - A record is a row in your table. It has a value for every column in your table. table - A table is exactly what it sounds like, think of tables in HTML. The HTML document (the database) can have more than one table. A table has columns (fields) and rows (records). # 6.2 Examples How to change every users password to 'password': UPDATE tblUsers SET Password = 'password' WHERE Username = *; How to delete every record: DELETE * FROM tblUsers WHERE Username = *; # 6.3 More Resources For a nice site to lookup SQL commands, check out http://comclub.dyndns.org:8081/sqlref/. ############ | 7. Closure ############ I hope you learned something from this tutorial. I will try to update it whenever I learn more. This is only version 3, maybe somewhere along the lines of version 5 this will be helpfull. Thanks to CoolerQ for hosting this document. To get the latest version of this document go to http://www.ciusa.net/~jrgrant/sqltutorial.txt or http://comclub.dyndns.org/li/tutorials/jrgrant/SQLTutorial.txt.