After toying with the syntax and different suggestions from many other stackoverflow questions/answers, I finally found the right combination of small code additions to have automatic "created" and "updated" columns in my database table.
In my hook_schema() function:
$schema['table'] = array(
'fields' => array(
'created' => array(
'mysql_type' => 'datetime',
'not null ' => TRUE,
'default' => format_date(time(), 'custom', 'Y-m-d 00:00:00'),
),
'updated' => array(
'mysql_type' => 'timestamp',
'not null' => TRUE,
),
),
//other parts of the schema (indexes, keys...)
);
In my hook_install() function:
//Automatically set created datetime for current timestamp upon insert
$query = db_query('
CREATE TRIGGER triggername BEFORE INSERT ON tablename FOR EACH ROW BEGIN SET
NEW.created=NOW(); END
');
//Automatically update 'updated' to current timestamp whenever a row is changed
$query = db_query('
ALTER TABLE tablename
MODIFY updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
');
The 'triggername' is whatever you want to name your trigger. The 'tablename' is the name of the table you established in hook_schema() that has the created and updated columns.
Hopefully this can be of use to somebody. When I was looking into this a couple weeks ago, there were no posts with someone doing this within drupal and it was very difficult to figure out how to adapt the different suggestions to the drupal framework. This is the best solution I figured out. If you have another one you believe to be better go ahead and post it!