Is it possible to Disable deletes on a table on MY

2020-07-10 10:17发布

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.

4条回答
兄弟一词,经得起流年.
2楼-- · 2020-07-10 10:57

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楼-- · 2020-07-10 11:03

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

查看更多
成全新的幸福
4楼-- · 2020-07-10 11:09

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.

查看更多
叼着烟拽天下
5楼-- · 2020-07-10 11:17

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 ;
查看更多
登录 后发表回答