Is it possible to Disable deletes on a table on MYSQL?

房东的猫 提交于 2020-01-13 02:53:09

问题


I'm using MySQL 5.0 and I would like to know if there's a way to disable deletes on a table. As in, not make it possible for ANY user to delete anything from the tablets, only update and insert.


回答1:


Yes, see the MySQL manual for the GRANT syntax

Here is an example of what you want:

GRANT SELECT, INSERT ON mydb.mytbl TO 'someuser'@'somehost';

Which gives only SELECT and INSERT privilages to a specific user/host on a specified table.




回答2:


You can use grant as proposed by others. Or you can create BEFORE DELETE trigger that raises an error, so nobody can issue delete against your table (keep in mind, it is still possible to TRUNCATE TABLE)




回答3:


Here's an example of a trigger:

DELIMITER $$

CREATE TRIGGER tr_table1_del BEFORE DELETE ON table1 FOR EACH ROW
BEGIN
  SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'DELETE cancelled'; 
END $$

DELIMITER ;



回答4:


Setting permissions on your table would let you disable the delete operations. You set permissions with GRANT.



来源:https://stackoverflow.com/questions/7948302/is-it-possible-to-disable-deletes-on-a-table-on-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!