I have created the following four tables: lookup_school, user, user_pwdhistory and user_activation
CREATE TABLE `lookup_school` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`Institution_ID` int(6) NOT NULL DEFAULT '-1',
`Name` varchar(128) NOT NULL,
`Address` varchar(128) NOT NULL,
`City` varchar(80) NOT NULL,
`StateOrProv` varchar(64) NOT NULL,
`PostalCode` varchar(10) NOT NULL DEFAULT '99999-9999',
`Country` varchar(4) NOT NULL,
`Enrollment` int(6) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `School_Name` (`Name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`userid` varchar(16) NOT NULL,
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
`emailaddress` varchar(255) NOT NULL,
`schoolid` int(6),
`institution` varchar(36) NOT NULL DEFAULT '999999',
`userpw` varchar(100) NOT NULL,
`owned` int NOT NULL DEFAULT 0,
`inbox` int NOT NULL DEFAULT 0,
`requests` int NOT NULL DEFAULT 0,
`friend` int(4) NOT NULL DEFAULT 0,
`notification` int(4) NOT NULL DEFAULT 0,
`events` int(4) NOT NULL DEFAULT 0,
`todo` int(4) NOT NULL DEFAULT 0,
`last_login` DATETIME,
`last_access` DATETIME,
`disabled` BOOL DEFAULT 0,
`reset_password` BOOL DEFAULT 0,
`reset_question` TEXT,
`reset_answer` TEXT,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`lastmodified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `notes_userid` (`userid`),
UNIQUE KEY `notes_email` (`emailaddress`),
INDEX `emailaddy_index` (`emailaddress`),
CONSTRAINT `user_fk1` FOREIGN KEY (`schoolid`) REFERENCES `lookup_school` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
--
-- Structure for password history table
--
CREATE TABLE `user_pwdhistory` (
`userid` int NOT NULL AUTO_INCREMENT,
`newpasswd` varchar(100) NOT NULL,
`modifytimestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX `pwdhistory_idx_1` (`userid`),
CONSTRAINT `pwdhistory_uid_fk1` FOREIGN KEY (`userid`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
--
-- Structure for account activations table
CREATE TABLE `user_activation` (
`id` int NOT NULL AUTO_INCREMENT,
`activationcode` text NOT NULL,
`activationname` varchar(50) NOT NULL,
`activationemail` varchar(255) NOT NULL,
`activationsent` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`activationdate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`activationstatus` enum('activated','waiting') NOT NULL DEFAULT 'waiting',
PRIMARY KEY (`id`),
INDEX `activation_emaii_idx` (`activationemail`),
CONSTRAINT `user_activation_fk1` FOREIGN KEY (`activationemail`) REFERENCES `user` (`emailaddress`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Then I created a trigger to execute some business rules that are applied when a new user is added so that an application doesn't have to
1. Update the school enrollment account
2. Insert a password history record for the newly added user
3. Insert an activation table record for the newly added user
Here's the trigger:
DELIMITER $$
CREATE TRIGGER `note_user_t2` AFTER INSERT ON `Notes_user`
FOR EACH ROW BEGIN
DECLARE actcode varchar(36);
DECLARE fullname varchar(60);
DECLARE emailaddy varchar(255);
DECLARE nuserid int;
DECLARE npassword text;
SET emailaddy = NEW.emailaddress;
SET nuserid = NEW.id;
SET npassword = NEW.userpw;
SET fullname = CONCAT(NEW.firstname," ",NEW.lastname);
SELECT MD5(CONCAT(fullname,NEW.emailaddress,now())) into actcode;
UPDATE `Notes_lookup_school` SET `Enrollment`=`Enrollment`+1 WHERE `id`=NEW.schoolid;
INSERT INTO `Notes_user_activation` (activationcode,activationname,activationemail) VALUES (actcode,fullname,emailaddy);
INSERT INTO `Notes_user_pwdhistory` (userid,newpasswd) VALUES (nuserid,npassword);
END $$
DELIMITER ;
I realize I probably don't need all the variables and that there may be a more elegant way to write the trigger, but I frankly don't see why this should take 2 minutes to complete. What am I missing?
mysql> insert into Notes_user (userid,firstname,lastname,emailaddress,schoolid,institution,userpw) VALUES ('joseph1','Joseph','Schmoe','joe@mail.joeschmoe.com',1708,'Montclair State University',SHA1('4dfklajdfa'));
Query OK, 1 row affected (2 min 0.81 sec)
CREATE TABLE `lookup_school` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`Institution_ID` int(6) NOT NULL DEFAULT '-1',
`Name` varchar(128) NOT NULL,
`Address` varchar(128) NOT NULL,
`City` varchar(80) NOT NULL,
`StateOrProv` varchar(64) NOT NULL,
`PostalCode` varchar(10) NOT NULL DEFAULT '99999-9999',
`Country` varchar(4) NOT NULL,
`Enrollment` int(6) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `School_Name` (`Name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`userid` varchar(16) NOT NULL,
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
`emailaddress` varchar(255) NOT NULL,
`schoolid` int(6),
`institution` varchar(36) NOT NULL DEFAULT '999999',
`userpw` varchar(100) NOT NULL,
`owned` int NOT NULL DEFAULT 0,
`inbox` int NOT NULL DEFAULT 0,
`requests` int NOT NULL DEFAULT 0,
`friend` int(4) NOT NULL DEFAULT 0,
`notification` int(4) NOT NULL DEFAULT 0,
`events` int(4) NOT NULL DEFAULT 0,
`todo` int(4) NOT NULL DEFAULT 0,
`last_login` DATETIME,
`last_access` DATETIME,
`disabled` BOOL DEFAULT 0,
`reset_password` BOOL DEFAULT 0,
`reset_question` TEXT,
`reset_answer` TEXT,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`lastmodified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `notes_userid` (`userid`),
UNIQUE KEY `notes_email` (`emailaddress`),
INDEX `emailaddy_index` (`emailaddress`),
CONSTRAINT `user_fk1` FOREIGN KEY (`schoolid`) REFERENCES `lookup_school` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
--
-- Structure for password history table
--
CREATE TABLE `user_pwdhistory` (
`userid` int NOT NULL AUTO_INCREMENT,
`newpasswd` varchar(100) NOT NULL,
`modifytimestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX `pwdhistory_idx_1` (`userid`),
CONSTRAINT `pwdhistory_uid_fk1` FOREIGN KEY (`userid`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
--
-- Structure for account activations table
CREATE TABLE `user_activation` (
`id` int NOT NULL AUTO_INCREMENT,
`activationcode` text NOT NULL,
`activationname` varchar(50) NOT NULL,
`activationemail` varchar(255) NOT NULL,
`activationsent` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`activationdate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`activationstatus` enum('activated','waiting') NOT NULL DEFAULT 'waiting',
PRIMARY KEY (`id`),
INDEX `activation_emaii_idx` (`activationemail`),
CONSTRAINT `user_activation_fk1` FOREIGN KEY (`activationemail`) REFERENCES `user` (`emailaddress`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Then I created a trigger to execute some business rules that are applied when a new user is added so that an application doesn't have to
1. Update the school enrollment account
2. Insert a password history record for the newly added user
3. Insert an activation table record for the newly added user
Here's the trigger:
DELIMITER $$
CREATE TRIGGER `note_user_t2` AFTER INSERT ON `Notes_user`
FOR EACH ROW BEGIN
DECLARE actcode varchar(36);
DECLARE fullname varchar(60);
DECLARE emailaddy varchar(255);
DECLARE nuserid int;
DECLARE npassword text;
SET emailaddy = NEW.emailaddress;
SET nuserid = NEW.id;
SET npassword = NEW.userpw;
SET fullname = CONCAT(NEW.firstname," ",NEW.lastname);
SELECT MD5(CONCAT(fullname,NEW.emailaddress,now())) into actcode;
UPDATE `Notes_lookup_school` SET `Enrollment`=`Enrollment`+1 WHERE `id`=NEW.schoolid;
INSERT INTO `Notes_user_activation` (activationcode,activationname,activationemail) VALUES (actcode,fullname,emailaddy);
INSERT INTO `Notes_user_pwdhistory` (userid,newpasswd) VALUES (nuserid,npassword);
END $$
DELIMITER ;
I realize I probably don't need all the variables and that there may be a more elegant way to write the trigger, but I frankly don't see why this should take 2 minutes to complete. What am I missing?
mysql> insert into Notes_user (userid,firstname,lastname,emailaddress,schoolid,institution,userpw) VALUES ('joseph1','Joseph','Schmoe','joe@mail.joeschmoe.com',1708,'Montclair State University',SHA1('4dfklajdfa'));
Query OK, 1 row affected (2 min 0.81 sec)