Monday 9 September 2013

database design issue and mails deleted upon account removal

database design issue and mails deleted upon account removal

My site is comprised of two types of users,regular users and business
users and super admins. All of them are listed in a users
table(name,email,password) with the addition of busines_users table that
holds business-only related info of the business users(type of business,
address, etc).
And there is a 3rd table that holds of the mails of the site. Mails sent
from the regular users to the business_users OR the super_admins(support
etc...).
As such this mails table has 2 cols(from,to) where they reference the
userID column is the users table. And here is the problem, suppose the
business_user deletes his account.I intend deleting also the mails that he
has sent(whether they are towards the regular users or the super admins)
The problem is that the way the mails table is structured it is inevitable
that deleting the mails described above, I will delete also the mails sent
to HIM by the regular user of super_admin.
There are 2 scenarios examined:
Delete no mails at all-at which case, the discussion ends here.
Redesign the db so that mails sent to him are not deleted(the reg_users
will still have the mails in their screen), but on the other hand mails
send by him are deleted.
What do you propose? If going with 2nd option how I should design the db.
Here are the 3 tables:
CREATE TABLE IF NOT EXISTS `alternative_appointm`.`users` (
`user_ID` INT(11) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`lastname` VARCHAR(45) NOT NULL ,
`email` VARCHAR(255) NULL DEFAULT NULL ,
`password` VARCHAR(100) NULL DEFAULT NULL ,
`hash` VARCHAR(32) NULL DEFAULT NULL ,
`usertype` ENUM('1','2','3','4') NULL DEFAULT NULL ,
PRIMARY KEY (`user_ID`) ,
INDEX `fk_users_usertype1_idx` (`usertype` ASC) ,
CONSTRAINT `fk_users_usertype1`
FOREIGN KEY (`usertype` )
REFERENCES `alternative_appointm`.`usertype` (`type_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
CREATE TABLE IF NOT EXISTS `alternative_appointm`.`business_users` (
`crID` INT(11) NOT NULL ,
`address` VARCHAR(45) NULL DEFAULT NULL ,
`url` VARCHAR(45) NULL DEFAULT NULL ,
`phone` VARCHAR(20) NULL ,
`city` VARCHAR(100) NULL ,
`municipality` VARCHAR(100) NULL ,
`bus_user_type` ENUM('1','2','3','4','5') CHARACTER SET 'latin1' NULL ,
PRIMARY KEY (`crID`) ,
INDEX `fk_business_users_buz_usertype1_idx` (`bus_user_type` ASC) ,
INDEX `crID` (`crID` ASC) ,
CONSTRAINT `fk_business_users_buz_usertype1`
FOREIGN KEY (`bus_user_type` )
REFERENCES `alternative_appointm`.`buz_usertype` (`Type_id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_business_users_users1`
FOREIGN KEY (`crID` )
REFERENCES `alternative_appointm`.`users` (`user_ID` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
CREATE TABLE IF NOT EXISTS `alternative_appointm`.`mails` (
`message-iD` INT(10) NOT NULL AUTO_INCREMENT ,
`from` INT(11) NULL DEFAULT NULL ,
`to` INT(11) NULL DEFAULT NULL ,
`datetime` DATETIME NULL DEFAULT NULL ,
`subject` TEXT NULL DEFAULT NULL ,
`message` TEXT NULL DEFAULT NULL ,
PRIMARY KEY (`message-iD`) ,
UNIQUE INDEX `message-iD_UNIQUE` (`message-iD` ASC) ,
INDEX `fk_mails_users1_idx` (`from` ASC) ,
INDEX `fk_mails_users2_idx` (`to` ASC) ,
CONSTRAINT `fk_mails_users1`
FOREIGN KEY (`from` )
REFERENCES `alternative_appointm`.`users` (`user_ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_mails_users2`
FOREIGN KEY (`to` )
REFERENCES `alternative_appointm`.`users` (`user_ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8

No comments:

Post a Comment