若是已忽略 FOR LOGIN,則新的數據庫用戶將被映射到同名的SQL Server登陸名。數據庫
默認架構將是服務器爲此數據庫用戶解析對象名時將搜索的第一個架構。 除非另外指定,不然默認架構將是此數據庫用戶建立的對象所屬的架構。windows
若是用戶具備默認架構,則將使用默認架構。 若是用戶不具備默認架構,但該用戶是具備默認架構的組的成員,則將使用該組的默認架構。 若是用戶不具備默認架構並且是多個組的成員,則該用戶的默認架構將是具備最低principle_id的Windows組的架構和一個顯式設置的默認架構。(不可能將可用的默認架構之一顯式選做首選架構。)若是不能爲用戶肯定默認架構,則將使用 dbo 架構。安全
DEFAULT_SCHEMA可在建立它所指向的架構前進行設置。服務器
在建立映射到證書或非對稱密鑰的用戶時,不能指定DEFAULT_SCHEMA。架構
若是用戶是sysadmin固定服務器角色的成員,則忽略DEFAULT_SCHEMA的值。sysadmin固定服務器角色的全部成員都有默認架構dbo。app
WITHOUT LOGIN子句可建立不映射到SQL Server登陸名的用戶。它能夠做爲guest鏈接到其餘數據庫。能夠將權限分配給這一沒有登陸名的用戶,當安全上下文更改成沒有登陸名的用戶時,原始用戶將收到無登陸名用戶的權限。 dom
只有映射到Windows主體的用戶才能包含反斜槓字符 ()。函數
不能使用CREATE USER建立guest用戶,由於每一個數據庫中均已存在guest用戶。可經過授予guest用戶CONNECT權限來啓用該用戶,以下所示:工具
能夠在 sys.database_principals 目錄視圖中查看有關數據庫用戶的信息。加密
一、鏈接服務器-》在對象資源管理器窗口選擇數據庫-》展開數據庫-》展開安全性-》展開用戶-》右鍵點擊用戶-》選擇新建。
二、在數據庫用戶-新建彈出框-》點擊常規-》選擇用戶類型-》輸入用戶名-》選擇登陸名-》選擇用戶所屬架構。
三、在數據庫用戶-新建彈出框-》選擇用戶所擁有的架構。
四、在數據庫用戶-新建彈出框-》點擊成員身份-》選擇數據庫成員身份。
五、在數據庫用戶-新建彈出框-》點擊搜索選擇一個安全對象-》選擇安全對象之後選擇安全對象所擁有的權限。
六、在數據庫用戶-新建彈出框-》選擇擴展屬性-》輸入註釋名稱-》輸入註釋值-》點擊肯定。
七、不須要刷新便可在對象資源管理器查看建立結果。
----建立用戶自定義數據庫用戶 ----聲明數據庫引用 --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 201七、SQL Database。
--取消在大容量複製操做期間對服務器進行加密元數據檢查。這使用戶可以在表或數據庫之間大容量複製加密數據,
--而無需對數據進行解密。默認爲 OFF。
--without login
--指定不該將用戶映射到現有登陸名。
--asymmetric KEY asym_key_name
--適用範圍:SQL Server 2008到SQL Server 201七、SQL Database。
--指定要爲其建立數據庫用戶的非對稱密鑰。
--certificate certificate_name
--適用範圍:SQL Server 2008到SQL Server 201七、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