Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to update a database structure?
Message
From
27/09/2011 12:04:59
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01524837
Message ID:
01524911
Views:
55
>>>Hi,
>>>
>>>How do you update a database structure (default keys, constraint, etc) installed on the customer site with the changes you have made to your copy of SQL Server database? That is, I understand that I have to create some script and send it to the customer and instruct them to run this script, correct? How do you create such a script? TIA.
>>
>>
>>Hi Dmitry,
>>
>>In addition to the other suggestions I'll explain our process.
>>
>>All DB changes are scripted during development, we never make changes using the SSMS interface. Using scripts during dev allows us to track what was changed, who made the change, and why it was changed. Our scripts are stored in source control. They typically check for existance of the added/modified object so they can be executed more than once.
>>
>>For migration, all scripts are concatenated into a single file for ease of use.
>>
>>I know these suggestions are of no use for your current scenario, but might help moving forward. I can post examples if that would help.
>>
>>Bruce
>
>Would you mind (since you kindly offered) to post a small example of your script(s) as you enter them in your "source" single file? TIA.

No problem. I apologize in advance for the naming convention (I did not invent it). Almost any object in the DB can be scripted. To see other example scripts, simply right-click on an object SSMS, select "Script object as", select "Create To", then I usually choose "New Query Editor Window". You can then modify the resulting script as required.

Under the main Tools-Options menu item there is a node called "Scripting" where you can set a number of options.
-- 2011.06.16	hewbxk	5055	Created Covering index

USE [MyDB]
GO
/****** Object:  Index [IDX_File_Name_File_Seq_No]    Script Date: 06/17/2011 15:09:19 ******/
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[Log_File]') AND name = N'IDX_File_Name_File_Seq_No')
DROP INDEX [IDX_File_Name_File_Seq_No] ON [dbo].[Log_File] WITH ( ONLINE = OFF )

	
CREATE NONCLUSTERED INDEX [IDX_File_Name_File_Seq_No] ON [dbo].[Log_File] 
(
	[File_Name] ASC,
	[User_ID] ASC,
	[Log_File_Seq_No] ASC,
	[Imported_File_Name] ASC
)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) ON [PRIMARY]
-- Add a column to a table

IF NOT EXISTS (
	SELECT * 
	FROM sys.columns 
	WHERE object_id = OBJECT_ID(N'[dbo].[My_Table]') 
		AND name = N'New_Code'
	)
ALTER TABLE [dbo].[My_Table] 
ADD [New_Code] CHAR(1) NULL
GO
-- *******************************************************************
-- Add table
-- *******************************************************************

IF NOT EXISTS (
	SELECT * 
	FROM sys.tables 
	WHERE object_id = OBJECT_ID(N'[dbo].[Contact]') 
	)
CREATE TABLE [dbo].[Contact](
	[Contact_Seq_No] INT IDENTITY(1,1) NOT NULL,
	[Parent_Seq_No] INT NOT NULL,
	[Contact_Name] VARCHAR(60) NULL,
	[Contact_Telephone] VARCHAR(15) NULL,
	[Contact_Telephone_Extension] VARCHAR(15) NULL,
	[Unique_Batch_ID] INT NULL,
CONSTRAINT [PK_Contact] PRIMARY KEY NONCLUSTERED 
(
	[Contact_Seq_No] ASC
)WITH (
	PAD_INDEX  = OFF, 
	STATISTICS_NORECOMPUTE  = OFF, 
	IGNORE_DUP_KEY = OFF, 
	ALLOW_ROW_LOCKS  = ON, 
	ALLOW_PAGE_LOCKS  = ON
	) ON [DATA]
) ON [DATA]

GO

SET ANSI_PADDING OFF
GO

IF EXISTS (
	SELECT * 
	FROM sys.tables 
	WHERE object_id = OBJECT_ID(N'[dbo].[Contact]') 
	)
	IF NOT EXISTS (
		SELECT *
		FROM sys.objects
		WHERE name = 'FK_Contact_Parent_Seq_No_Parent_Parent_Seq_No'  
			AND parent_object_id = OBJECT_ID('[dbo].[Contact]')
		)
		ALTER TABLE [dbo].[Contact] WITH CHECK 
		ADD CONSTRAINT [FK_Contact_Parent_Seq_No_Parent_Parent_Seq_No] FOREIGN KEY([Claim_Seq_No])
		REFERENCES [dbo].[Parent] ([Parent_Seq_No])
GO

IF EXISTS (
	SELECT * 
	FROM sys.tables 
	WHERE object_id = OBJECT_ID(N'[dbo].[Contact]') 
	)
ALTER TABLE [dbo].[Contact] 
CHECK CONSTRAINT [FK_Contact_Parent_Seq_No_Parent_Parent_Seq_No]
GO
and so on....

HTH

Bruce
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform