Solved

Please help explain create table syntax and reason for each line

Posted on 2012-12-28
2
219 Views
Last Modified: 2012-12-28
Please explain why each line of code exists in this script.

I am not a very experienced T-SQL programmer.

Thank you,

Tom





USE [PP]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ProductMatrix_ProductType]') AND parent_object_id = OBJECT_ID(N'[dbo].[ProductMatrix]'))
ALTER TABLE [dbo].[ProductMatrix] DROP CONSTRAINT [FK_ProductMatrix_ProductType]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ProductMatrix_Task]') AND parent_object_id = OBJECT_ID(N'[dbo].[ProductMatrix]'))
ALTER TABLE [dbo].[ProductMatrix] DROP CONSTRAINT [FK_ProductMatrix_Task]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ProductMatrix_TaskWorkFlow]') AND parent_object_id = OBJECT_ID(N'[dbo].[ProductMatrix]'))
ALTER TABLE [dbo].[ProductMatrix] DROP CONSTRAINT [FK_ProductMatrix_TaskWorkFlow]
GO

IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_ProductMatrix_ProductMatrixSID]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[ProductMatrix] DROP CONSTRAINT [DF_ProductMatrix_ProductMatrixSID]
END

GO

USE [PP]
GO

/****** Object:  Table [dbo].[ProductMatrix]    Script Date: 11/16/2012 10:52:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ProductMatrix]') AND type in (N'U'))
DROP TABLE [dbo].[ProductMatrix]
GO

USE [PP]
GO

/****** Object:  Table [dbo].[ProductMatrix]    Script Date: 11/16/2012 10:52:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ProductMatrix](
	[ProductMatrixID] [int] IDENTITY(1,1) NOT NULL,
	[ProductMatrixSID] [int] NOT NULL,
	[TaskWorkFlowID] [int] NULL,
	[TaskWorkFlowSID] [int] NULL,
	[IDTemplate] [int] NULL,
	[TaskID] [int] NULL,
	[NewProductID] [int] NULL,
 CONSTRAINT [PK_ProductMatrix] PRIMARY KEY CLUSTERED 
(
	[ProductMatrixID] ASC,
	[ProductMatrixSID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 85) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[ProductMatrix]  WITH CHECK ADD  CONSTRAINT [FK_ProductMatrix_ProductType] FOREIGN KEY([NewProductID])
REFERENCES [dbo].[ProductType] ([ProductTypeID])
GO

ALTER TABLE [dbo].[ProductMatrix] CHECK CONSTRAINT [FK_ProductMatrix_ProductType]
GO

ALTER TABLE [dbo].[ProductMatrix]  WITH CHECK ADD  CONSTRAINT [FK_ProductMatrix_Task] FOREIGN KEY([TaskID])
REFERENCES [dbo].[Task] ([TaskID])
GO

ALTER TABLE [dbo].[ProductMatrix] CHECK CONSTRAINT [FK_ProductMatrix_Task]
GO

ALTER TABLE [dbo].[ProductMatrix]  WITH CHECK ADD  CONSTRAINT [FK_ProductMatrix_TaskWorkFlow] FOREIGN KEY([TaskWorkFlowID], [TaskWorkFlowSID])
REFERENCES [dbo].[TaskWorkFlow] ([TaskWorkFlowID], [TaskWorkFlowSID])
GO

ALTER TABLE [dbo].[ProductMatrix] CHECK CONSTRAINT [FK_ProductMatrix_TaskWorkFlow]
GO

ALTER TABLE [dbo].[ProductMatrix] ADD  CONSTRAINT [DF_ProductMatrix_ProductMatrixSID]  DEFAULT (right(@@servername,(1))) FOR [ProductMatrixSID]
GO


USE [pp]
GO

ALTER TABLE [dbo].ProductMatrix ADD  CONSTRAINT [UK_ProductMatrix_All] UNIQUE NONCLUSTERED 
(
	[ProductMatrixID] ,
	[ProductMatrixSID] ,
	[TaskWorkFlowID] ,
	[TaskWorkFlowSID] ,
	[IDTemplate],
	[TaskID] ,
	[NewProductID]
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 85) ON [PRIMARY]
GO

Open in new window

0
Comment
Question by:knowlton
2 Comments
 
LVL 18

Accepted Solution

by:
Jerry Miller earned 500 total points
ID: 38728277
--Lines 4-20 are looking for the existence of Foreign Key constraints and dropping them if they exists. The table cannot be dropped as long as these constraints are in place

--Lines 29-32 are checking for the existence of the current table and dropping it if it is there

--the rest of the lines are creating the table with the necessary foreign key constraints that were dropped earlier

It is generally good practice to check for the existence of an object before dropping it so that the stored procedure doesn't error if it is not there. Dropping the objects avoids errors generated upon the creation in the second half of the procedure.
0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 38728304
B - E - A utiful  !!!

: )
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now