该语句未引起错误,但未创建一个架构。
CREATE SCHEMA 语句所创建的对象将在要创建的架构内进行创建。
如果 CREATE SCHEMA 语句执行期间出现任何错误,则不会创建任何指定的安全对象,也不会授予任何权限。
在这种情况下,被引用的视图必须在引用它的视图之前创建。
同样,CREATE TABLE 语句可以在 CREATE SCHEMA 语句定义表之前声明表的外键。
完成此操作需要另外的权限,如本主题下文中的“权限”部分所述。
架构所包含对象的所有权可转让给任何数据库级主体,但架构所有者始终保留对该架构内对象的 CONTROL 权限。
隐式架构和用户创建
这可发生在以下情况中:
-
登录名具有 CONTROL SERVER 特权。
-
Windows 用户没有单独的数据库用户帐户(数据库中的数据库主体),但以具有数据库用户帐户(Windows 组的数据库主体)的 Windows 组成员的身份访问数据库。
SQL Server 身份验证登录名或 Windows 用户名)相同。
或者,在数据库中创建对象时,使用由两部分或三部分组成的对象名称显式声明现有架构。
SQL Server 的未来版本中删除。
需要对数据库拥有 CREATE SCHEMA 权限。
若要创建在 CREATE SCHEMA 语句中指定的对象,用户必须拥有相应的 CREATE 权限。
如果指定一个数据库角色作为所有者,则调用方必须拥有该角色的成员身份或对该角色拥有 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脚本创建数据库架构需要刷新数据库才能查看结果。