創建資料庫架構註意事項 包含 CREATE SCHEMA AUTHORIZATION 但未指定名稱的語句僅允許用於向後相容性。 該語句未引起錯誤,但未創建一個架構。 包含 CREATE SCHEMA AUTHORIZATION 但未指定名稱的語句僅允許用於向後相容性。 該語句未引起錯誤,但未創建一個 ...
創建資料庫架構註意事項
包含 CREATE SCHEMA AUTHORIZATION 但未指定名稱的語句僅允許用於向後相容性。 該語句未引起錯誤,但未創建一個架構。
CREATE SCHEMA 可以在單條語句中創建架構以及該架構所包含的表和視圖,並授予對任何安全對象的 GRANT、REVOKE 或 DENY 許可權。 此語句必須作為一個單獨的批處理執行。 CREATE SCHEMA 語句所創建的對象將在要創建的架構內進行創建。
CREATE SCHEMA 事務是原子級的。 如果 CREATE SCHEMA 語句執行期間出現任何錯誤,則不會創建任何指定的安全對象,也不會授予任何許可權。
由 CREATE SCHEMA 創建的安全對象可以任何順序列出,但引用其他視圖的視圖除外。 在這種情況下,被引用的視圖必須在引用它的視圖之前創建。
因此,GRANT 語句可以在創建某個對象自身之前對該對象授予許可權,CREATE VIEW 語句也可以出現在創建該視圖所引用表的 CREATE TABLE 語句之前。 同樣,CREATE TABLE 語句可以在 CREATE SCHEMA 語句定義表之前聲明表的外鍵。
執行 CREATE SCHEMA 的主體可以將另一個資料庫主體指定為要創建的架構的所有者。 完成此操作需要另外的許可權,如本主題下文中的“許可權”部分所述。
新架構由以下資料庫級別主體之一擁有:資料庫用戶、資料庫角色或應用程式角色。 在架構內創建的對象由架構所有者擁有,這些對象在 sys.objects 中的 principal_id為 NULL。 架構所包含對象的所有權可轉讓給任何資料庫級主體,但架構所有者始終保留對該架構內對象的 CONTROL 許可權。
隱式架構和用戶創建
在某些情況下,用戶可在沒有資料庫用戶帳戶(資料庫中的資料庫主體)的情況下使用資料庫。 這可發生在以下情況中:
-
登錄名具有 CONTROL SERVER 特權。
-
Windows 用戶沒有單獨的資料庫用戶帳戶(資料庫中的資料庫主體),但以具有資料庫用戶帳戶(Windows 組的資料庫主體)的 Windows 組成員的身份訪問資料庫。
如果沒有資料庫用戶帳戶的用戶在不指定現有架構的情況下創建對象,則將在資料庫中自動為該用戶創建資料庫主體和預設架構。 創建的資料庫主體和架構採用的名稱將與連接到 SQL Server 時用戶使用的名稱( SQL Server 身份驗證登錄名或 Windows 用戶名)相同。
若要允許基於 Windows 組的用戶創建和擁有對象,此行為很有必要。 但這種行為可能將導致意外創建架構和用戶。 為了避免隱式創建用戶和架構,請儘可能顯式創建資料庫主體和分配預設架構。 或者,在資料庫中創建對象時,使用由兩部分或三部分組成的對象名稱顯式聲明現有架構。
當前支持不指定架構名稱的 CREATE SCHEMA 語句,目的是為了向後相容。 此類語句並不在資料庫中實際創建架構,但它們會創建表和視圖,並授予許可權。 主體不需要 CREATE SCHEMA 許可權來執行這一早期形式的 CREATE SCHEMA,因為不會創建任何架構。 此功能將從 SQL Server 的未來版本中刪除。
需要對資料庫擁有 CREATE SCHEMA 許可權。
若要創建在 CREATE SCHEMA 語句中指定的對象,用戶必須擁有相應的 CREATE 許可權。
若要指定其他用戶作為所創建架構的所有者,則調用方必須具有對該用戶的 IMPERSONATE 許可權。 如果指定一個資料庫角色作為所有者,則調用方必須擁有該角色的成員身份或對該角色擁有 ALTER 許可權。
使用SSMS資料庫管理工具創建資料庫架構
1、連接伺服器-》展開資料庫文件夾-》選擇資料庫並展開-》展開安全性-》展開架構-》右鍵單擊架構文件夾選擇創建架構。
2、在新建架構彈出框-》點擊常規-》輸入新建架構名稱-》點擊搜索選擇架構所有者。
3、在新建架構彈出框-》點擊許可權-》點擊搜索選擇新建架構的用戶或角色-》選擇用戶或角色後選擇新建架構的許可權。
4、在新建架構彈出框-》點擊擴展屬性-》輸入擴展屬性名稱和值-》點擊確定。
5、不需要刷新即可在對象資源管理器中查看創建結果。
使用T-SQL腳本創建資料庫架構
語法
----聲明資料庫引用
--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
示例結果:使用T-SQL腳本創建資料庫架構需要刷新資料庫才能查看結果。