创建用户自定义数据库用户注意事项
如果已忽略 FOR LOGIN,则新的数据库用户将被映射到同名的 SQL Server 登录名。
默认架构将是服务器为此数据库用户解析对象名时将搜索的第一个架构。除非另外指定,否则默认架构将是此数据库用户创建的对象所属的架构。
如果用户具有默认架构,则将使用默认架构。如果用户不具有默认架构,但该用户是具有默认架构的组的成员,则将使用该组的默认架构。如果用户不具有默认架构而且是多个组的成员,则该用户的默认架构将是具有最低 principle_id 的 Windows 组的架构和一个显式设置的默认架构。(不可能将可用的默认架构之一显式选作首选架构。)如果不能为用户确定默认架构,则将使用 dbo 架构。
DEFAULT_SCHEMA 可在创建它所指向的架构前进行设置。
在创建映射到证书或非对称密钥的用户时,不能指定 DEFAULT_SCHEMA。
如果用户是 sysadmin 固定服务器角色的成员,则忽略 DEFAULT_SCHEMA 的值。sysadmin 固定服务器角色的所有成员都有默认架构 dbo。
WITHOUT LOGIN 子句可创建不映射到 SQL Server 登录名的用户。它可以作为 guest 连接到其他数据库。可以将权限分配给这一没有登录名的用户,当安全上下文更改为没有登录名的用户时,原始用户将收到无登录名用户的权限。
只有映射到 Windows 主体的用户才能包含反斜杠字符 ()。
不能使用 CREATE USER 创建 guest 用户,因为每个数据库中均已存在 guest 用户。可通过授予 guest 用户 CONNECT 权限来启用该用户,如下所示:
可以在 sys.database_principals 目录视图中查看有关数据库用户的信息。
使用 SSMS 数据库管理工具创建用户自定义数据库用户
1、连接服务器 -》在对象资源管理器窗口选择数据库 -》展开数据库 -》展开安全性 -》展开用户 -》右键点击用户 -》选择新建。
2、在数据库用户 - 新建弹出框 -》点击常规 -》选择用户类型 -》输入用户名 -》选择登录名 -》选择用户所属架构。
3、在数据库用户 - 新建弹出框 -》选择用户所拥有的架构。
4、在数据库用户 - 新建弹出框 -》点击成员身份 -》选择数据库成员身份。
5、在数据库用户 - 新建弹出框 -》点击搜索选择一个安全对象 -》选择安全对象以后选择安全对象所拥有的权限。
6、在数据库用户 - 新建弹出框 -》选择扩展属性 -》输入注释名称 -》输入注释值 -》点击确定。
7、不需要刷新即可在对象资源管理器查看创建结果。
使用 T -SQL 脚本创建用户自定义数据库用户
语法
—- 创建用户自定义数据库用户
—- 声明数据库引用
–use database_name;
–go
—-windows 用户
–create user user_name for login login_name with default_schema=architecture_name,allow_encrypted_value_modifications={on | off};
—- 不带登录名的 SQL 用户
–create user user_name without login with default_schema=architecrure_name,allow_encrypted_value_modifications={on | off};
—- 带登录名的 SQL 用户
–create user user_name for login login_name with default_schema=architecture_name,allow_encrypted_value_modifications={on | off};
—- 映射到非对称密钥的用户
–create user user_name for asymmetric key asym_key_name;
—- 映射到证书的用户
–create user user_name for certificate certificate_name;
– 拥有的架构
–use database_name;
–go
–alter authorization on schema::[db_accessadmin] to user_name;
–go
–alter authorization on schema::[db_backupoperator] to user_name;
–go
–alter authorization on schema::[db_datareader] to user_name;
–go
–alter authorization on schema::[db_datawriter] to user_name;
–go
–alter authorization on schema::[db_ddladmin] to user_name;
–go
–alter authorization on schema::[db_denydatareader] to user_name;
–go
–alter authorization on schema::[db_denydatawriter] to user_name;
–go
–alter authorization on schema::[db_owner] to user_name;
–go
–alter authorization on schema::[db_securityadmin] to user_name;
–go
–alter authorization on schema::[guest] to user_name;
–go
– 成员身份
–use database_name;
–go
–alter role [db_accessadmin] add member user_name;
–go
–alter role [db_backupoperator] add member user_name;
–go
–alter role [db_datareader] add member user_name;
–go
–alter role [db_datawriter] add member user_name;
–go
–alter role [db_ddladmin] add member user_name;
–go
–alter role [db_denydatareader] add member user_name;
–go
–alter role [db_denydatawriter] add member user_name;
–go
–alter role [db_owner] add member user_name;
–go
–alter role [db_securityadmin] add member user_name;
–go
—- 安全对象
—-use database_name;
—-go
—- 授予权限
—- 备份日志
–grant backup log to user_name;
–go
—- 备份数据库
–grant backup database to user_name;
–go
—- 插入
–grant insert to user_name;
–go
—- 查看定义
–grant view definition to user_name;
–go
—- 查看任意列加密密钥定义
–grant view any column encryption key definition to user_name;
–go
—- 查看任意列主密钥定义
–grant view any column master key definition to user_name;
–go
—- 查看数据库状态
–grant view database state to user_name;
–go
—- 撤销掩码
–grant unmask to user_name;
–go
—- 创建 xml 架构集合
–grant create xml schema collection to user_name;
–go
—- 创建表
–grant create table to user_name;
–go
—- 创建程序集
–grant create assembly to user_name;
–go
—- 创建队列
–GRANT CREATE QUEUE to user_name;
–go
—- 创建对称密钥
–grant create symmetric key to user_name;
–go
—- 创建非对称密钥
–grant create asymmetric key to user_name;
–go
—- 创建服务
–grant create service to user_name;
–go
—- 创建规则
–grant create rule to user_name;
–go
—- 创建过程
–grant create procedure to user_name;
–go
—- 创建函数
–grant create function to user_name;
–go
—- 创建架构
–grant create schema to user_name;
–go
—- 创建角色
–grant create role to user_name;
–go
—- 创建类型
–grant create type to user_name;
–go
—- 创建路由
–grant create route to user_name;
–go
—- 创建默认值
–grant create default to user_name;
–go
—- 创建全文目录
–grant create fulltext catalog to user_name;
–go
—- 创建视图
–grant create view to user_name;
–go
—- 创建数据库 DDL 事件通知
–grant create database dll event notification to user_name;
–go
—- 创建同义词
–grant create synonym to user_name;
–go
—- 创建消息类型
–grant create message type to user_name;
–go
—- 创建远程服务绑定
–grant create remote service binding to user_name;
–go
—- 创建约定
–grant create contract to user_name;
–go
—- 创建证书
–grant create certificate to user_name;
–go
—- 订阅查询通知
–grant subscribe query notifications to user_name;
–go
—- 更改
–grant alter to user_name;
–go
—- 更改任何外部数据源
–grant alter any external data source to user_name;
–go
—- 更改任何外部文件格式
–grant alter any external file format to user_name;
–go
—- 更改任何掩码
–grant alter any mask to user_name;
–go
—- 更改任意安全策略
–grant alter any security policy to user_name;
–go
—- 更改任意程序集
–grant alter any assembly to user_name;
–go
—- 更改任意对称密钥
–grant alter any symmetric key to user_name;
–go
—- 更改任意非对称密钥
–grant alter any asymmetric key to user_name;
–go
—- 更改任意服务
–grant alter any service to user_name;
–go
—- 更改任意架构
–grant alter any schema to user_name;
–go
—- 更改任意角色
–grant alter any role to user_name;
–go
—- 更改任意路由
–grant alter any route to user_name;
–go
—- 更改任意全文目录
–grant alter any fulltext catalog to user_name;
–go
—- 更改任意数据空间
–grant alter any dataspace to user_name;
–go
—- 更改任意数据库 DDL 数据库触发器
–grant alter any database ddl trigger to user_name;
–go
—- 更改任意数据库审核
–grant alter any database audit to user_name;
–go
—- 更改任意数据库事件通知
–grant alter any database event notification to user_name;
–go
—- 更改任意消息类型
–grant alter any message type to user_name;
–go
—- 更改任意应用程序角色
–grant alter any application role to user_name;
–go
—- 更改任意用户
–grant alter any user to user_name;
–go
—- 更改任意远程服务绑定
–grant alter any remote service binding to user_name;
–go
—- 更改任意约定
–grant alter any contract to user_name;
–go
—- 更改任意证书
–grant alter any certificate to user_name;
–go
—- 更新
–grant update to user_name;
–go
—- 检查点
–grant checkpoint to user_name;
–go
—- 接管所有权
–grant take ownership to user_name;
–go
—- 控制
–grant control to user_name;
–go
—- 控制聚合
–grant create aggregate to user_name;
–go
—- 连接
–grant connect to user_name;
–go
—- 连接复制
–grant connect replication to user_name;
–go
—- 删除
–grant delete to user_name;
–go
—- 身份验证
–grant authenticate to user_name;
–go
—- 显示计划
–grant showplan to user_name;
–go
—- 选择
–grant select to user_name;
–go
—- 引用
–grant references to user_name;
–go
—- 执行
–grant execute to user_name;
–go
—- 授予并允许转售权限
—- 安全对象
—-use database_name;
—-go
—- 备份日志
–grant backup log to user_name with grant option;
–go
—- 备份数据库
–grant backup database to user_name with grant option;
–go
—- 插入
–grant insert to user_name with grant option;
–go
—- 查看定义
–grant view definition to user_name with grant option;
–go
—- 查看任意列加密密钥定义
–grant view any column encryption key definition to user_name with grant option;
–go
—- 查看任意列主密钥定义
–grant view any column master key definition to user_name with grant option;
–go
—- 查看数据库状态
–grant view database state to user_name with grant option;
–go
—- 撤销掩码
–grant unmask to user_name with grant option;
–go
—- 创建 xml 架构集合
–grant create xml schema collection to user_name with grant option;
–go
—- 创建表
–grant create table to user_name with grant option;
–go
—- 创建程序集
–grant create assembly to user_name with grant option;
–go
—- 创建队列
–GRANT CREATE QUEUE to user_name with grant option;
–go
—- 创建对称密钥
–grant create symmetric key to user_name with grant option;
–go
—- 创建非对称密钥
–grant create asymmetric key to user_name with grant option;
–go
—- 创建服务
–grant create service to user_name with grant option;
–go
—- 创建规则
–grant create rule to user_name with grant option;
–go
—- 创建过程
–grant create procedure to user_name with grant option;
–go
—- 创建函数
–grant create function to user_name with grant option;
–go
—- 创建架构
–grant create schema to user_name with grant option;
–go
—- 创建角色
–grant create role to user_name with grant option;
–go
—- 创建类型
–grant create type to user_name with grant option;
–go
—- 创建路由
–grant create route to user_name with grant option;
–go
—- 创建默认值
–grant create default to user_name with grant option;
–go
—- 创建全文目录
–grant create fulltext catalog to user_name with grant option;
–go
—- 创建视图
–grant create view to user_name with grant option;
–go
—- 创建数据库 DDL 事件通知
–grant create database dll event notification to user_name with grant option;
–go
—- 创建同义词
–grant create synonym to user_name with grant option;
–go
—- 创建消息类型
–grant create message type to user_name with grant option;
–go
—- 创建远程服务绑定
–grant create remote service binding to user_name with grant option;
–go
—- 创建约定
–grant create contract to user_name with grant option;
–go
—- 创建证书
–grant create certificate to user_name with grant option;
–go
—- 订阅查询通知
–grant subscribe query notifications to user_name with grant option;
–go
—- 更改
–grant alter to user_name with grant option;
–go
—- 更改任何外部数据源
–grant alter any external data source to user_name with grant option;
–go
—- 更改任何外部文件格式
–grant alter any external file format to user_name with grant option;
–go
—- 更改任何掩码
–grant alter any mask to user_name with grant option;
–go
—- 更改任意安全策略
–grant alter any security policy to user_name with grant option;
–go
—- 更改任意程序集
–grant alter any assembly to user_name with grant option;
–go
—- 更改任意对称密钥
–grant alter any symmetric key to user_name with grant option;
–go
—- 更改任意非对称密钥
–grant alter any asymmetric key to user_name with grant option;
–go
—- 更改任意服务
–grant alter any service to user_name;
–go
—- 更改任意架构
–grant alter any schema to user_name with grant option;
–go
—- 更改任意角色
–grant alter any role to user_name with grant option;
–go
—- 更改任意路由
–grant alter any route to user_name with grant option;
–go
—- 更改任意全文目录
–grant alter any fulltext catalog to user_name with grant option;
–go
—- 更改任意数据空间
–grant alter any dataspace to user_name with grant option;
–go
—- 更改任意数据库 DDL 数据库触发器
–grant alter any database ddl trigger to user_name with grant option;
–go
—- 更改任意数据库审核
–grant alter any database audit to user_name with grant option;
–go
—- 更改任意数据库事件通知
–grant alter any database event notification to user_name with grant option;
–go
—- 更改任意消息类型
–grant alter any message type to user_name with grant option;
–go
—- 更改任意应用程序角色
–grant alter any application role to user_name with grant option;
–go
—- 更改任意用户
–grant alter any user to user_name with grant option;
–go
—- 更改任意远程服务绑定
–grant alter any remote service binding to user_name with grant option;
–go
—- 更改任意约定
–grant alter any contract to user_name with grant option;
–go
—- 更改任意证书
–grant alter any certificate to user_name with grant option;
–go
—- 更新
–grant update to user_name with grant option;
–go
—- 检查点
–grant checkpoint to user_name with grant option;
–go
—- 接管所有权
–grant take ownership to user_name with grant option;
–go
—- 控制
–grant control to user_name with grant option;
–go
—- 控制聚合
–grant create aggregate to user_name with grant option;
–go
—- 连接
–grant connect to user_name with grant option;
–go
—- 连接复制
–grant connect replication to user_name with grant option;
–go
—- 删除
–grant delete to user_name with grant option;
–go
—- 身份验证
–grant authenticate to user_name with grant option;
–go
—- 显示计划
–grant showplan to user_name with grant option;
–go
—- 选择
–grant select to user_name with grant option;
–go
—- 引用
–grant references to user_name with grant option;
–go
—- 执行
–grant execute to user_name with grant option;
–go
—- 拒绝权限
—- 安全对象
–use database_name;
–go
—- 备份日志
–deny backup log to user_name;
–go
—- 备份数据库
–deny backup database to user_name;
–go
—- 插入
–deny insert to user_name;
–go
—- 查看定义
–deny view definition to user_name;
–go
—- 查看任意列加密密钥定义
–deny view any column encryption key definition to user_name;
–go
—- 查看任意列主密钥定义
–deny view any column master key definition to user_name;
–go
—- 查看数据库状态
–deny view database state to user_name;
–go
—- 撤销掩码
–deny unmask to user_name;
–go
—- 创建 xml 架构集合
–deny create xml schema collection to user_name;
–go
—- 创建表
–deny create table to user_name;
–go
—- 创建程序集
–deny create assembly to user_name;
–go
—- 创建队列
–deny CREATE QUEUE to user_name;
–go
—- 创建对称密钥
–deny create symmetric key to user_name;
–go
—- 创建非对称密钥
–deny create asymmetric key to user_name;
–go
—- 创建服务
–deny create service to user_name;
–go
—- 创建规则
–deny create rule to user_name;
–go
—- 创建过程
–deny create procedure to user_name;
–go
—- 创建函数
–deny create function to user_name;
–go
—- 创建架构
–deny create schema to user_name;
–go
—- 创建角色
–deny create role to user_name;
–go
—- 创建类型
–deny create type to user_name;
–go
—- 创建路由
–deny create route to user_name;
–go
—- 创建默认值
–deny create default to user_name;
–go
—- 创建全文目录
–deny create fulltext catalog to user_name;
–go
—- 创建视图
–deny create view to user_name;
–go
—- 创建数据库 DDL 事件通知
–deny create database dll event notification to user_name;
–go
—- 创建同义词
–deny create synonym to user_name;
–go
—- 创建消息类型
–deny create message type to user_name;
–go
—- 创建远程服务绑定
–deny create remote service binding to user_name;
–go
—- 创建约定
–deny create contract to user_name;
–go
—- 创建证书
–deny create certificate to user_name;
–go
—- 订阅查询通知
–deny subscribe query notifications to user_name;
–go
—- 更改
–deny alter to user_name;
–go
—- 更改任何外部数据源
–deny alter any external data source to user_name;
–go
—- 更改任何外部文件格式
–deny alter any external file format to user_name;
–go
—- 更改任何掩码
–deny alter any mask to user_name;
–go
—- 更改任意安全策略
–deny alter any security policy to user_name;
–go
—- 更改任意程序集
–deny alter any assembly to user_name;
–go
—- 更改任意对称密钥
–deny alter any symmetric key to user_name;
–go
—- 更改任意非对称密钥
–deny alter any asymmetric key to user_name;
–go
—- 更改任意服务
–deny alter any service to user_name;
–go
—- 更改任意架构
–deny alter any schema to user_name;
–go
—- 更改任意角色
–deny alter any role to user_name;
–go
—- 更改任意路由
–deny alter any route to user_name;
–go
—- 更改任意全文目录
–deny alter any fulltext catalog to user_name;
–go
—- 更改任意数据空间
–deny alter any dataspace to user_name;
–go
—- 更改任意数据库 DDL 数据库触发器
–deny alter any database ddl trigger to user_name;
–go
—- 更改任意数据库审核
–deny alter any database audit to user_name;
–go
—- 更改任意数据库事件通知
–deny alter any database event notification to user_name;
–go
—- 更改任意消息类型
–deny alter any message type to user_name;
–go
—- 更改任意应用程序角色
–deny alter any application role to user_name;
–go
—- 更改任意用户
–deny alter any user to user_name;
–go
—- 更改任意远程服务绑定
–deny alter any remote service binding to user_name;
–go
—- 更改任意约定
–deny alter any contract to user_name;
–go
—- 更改任意证书
–deny alter any certificate to user_name;
–go
—- 更新
–deny update to user_name;
–go
—- 检查点
–deny checkpoint to user_name;
–go
—- 接管所有权
–deny take ownership to user_name;
–go
—- 控制
–deny control to user_name;
–go
—- 控制聚合
–deny create aggregate to user_name;
–go
—- 连接
–deny connect to user_name;
–go
—- 连接复制
–deny connect replication to user_name;
–go
—- 删除
–deny delete to user_name;
–go
—- 身份验证
–deny authenticate to user_name;
–go
—- 显示计划
–deny showplan to user_name;
–go
—- 选择
–deny select to user_name;
–go
—- 引用
–deny references to user_name;
–go
—- 执行
–deny execute to user_name;
–go
—- 扩展属性
—- 声明数据库引用
—-use database_name
–go
—- 添加扩展注释
–exec sys.sp_addextendedproperty @name=N’description_name’, @value=N’description_value’, @level0type=N’user’,@level0name=N’user_name’;
–go
语法注释
–database_name– 数据库名称 –user_name– 指定在此数据库中用于识别该用户的名称。user_name 为 sysname。– 它的长度最多是 128 个字符。在创建基于 Windows 主体的用户时,除非指定其他用户名,否则 Windows 主体名称将成为用户名。–login_name– 指定要为其创建数据库用户的登录名。login_name 必须是服务器中的有效登录名。– 可以是基于 Windows 主体(用户或组)的登录名,也可以是使用 SQL Server 身份验证的登录名。– 当此 SQL Server 登录名进入数据库时,它将获取正在创建的这个数据库用户的名称和 ID。– 在创建从 Windows 主体映射的登录名时,请使用格式 [<domainName><loginName>]。– 如果 CREATE USER 语句是 SQL 批处理中唯一的语句,则 Windows Azure SQL Databas 将支持 WITH LOGIN 子句。– 如果 CREATE USER 语句不是 SQL 批处理中唯一的语句或在动态 SQL 中执行,则不支持 WITH LOGIN 子句。–with default_schema=architecture_name;– 指定服务器为此数据库用户解析对象名时将搜索的第一个架构。–allow_encrypted_value_modifications={on | off} – 适用范围:SQL Server 2016 (13.x) 到 SQL Server 2017、SQL Database。– 取消在大容量复制操作期间对服务器进行加密元数据检查。这使用户能够在表或数据库之间大容量复制加密数据,– 而无需对数据进行解密。默认为 OFF。–without login– 指定不应将用户映射到现有登录名。–asymmetric KEY asym_key_name– 适用范围:SQL Server 2008 到 SQL Server 2017、SQL Database。– 指定要为其创建数据库用户的非对称密钥。–certificate certificate_name– 适用范围:SQL Server 2008 到 SQL Server 2017、SQL Database。– 指定要为其创建数据库用户的证书。–description_name– 用户自定义用户注释名称。–description_value– 用户自定义用户注释值。
示例
/********** 示例 **********/
– 声明数据库引用
use [testss];
go
– 判断用户是否存在,如果存在则删除,不存在则创建
if exists(select * from sys.database_principals where name=’tests’)
– 把架构修改回来架构自身
alter authorization on schema::[db_accessadmin] to db_accessadmin;
– 删除角色拥有的成员
alter role [db_accessadmin] drop member tests;
– 删除用户
drop user tests;
go
– 创建当前数据库用户自定义用户
create user tests
for login tests
with default_schema=dbo,allow_encrypted_value_modifications=on;
– 拥有的架构
use testss;
go
alter authorization on schema::[db_accessadmin] to tests;
go
– 成员身份
use testss;
go
alter role [db_accessadmin] add member tests;
go
– 安全对象
use testss;
go
– 授予权限
– 备份日志
grant backup log to tests;
go
– 扩展属性
– 声明数据库引用
–use database_name
go
– 添加扩展注释
exec sys.sp_addextendedproperty @name=N’tests_description’, @value=N’ 用户自定义用户描述 ’, @level0type=N’user’,@level0name=N’tests’;
go
示例结果