SQLServer之建立数据库架构

建立数据库架构注意事项

包含 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 权限。

使用SSMS数据库管理工具建立数据库架构

一、链接服务器-》展开数据库文件夹-》选择数据库并展开-》展开安全性-》展开架构-》右键单击架构文件夹选择建立架构。

二、在新建架构弹出框-》点击常规-》输入新建架构名称-》点击搜索选择架构全部者。

三、在新建架构弹出框-》点击权限-》点击搜索选择新建架构的用户或角色-》选择用户或角色后选择新建架构的权限。

四、在新建架构弹出框-》点击扩展属性-》输入扩展属性名称和值-》点击肯定。

五、不须要刷新便可在对象资源管理器中查看建立结果。

使用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脚本建立数据库架构须要刷新数据库才能查看结果。

相关文章
相关标签/搜索