实现说明
一个文件里不止一条语句:SELECT / CREATE,information_schema.role_table_grants、report_owner 都在涉及之列。
逐子句看,这些语句使用了:WHERE 按条件筛出这条语句真正触碰的行;用 COUNT 做聚合。
常见问题
这里用到哪些关系?
引用的是 information_schema.role_table_grants、report_owner。
核心语句
SELECT grantor, grantee, privilege_type
FROM information_schema.role_table_grants
WHERE table_name = 'sales' AND grantee = 'report_reader';
SELECT count(*) AS remaining_grants
FROM information_schema.role_table_grants
WHERE table_name = 'sales' AND grantee = 'report_reader';
GRANT SELECT ON sales TO report_owner WITH GRANT OPTION;
代码示例
-- WITH GRANT OPTION lets grantees re-grant downstream:转授链从此可能变长
CREATE ROLE report_owner;
CREATE ROLE report_reader;
GRANT SELECT ON sales TO report_owner WITH GRANT OPTION;
-- 以 report_owner 身份把权限转授下去
SET ROLE report_owner;
GRANT SELECT ON sales TO report_reader;
RESET ROLE;
-- information_schema 里能看到下游授权:grantor 是 report_owner 而不是表主
SELECT grantor, grantee, privilege_type
FROM information_schema.role_table_grants
WHERE table_name = 'sales' AND grantee = 'report_reader';
-- 撤销源头授权时 CASCADE 连带撤销 re-grant 出去的下游权限
REVOKE SELECT ON sales FROM report_owner CASCADE;
SELECT count(*) AS remaining_grants
FROM information_schema.role_table_grants
WHERE table_name = 'sales' AND grantee = 'report_reader';