![]() |
|
|
So, you've connected to your favorite database, now what? Well, you've got to be able to tell your database what you want to do, and to do that, you have to speak its language. All relational databases speak in a language known as SQL, or Structured Query Language. MS-SQL Server speaks in SQL; Access speaks in SQL; Oracle and Informix speak in SQL. It's good to know SQL! SQL is basically good for two things: updating/deleting/inserting information, and retrieving information. Let's look at retrieving information first. To tell a database that you want to retrieve some information, you've got to supply the following information: what columns to get from what table. That's all that is required, although you can be more specific. For example you can limit data on certain boolean conditions; we'll deal with this later. So, the way you tell your database that you want to select a column from a database is by using a SELECT statement. A SELECT statement takes the following form: ColumnList is a comma-delimited list of columns in the table specified by TableName. To select all of the columns, use an asterisk (*). Now, let's say we had a database which had a table named Products, and Products had three columns: ProductID, Name, and Price. If we wanted to get the ProductIDs of all products, we would simple issue this SQL query: It's that simple. The thing to remember with relational databases is that sets of information are returned. So in the above example we are getting back all of the ProductIDs from the Products table. If we wanted to get all of columns from the Products table, we would need but issue the following command: We can limit the set of data being returned from a query by using the WHERE clause. The WHERE clause comes after the TableName in the FROM clause. WHERE statements must evaluate to boolean expressions (expressions that return either true or false). Here are some examples of WHERE statements on our Products table example:
Get all of the names of the products that are priced at 5 dollars:
Get all of the columns from the products that do not equal 5 dollars:
Get all of the columns from the products that are less than or equal to 10 dollars:
Get the price for all of the products whose name is "Chair":
I hope that the above examples make sense. There are many other clauses in the SELECT statement which are
optional (remember - the only two required ones are
Updating/Inserting/Deleting Data
UPDATE The WHERE part is very important. If you don't use it, you basically are saying to the database, UPDATE every row in the table TableName! So, back to our Products example, the following command: Would set EVERY product in the Product table at a price of 5 dollars. This is probably not what we want to do. Using the WHERE clause, though, will allow us to update a single row, or a smaller set of rows. Would set all products whose name was Chair to a price of 5 dollars. You can set more than one column at a time, just delimit the list with commas:
DELETE 99.99% of the time you will definitely want to have a WHERE statement here. Again, it is optional, but if you were to issue: all of the data in the table would vanish in a poof of smoke. Perhaps you might want to do something like: which would delete all products with a price tag greater than 100 dollars. Or maybe you want to just delete the product with ProductID 88: In any case, be sure to be careful when using DELETE. I've gotten myself into trouble more than once by forgetting to use a WHERE clause!
INSERT ColumnList is a comma-delimited list of columns in the table in any order. The ValueList is a comma-delimited list of values corresponding to the columns specified in the ColumnList. Let's look at a quick example: This would add a row to the Products table with a ProductID of 10, a Name of "Sofa" and a Price of 90 dollars. Just be sure to match up your ColumnList values and ValueList values. For example, if your ColumnList is ProductID, Name, Price, make sure that your ValueList has the value that you want to insert for the ProductID first, then for the Name, then for the Price. Well, you now have a better understanding of how to talk to your database! You're definately a bit wiser now, and a bit more lingual! :) Happy Programming!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||