 ALTER TABLE `itemattributes` CHANGE `itemId` `itemId` INT( 10 ) UNSIGNED NOT NULL;
 ALTER TABLE `itemstatus` CHANGE `itemId` `itemId` INT( 10 ) UNSIGNED NOT NULL;
 
-UPDATE `items` SET `prikey`=`itemId`+(SELECT (SELECT MAX(`id`) FROM `goals`)+(SELECT MAX(`projectId`) FROM `projects`));
-UPDATE `itemattributes` SET `prikey`=`itemId`+(SELECT (SELECT MAX(`id`) FROM `goals`)+(SELECT MAX(`projectId`) FROM `projects`));
-UPDATE `itemstatus` SET `prikey`=`itemId`+(SELECT (SELECT MAX(`id`) FROM `goals`)+(SELECT MAX(`projectId`) FROM `projects`));
+UPDATE `items` SET `prikey`=`itemId` +( 
+	CASE  WHEN (SELECT MAX(`id`) FROM `goals`) IS NULL THEN 0
+		ELSE (SELECT MAX(`id`) FROM `goals`)
+	END
+	)+(
+	CASE  WHEN (SELECT MAX(`projectId`) FROM `projects`) IS NULL THEN 0
+		ELSE (SELECT MAX(`projectId`) FROM `projects`)
+	END
+	);
+
+UPDATE `itemattributes` SET `prikey`=`itemId`+(
+	CASE  WHEN (SELECT MAX(`id`) FROM `goals`) IS NULL THEN 0
+		ELSE (SELECT MAX(`id`) FROM `goals`)
+	END
+	)+(
+	CASE  WHEN (SELECT MAX(`projectId`) FROM `projects`) IS NULL THEN 0
+		ELSE (SELECT MAX(`projectId`) FROM `projects`)
+	END
+	);
+
+
+UPDATE `itemstatus` SET `prikey`=`itemId`+(
+	CASE  WHEN (SELECT MAX(`id`) FROM `goals`) IS NULL THEN 0
+		ELSE (SELECT MAX(`id`) FROM `goals`)
+	END
+	)+(
+	CASE  WHEN (SELECT MAX(`projectId`) FROM `projects`) IS NULL THEN 0
+		ELSE (SELECT MAX(`projectId`) FROM `projects`)
+	END
+	);
+
 ALTER TABLE `items` DROP PRIMARY KEY, ADD PRIMARY KEY (`prikey`);
 ALTER TABLE `itemattributes` DROP PRIMARY KEY, ADD PRIMARY KEY (`prikey`);
 ALTER TABLE `itemstatus` DROP PRIMARY KEY, ADD PRIMARY KEY (`prikey`);


 -- rename old database to name-old --
 -- rename new database to preferred name -- 
 -- User may need to adjust privileges --


ALTER TABLE `timeitems` ADD `type` ENUM( 'v', 'o', 'g', 'p', 'a' ) NOT NULL DEFAULT 'a';
ALTER TABLE `timeitems` ADD INDEX ( `type` );
ALTER TABLE `goals` ADD `timeframeId` INT UNSIGNED NOT NULL;
UPDATE `goals` SET `timeframeId`= (1 + (
	CASE  WHEN (SELECT MAX(`timeframeId`) FROM `timeitems`) IS NULL THEN 0
		ELSE (SELECT MAX(`timeframeId`) FROM `timeitems`)
	END
	)) WHERE `type`='weekly';
UPDATE `goals` SET `timeframeId`= (2 + (
	CASE  WHEN (SELECT MAX(`timeframeId`) FROM `timeitems`) IS NULL THEN 0
		ELSE (SELECT MAX(`timeframeId`) FROM `timeitems`)
	END
	)) WHERE `type`='quarterly';
ALTER TABLE `goals` CHANGE `type` `type` ENUM('g') NOT NULL DEFAULT 'g';

 -- Copy over data from goals -- 
