包含 CREATE SCHEMA AUTHORIZATION 但未指定名稱的語句僅容許用於向後兼容性。 該語句未引發錯誤,但未建立一個架構。數據庫
CREATE SCHEMA 能夠在單條語句中建立架構以及該架構所包含的表和視圖,並授予對任何安全對象的 GRANT、REVOKE 或 DENY 權限。 此語句必須做爲一個單獨的批處理執行。 CREATE SCHEMA 語句所建立的對象將在要建立的架構內進行建立。安全
CREATE SCHEMA 事務是原子級的。 若是 CREATE SCHEMA 語句執行期間出現任何錯誤,則不會建立任何指定的安全對象,也不會授予任何權限。bash
由 CREATE SCHEMA 建立的安全對象能夠任何順序列出,但引用其餘視圖的視圖除外。 在這種狀況下,被引用的視圖必須在引用它的視圖以前建立。服務器
所以,GRANT 語句能夠在建立某個對象自身以前對該對象授予權限,CREATE VIEW 語句也能夠出如今建立該視圖所引用表的 CREATE TABLE 語句以前。 一樣,CREATE TABLE 語句能夠在 CREATE SCHEMA 語句定義表以前聲明表的外鍵。架構
執行 CREATE SCHEMA 的主體能夠將另外一個數據庫主體指定爲要建立的架構的全部者。 完成此操做須要另外的權限,如本主題下文中的「權限」部分所述。ide
新架構由如下數據庫級別主體之一擁有:數據庫用戶、數據庫角色或應用程序角色。 在架構內建立的對象由架構全部者擁有,這些對象在 sys.objects 中的 principal_id爲 NULL。 架構所包含對象的全部權可轉讓給任何數據庫級主體,但架構全部者始終保留對該架構內對象的 CONTROL 權限。工具
隱式架構和用戶建立測試
在某些狀況下,用戶可在沒有數據庫用戶賬戶(數據庫中的數據庫主體)的狀況下使用數據庫。 這可發生在如下狀況中:ui
登陸名具備 CONTROL SERVER 特權。spa
Windows 用戶沒有單獨的數據庫用戶賬戶(數據庫中的數據庫主體),但以具備數據庫用戶賬戶(Windows 組的數據庫主體)的 Windows 組成員的身份訪問數據庫。
若是沒有數據庫用戶賬戶的用戶在不指定現有架構的狀況下建立對象,則將在數據庫中自動爲該用戶建立數據庫主體和默認架構。 建立的數據庫主體和架構採用的名稱將與鏈接到 SQL Server 時用戶使用的名稱( SQL Server 身份驗證登陸名或 Windows 用戶名)相同。
若要容許基於 Windows 組的用戶建立和擁有對象,此行爲頗有必要。 但這種行爲可能將致使意外建立架構和用戶。 爲了不隱式建立用戶和架構,請儘量顯式建立數據庫主體和分配默認架構。 或者,在數據庫中建立對象時,使用由兩部分或三部分組成的對象名稱顯式聲明現有架構。
當前支持不指定架構名稱的 CREATE SCHEMA 語句,目的是爲了向後兼容。 此類語句並不在數據庫中實際建立架構,但它們會建立表和視圖,並授予權限。 主體不須要 CREATE SCHEMA 權限來執行這一早期形式的 CREATE SCHEMA,由於不會建立任何架構。 此功能將從 SQL Server 的將來版本中刪除。
須要對數據庫擁有 CREATE SCHEMA 權限。
若要建立在 CREATE SCHEMA 語句中指定的對象,用戶必須擁有相應的 CREATE 權限。
若要指定其餘用戶做爲所建立架構的全部者,則調用方必須具備對該用戶的 IMPERSONATE 權限。 若是指定一個數據庫角色做爲全部者,則調用方必須擁有該角色的成員身份或對該角色擁有 ALTER 權限。
一、鏈接服務器-》展開數據庫文件夾-》選擇數據庫並展開-》展開安全性-》展開架構-》右鍵單擊架構文件夾選擇建立架構。
二、在新建架構彈出框-》點擊常規-》輸入新建架構名稱-》點擊搜索選擇架構全部者。
三、在新建架構彈出框-》點擊權限-》點擊搜索選擇新建架構的用戶或角色-》選擇用戶或角色後選擇新建架構的權限。
四、在新建架構彈出框-》點擊擴展屬性-》輸入擴展屬性名稱和值-》點擊肯定。
五、不須要刷新便可在對象資源管理器中查看建立結果。
----聲明數據庫引用
--use database_name;
--go
----建立數據庫架構
--create schema schema_name authorization owner_name
--{ table_definition | view_definition | grant_statement | revoke_statement | deny_statement }
--;
--go
複製代碼
--語法解析
--database_name
--架構所在的數據庫名
--schema_name
--在數據庫內標識架構的名稱。
--authorization owner_name
--指定將擁有架構的數據庫級主體的名稱。此主體還能夠擁有其餘架構,而且能夠不使用當前架構做爲其默認架構。
--table_definition
--指定在架構內建立表的CREATE TABLE語句。執行此語句的主體必須對當前數據庫具備CREATE TABLE權限。
--view_definition
--指定在架構內建立視圖的CREATE VIEW語句。執行此語句的主體必須對當前數據庫具備CREATE VIEW權限。
--grant_statement
--指定可對除新架構外的任何安全對象授予權限的GRANT語句。
--revoke_statement
--指定可對除新架構外的任何安全對象撤消權限的REVOKE語句。
--deny_statement
--指定可對除新架構外的任何安全對象拒絕授予權限的DENY語句。
--聲明數據庫引用
use [testss];
go
if exists(select * from sys.schemas where name='testarchitecture')
--刪除數據庫架構註釋
exec sys.sp_dropextendedproperty @name=N'testcrituer' , @level0type=N'schema',@level0name=N'testarchitecture';
--刪除架構下的全部表
if exists(select * from sys.tables where name='schema_table1')
drop table [testarchitecture].[schema_table1];
go
--刪除數據庫架構
drop schema testarchitecture;
go
--建立數據庫架構
create schema [testarchitecture] authorization [db_accessadmin]
create table schema_table1
(
id int identity(1,1) not null,
name nvarchar(50),
primary key clustered(id asc) with(ignore_dup_key=off) on [primary]
)on [primary]
go
--授予插入
grant insert on schema::[testarchitecture] to [public];
go
--授予查看定義
grant view definition on schema::[testarchitecture] to [public];
go
--授予查看更改跟蹤
grant view change tracking on schema::[testarchitecture] to [public];
go
--授予建立序列
grant create sequence on schema::[testarchitecture] to [public];
go
--授予更改
grant alter on schema::[testarchitecture] to [public];
go
--授予更新
grant update on schema::[testarchitecture] to [public];
go
--接管全部權
grant take ownership on schema::[testarchitecture] to [public];
go
--授予控制
grant control on schema::[testarchitecture] to [public];
go
--授予刪除
grant delete on schema::[testarchitecture] to [public];
go
--授予選擇
grant select on schema::[testarchitecture] to [public];
go
--授予引用
grant references on schema::[testarchitecture] to [public];
go
--授予執行
grant execute on schema::[testarchitecture] to [public];
go
----授予並容許轉授插入
--grant insert on schema::[testarchitecture] to [public] with grant option;
--go
----授予並容許轉授查看定義
--grant view definition on schema::[testarchitecture] to [public] with grant option;
--go
----授予並容許轉授查看更改跟蹤
--grant view change tracking on schema::[testarchitecture] to [public] with grant option;
--go
----授予並容許轉授建立序列
--grant create sequence on schema::[testarchitecture] to [public] with grant option;
--go
----授予並容許轉授更改
--grant alter on schema::[testarchitecture] to [public] with grant option;
--go
-- --授予並容許轉授更新
--grant update on schema::[testarchitecture] to [public] with grant option;
--go
----接管並容許轉授全部權
--grant take ownership on schema::[testarchitecture] to [public] with grant option;
--go
----授予並容許轉授控制
--grant control on schema::[testarchitecture] to [public] with grant option;
--go
----授予並容許轉授刪除
--grant delete on schema::[testarchitecture] to [public] with grant option;
--go
----授予並容許轉授選擇
--grant select on schema::[testarchitecture] to [public] with grant option;
--go
----授予並容許轉授引用
--grant references on schema::[testarchitecture] to [public] with grant option;
--go
----授予並容許轉授執行
--grant execute on schema::[testarchitecture] to [public] with grant option;
--go
----拒絕插入
--deny insert on schema::[testarchitecture] to [public];
--go
----拒絕查看定義
--deny view definition on schema::[testarchitecture] to [public];
--go
----拒絕查看更改跟蹤
--deny view change tracking on schema::[testarchitecture] to [public];
--go
----拒絕建立序列
--deny create sequence on schema::[testarchitecture] to [public];
--go
----拒絕更改
--deny alter on schema::[testarchitecture] to [public];
--go
----拒絕更新
--deny update on schema::[testarchitecture] to [public];
--go
----拒絕全部權
--deny take ownership on schema::[testarchitecture] to [public];
--go
----拒絕控制
--deny control on schema::[testarchitecture] to [public];
--go
----拒絕刪除
--deny delete on schema::[testarchitecture] to [public];
--go
----拒絕選擇
--deny select on schema::[testarchitecture] to [public];
--go
----拒絕引用
--deny references on schema::[testarchitecture] to [public];
--go
----拒絕執行
--deny execute on schema::[testarchitecture] to [public];
--go
--用戶或者角色
alter authorization on schema::[testarchitecture] to [public];
go
--建立擴展屬性
exec sys.sp_addextendedproperty @name=N'testcrituer', @value=N'測試建立數據庫架構' , @level0type=N'schema',@level0name=N'testarchitecture'
go
複製代碼