Database Essentials - So That's Where All My Money Went...
(Page 9 of 9 )
Database servers like Oracle also come with the ability to create "stored procedures" at the database layer - essentially, blocks of program code that can be executed at will, similar to the user-defined functions you can write in Perl or PHP.
The advantage of creating such stored program blocks at the database level is very simple, and very powerful - all applications which interact with the database can use these stored procedures, thereby reducing the time and effort required for application development. For example, if your application uses both Perl and PHP to communicate with a database, you would need to write procedures to accomplish the same tasks in both languages. By writing the procedure once and moving it to the common point of intersection - the database - application code becomes easier to write and maintain, and your time is used more effectively.
For example, here's a simple procedure, again from Oracle's PL/SQL language, which would reduce a specified customer's balance by 10%:
sql> CREATE PROCEDURE reducebalance (ID IN int, Balance IN int) AS
BEGIN
UPDATE balance SET AccountBalance=(Balance-Balance/10) WHERE CustomerID=ID
END;
This procedure would accept a CustomerID and AccountBalance as
parameters, and use this information to update a database entry in the table. As you can see, this is similar to a Perl or PHP function, which accepts values as parameters and performs actions based on those parameters (incidentally, you can also have the stored procedure return a value to you, based on the results of code execution - just like a regular function)
And that's about it. I hope you found this information useful, and that it served as a good starting point for your journey into the world of databases. If you're interested in learning more, you should also take a look at the "Speaking SQL" tutorials at
http://www.devshed.com/c/a/MySQL/Speaking-SQL-part-1/ , and at the very interesting article on database normalization at
http://www.devshed.com/c/a/MySQL/An-Introduction-to-Database-Normalization/ . If you want to get right into building data-driven Web applications, take a look at PHP at
http://www.php.net/, or check out the PHP 101 series of tutorials at
http://www.devshed.com/c/b/PHP/ . See you soon!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |