Having Your Database Do The Work… not ColdFusion!

USING YOUR DATABASE TO DO THE WORK

A lot of people have asked me "what can I do to make the database work for me, instead of ColdFusion?".  The truth is that any veteran developer will tell you that making the database do the work, is the best practice in web development. I can think of a few different ways to make the database work for you.   By this I mean telling the database to insert values, create tables and much more. In this tutorial I will show you the basics of how to make your database do the work for you! let's begin.

In this example we will be using an access database to have the current date and time be automatically inserted, when a new record comes in.   Traditionally you had to have ColdFusion insert the date with the command #CREATEODBCDateTime#, but making the database do the work is much faster and way more reliable.  Let's show you how it's done..

The first thing you will need is a date/time field in your table.  call this field:
PostDateTime

Set the value of the field to be Date/Time (for more see picture below).
 Creating The Initial Fields...
Notice that the field is called PostDateTime and the data type is set to Date/Time.  The next thing we need to do is tell this field to automatically place the current date/time when a new record is inserted into the database,  to achieve this you simply make the following addition to this field:
 Adding a simple command to cut your work in half!
Notice that in the default value, I simply added the command Now(), this let's the database know that when a record is inserted that it should automatically add the current date/time. Let's see the end result within the database.
 Final Results!
Notice that even though there isn't a value in "Name", the date time field is automatically there.  If you close and reopen the table, the date changes to the current date.  So when a record is inserted it keeps that date and you can therefore use it to display on your page with the DateFormat and TimeFormat functions.

This is just a simple way of making the database do the work for you, instead of making ColdFusion (or whatever other language you use) do extra work to achieve the same result.

Congratulations, you now know how to make the database do the work, instead of your development language.

All ColdFusion Tutorials By Author: Pablo Varando
Download the EasyCFM.COM Browser Toolbar!