DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Jakarta Query: Unifying Queries Across SQL and NoSQL in Jakarta EE 12
  • Useful System Table Queries in Relational Databases
  • Why Should Databases Go Natural?
  • SQL Interview Preparation Series: Mastering Questions and Answers Quickly

Trending

  • Improving DAG Failure Detection in Airflow Using AI Techniques
  • The 7 Pillars of Meeting Design: Transforming Expensive Conversations into Decision Assets
  • Why Your QA Engineer Should Be the Most Stubborn Person on the Team
  • Building Production-Grade GenAI on GCP with Vertex AI Agent Builder
  1. DZone
  2. Data Engineering
  3. Databases
  4. Set Up a Database Diagram Using a Stored Procedure In SQL Server

Set Up a Database Diagram Using a Stored Procedure In SQL Server

This tutorial explains how to set up a database diagram using a stored procedure in SQL server.

By 
Satyaprakash Samantaray user avatar
Satyaprakash Samantaray
·
Updated May. 24, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
65.3K Views

Join the DZone community and get the full member experience.

Join For Free

Steps to be Followed:

  1. Create tables
  2. Create stored procedure using inner join between two tables
  3. Find out stored procedure syntax as text using SQL query
  4. Execute stored procedure to get results
  5. List of tables used in a stored procedure
  6. Then create a database diagram

Step 1

Create a table named: Supplier_Type_Master.

SQL syntax

USE [MyDB]  
GO  
SET ANSI_NULLS ON  
GO  

SET QUOTED_IDENTIFIER ON  
GO  

SET ANSI_PADDING ON  
GO  

CREATE TABLE [dbo].[Supplier_Type_Master](  
    [supp_type_id] [char](2) NOT NULL,  
    [supp_type] [varchar](30) NOT NULL,  
 CONSTRAINT [PK_Supplier_Type_Master] PRIMARY KEY CLUSTERED   
(  
    [supp_type_id] ASC  
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  
) ON [PRIMARY]  

GO  

SET ANSI_PADDING OFF  
GO 

Design Output

Design output

INSERT data output

INSERT data output

Step 2

CREATE SECOND TABLE NAMED Supplier_Master  

Before creating this table, I have to create a constraint.

Named DF_Supplier_Master_user_delete_flag.

Note

The constraints are used on a column or a table such that wrong data can't be inserted into the tables. The constraints helps for data integrity and accuracy in the table.

SQL syntax

USE [MyDB]  

GO  

ALTER TABLE [dbo].[Supplier_Master] ADD CONSTRAINT [DF_Supplier_Master_user_delete_flag] DEFAULT ('N') FOR [user_delete_flag]  

GO  

Now, create one foreign key relationship between Supplier_master and supplier_type_master.

Note

The foreign key is a table that uniquely identifies a row of other table. The foreign key is defined in a second table but it refers to the primary key in the first table.

SQL syntax

USE [MyDB]  

GO  

ALTER TABLE [dbo].[Supplier_Master] WITH NOCHECK ADD CONSTRAINT [FK_Supplier_Master_Supplier_Type_Master] FOREIGN KEY([supp_type_id])  

REFERENCES [dbo].[Supplier_Type_Master] ([supp_type_id])  

GO  

ALTER TABLE [dbo].[Supplier_Master] CHECK CONSTRAINT [FK_Supplier_Master_Supplier_Type_Master]  

GO 

Finally, we create a table named Supplier_Master.

SQL syntax

USE [MyDB]  

GO  

SET ANSI_NULLS ON  

GO  

SET QUOTED_IDENTIFIER ON  

GO  

SET ANSI_PADDING ON  

GO  

CREATE TABLE [dbo].[Supplier_Master](  

[party_id] [varchar](40) NOT NULL,  

[supplier_name] [varchar](50) NOT NULL,  

[supplier_creation_date] [datetime] NOT NULL,  

[supp_type_id] [char](2) NOT NULL,  

[insurance_details] [varchar](4000) NOT NULL,  

[vendor_option_in] [varchar](15) NOT NULL,  

[memo] [varchar](4000) NULL,  

[file_name] [varchar](100) NULL,  

[file_path] [varchar](250) NULL,  

[time_stamp_inserted] [datetime] NOT NULL,  

[time_stamp_modified] [datetime] NULL,  

[time_stamp_deleted] [datetime] NULL,  

[user_delete_flag] [char](1) NOT NULL CONSTRAINT [DF_Supplier_Master_user_delete_flag]DEFAULT ('N'),  

[user_login_name] [varchar](100) NOT NULL,  

CONSTRAINT [PK_Supplier_Master] PRIMARY KEY CLUSTERED  

(  

[party_id] ASC  

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  

) ON [PRIMARY]  

GO  

SET ANSI_PADDING OFF  

GO  

ALTER TABLE [dbo].[Supplier_Master] WITH NOCHECK ADD CONSTRAINT [FK_Supplier_Master_Supplier_Type_Master] FOREIGN KEY([supp_type_id])  

REFERENCES [dbo].[Supplier_Type_Master] ([supp_type_id])  

GO  

ALTER TABLE [dbo].[Supplier_Master] CHECK CONSTRAINT [FK_Supplier_Master_Supplier_Type_Master]  

GO   

Design output 

Design output

INSERT data output

INSERT data output

Step 3

Create stored procedure using inner join between the two tables mentioned above.

USE [MyDB]  

GO  

SET ANSI_NULLS ON  

GO  

SET QUOTED_IDENTIFIER ON  

GO  

ALTER PROCEDURE [dbo].[SP_DATABASE_DIAGRAM]  

AS  

BEGIN  

SET NOCOUNT OFF; --SET NOCOUNT ON;  

select sm.supplier_name , stm.supp_type from Supplier_Master Sm  

inner join Supplier_Type_Master stm on sm.supp_type_id = stm.supp_type_id  

END 

Step 4

Find out the stored procedure syntax as text, using SQL query.

SQL syntax

sp_helptext SP_DATABASE_DIAGRAM

Output

Output

Step 5

Execute the stored procedure to get the results.

SQL syntax

exec SP_DATABASE_DIAGRA

Output

output

Step 6

Difference between NOCOUNT ON and NOCOUNT OFF used in stored procedure.

If we put NOCOUNT OFF used in stored procedure, then after the execution of the stored procedure; the message tab in SQL Server will show (1429 row(s) affected).

SQL syntax

ALTER PROCEDURE [dbo].[SP_DATABASE_DIAGRAM]  

AS  

BEGIN  

SET NOCOUNT OFF; --SET NOCOUNT ON;  

select sm.supplier_name , stm.supp_type from Supplier_Master Sm  

inner join Supplier_Type_Master stm on sm.supp_type_id = stm.supp_type_id  

END  

exec SP_DATABASE_DIAGRAM 

Step 7

If we put NOCOUNT ON used in stored procedure, then after the execution of the stored procedure, the message tab in SQL Server will show the command(s) completed successfully.

SQL syntax

ALTER PROCEDURE [dbo].[SP_DATABASE_DIAGRAM]  

AS  

BEGIN  

SET NOCOUNT ON; --SET NOCOUNT OFF;  

select sm.supplier_name , stm.supp_type from Supplier_Master Sm  

inner join Supplier_Type_Master stm on sm.supp_type_id = stm.supp_type_id  

END  

exec SP_DATABASE_DIAGRAM

Step 8

List of tables used in a stored procedure.

SQL syntax

SELECT  

NAME 'Table Names'  

FROM SYSOBJECTS  

WHERE ID IN ( SELECT SD.DEPID  

FROM SYSOBJECTS SO,  

SYSDEPENDS SD  

WHERE SO.NAME = 'SP_DATABASE_DIAGRAM'  

--Name Of Stored Procedure  

AND SD.ID = SO.ID  

)  

Result

It will show two table names used in this stored procedure i.e. Supplier_Master & Supplier_Type_Master.

Output

Output

Step 9

Steps to create a database diagram.

Right click on the database diagram and new database diagram.

Click new database diagram

Add the tables used in stored procedure to know the relationship between them.

Add the tables used in stored procedure

After adding these two tables, it will show the foreign key relationship between the two tables.

Foreign key relationship between the two tables

By clicking the "Show Relationship Labels" icon In SQL Server, it will show what kind of relationship is there and what the foreign key relationship name is.

Click the "Show Relationship Labels" icon In SQL Server

Step 10

You can name your own created database diagram.

Name your own created database diagram

After expanding database diagrams, it will show your own created database diagram.

Your own created database diagram

Summary

  • Create tables
  • Make constraint and foreign key relationship between two tables
  • Create stored procedure using inner join between two tables
  • Find out stored procedure syntax as text, using SQL query
  • Execute stored procedure to get the results
  • Difference between NOCOUNT ON and NOCOUNT OFF
  • List of tables used in a stored procedure
  • Create database diagram
  • Find your own created database diagram
Database sql Relational database Diagram

Opinions expressed by DZone contributors are their own.

Related

  • Jakarta Query: Unifying Queries Across SQL and NoSQL in Jakarta EE 12
  • Useful System Table Queries in Relational Databases
  • Why Should Databases Go Natural?
  • SQL Interview Preparation Series: Mastering Questions and Answers Quickly

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook