SQLServer之建立資料庫架構
建立資料庫架構注意事項
包含 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指令碼建立資料庫架構需要重新整理資料庫才能檢視結果。