被刪除或更新如果你是我們MySQL 5.5或更高版本,這很簡單。
DELIMITER $$
DROP TRIGGER IF EXISTS demo_bd $$
CREATE TRIGGER demo_bd BEFORE DELETE ON demo FOR EACH ROW
BEGIN
IF OLD.id = 1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'This magical user cannot be deleted';
END IF;
END $$
DELIMITER ;
對於更新,它是完全相同的代碼,只需稍作調整。
DELIMITER $$
DROP TRIGGER IF EXISTS demo_bu $$
CREATE TRIGGER demo_bu BEFORE UPDATE ON demo FOR EACH ROW
BEGIN
IF OLD.id = 1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'This magical user cannot be updated';
END IF;
END $$
DELIMITER ;
另外...不要在數據庫中存儲密碼。
你的問題很一般,你到目前爲止試過了什麼? – RedFilter
http://stackoverflow.com/questions/5132356/how-to-reject-delete-statement-in-before-delete-triger-mysql – ethrbunny