1. 程式人生 > >SQLServer之修改數據庫架構

SQLServer之修改數據庫架構

sts dex 對象類型 object 拒絕 所有 相關 format 庫文件

修改數據庫架構註意事項

用戶與架構完全分離。

ALTER SCHEMA 僅可用於在同一數據庫中的架構之間移動安全對象。 若要更改或刪除架構中的安全對象,請使用特定於該安全對象的 ALTER 或 DROP 語句。

如果對 securable_name 使用了由一部分組成的名稱,則將使用當前生效的名稱解析規則查找該安全對象。

將安全對象移入新架構時,將刪除與該安全對象關聯的全部權限。 如果已顯式設置安全對象的所有者,則該所有者保持不變。 如果安全對象的所有者已設置為 SCHEMA OWNER,則該所有者將保持為 SCHEMA OWNER;但移動之後,SCHEMA OWNER 將解析為新架構的所有者。 新所有者的 principal_id 將為 NULL。

無論是 sys.sql_modules 目錄視圖的 definition 列中的相應對象,還是使用 OBJECT_DEFINITION 內置函數獲取的相應對象,移動存儲過程、函數、視圖或觸發器都不會更改其架構名稱(如有)。 因此,我們建議不要使用 ALTER SCHEMA 移動這些對象類型。 而是刪除對象,然後在新架構中重新創建該對象。

移動表或同義詞不會自動更新對該對象的引用。 必須手動修改引用已移動對象的任何對象。 例如,如果移動了某個表,並且觸發器中引用了該表,則必須修改觸發器以反映新的架構名稱。 請使用 sys.sql_expression_dependencies 列出該對象上的依賴關系,然後再進行移動。

若要通過使用 SQL Server Management Studio 更改表的架構,請在對象資源管理器中右鍵單擊該表,然後單擊“設計”。 按 F4 以打開“屬性”窗口。 在“架構”框中,選擇新架構。

若要從另一個架構中傳輸安全對象,當前用戶必須擁有對該安全對象(非架構)的 CONTROL 權限,並擁有對目標架構的 ALTER 權限。

如果已為安全對象指定 EXECUTE AS OWNER,且所有者已設置為 SCHEMA OWNER,則用戶還必須擁有對目標架構所有者的 IMPERSONATE 權限。

在移動安全對象後,將刪除與所傳輸的安全對象相關聯的所有權限。

使用SSMS數據庫管理工具修改架構

1、連接服務器-》展開數據庫文件夾-》選擇數據庫並展開-》展開安全性文件夾-》展開架構文件夾-》選擇要修改的架構右鍵點擊屬性。

技術分享圖片

2、在架構屬性彈出框-》點擊常規-》點擊搜索修改架構所有者。

技術分享圖片

3、在架構屬性彈出框-》點擊權限-》點擊搜索選擇用戶或角色-》選擇用戶或角色權限。

技術分享圖片

4、在架構屬性彈出框-》點擊擴展屬性-》新增或者刪除擴展屬性。

技術分享圖片

使用T-SQL腳本修改數據庫架構

語法

--聲明數據庫引用
use database_name;
go

修改用戶或者角色
alter authorization on schema::[ArchitectureName] to [schemaOwner];
go

--修改用戶或角色權限
--授予插入
grant insert on schema::[ArchitectureName] to [rolename_username];
go

--授予查看定義
grant view definition on schema::[ArchitectureName] to [rolename_username];
go

--授予查看更改跟蹤
grant view change tracking on schema::[ArchitectureName] to [rolename_username];
go

--授予創建序列
grant create sequence on schema::[ArchitectureName] to [rolename_username];
go

--授予更改
grant alter on schema::[ArchitectureName] to [rolename_username];
go
 
 --授予更新
grant update on schema::[ArchitectureName] to [rolename_username];
go

--接管所有權
grant take ownership on schema::[ArchitectureName] to [rolename_username];
go

--授予控制
grant control on schema::[ArchitectureName] to [rolename_username];
go

--授予刪除
grant delete on schema::[ArchitectureName] to [rolename_username];
go

--授予選擇
grant select on schema::[ArchitectureName] to [rolename_username];
go

--授予引用
grant references on schema::[ArchitectureName] to [rolename_username];
go

--授予執行
grant execute on schema::[ArchitectureName] to [rolename_username];
go

--授予並允許轉授插入
grant insert on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--授予並允許轉授查看定義
grant view definition on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--授予並允許轉授查看更改跟蹤
grant view change tracking on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--授予並允許轉授創建序列
grant create sequence on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--授予並允許轉授更改
grant alter on schema::[ArchitectureName] to [rolename_username] with grant option;
go
 
 --授予並允許轉授更新
grant update on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--接管並允許轉授所有權
grant take ownership on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--授予並允許轉授控制
grant control on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--授予並允許轉授刪除
grant delete on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--授予並允許轉授選擇
grant select on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--授予並允許轉授引用
grant references on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--授予並允許轉授執行
grant execute on schema::[ArchitectureName] to [rolename_username] with grant option;
go

--拒絕插入
deny insert on schema::[ArchitectureName] to [rolename_username];
go

--拒絕查看定義
deny view definition on schema::[ArchitectureName] to [rolename_username];
go

--拒絕查看更改跟蹤
deny view change tracking on schema::[ArchitectureName] to [rolename_username];
go

--拒絕創建序列
deny create sequence on schema::[ArchitectureName] to [rolename_username];
go

--拒絕更改
deny alter on schema::[ArchitectureName] to [rolename_username];
go
 
--拒絕更新
deny update on schema::[ArchitectureName] to [rolename_username];
go

--拒絕所有權
deny take ownership on schema::[ArchitectureName] to [rolename_username];
go

--拒絕控制
deny control on schema::[ArchitectureName] to [rolename_username];
go

--拒絕刪除
deny delete on schema::[ArchitectureName] to [rolename_username];
go

--拒絕選擇
deny select on schema::[ArchitectureName] to [rolename_username];
go

--拒絕引用
deny references on schema::[ArchitectureName] to [rolename_username];
go

--拒絕執行
deny execute on schema::[ArchitectureName] to [rolename_username];
go

刪除數據庫架構擴展屬性
exec sys.sp_dropextendedproperty @name=N‘extendedAttributeName‘,@level0type=N‘schema‘,@level0name=N‘extendedAttributeValue‘
go

創建數據庫架構擴屬性
exec sys.sp_addextendedproperty @name=N‘newExtendedAttributeName‘,@value=N‘newExtendedAttributeValue‘ , @level0type=N‘schema‘,@level0name=N‘ArchitectureName‘
go

--修改數據庫架構
alter schema schema_name(你要修改成得新架構)
transfer { object | type | xml schema collection } securable_name (原架構名.對象名);
go   

語法解析

--語法解析
--schema_name
--當前數據庫中的架構名稱,安全對象將移入其中。其數據類型不能為sys或information_schema。

--ArchitectureName
--架構名稱

--schemaOwner
--架構所有者

--rolename_username
--用戶或角色

--extendedAttributeName
--要刪除的擴展屬性名稱

--extendedAttributeValue
--要刪除的擴展屬性值

--newExtendedAttributeName
--新添加擴展屬性名稱

--newExtendedAttributeValue
--新添加的擴展屬性值

--transfer { object | type | xml schema collection }
--更改其所有者的實體的類。object是默認值。

--securable_name
--要移入架構中的架構範圍內的安全對象的一部分或兩部分名稱。

示例

--聲明數據庫引用
use [testss];
go						   

--修改數據庫架構
--修改架構所有者
alter authorization on schema::[testarchitecture] to [db_datareader];
go

--修改用戶或角色權限
--授予插入
grant insert on schema::[testarchitecture] to [guest];
go

--授予查看定義
grant view definition on schema::[testarchitecture] to [guest];
go

--授予查看更改跟蹤
grant view change tracking on schema::[testarchitecture] to [guest];
go

--授予創建序列
grant create sequence on schema::[testarchitecture] to [guest];
go

--授予更改
grant alter on schema::[testarchitecture] to [guest];
go
 
 --授予更新
grant update on schema::[testarchitecture] to [guest];
go

--接管所有權
grant take ownership on schema::[testarchitecture] to [guest];
go

--授予控制
grant control on schema::[testarchitecture] to [guest];
go

--授予刪除
grant delete on schema::[testarchitecture] to [guest];
go

--授予選擇
grant select on schema::[testarchitecture] to [guest];
go

--授予引用
grant references on schema::[testarchitecture] to [guest];
go

--授予執行
grant execute on schema::[testarchitecture] to [guest];
go

----授予並允許轉授插入
--grant insert on schema::[testarchitecture] to [[guest]] with grant option;
--go

----授予並允許轉授查看定義
--grant view definition on schema::[testarchitecture] to [guest] with grant option;
--go

----授予並允許轉授查看更改跟蹤
--grant view change tracking on schema::[testarchitecture] to [guest] with grant option;
--go

----授予並允許轉授創建序列
--grant create sequence on schema::[testarchitecture] to [guest] with grant option;
--go

----授予並允許轉授更改
--grant alter on schema::[testarchitecture] to [guest] with grant option;
--go
 
-- --授予並允許轉授更新
--grant update on schema::[testarchitecture] to [guest] with grant option;
--go

----接管並允許轉授所有權
--grant take ownership on schema::[testarchitecture] to [guest] with grant option;
--go

----授予並允許轉授控制
--grant control on schema::[testarchitecture] to [guest] with grant option;
--go

----授予並允許轉授刪除
--grant delete on schema::[testarchitecture] to [guest] with grant option;
--go

----授予並允許轉授選擇
--grant select on schema::[testarchitecture] to [guest] with grant option;
--go

----授予並允許轉授引用
--grant references on schema::[testarchitecture] to [guest] with grant option;
--go

----授予並允許轉授執行
--grant execute on schema::[testarchitecture] to [guest] with grant option;
--go

----拒絕插入
--deny insert on schema::[testarchitecture] to [guest];
--go

----拒絕查看定義
--deny view definition on schema::[testarchitecture] to [guest];
--go

----拒絕查看更改跟蹤
--deny view change tracking on schema::[testarchitecture] to [guest];
--go

----拒絕創建序列
--deny create sequence on schema::[testarchitecture] to [guest];
--go

----拒絕更改
--deny alter on schema::[testarchitecture] to [guest];
--go
 
----拒絕更新
--deny update on schema::[testarchitecture] to [guest];
--go

----拒絕所有權
--deny take ownership on schema::[testarchitecture] to [guest];
--go

----拒絕控制
--deny control on schema::[testarchitecture] to [guest];
--go

----拒絕刪除
--deny delete on schema::[testarchitecture] to [guest];
--go

----拒絕選擇
--deny select on schema::[testarchitecture] to [guest];
--go

----拒絕引用
--deny references on schema::[testarchitecture] to [guest];
--go

----拒絕執行
--deny execute on schema::[testarchitecture] to [guest];
--go

--刪除數據庫架構擴展屬性
exec sys.sp_dropextendedproperty @name=N‘testcrituer‘ , @level0type=N‘schema‘,@level0name=N‘testarchitecture‘
go

--創建數據庫架構擴屬性
exec sys.sp_addextendedproperty @name=N‘testcrituer‘, @value=N‘測試創建數據庫架構‘ , @level0type=N‘schema‘,@level0name=N‘testarchitecture‘
go

--修改架構下對象所有權,從[testarchitecture]轉移到[dbo]
alter schema [dbo] transfer [testarchitecture].[schema_table1];
go

示例結果:執行T-SQL腳本需要刷新表文件夾才能查看執行結果。

技術分享圖片

SQLServer之修改數據庫架構