如何:删除对象和解析依赖关系How to: Delete Objects and Resolve Dependencies

在 SQL Server 对象资源管理器 中重命名或删除某一对象时,SQL Server Data Tools 将自动检测其所有依赖对象,并且将准备 ALTER 脚本以便根据需要重命名或删除这些依赖对象。When you rename or delete an object in SQL Server Object Explorer, SQL Server Data Tools automatically detects all its dependency objects, and will prepare an ALTER script to rename or drop the dependency as needed.

警告

下面的过程利用在连接的数据库开发一节的前面的过程中创建的实体。The following procedures uses entities created in previous procedures in the Connected Database Development section.

删除数据库To delete a database

  1. 右键单击“SQL Server 对象资源管理器” 中的某个数据库,然后选择“删除” 。Right-click a database in SQL Server Object Explorer, and select Delete.

  2. 在“删除数据库” 对话框中接受所有默认设置,然后单击“确定” 。Accept all the default settings in the Delete Database dialog, and click OK.

重命名表To rename a table

  1. 请确保 Customer 表未在表设计器或 Transact-SQL 编辑器中打开。Make sure that the Customer table is not opened in either the Table Designer or the Transact-SQL Editor.

  2. 展开 SQL Server 对象资源管理器 中的“表” 节点。Expand the Tables node in SQL Server Object Explorer. 右键单击 Customer 表,然后选择“重命名” 。Right-click the Customer table and select Rename.

  3. 将表名称更改为 Customers ,然后按 ENTER 键。Change the table name to Customers and press ENTER.

  4. 请注意,该工具将立即代表你调用“数据库更新” 操作。Notice that a Database Update operation is immediately invoked on your behalf. SSDT 仍将代表您调用 sp_rename 存储过程以便重命名该表。SSDT will call the sp_rename stored procedure on your behalf to rename the table. 如果存在诸如外键约束之类的任何依赖对象,则这些对象也将更新。If there are any dependent objects such as foreign key constraints, they will also be updated.

    警告

    SSDT 不自动更新基于脚本的依赖项,例如对来自视图的表或存储过程的引用。Script-based dependencies such as references to a table from a view, or stored procedures are not automatically updated by SSDT. 重命名后,可以使用“错误列表” 窗格找到其他所有依赖项,并手动修复它们。After the rename, you can use the Error List pane to locate all other dependencies and manually fix them.

  5. 在执行了上一过程中的步骤后应用更改,即如何:使用 Power Buffer 更新连接的数据库Apply the change following the steps in the previous How to: Update a Connected Database with Power Buffer procedure.

  6. 再次右键单击“SQL Server 对象资源管理器” 中的“Customers” 表,然后选择“查看数据” 。Right-click the Customers table in SQL Server Object Explorer again, and select View Data. 请注意,在重命名操作后表数据将保持不变。Notice that table data is intact after the rename operation.

  7. 右键单击 Products 表,然后选择“查看代码” 。Right-click the Products table and select View Code. 请注意,外键引用已自动更新为 REFERENCES [dbo].[Customers] ([Id]) 以便反映此重命名操作。Notice that the foreign key reference has been automatically updated to REFERENCES [dbo].[Customers] ([Id]) to reflect the renaming.

删除表To delete a table

  1. 右键单击“SQL Server 对象资源管理器” 中的“Customers” 表,然后选择“删除” 。Right-click the Customers table in SQL Server Object Explorer, and select Delete.

  2. 在“预览数据库更新” 对话框的“用户操作” 下,请注意 SSDT 已标识了所有依赖对象,在此情况下,依赖对象是将删除的外键引用。In the Preview Database Updates dialog, under User Action, notice that SSDT has identified all the dependent objects, in this case, a foreign key reference that will be dropped.

  3. 单击“更新数据库” 。Click Update Database.

  4. 右键单击 “SQL Server 对象资源管理器”中的 “Products”表,然后选择“查看代码” 。Right-click the Products table in SQL Server Object Explorer, and select View Code. 请注意,对 Customers 表的外键引用已消失。Notice that the foreign key reference to the Customers table is gone.

    警告

    如果在删除操作发生时已在表设计器或 Transact-SQL 编辑器中打开了 Products 表,则该表将不会自动刷新以显示外键引用的删除。If you already have the Products table opened in Table Designer or Transact-SQL Editor when the delete operation occurs, it will not automatically refresh to show the deletion of the foreign key reference. 此外,与无法解析的引用有关的错误可能会出现在“错误列表” 中。In addition, errors about unresolved references may show up in the Error List. 若要解决此问题,请关闭表设计器或 Transact-SQL 编辑器,然后重新打开 Products 表。To resolve this issue, close the Table Designer or Transact-SQL Editor, and reopen the Products table.