MySQL database security-how to prevent write changes

Source: Internet
Author: User

MySQL Databases can use triggers to make it impossible to modify the data of certain fields without affecting the modification of other fields.

DROP TRIGGER IF EXISTS `members`;SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='';DELIMITER //CREATE TRIGGER `members` BEFORE UPDATE ON `members` FOR EACH ROW BEGIN set new.name  = old.name; set new.cellphone = old.cellphone; set new.email  = old.email;  set new.password = old.password;END//DELIMITER ;SET SQL_MODE=@OLD_SQL_MODE;

Another example:

CREATE TABLE `account` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `user` VARCHAR(50) NOT NULL DEFAULT '0', `cash` FLOAT NOT NULL DEFAULT '0', PRIMARY KEY (`id`))COLLATE='utf8_general_ci'ENGINE=InnoDB;

Each data change adds a new data entry.

INSERT INTO `test`.`account` (`user`, `cash`) VALUES ('neo', -10);INSERT INTO `test`.`account` (`user`, `cash`) VALUES ('neo', -5);INSERT INTO `test`.`account` (`user`, `cash`) VALUES ('neo', 30);INSERT INTO `test`.`account` (`user`, `cash`) VALUES ('neo', -20);

Protect your balance from Modification

DROP TRIGGER IF EXISTS `account`;SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='';DELIMITER //CREATE TRIGGER `account` BEFORE UPDATE ON `account` FOR EACH ROW BEGIN set new.cash  = old.cash;END//DELIMITER ;SET SQL_MODE=@OLD_SQL_MODE;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.