经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS SQL Server » 查看文章
SQLServer之修改数据库架构
来源:cnblogs  作者:小子pk了  时间:2019/1/28 9:34:57  对本文有异议

修改数据库架构注意事项

用户与架构完全分离。

ALTER SCHEMA 仅可用于在同一数据库中的架构之间移动安全对象。 若要更改或删除架构中的安全对象,请使用特定于该安全对象的 ALTER 或 DROP 语句。

如果对 securable_name 使用了由一部分组成的名称,则将使用当前生效的名称解析规则查找该安全对象。

将安全对象移入新架构时,将删除与该安全对象关联的全部权限。 如果已显式设置安全对象的所有者,则该所有者保持不变。 如果安全对象的所有者已设置为 SCHEMA OWNER,则该所有者将保持为 SCHEMA OWNER;但移动之后,SCHEMA OWNER 将解析为新架构的所有者。 新所有者的 principal_id 将为 NULL。

无论是 sys.sql_modules 目录视图的 definition 列中的相应对象,还是使用 OBJECT_DEFINITION 内置函数获取的相应对象,移动存储过程、函数、视图或触发器都不会更改其架构名称(如有)。 因此,我们建议不要使用 ALTER SCHEMA 移动这些对象类型。 而是删除对象,然后在新架构中重新创建该对象。

移动表或同义词不会自动更新对该对象的引用。 必须手动修改引用已移动对象的任何对象。 例如,如果移动了某个表,并且触发器中引用了该表,则必须修改触发器以反映新的架构名称。 请使用 sys.sql_expression_dependencies 列出该对象上的依赖关系,然后再进行移动。

若要通过使用 SQL Server Management Studio 更改表的架构,请在对象资源管理器中右键单击该表,然后单击“设计”。 按 F4 以打开“属性”窗口。 在“架构”框中,选择新架构。

若要从另一个架构中传输安全对象,当前用户必须拥有对该安全对象(非架构)的 CONTROL 权限,并拥有对目标架构的 ALTER 权限。

如果已为安全对象指定 EXECUTE AS OWNER,且所有者已设置为 SCHEMA OWNER,则用户还必须拥有对目标架构所有者的 IMPERSONATE 权限。

在移动安全对象后,将删除与所传输的安全对象相关联的所有权限。

使用SSMS数据库管理工具修改架构

1、连接服务器-》展开数据库文件夹-》选择数据库并展开-》展开安全性文件夹-》展开架构文件夹-》选择要修改的架构右键点击属性。

2、在架构属性弹出框-》点击常规-》点击搜索修改架构所有者。

3、在架构属性弹出框-》点击权限-》点击搜索选择用户或角色-》选择用户或角色权限。

4、在架构属性弹出框-》点击扩展属性-》新增或者删除扩展属性。

使用T-SQL脚本修改数据库架构

语法

  1. --声明数据库引用
  2. use database_name;
  3. go
  4.  
  5. 修改用户或者角色
  6. alter authorization on schema::[ArchitectureName] to [schemaOwner];
  7. go
  8.  
  9. --修改用户或角色权限
  10. --授予插入
  11. grant insert on schema::[ArchitectureName] to [rolename_username];
  12. go
  13.  
  14. --授予查看定义
  15. grant view definition on schema::[ArchitectureName] to [rolename_username];
  16. go
  17.  
  18. --授予查看更改跟踪
  19. grant view change tracking on schema::[ArchitectureName] to [rolename_username];
  20. go
  21.  
  22. --授予创建序列
  23. grant create sequence on schema::[ArchitectureName] to [rolename_username];
  24. go
  25.  
  26. --授予更改
  27. grant alter on schema::[ArchitectureName] to [rolename_username];
  28. go
  29.  
  30.  --授予更新
  31. grant update on schema::[ArchitectureName] to [rolename_username];
  32. go
  33.  
  34. --接管所有权
  35. grant take ownership on schema::[ArchitectureName] to [rolename_username];
  36. go
  37.  
  38. --授予控制
  39. grant control on schema::[ArchitectureName] to [rolename_username];
  40. go
  41.  
  42. --授予删除
  43. grant delete on schema::[ArchitectureName] to [rolename_username];
  44. go
  45.  
  46. --授予选择
  47. grant select on schema::[ArchitectureName] to [rolename_username];
  48. go
  49.  
  50. --授予引用
  51. grant references on schema::[ArchitectureName] to [rolename_username];
  52. go
  53.  
  54. --授予执行
  55. grant execute on schema::[ArchitectureName] to [rolename_username];
  56. go
  57.  
  58. --授予并允许转授插入
  59. grant insert on schema::[ArchitectureName] to [rolename_username] with grant option;
  60. go
  61.  
  62. --授予并允许转授查看定义
  63. grant view definition on schema::[ArchitectureName] to [rolename_username] with grant option;
  64. go
  65.  
  66. --授予并允许转授查看更改跟踪
  67. grant view change tracking on schema::[ArchitectureName] to [rolename_username] with grant option;
  68. go
  69.  
  70. --授予并允许转授创建序列
  71. grant create sequence on schema::[ArchitectureName] to [rolename_username] with grant option;
  72. go
  73.  
  74. --授予并允许转授更改
  75. grant alter on schema::[ArchitectureName] to [rolename_username] with grant option;
  76. go
  77.  
  78.  --授予并允许转授更新
  79. grant update on schema::[ArchitectureName] to [rolename_username] with grant option;
  80. go
  81.  
  82. --接管并允许转授所有权
  83. grant take ownership on schema::[ArchitectureName] to [rolename_username] with grant option;
  84. go
  85.  
  86. --授予并允许转授控制
  87. grant control on schema::[ArchitectureName] to [rolename_username] with grant option;
  88. go
  89.  
  90. --授予并允许转授删除
  91. grant delete on schema::[ArchitectureName] to [rolename_username] with grant option;
  92. go
  93.  
  94. --授予并允许转授选择
  95. grant select on schema::[ArchitectureName] to [rolename_username] with grant option;
  96. go
  97.  
  98. --授予并允许转授引用
  99. grant references on schema::[ArchitectureName] to [rolename_username] with grant option;
  100. go
  101.  
  102. --授予并允许转授执行
  103. grant execute on schema::[ArchitectureName] to [rolename_username] with grant option;
  104. go
  105.  
  106. --拒绝插入
  107. deny insert on schema::[ArchitectureName] to [rolename_username];
  108. go
  109.  
  110. --拒绝查看定义
  111. deny view definition on schema::[ArchitectureName] to [rolename_username];
  112. go
  113.  
  114. --拒绝查看更改跟踪
  115. deny view change tracking on schema::[ArchitectureName] to [rolename_username];
  116. go
  117.  
  118. --拒绝创建序列
  119. deny create sequence on schema::[ArchitectureName] to [rolename_username];
  120. go
  121.  
  122. --拒绝更改
  123. deny alter on schema::[ArchitectureName] to [rolename_username];
  124. go
  125.  
  126. --拒绝更新
  127. deny update on schema::[ArchitectureName] to [rolename_username];
  128. go
  129.  
  130. --拒绝所有权
  131. deny take ownership on schema::[ArchitectureName] to [rolename_username];
  132. go
  133.  
  134. --拒绝控制
  135. deny control on schema::[ArchitectureName] to [rolename_username];
  136. go
  137.  
  138. --拒绝删除
  139. deny delete on schema::[ArchitectureName] to [rolename_username];
  140. go
  141.  
  142. --拒绝选择
  143. deny select on schema::[ArchitectureName] to [rolename_username];
  144. go
  145.  
  146. --拒绝引用
  147. deny references on schema::[ArchitectureName] to [rolename_username];
  148. go
  149.  
  150. --拒绝执行
  151. deny execute on schema::[ArchitectureName] to [rolename_username];
  152. go
  153.  
  154. 删除数据库架构扩展属性
  155. exec sys.sp_dropextendedproperty @name=N'extendedAttributeName',@level0type=N'schema',@level0name=N'extendedAttributeValue'
  156. go
  157.  
  158. 创建数据库架构扩属性
  159. exec sys.sp_addextendedproperty @name=N'newExtendedAttributeName',@value=N'newExtendedAttributeValue' , @level0type=N'schema',@level0name=N'ArchitectureName'
  160. go
  161.  
  162. --修改数据库架构
  163. alter schema schema_name(你要修改成得新架构)
  164. transfer { object | type | xml schema collection } securable_name (原架构名.对象名);
  165. go

语法解析

--语法解析
--schema_name
--当前数据库中的架构名称,安全对象将移入其中。其数据类型不能为sys或information_schema。

--ArchitectureName
--架构名称

--schemaOwner
--架构所有者

--rolename_username
--用户或角色

--extendedAttributeName
--要删除的扩展属性名称

--extendedAttributeValue
--要删除的扩展属性值

--newExtendedAttributeName
--新添加扩展属性名称

--newExtendedAttributeValue
--新添加的扩展属性值

--transfer { object | type | xml schema collection }
--更改其所有者的实体的类。object是默认值。

--securable_name
--要移入架构中的架构范围内的安全对象的一部分或两部分名称。

示例

  1. --声明数据库引用
  2. use [testss];
  3. go    
  4.  
  5. --修改数据库架构
  6. --修改架构所有者
  7. alter authorization on schema::[testarchitecture] to [db_datareader];
  8. go
  9.  
  10. --修改用户或角色权限
  11. --授予插入
  12. grant insert on schema::[testarchitecture] to [guest];
  13. go
  14.  
  15. --授予查看定义
  16. grant view definition on schema::[testarchitecture] to [guest];
  17. go
  18.  
  19. --授予查看更改跟踪
  20. grant view change tracking on schema::[testarchitecture] to [guest];
  21. go
  22.  
  23. --授予创建序列
  24. grant create sequence on schema::[testarchitecture] to [guest];
  25. go
  26.  
  27. --授予更改
  28. grant alter on schema::[testarchitecture] to [guest];
  29. go
  30.  
  31.  --授予更新
  32. grant update on schema::[testarchitecture] to [guest];
  33. go
  34.  
  35. --接管所有权
  36. grant take ownership on schema::[testarchitecture] to [guest];
  37. go
  38.  
  39. --授予控制
  40. grant control on schema::[testarchitecture] to [guest];
  41. go
  42.  
  43. --授予删除
  44. grant delete on schema::[testarchitecture] to [guest];
  45. go
  46.  
  47. --授予选择
  48. grant select on schema::[testarchitecture] to [guest];
  49. go
  50.  
  51. --授予引用
  52. grant references on schema::[testarchitecture] to [guest];
  53. go
  54.  
  55. --授予执行
  56. grant execute on schema::[testarchitecture] to [guest];
  57. go
  58.  
  59. ----授予并允许转授插入
  60. --grant insert on schema::[testarchitecture] to [[guest]] with grant option;
  61. --go
  62.  
  63. ----授予并允许转授查看定义
  64. --grant view definition on schema::[testarchitecture] to [guest] with grant option;
  65. --go
  66.  
  67. ----授予并允许转授查看更改跟踪
  68. --grant view change tracking on schema::[testarchitecture] to [guest] with grant option;
  69. --go
  70.  
  71. ----授予并允许转授创建序列
  72. --grant create sequence on schema::[testarchitecture] to [guest] with grant option;
  73. --go
  74.  
  75. ----授予并允许转授更改
  76. --grant alter on schema::[testarchitecture] to [guest] with grant option;
  77. --go
  78.  
  79. -- --授予并允许转授更新
  80. --grant update on schema::[testarchitecture] to [guest] with grant option;
  81. --go
  82.  
  83. ----接管并允许转授所有权
  84. --grant take ownership on schema::[testarchitecture] to [guest] with grant option;
  85. --go
  86.  
  87. ----授予并允许转授控制
  88. --grant control on schema::[testarchitecture] to [guest] with grant option;
  89. --go
  90.  
  91. ----授予并允许转授删除
  92. --grant delete on schema::[testarchitecture] to [guest] with grant option;
  93. --go
  94.  
  95. ----授予并允许转授选择
  96. --grant select on schema::[testarchitecture] to [guest] with grant option;
  97. --go
  98.  
  99. ----授予并允许转授引用
  100. --grant references on schema::[testarchitecture] to [guest] with grant option;
  101. --go
  102.  
  103. ----授予并允许转授执行
  104. --grant execute on schema::[testarchitecture] to [guest] with grant option;
  105. --go
  106.  
  107. ----拒绝插入
  108. --deny insert on schema::[testarchitecture] to [guest];
  109. --go
  110.  
  111. ----拒绝查看定义
  112. --deny view definition on schema::[testarchitecture] to [guest];
  113. --go
  114.  
  115. ----拒绝查看更改跟踪
  116. --deny view change tracking on schema::[testarchitecture] to [guest];
  117. --go
  118.  
  119. ----拒绝创建序列
  120. --deny create sequence on schema::[testarchitecture] to [guest];
  121. --go
  122.  
  123. ----拒绝更改
  124. --deny alter on schema::[testarchitecture] to [guest];
  125. --go
  126.  
  127. ----拒绝更新
  128. --deny update on schema::[testarchitecture] to [guest];
  129. --go
  130.  
  131. ----拒绝所有权
  132. --deny take ownership on schema::[testarchitecture] to [guest];
  133. --go
  134.  
  135. ----拒绝控制
  136. --deny control on schema::[testarchitecture] to [guest];
  137. --go
  138.  
  139. ----拒绝删除
  140. --deny delete on schema::[testarchitecture] to [guest];
  141. --go
  142.  
  143. ----拒绝选择
  144. --deny select on schema::[testarchitecture] to [guest];
  145. --go
  146.  
  147. ----拒绝引用
  148. --deny references on schema::[testarchitecture] to [guest];
  149. --go
  150.  
  151. ----拒绝执行
  152. --deny execute on schema::[testarchitecture] to [guest];
  153. --go
  154.  
  155. --删除数据库架构扩展属性
  156. exec sys.sp_dropextendedproperty @name=N'testcrituer' , @level0type=N'schema',@level0name=N'testarchitecture'
  157. go
  158.  
  159. --创建数据库架构扩属性
  160. exec sys.sp_addextendedproperty @name=N'testcrituer', @value=N'测试创建数据库架构' , @level0type=N'schema',@level0name=N'testarchitecture'
  161. go
  162.  
  163. --修改架构下对象所有权,从[testarchitecture]转移到[dbo]
  164. alter schema [dbo] transfer [testarchitecture].[schema_table1];
  165. go

示例结果:执行T-SQL脚本需要刷新表文件夹才能查看执行结果。

 

原文链接:http://www.cnblogs.com/vuenote/p/10307204.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号