![]() |
|
|
Published: Thursday, September 09, 1999
Did you know that SQL allows set-like operations? Using sets, you can quickly delete a set of rows with one SQL statement, or update a set of rows using a single statement. Heck, you can even query data against a set of acceptable parameters! Sets are really cool things, and SQL let's you take advantage of the power of sets! You may be scratching your head, trying to remember what, exactly, sets are. We've all likely had a high school math course that dealt with fundamental set theory, but that was ages ago! :) Here are some set basics to help stir your memory.
{1, 3, 2, 5} and
{5, 2, 3, 1} are identicle sets.
{1, 2, 3}, we say that the set has a cardnality of three. The cardnality
refers to the number of elements in the set (which gets a tad difficult once you start dealing with sets
with an infinite number of elements, but that is a bit beyond this article!). Also, we say that 1 is an element in the set
{1, 2, 3}, as are 2 and 3.
That's really all the refreshing you'll need. SQL doesn't allow any complex operations on sets.
Rather SQL uses set notation to determine if a given value in a table is an element of a given set.
That is probably as clear as mud. Let's look at an example. We've probably all done SQL queries. Let's
construct a SQL query to search for all items in a
Using set notation, we can write that query with a bit more elegance. SQL uses the parenthesis to denote
the start and stop of a set. For example, the set
Here we are saying, "Get us all the records from the Products table where the cost of the product is in
the set
Using set notation, we can write that query with a bit more elegance. SQL uses the parenthesis to denote
the start and stop of a set. For example, the set
You can also use set notation for SQL
What if you wanted to write a SQL statement that would delete all of the records in a table, except
those with an ID of 5, 6, or 7? You can use set operators still! Instead of saying,
You can also use set notation for SQL
The above query will delete any rows from the table that do not have an ID of 5, 6, or 7.
The coolest thing about sets, in my opinion, is that you can generate a set in one of two ways. Through
hard coding it, like in any of our above queries, or, more powerfully, through a separate
What does this code do? Well, first look at the Hopefully you are thinking, "Sets are freakin' cool." I suspect, though, that many folks out there are thinking, "Yeah, it's neat and all, but I don't see why it's so useful." Well, I will tell you why: it makes batch deletes or batch updates through ASP pages very easy! I will demonstrate how to accomplish this in my next article! Heck, I just might talk more about set theory (a topic that interests me)! :) For now, go ahead and play around with the set notation, construct some queries, delete some data. Happy Programming!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|