Hi, I am a MySQL newbie but lots of experience in other dbms systems. I am converting an Access DB / Excel SPreadsheet Application into MySQL as a learning experience in mySQL and to make sure i know the gotchas before starting the bigger application.
My problem.
I have an invoiceheader table ( see fig 1 below )
I want to set a default date on the invoicedate field (e.g. invoicedate=Today() )
I know it cannot be done with a date / datetime field as it needs a constant.
I created a before insert trigger to get around this (fig 2)
My trigger does not seem to run on my insert statement (fig 3). when run from phpMyAdmin unless I manually change the SQL
I cannot see it in the triggrs table of mySql either (see my query fig 4 below)
I am logged in as root via phpMyAdmin but no errors come back on the trigger creation or the insert run. I have god like access to everything - no holds barred - as both root / su on linux and as root in mySql via phpMyAdmin (fig 5).
Table creation, dropping, insert and update all no issues.
Any thoughts or help appreciated. I also tried it as an after insert as well.
fig 1 - The table
-- Table structure for table `invoiceheader`
--
CREATE TABLE IF NOT EXISTS `invoiceheader` (
`id` int(10) unsigned NOT NULL auto_increment,
`custid` int(11) NOT NULL,
`invoicedate` date default NULL,
`paybydate` date default NULL,
`totalincl` decimal(10,0) default NULL,
`gstamount` decimal(10,0) NOT NULL,
`emailedto` text NOT NULL,
`isposted` tinyint(1) NOT NULL default '0',
`isprinted` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `custid` (`custid`,`invoicedate`,`isposted`,`isprinted`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='holds details of an invoice' AUTO_INCREMENT=6 ;
--
fig 2 - The trigger
DELIMITER //
CREATE TRIGGER defaultinvoiceDate BEFORE INSERT ON invoiceheader
FOR EACH ROW BEGIN
SET new.invoiceheader = now();
END
DELIMITER ;
- fig 3 - The insert
This insert generated by phpMyAdmin
INSERT INTO `ajp`.`invoiceheader` (
`id` ,`custid` ,`invoicedate` ,`paybydate` ,`totalincl` ,`gstamount` ,`emailedto` ,`isposted` ,`isprinted`)
VALUES (NULL , '6', NULL , '2011-06-29', '33', '3', 'test@test.com', '0', '0'
);
I manually changed it to the following to make sure no null is manually added.
INSERT INTO `ajp`.`invoiceheader` (
`id` ,`custid` ,`paybydate` ,`totalincl` ,`gstamount` ,`emailedto` ,`isposted` ,`isprinted`)
VALUES (NULL , '7', '2011-07-01', '66', '6', 'test2@test2.com', '0', '0'
);
and tried third time with an explicitly empty invoicedate field
INSERT INTO `ajp`.`invoiceheader` (
`id` ,`custid` ,`invoicedate` ,`paybydate` ,`totalincl` ,`gstamount` ,`emailedto` ,`isposted` ,`isprinted`)
VALUES (NULL , '8', '', '2011-06-29', '99', '9', 'test3@test3.com', '0', '0'
)
It gives me a value but they are all zeros.
Results show as
SQL query: SELECT * FROM `invoiceheader` WHERE id > 5 LIMIT 0, 30 ;
Rows: 3
id custid invoicedate paybydate totalincl gstamount emailedto isposted isprinted
6 6 NULL 2011-06-29 33 3
test@test.com 0 0
7 7 NULL 2011-07-01 66 6
test2@test2.com 0 0
8 8 0000-00-00 2011-06-29 99 9
test3@test3.com 0 0
- fig 4 -
from the information schemea db.
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0124 sec)
SQL query:
SELECT *
FROM triggers
LIMIT 0 , 30;
- fig 5 -
-- version 2.11.3deb1ubuntu1.3
--
http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 20, 2011 at 01:06 PM
-- Server version: 5.0.51
-- PHP Version: 5.2.4-2ubuntu5.12
Edited 1 time(s). Last edit at 06/19/2011 08:28PM by Shane Hollis.