![]() | |||||||||||||||||||||
![]() |
|
|
You Gotta Love SQL Once either the User or System data source has been configured we can begin creating programs utilizing SQL to access a databases. The reference to SQL can either be pronounced S Q L or Sequel as tribute to the first relational database language actually known as Sequel. SQL (Structured Query Language) is a language standard for querying and manipulating databases. It allows for programmers to easily switch between various databases by standardizing the commands needed to perform the necessary tasks. SQL is comprised of fairly basic, straightforward commands that can be used to accomplish complex events. The language is broken up into threeparts, DDL (Data Definition Language) which creates, modifies, and deletes tables, DML (Data Manipulation Language) which works with the actual data contained in the databases, and DCL (Data Control Language) which controls security on database objects. SQL is a case-insensitive language, but most of the time the SQL commands will be coded in uppercase letters to distinguish it from the data and surrounding code. Retrieving Data from the Database Here is a simple command that will pull only the first name and last name of the students in a particular database.
If we wish to qualify the list so that only students from Omaha are returned we add the WHERE command. This selects all the students from the database WHERE the city field is Omaha. The ORDER BY command will place the selected entries in alphabetical order first by their last name and then by their first. SELECT firstname, lastname FROM students Adding Data to the Database Before we can retrieve data from the database, we must have data populating the database. Here is a simple command for inserting data into the SQL database: INSERT INTO Students (lastname, firstname) The values of John and Smith are placed in the Students database in the lastname and firstname fields. If the values of "John" and "Smith" were reversed they would not be placed in the correct fields in the database. The field order must correspond to the value order. Deleting Data from the Database Deleting data from the database is an easy task as well.
All the students that have a graduation date of 1999 will be deleted from the Students database. Updating the Database Old Value: ShotneyNew Value: Chuney UPDATE Students In this instance someone had mistakenly misspelled Chuney’s name as Shotney. To correct this error an update needed to occur as evidence by the UPDATE SQL command. This only works, however, if there is only one member in the database with the last name of Johnson. If there are multiple instances of "Johnson" in the database then it would update all the first name fields of all the Johnsons. To make sure that this type of error does not occur a primary key field must be identified. A primary key field is a field that can only correspond to one record. A good primary field key to set up in the case of a student database would be the Social Security field. The commands that control creating databases, deleting databases, deleting all the information contained in a database are also very simple SQL commands. Back to the BASICs Ok, so we all remember the Commodore 64s and Apple IIs and whatever else we had when we were younger on which we sometimes programmed. The code looked something like this: 100 Print "Hi" Well, BASIC has improved and has now become Visual. One of the three great strengths of Visual Basic is the ability to easily write programs. Well, I guess that was always a strength. Another strength of Visual Basic is the ease of connecting to databases. Access, FoxPro, SQL Server, you name it and chances are there is some way you can easily write a few lines of code to obtain data from these databases and utilize it within your programs. The two ways of connecting to databases are through the Microsoft Jet engine or through ODBC. The type of connection we will be focusing on is ODBC. Since we already have a working knowledge of SQL and we know how to set up a data source we can start writing programs with Visual Basic to access a database.
|
Section 2: SQL and Stuff Section 3: VBPower! Section 4: ASP Results Printable Version
| ||||||||||||||||||||||