Solved

Please help explain create table syntax and reason for each line

Posted on 2012-12-28
2
224 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:Tom 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:Tom Knowlton
ID: 38728304
B - E - A utiful  !!!

: )
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 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