vb6 and sql tutorial
Post on 28-Nov-2014
Embed Size (px)
Working with DatabasesNo matter what programming language you use at some point and time you will need to access a database. While you don't have to be a database whiz you should at least, know the basics of working with databases. The first thing you will need to understand is the basics of SQL (Structured Query Language ). SQL allows users to access data in relational database management systems, such as Oracle, Sybase, Informix, Microsoft SQL Server, Access, and others, by allowing users to describe the data the user wishes to see. SQL also allows users to define the data in a database, and manipulate that data. This tutorial is not a Database programming or Database Design tutorial. Its purpose is to give you a basic understanding of SQL so you can work with databases in your applications. Whether your language of choice is VB, C/C++ or Java. Or your database of choice is Access, Oracle, Microsoft SQL, MSSQL or MySQL. This tutorial should help you with accessing databases. For the purpose of this tutorial I will be demostrating examples in VB and Java. These examples have be compiled using VB5/6 and Java 1.3.1 and Access 97. SQL stands for "Structured Query Language". This language allows us to pose complex questions of a database. It also provides a means of creating databases. Many database products support SQL, this means that if learn how to use SQL you can apply this knowledge to countless other databases. SQL works with relational databases. A relational database stores data in tables (relations). A database is a collection of tables. A table consists of a list of records - each record in a table has the same structure, each has a fixed number of "fields" of a given type. SQL also has commands to create tables, to add records, to delete records and to change the value of fields of existing records; you can specify read and write permissions for other users; you can commit and roll back transactions; you can add and delete fields from existing tables, specify indexes and create views. SQL is easy to learn and because of that, with the information I provide here you will be able to add, update, retrieve and delete records in any database. Element K has two SQL self-study courses as well as two
instructor-led courses. If you have access to the self-study course I would recommend that you take a look. The SQL courses links below. SQL Fundamentals of Querying SQL Advanced Querying
SelectWhen querying a database you must tell the database what record(s) you want to view, add, edit or delete. You do this by using the Select statement. With "Select", you can tell the database what table you will be querying. Say we have a database that has a table named Employees and has the following records. FistName LastName Address Phone SSNumber To retrieve the Employees table all we have to do is query the database with the following statement. Select * From Employees; The wild card character (*) allows you to retrieve all the records in a table with out listing them one by one. With out the wild card the statement would look like the statement below. Select FirstName, LastName, Address, Phone, SSNumber From Employees; Lets say we only want a list of employees first and last names, the statement would look like this: Select FistName, LastName From Employees;
FromYou can not have a SQL statement with out using "From", since the From clause tells the database what table you are querying. If you just typed, "Select FirstName, LastName" the database would not know where to find that information. So the "From" clause tells the database what table you are selecting your records from. So the statement below tells the database that you are selecting all the records in the Employees table.
Select * From Employees
Quick Test Using the table below use the Select From statement to retrieve the employees first and last name and SS# Employees FirstName LastName Address John Doe 123 Elm St Jane Doe 5525 Oak LN John Smith 3325 LakeView Dr Jane Smith 882 S Main
Phone 408 225-0000 415 332-1250 880 557-5484 303 377-5774
SSNumber 223-23-1631 332-22-6657 552-11-3321 441-55-8141
Conditional SelectionThere will be times when you want to filter your queries to the database, this can be done using "Where" and "Like" and by using the "And" and "Or " operators.
Where clauseThe "Where" clause is used as a filter. So if you only want to retrieve records "Where " the last name is Smith then the Select statement would look like this. Select * From Employees Where LastName = 'Smith'; As you can see we used the equal sign (= ) to tell the database the last name should equal Smith. Below is the list of Conditional selections. Also you will notice that I used single quotes around the name, This is required when querying a string. = or != (see manual) < > = Equal Not Equal Less Than Greater Than Less Than or Equal To Greater Than or Equal To
Lets say we want to retrieve a list of employees who's first name is John. Select * From Employees Where FirstName = 'John';
Like operatorThe Likeoperator like the Where clause filters your request to the database. Say you only want to view a list of employees who's last name start with a "M" or end with a "S", then you would use the Like command with your Select statement. Select * From Employees Where LastName Like 'S%'; The above statement tells the database that you want a list of all employees who's last name starts with a "S". In SQL the percent(%) key is used as a wild card. So you will get a list of employees who's last name starts with a "S", no matter what precedes the "S" it could be numbers or letters. Once again it will depend on your database as to what wild card you use. If you are accessing an Access DB then the asterisk (*) would be used. Select * From Employees Where FirstName Like '%W'; The above statement tells the database that you want a list of all employees who's first name ends with a "W". Notice that the percent(%) key is now in front of the letter we want to filter. If you want to find an employee who had a "D" in there last name you would use the following statement. Select * From Employees Where LastName Like '%D%'; Other possibilities of using LIKE, or any of these discussed conditionals, are available, though it depends on what DBMS you are using; as usual, consult a manual for the available features on your system, or just to make sure that what you are trying to do is available and allowed.
And & OrThe AND operator joins two or more conditions, and displays a row only if that row's data satisfies ALL conditions listed (i.e. all conditions hold true). The OR operator joins two or more conditions, but returns a row if ANY of the conditions listed hold true.
To further discuss the SELECT statement, let's look at a new example table EmployeesStats Benefits 15000 15000 15000 12500 12000 10000 10000 7500 7500
EmployeeID 010 105 152 215 244 300 335 400 441
Salary 75000 65000 60000 60000 50000 45000 40000 32000 28000
EmpPosition Manager Manager Manager Manager Staff Staff Staff Entry-Level Entry-Level
Lets say we want a list of employees who has a salary over 40k and is part of the staff, you would use the following select statement using the And operator. SELECT * FROM EmployeesStats WHERE Salary > 40000 AND EmpPosition = 'Staff'; Lets say we want a list of employees who make less then 40k or have benefits less then 10k. We would use the select statement using the Or operator. SELECT * FROM EmployeesStats WHERE Salary < 40000 OR Benefits < 10000; You can also combine the And and Or operators to further filter your query. Lets say you want to a list of employees how are Managers and have a salary over 65k or have benefits over 12.5k. You would use the following select statement. Select * From EmployeesStats Where EmpPosition = 'Managers' And Salary > 65000 Or Benefits > 12500; First, SQL finds the rows where the salary is greater than $65,000 and the position column is equal to Manager, then taking this new list of rows, SQL then sees if any of these rows satisfies the previous AND condition or the condition that the Benefits column is greater than $12,500. Subsequently, SQL only displays this second new list of rows, keeping in
mind that anyone with Benefits over $12,500 will be included as the OR operator includes a row if either resulting condition is True. Also note that the AND operation is done first.
Quick Test Using the above Employees table what would be the results of the three(3) select statements?
Final words on SQLWe will rap up this quick introduction to SQL by demonstration Adding, Editing and Deleting data in the database. Adding Data When working with a database you will need to add data at some point. To add data you will use the Insert Into statement. When inserting new records you will also need to use the Value command. Using our previous Employees table we will use the statement below to add and employee to the Employees table. Insert Into EmployeesStats Values (500, 35000, 10000, 'Staff'); The above code will add EmployeeID with the number of 500, Salary of 35k , Benefits of 10k and EmpPosition of Staff. You may also notice that the order of insertions are according to the way the database is set up. Once again when strings are involved single quotes are used.
The Updated Database EmployeesStats Benefits 15000 15000 15000 12500
EmployeeID 010 105 152 215
Salary 75000 65000 60000 60000
EmpPosition Manager Manager Manager Manager
244 300 335 400 441 500
50000 45000 40000 32000 28000 35000
12000 10000 10000 7500 7500 10000
Staff Staff Staff Entry-Level Entry-Level Staff
Depending on how the database was designed the record can either be placed at the end of the records, as it did here.