These functions provide administrators or people querying the database a way of performing complex tasks within a query. Some of these tasks may be string manipulation or complex mathematical equations. This allows users to take advantage of the more powerful processing power of a database server.
MySQL provides a lot of intrinsic functions. Today, you will learn
Programming at the server level and how MySQL can help
Basic functions, such as numerical operations, string maniplations, and logical conditions
Programming at the Server Level
A lot of discussion goes on about where to put the programming logic. Do you let the application that is installed on the user's desktop do all the heavy processing, or do you let it take place on the database server? What about a third place—a middle tier? The answers to these questions are not easy. A lot depends on the application and the purposes for which it is used. A traditional Web-based application has different needs than a three-tier application. These needs vary from heavy database traffic, as in a Web-based store, to heavy data manipulation, as in a finance application. The remainder of this section takes a look at some scenarios and explains why and where the best place to put the logic is.
The first example is a Web-based application, such as the Meet-A-Geek Web site. The following are the general steps in a Web-based application:
1. A user types the URL of a Web site into his or her browser.
2. The browser then finds the server and requests a Web page.
3. The Web server answers the request and sends the page.
4. The browser receives the page.
5. The user navigates around the site repeating steps 1–4 until he or she gets to a page that requires some sort of database access (a form or dynamically generated page).
6. A program or script runs, getting the requested data from the database server.
7. The page is then returned to the user.
As you can see, there are a lot of trips back and forth to the database and Web servers. Now imagine a site that receives a lot of traffic—the trips and requests to and from the server could be astronomical.
To help ease the load on the servers, you could add the programming logic at the client. The client is the computer or application that is either receiving or sending the requests. The more logic you can place at the client level, such as error handling or string manipulation, the better off you are.
Now don't think you can't use any intrinsic functions on the database server—you can, just consider the size of your database and the amount of traffic you think that your site is going to have. If you have a small site with a relatively small amount of data, you are pretty free to do anything you want. But if your site is extremely busy and you need a lot of calculations done or a lot of string manipulation needs to be provided, think about placing this logic on the client.
- 85 -
In the Enterprise, things may be a little different. At the Enterprise level, you may have many client machines accessing your database via an application. This application could be written in Java, Visual Basic, C++, or any other programming language. The application talks to a middle tier. This middle tier handles all the business logic. It accesses the database and returns requests to the client. The middle tier is an application too. Its responsibility is to process the requests and send them to the appropriate places.
The following is the flow in a three-tier design:
1. A user uses a front-end application and issues a request.
2. The request is checked for any errors and sent to the middle tier.
3. The middle tier receives the request, performs any business logic, and passes it on to the database.
4. The database performs its function on the request—whether it is to return a query from a SELECT statement or update some rows. It then passes the data back to the
middle tier.
5. The middle tier performs any other logic that is required and returns the request to the client.
6. The client receives the data and displays the results to the user.
The majority of the business logic is done at the middle tier, while simple parsing and error handling is done on the front-end application. Generally, the database server's only responsibility is to serve up the data that is requested or manipulate the data that it stores. In this type of architecture, the load is balanced between all tiers. Each is responsible for its own duties. The front-end application is pretty much a display tool—a way for the user to interact with the data. The middle tier is responsible for all the heavy logic and also handles the requests and connections to the database. The database does what it does best—serve up data.
|