Home > Database > Mysql Tutorial > body text

MySQL数据库安全之防止撰改的方法_MySQL

WBOY
Release: 2016-05-31 08:48:27
Original
1090 people have browsed it

MySQL数据库可以通过触发器,使之无法修改某些字段的数据,同时又不会影响修改其他字段。

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;
Copy after login

再举一个例子:

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;
Copy after login

每一次数据变化新增一条数据

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);
Copy after login

保护用户的余额不被修改

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;
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!