1. 程式人生 > >SQLServer之修改使用者自定義資料庫使用者

SQLServer之修改使用者自定義資料庫使用者

修改使用者自定義資料庫使用者注意事項

預設架構將是伺服器為此資料庫使用者解析物件名時將搜尋的第一個架構。 除非另外指定,否則預設架構將是此資料庫使用者建立的物件所屬的架構。

如果使用者具有預設架構,則將使用預設架構。 如果使用者不具有預設架構,但該使用者是具有預設架構的組的成員,則將使用該組的預設架構。 如果使用者不具有預設架構而且是多個組的成員,則該使用者的預設架構將是具有最低 principle_id 的 Windows 組的架構和一個顯式設定的預設架構。 如果不能為使用者確定預設架構,則將使用 dbo 架構。

可以將 DEFAULT_SCHEMA 設定為資料庫中當前不存在的架構。 因此,可以在建立架構之前將 DEFAULT_SCHEMA 分配給使用者。

不能為對映到證書或非對稱金鑰的使用者指定 DEFAULT_SCHEMA。

如果使用者是 sysadmin 固定伺服器角色的成員,則忽略 DEFAULT_SCHEMA 的值。 sysadmin 固定伺服器角色的所有成員都有預設架構 dbo

僅當新使用者名稱的 SID 與在資料庫中記錄的 SID 匹配時,才能更改對映到 Windows 登入名或組的使用者的名稱。此檢查將幫助防止資料庫中的 Windows 登入名欺騙。

使用 WITH LOGIN 子句可以將使用者重新對映到一個不同的登入名。 不能使用此子句重新對映以下使用者:不具有登入名的使用者、對映到證書的使用者或對映到非對稱金鑰的使用者。 只能重新對映 SQL 使用者和 Windows 使用者(或組)。 不能使用 WITH LOGIN 子句更改使用者型別,例如將 Windows 帳戶更改為 SQL Server 登入名。

如果滿足以下條件,則使用者的名稱會自動重新命名為登入名。

  • 使用者是一個 Windows 使用者。

  • 名稱是一個 Windows 名稱(包含反斜槓)。

  • 未指定新名稱。

  • 當前名稱不同於登入名。

    如果不滿足上述條件,則不會重新命名使用者,除非呼叫方另外呼叫了 NAME 子句。

被對映到 SQL Server 登入名、證書或非對稱金鑰的使用者名稱不能包含反斜槓字元 (\)。

更改使用者名稱需要具有 ALTER ANY USER 許可權。

更改使用者的目標登入名需要對資料庫擁有 CONTROL 許可權。

若要更改對資料庫擁有 CONTROL 許可權的使用者名稱名稱,則需要對資料庫擁有 CONTROL 許可權。

更改預設架構或語言需要對使用者擁有 ALTER 許可權。 使用者可更改自己的預設架構或語言。

使用SSMS資料庫管理工具修改使用者自定義資料庫使用者

1、連線伺服器-》在物件資源管理器視窗-》展開資料庫-》選擇資料庫並展開-》展開安全性-》展開使用者-》選擇要修改的使用者右鍵點選-》選擇屬性。

2、在資料庫使用者彈出框-》點選常規-》修改使用者預設架構。

3、在資料庫使用者彈出框-》點選擁有的架構-》新增此使用者擁有的架構。

4、在資料庫使用者彈出框-》點選成員身份-》新增或者刪除資料庫角色成員身份。

5、在資料庫使用者彈出框-》點選安全物件-》點選搜尋新增安全物件-》點選安全物件修改安全物件擁有的許可權。

6、在資料庫使用者彈出框-》點選擴充套件屬性-》新增或者刪除擴充套件屬性。

使用T-SQL指令碼修改使用者自定義資料庫使用者

語法

--建立使用者自定義資料庫使用者
--宣告資料庫引用
use database_name;
go
--修改使用者自定義資料庫使用者
alter user user_name
with 
name=new_user_name,
default_schema={ schemaname | null },
login=login_name,
password='password' [old_password='old_password'],
default_language={ none | <lcid> | <language_name> | <language alias> },
allow_encrypted_value_modifications={ on | off } 

--新增擁有的架構
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
刪除擁有的架構(把架構付給自己就行了)
go
alter authorization on schema::[db_accessadmin] to db_accessadmin;
go
alter authorization on schema::[db_backupoperator] to db_backupoperator;
go
alter authorization on schema::[db_datareader] to db_datareader;
go
alter authorization on schema::[db_datawriter] to db_datawriter;
go
alter authorization on schema::[db_ddladmin] to db_ddladmin;
go
alter authorization on schema::[db_denydatareader] to db_denydatareader;
go
alter authorization on schema::[db_denydatawriter] to db_denydatawriter;
go
alter authorization on schema::[db_owner] to db_owner;
go
alter authorization on schema::[db_securityadmin] to db_securityadmin;
go
alter authorization on schema::[guest] to guest;
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
alter role [db_accessadmin] drop member user_name;
go
alter role [db_backupoperator] drop member user_name;
go
alter role [db_datareader] drop member user_name;
go
alter role [db_datawriter] drop member user_name;
go
alter role [db_ddladmin] drop member user_name;
go
alter role [db_denydatareader] drop member user_name;
go
alter role [db_denydatawriter] drop member user_name;
go
alter role [db_owner] drop member user_name;
go
alter role [db_securityadmin] drop 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
--刪除擴充套件註釋
exec sys.sp_dropextendedproperty @name=N'description_name', @level0type=N'user',@level0name=N'user_name'
go

語法註釋

--database_name
--資料庫名稱
--user_name
--指定在此資料庫中用於識別該使用者的名稱。
--login=login_name
--通過將使用者的安全識別符號(SID)更改為另一個登入名的SID,使使用者重新對映到該登入名。
--如果ALTER USER語句是SQL批處理中唯一的語句,則Windows Azure SQL Database將支援WITH LOGIN子句。
--如果 ALTER USER 語句不是SQL批處理中唯一的語句或在動態SQL中執行,則不支援WITH LOGIN子句。
--name=new_user_name
--指定此使用者的新名稱。 newUserName 不能已存在於當前資料庫中。
--default_schema={ schemaname | null }
--指定伺服器在解析此使用者的物件名時將搜尋的第一個架構。
--將預設架構設定為NULL將從Windows組中刪除預設架構。Windows使用者不能使用NULL選項。
--password='password' [old_password='old_password']
--適用範圍:SQL Server 2012 (11.x)到SQL Server 2017、SQL Database。
--指定正在更改的使用者的密碼。 密碼是區分大小寫的。
--old_password='old_password'
--適用範圍:SQL Server 2012 (11.x)到SQL Server 2017、SQL Database。
--將替換為“password”的當前使用者密碼。密碼是區分大小寫的。
--除非擁有ALTER ANY USER許可權,否則需要具有OLD_PASSWORD才能更改密碼。需要OLD_PASSWORD可防止擁有IMPERSONATION許可權的使用者更改密碼。
--此選項僅適用於包含的使用者。
--default_language={ none | <lcid> | <language_name> | <language alias> }
--適用範圍: SQL Server 2012 (11.x) 到 SQL Server 2017。
--指定將指派給使用者的預設語言。如果將此選項設定為NONE,則預設語言將設定為資料庫的當前預設語言。如果之後更改資料庫的預設語言,使用者的預設語言將保持不變。
--DEFAULT_LANGUAGE可以為本地 ID (lcid)、語言的名稱或語言別名。
--此選項只能在包含資料庫中指定,且只能用於包含的使用者。
--allow_encrypted_value_modifications={ on | off }
--適用範圍:SQL Server 2016 (13.x)到SQL Server 2017、SQL Database。
--取消在大容量複製操作期間對伺服器進行加密元資料檢查。 這使使用者能夠在表或資料庫之間大容量複製加密資料,而無需對資料進行解密。 預設為 OFF。
--description_name
--使用者自定義使用者註釋名稱
--description_value
--使用者自定義使用者註釋值

示例

/**********示例**********/
--宣告資料庫引用
use [testss];
go

--新增擁有的架構
alter authorization on schema::[db_accessadmin] to test1;
go
--刪除擁有的架構
alter authorization on schema::[db_accessadmin] to db_accessadmin;
go

--新增成員身份
alter role [db_backupoperator] add member test1;
go
alter role [db_datareader] add member test1;
go
--刪除成員身份
alter role [db_backupoperator] drop member test1;
go
alter role [db_datareader] drop member test1;
go

--安全物件
--授予許可權
--備份日誌
grant backup log to test1;
go

--擴充套件屬性
--刪除擴充套件屬性
exec sys.sp_dropextendedproperty @name=N'tests_description', @level0type=N'user',@level0name=N'test1'
go
--新增擴充套件註釋
exec sys.sp_addextendedproperty	@name=N'tests_description', @value=N'使用者自定義使用者描述', @level0type=N'user',@level0name=N'test1';
go

--修改當前資料庫使用者自定義使用者屬性
alter user test1
with 
name=test1,
default_schema=dbo,
--login=tests,
--password='1234' old_password='1234',
--default_language=English,
allow_encrypted_value_modifications=off;
go

示例結果