如何将数据库图从一台服务器移动到另一台服务器

发布于 2024-11-07 01:39:19 字数 75 浏览 4 评论 0原文

我在测试数据库中创建了一个新的数据库图,它位于 sitede01 服务器中。现在我想将其移动到另一台服务器。如何将其迁移到另一台服务器。

I have created a new Database Diagram in Test Database and it is in sitde01 Server. Now I want to move it to another server. How do I migrate it to another server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的巷 2024-11-14 01:39:19

这是可以做到的,但这是一种巨大的痛苦。以下是该过程的概述和一些脚本。

图表存储在名为 sysDiagrams 的“系统”表中。当您单击 SSMS 中的图表节点时,会创建此表(仅?),它会询问您是否要创建支持图表的对象,然后单击“是”。在源数据库和目标数据库上执行此操作。

在“源”数据库中创建一个或多个图表。

查看 sysDiagrams 的结构和内容。请注意,diagram_id 列是一个标识列。每个图表存储 1 行。 (你不在乎,但在 SQL 2000 中它曾经是 4 或 5 行。)

要复制到同一 SQL 实例上的另一个数据库,最简单的方法是执行 INSERT...SELECT ... 桌子之间。由于该标识列妨碍了您,您将不得不使用 SET IDENTITY_INSERT,并且可能在目标计算机上分配一个新的标识值。令人恼火,但并不严重。

以下脚本会将所有图表从一个数据库复制到同一服务器上的另一个数据库(这就是我从容易被删除和重新创建的数据库中归档花费了太长时间创建的复杂图表的方法):

USE TargetDatabase

DELETE sysDiagrams
 where name in (select name from SourceDatabase.dbo.sysDiagrams)

SET identity_insert sysDiagrams on

INSERT sysDiagrams (name, principal_id, diagram_id, version, definition)
 select name, principal_id, diagram_id, version, definition
  from SourceDatabase.dbo.sysDiagrams

SET identity_insert sysDiagrams off

复制到另一个数据库在不同的 SQL 实例(或服务器)上, 好吧,这会变得更加困难。我使用临时创建的链接服务器定义,使用多年前我费尽心思的脚本,永远不想再次修改(即发布一个不同的问题,以便知道的人可以告诉您它们是如何工作的),并使用适当的四修改脚本零件命名约定。其他选项(OPENROWSET 等)也是可能的,但我对这些更不熟悉。

It can be done, but it's a royal pain. Here's an outline of the process and some scripts.

Diagrams are stored in a "system" table named sysDiagrams. This table (only?) gets created when you click on the diagrams node in SSMS, it asks you if you want to create the objects that support diagrams, and you click "Yes". Do so on both your source and target databases.

Create the diagram or diagrams in the "source" database.

Review the structure and contents of sysDiagrams. Note that column diagram_id is an identity column. 1 row gets stored for every diagram. (You don't care, but it used to be 4 or 5 rows in SQL 2000.)

To copy to another database on the same SQL instance, the simplest way is to do INSERT... SELECT... between tables. With that identity column in the way, you'll have to fuss with SET IDENTITY_INSERT, and perhaps assign a new identity value on the target computer. Irritating, but not critically hard.

The following script will copy all diagrams from one database to another that's on the same server (this is how I archive complex diagrams that took waaaay too long to create, from databases that are prone to get dropped and recreated):

USE TargetDatabase

DELETE sysDiagrams
 where name in (select name from SourceDatabase.dbo.sysDiagrams)

SET identity_insert sysDiagrams on

INSERT sysDiagrams (name, principal_id, diagram_id, version, definition)
 select name, principal_id, diagram_id, version, definition
  from SourceDatabase.dbo.sysDiagrams

SET identity_insert sysDiagrams off

To copy to another database on a different SQL instance (or server), well, it gets even harder. I use temporarily created Linked Server definitions, using scripts I sweated bullets over years ago and never want to have to modify again (i.e. post a different question so someone who knows can tell you how they work), and modify the scripts with appropriate four-part naming conventions. Other options (OPENROWSET and the like) are possible, but I'm even less familiar with those.

夏の忆 2024-11-14 01:39:19

如果您想要将图表从一个实例或服务器移动到另一实例或服务器,并且不想恢复整个数据库,则可以执行以下操作。

  1. 如果不存在,请在目标服务器上创建数据库。您还必须单击 SSMS 中的“数据库图”节点以使其创建 dbo.sysDiagrams 表。
  2. 然后确保导入图表中所需的所有架构信息。因为你的图表会指向这些。即表、PK、FK 等必须存在。
  3. 备份源服务器上的数据库。
  4. 将其恢复到目标服务器上的临时数据库中。通过这种方式,您可以将所有图表信息发送到目标服务器中。
  5. 将临时数据库中的 dbo.sysDiagrams 表中的信息复制到目标数据库的 dbo.sysDiagrams 表中。你可以这样做(改编自 Philip Kelley 的代码):

    使用目标数据库 
    
    SET Identity_insert sysDiagrams on 
    
    INSERT sysDiagrams(名称、principal_id、diagram_id、版本、定义) 
     选择名称、principal_id、diagram_id、版本、定义 
      来自 TempDatabase.dbo.sysDiagrams 
    
    SET Identity_insert sysDiagrams 关闭 
    

这个解决方案对我来说非常有效。当然,如果您不想要所有图表或者目标数据库中存在其他图表,则必须过滤 select 语句并执行一些identity_insert 操作,但这应该不会太困难。

If you want to move your diagrams from one instance or server to a different one, and you don't want to restore the whole database, you can do the following.

  1. If it doesn't exist, create the database on your target server. You also have to click on the "Database Diagrams" node in SSMS to have it create a dbo.sysDiagrams table.
  2. Then make sure to import all the schema information you need in your diagram. Because your diagram will point to these. I.e. tables, PK, FK etc. must be present.
  3. Backup your database on the source server.
  4. Restore it into a temporary database on the target server. In this way you get all your diagram information into the target server.
  5. Copy the information from the dbo.sysDiagrams table in the temporary database into the dbo.sysDiagram table of your target database. You could do something like this (adapted the code from Philip Kelley):

    USE TargetDatabase 
    
    SET identity_insert sysDiagrams on 
    
    INSERT sysDiagrams (name, principal_id, diagram_id, version, definition) 
     select name, principal_id, diagram_id, version, definition 
      from TempDatabase.dbo.sysDiagrams 
    
    SET identity_insert sysDiagrams off 
    

This solution worked excelent for me. Of course if you don't want all the diagrams or if other diagrams are existing in the target database, you have to filter the select statement and do some identity_insert manipulation, but this shouldn't be too difficult.

错爱 2024-11-14 01:39:19

为了移动数据库图,您将需要迁移该图中包含的所有表和触发器。最简单的方法是备份数据库并在另一台服务器上恢复。

In order to move the database-diagram you will need to migrate all the tables and triggers included in that diagram. The easiest way to do this is backup the db and restore on the other server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文