Solved

Please help explain create table syntax and reason for each line

Posted on 2012-12-28
2
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Split string into 3 separate fields 5 36
Can a Trigger trigger a Trigger? 4 47
store vs query adhoc - no show rows 4 37
SQL Server replace between tags 6 25
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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