Whoami:5年+金融、政府、医疗领域工作经验的DBACertificate:OCP、PCPSkill:Oracle、Mysql、PostgreSQLPlatform:CSDN、墨天伦、公众号(呆呆的私房菜)
阅读本文可以了解PostgreSQL、Oracle 和 Mysql关于用户角色和权限的区别。
某家大型零售公司希望建立一个数据中台来统一管理其销售、库存和市场营销数据,公司需要为不同的部门和合作伙伴创建不同的访问权限。

-- 创建一个名为 ECommerceDataHub 的新数据库create database ecommercedatahub;-- 创建销售部门用户create role sales_user with login password 'sales_pwd';-- 创建库存部门用户create role inventory_user with login password 'inventory_pwd';-- 创建市场部门用户create role marketing_user with login password 'marketing_pwd';-- 创建合作伙伴用户create role partner_user with login password 'partner_pwd';-- 授权上述用户拥有业务数据库访问和创建权限grant connect,create on database ecommercedatahub to sales_user, inventory_user, marketing_user;grant connect on database ecommercedatahub to partner_user;-- 创建销售部门schema并授权所有表权限,创建表并插入数据psql -Usales_user -decommercedatahubcreate schema sales authorization sales_user;CREATE TABLE sales.sales_orders (order_id SERIAL PRIMARY KEY,customer_id INT,order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,total_amount NUMERIC(10, 2));insert into sales.sales_orders values (1, 101, current_date, 10000);-- 创建库存部门schema并授权所有表权限,创建表并插入数据psql -Uinventory_user -decommercedatahubcreate schema inventory authorization inventory_user;CREATE TABLE inventory.product_stock (product_id SERIAL PRIMARY KEY,product_name VARCHAR(255),stock_quantity INT);insert into inventory.product_stock values (1, 'PROD1', 12345);-- 创建市场部门schema并授权所有表权限,创建表并插入数据psql -Umarketing_user -decommercedatahubcreate schema marketing authorization marketing_user;CREATE TABLE marketing.campaigns (campaign_id SERIAL PRIMARY KEY,campaign_name VARCHAR(255),start_date TIMESTAMP,end_date TIMESTAMP);insert into marketing.campaigns values (1, 'CN1', current_date, current_date);-- 授权合作伙伴查看所有表的权限psql -Upostgre -decommercedatahubgrant usage on schema sales, inventory, marketing to partner_user;grant select on all tables in schema sales, inventory, marketing to partner_user;

-- 创建销售部门用户CREATE USER sales_user IDENTIFIED BY sales_pwd default tablespace quota unlimited on users;-- 创建库存部门用户CREATE USER inventory_user IDENTIFIED BY inventory_pwd default tablespace quota unlimited on users;-- 创建市场部门用户CREATE USER marketing_user IDENTIFIED BY marketing_pwd default tablespace quota unlimited on users;-- 创建合作用户CREATE USER partner_user IDENTIFIED BY partner_pwd;-- 授权用户系统权限grant connect, resource to sales_user, inventory_user, marketing_user;grant connect to partner_user;-- Oracle的schema与用户是相关联的,所以创建用户时已经定义了schema-- 创建销售部门表并插入数据sqlplus sales_user/sales_pwdCREATE TABLE sales_user.sales_orders (order_id number(10) PRIMARY KEY,customer_id INT,order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,total_amount NUMERIC(10, 2));insert into sales_user.sales_orders values (1, 101, current_date, 10000);commit;-- 创建库存部门表并插入数据sqlplus inventory_user/inventory_pwdCREATE TABLE inventory_user.product_stock (product_id number(10) PRIMARY KEY,product_name VARCHAR(255),stock_quantity INT);insert into inventory_user.product_stock values (1, 'PROD1', 12345);commit;-- 创建市场部门表并插入数据sqlplus marketing_user/marketing_pwdCREATE TABLE marketing_user.campaigns (campaign_id number(10) PRIMARY KEY,campaign_name VARCHAR(255),start_date TIMESTAMP,end_date TIMESTAMP);insert into marketing_user.campaigns values (1, 'CN1', current_date, current_date);commit;-- 授权合作伙伴查看所有表的权限grant select on sales_user.sales_orders to partner_user;grant select on inventory_user.product_stock to partner_user;grant select on marketing_user.campaigns to partner_user;

-- 创建销售部门用户create user 'sales_user'@'%' identified by 'sales_pwd';-- 创建库存部门用户create user 'inventory_user'@'%' identified by 'inventory_pwd';-- 创建市场部门用户create user 'marketing_user'@'%' identified by 'marketing_pwd';-- 创建合作伙伴用户create user 'partner_user'@'%' identified by 'partner_pwd';-- 创建数据库(等同于schema)create database sales;create database inventory;create database marketing;-- 授权用户拥有业务数据库访问和创建权限grant all privileges on sales.* to 'sales_user'@'%';grant all privileges on inventory.* to 'inventory_user'@'%';grant all privileges on marketing .* to 'marketing_user'@'%';-- 创建销售部门表并插入数据mysql -usales_user -psales_pwd -DsalesCREATE TABLE sales.sales_orders (order_id INT AUTO_INCREMENT PRIMARY KEY,customer_id INT,order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,total_amount DECIMAL(10, 2));insert into sales.sales_orders values (1, 101, now(), 10000);-- 创建库存部门表并插入数据mysql -uinventory_user -pinventory_pwd -DinventoryCREATE TABLE inventory.product_stock (product_id INT AUTO_INCREMENT PRIMARY KEY,product_name VARCHAR(255),stock_quantity INT);insert into inventory.product_stock values (1, 'PROD1', 12345);-- 创建市场部门表并插入数据mysql -umarketing_user -pmarketing_pwd -DmarketingCREATE TABLE marketing.campaigns (campaign_id INT AUTO_INCREMENT PRIMARY KEY,campaign_name VARCHAR(255),start_date TIMESTAMP,end_date TIMESTAMP);insert into marketing.campaigns values (1, 'CN1', current_date, current_date);-- 授权合作伙伴查看所有表的权限grant select on sales.* to 'partner_user'@'%';grant select on inventory.* to 'partner_user'@'%';grant select on marketing.* to 'partner_user'@'%';
1. PostgreSQL中database是逻辑存储单元,1个database可以对应多个schema;schema是数据库对象的命名空间,不同schema可以包含相同的数据库名称;schema中的对象通过授权的方式允许用户访问。 2. Oracle中schema与用户名称相同,每个用户都有一个默认的schema;用户没办法直接创建schema,但可以拥有多个schema的访问权限。 3. MySQL中database和schema在概念上是相同的,用户可以通过授权的方式访问不同database里的数据。 总的来说,PostgreSQL里的schema用于在单个物理数据库内逻辑分割数据;Oracle中schema与用户账户紧密联系,用于组织和管理用户创建的数据库对象;MySQL中schema与databae是相同的概念。在授权和安全性方面,不同的数据库提供了不同级别的控制以达到允许或限制用户访问特定数据库对象。
本文内容就到这啦,阅读完本篇,相信你对各主流数据库的角色和权限应该有更深的理解了吧!我们下篇再见!

文章转载自呆呆的私房菜,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




