t_girl=# create role ytt3 with login connection limit 1 password 'ytt3';
CREATE ROLE
t_girl=# alter schema ytt owner to ytt3;
ALTER SCHEMA
t_girl=# grant select on all tables in schema ytt to ytt3;
GRANT
现在用新用户ytt3登陆并且执行TRUNCATE,发现被禁止。
bash-4.1$ psql -U ytt3 t_girl
psql (9.3.4)
Type "help" for help.
t_girl=> truncate table j2;
ERROR: permission denied for relation j2
t_girl=# \sf prevent_truncate
CREATE OR REPLACE FUNCTION public.prevent_truncate()
RETURNS trigger
LANGUAGE plpgsql
AS $functio$
BEGIN
RAISE EXCEPTION 'Prevent "%" to be truncated!', TG_TABLE_SCHEMA||TG_TABLE_NAME;
RETURN NEW;
END;
$function$
t_girl=# \d j2
Table "ytt.j2"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
str2 | text |
Triggers:
trigger_truncate_before BEFORE TRUNCATE ON j2 FOR EACH STATEMENT EXECUTE PROCEDURE ytt.prevent_truncate()
t_girl=#
t_girl=# truncate table j2;
ERROR: Prevent "ytt.j2" to be truncated!
mysql> truncate table j2;
ERROR 1142 (42000): DROP command denied to user 'ytt3'@'localhost' for table 'j2'
要么,就对数据库的操作用SPROC封装起来,
+------------------------------------+
| Error |
+------------------------------------+
| Prevent t_girl.j2 to be truncated! |
+------------------------------------+
1 row in set (0.00 sec)