Wednesday, June 8, 2011

Sql Server 2008 Database Diagrams


SQL Server Database Diagram is visual model of a database. We can use only Tables in SQL Server database diagrams. Any changes made outside Database Diagram designer will reflect automatically in the diagram and vice versa.

We can create them using built-in SQL Server Database Diagram designer tool in SQL Server Management Studio. There is no Undo and Redo option, so we need to be careful when deleting columns or removing relationships

Database Diagrams are stored in dbo.sysdiagrams system table in the database we are creating diagram. This table will be created automatically, when we create the first database diagram in the database.

we can copy the diagram by copy the data of dbo.sysdiagrams table from source database to destination database, but the tables in the diagram should exist in the destination database or else while viewing or modifying the diagram, we will get the below error and corresponding tables will be removed from the diagram
“Table(s) were removed from the diagram because privileges were removed to these table(s) or the table(s) were dropped.

Query to copy database diagrams
INSERT INTO [DESTINATIONDB].[dbo].[sysdiagrams]
([name]
,[principal_id]
,[version]
,[definition])
SELECT [name]
,[principal_id]
,[version]
,[definition]
FROM [SOURCEDB].[dbo].[sysdiagrams]

For copying to remote servers, we can use Linked Servers and use Four part name or use SSIS to transfer data

We can use “Edit -> Copy Diagram to Clipboard” or “Database Diagram – Copy Diagram to Clipboard” menu option to copy the diagram and then paste it in the document required. If the diagram is bigger, we can paste it to Paint Brush or any image editor and then select specific portions and copy one by one in document.
We can also the print the database diagram as PDF, if we have PDF Print drivers such as BioPDF or CutePDF installed.

Link For Reference :http://sqlxpertise.com/2011/06/01/sql-server-2008-r2-database-diagrams-questions-answered/