SQLServer创建用户自定义数据库用户
創建用戶自定義數據庫用戶注意事項
如果已忽略 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 主體映射的登錄名時,請使用格式 [<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 復制代碼示例結果
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的SQLServer创建用户自定义数据库用户的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python-判断语句介绍
- 下一篇: Python的5种传参姿势,两分钟就能了