mysql用触发器禁止某条记录的删除、修改:
delimiter //
CREATE TRIGGER xx_company_agent_update BEFORE UPDATE ON xx_company_agent
FOR EACH ROW
BEGIN
IF OLD.bank_card is not null THEN
SET NEW.bank_card = OLD.bank_card,NEW.money= OLD.money,NEW.real_money=OLD.real_money,NEW.bank_name=OLD.bank_name
END IF
END//
delimiter
扩展资料
mysql触发器的使用
用户记录用户的预期事件(schema是特殊用户的数据库对象集合。这些对象包括:表,索引,视图,存储程序等。在Oracle里, schema要求创建一个用户。但是也可以创建一个没有schema的用户(根本没有对象)。
所以在Oracle-中,用户就是一个帐户而schema就是对象。可能在其他的数据库平台上可以创建一个没有用户的schema。LOGOFF不能是AFTER LOGOFF,将会重新 *** 作,这里应该是BEFORE,否则就会报错:替换可以插入不能有AFTER。创建或替换触发器MYLOGOFFTRIGGER。
参考资料来源:百度百科—mySQL
删除语句时触发:例:删除表desk中信息时,触动触发器,向表booksheet删除数据
mysql>delimiter $
mysql>create trigger Desk2(触发器名) after delete on desk(表名) for each row
->begin
->delete from booksheet where Phone=old.Phone and Number = old.Number and Time=old.Time and Date=old.Date and Environment=old.Environment
->end $删除语句时触发:
例:删除表desk中信息时,触动触发器,向表booksheet删除数据
mysql>delimiter $
mysql>create trigger Desk2(触发器名) after delete on desk(表名) for each row
->begin
->delete from booksheet where Phone=old.Phone and Number = old.Number and Time=old.Time and Date=old.Date and Environment=old.Environment
->end $
我就不造你的 学生表和成绩表了.
用 test_main 表 和 test_sub 代替说明
-- 创建测试主表. ID 是主键.CREATE TABLE test_main (
id INT NOT NULL,
value VARCHAR(10),
PRIMARY KEY(id)
)
-- 创建测试子表.
CREATE TABLE test_sub (
id INT NOT NULL,
main_id INT ,
value VARCHAR(10),
PRIMARY KEY(id)
)
-- 插入测试主表数据.
INSERT INTO test_main(id, value) VALUES (1, 'ONE')
INSERT INTO test_main(id, value) VALUES (2, 'TWO')
-- 插入测试子表数据.
INSERT INTO test_sub(id, main_id, value) VALUES (1, 1, 'ONEONE')
INSERT INTO test_sub(id, main_id, value) VALUES (2, 2, 'TWOTWO')
通过 DELETE CASCADE 外键约束的处理机制来处理
mysql> ALTER TABLE test_sub-> ADD CONSTRAINT main_id_cons
-> FOREIGN KEY (main_id)
-> REFERENCES test_main(id)
-> ON DELETE CASCADE//
Query OK, 2 rows affected (0.16 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> DELETE FROM
-> test_main
-> WHERE
-> id = 1
-> //
Query OK, 1 row affected (0.02 sec)
mysql> SELECT
-> *
-> FROM
-> test_sub
-> //
+----+---------+--------+
| id | main_id | value |
+----+---------+--------+
| 2 | 2 | TWOTWO |
+----+---------+--------+
1 row in set (0.00 sec)
如果你非要使用触发器来处理, 那么下面是一个例子:
-- 创建测试主表. ID 是主键.
CREATE TABLE t_test_main (
id INT NOT NULL,
value VARCHAR(10),
PRIMARY KEY(id)
)
-- 创建测试子表.
CREATE TABLE t_test_sub (
id INT NOT NULL,
main_id INT ,
value VARCHAR(10),
PRIMARY KEY(id)
)
-- 插入测试主表数据.
INSERT INTO t_test_main(id, value) VALUES (1, 'ONE')
INSERT INTO t_test_main(id, value) VALUES (2, 'TWO')
-- 插入测试子表数据.
INSERT INTO t_test_sub(id, main_id, value) VALUES (1, 1, 'ONEONE')
INSERT INTO t_test_sub(id, main_id, value) VALUES (2, 2, 'TWOTWO')
DELIMITER //
CREATE TRIGGER tr_t_test_main_Del
BEFORE DELETE ON t_test_main
FOR EACH ROW
BEGIN
DELETE FROM t_test_sub WHERE main_id = OLD.id
END
//
DELIMITER
mysql> delete from t_test_main where id = 1
Query OK, 1 row affected (0.01 sec)
mysql> select * from t_test_sub
+----+---------+--------+
| id | main_id | value |
+----+---------+--------+
| 2 | 2 | TWOTWO |
+----+---------+--------+
1 row in set (0.00 sec)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)