tags:

views:

463

answers:

5

I have created many tables on my local database and moved them to production database.

Now I am working on fine tuning the database and created many constraints on my local database tables such as PK, FK, Default Values, Indexes etc. etc.

Now I would like to copy only these constraints to production database. Is there a way to do it?

Please note that my production database tables already populated with some data. So I can’t drop and recreate them.

+2  A: 

Red Gate's SQL Compare is a popular, non-free way to do this.

RedFilter
+3  A: 

The best way would be to store all your DDL code in a source control. Then deploy it to production using tools like dbGhost (my favorite) or SQL Compare

van
I've used DbGhost for 10 years and it's never let me down, I wouldn't attempt medium-scale SQL development without it.
ip
A: 

If you don't want to buy any tools (which are totally worth their price, BTW), you can always interrogate the system catalog views, and extract the info from there to create scripts you could execute on your new database.

In the case of e.g. the default constraints, this query shows you a list of all the default constraints in your database:

SELECT
    dc.name 'Constraint Name',
    OBJECT_NAME(parent_object_id) 'Table Name',
    c.name 'Column Name',
    definition
FROM sys.default_constraints dc
INNER JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
ORDER BY OBJECT_NAME(parent_object_id), c.name

and based on that, you could of course create a query which would emit T-SQL statements to recreate those default constraints on your target server:

SELECT
    'ALTER TABLE dbo.' + OBJECT_NAME(parent_object_id) + 
    ' ADD CONSTRAINT ' + dc.name + ' DEFAULT(' + definition 
    + ') FOR ' + c.name
FROM sys.default_constraints dc
INNER JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id

You'd get something like this (for the AdventureWorks sample DB):

ALTER TABLE dbo.Store ADD CONSTRAINT DF_Store_rowguid DEFAULT((newid())) FOR rowguid
ALTER TABLE dbo.Store ADD CONSTRAINT DF_Store_ModifiedDate DEFAULT((getdate())) FOR ModifiedDate
ALTER TABLE dbo.ProductPhoto ADD CONSTRAINT DF_ProductPhoto_ModifiedDate DEFAULT((getdate())) FOR ModifiedDate
ALTER TABLE dbo.ProductProductPhoto ADD CONSTRAINT DF_ProductProductPhoto_Primary DEFAULT(((0))) FOR Primary
ALTER TABLE dbo.ProductProductPhoto ADD CONSTRAINT DF_ProductProductPhoto_ModifiedDate DEFAULT((getdate())) FOR ModifiedDate
ALTER TABLE dbo.StoreContact ADD CONSTRAINT DF_StoreContact_rowguid DEFAULT((newid())) FOR rowguid
ALTER TABLE dbo.StoreContact ADD CONSTRAINT DF_StoreContact_ModifiedDate DEFAULT((getdate())) FOR ModifiedDate
ALTER TABLE dbo.Address ADD CONSTRAINT DF_Address_rowguid DEFAULT((newid())) FOR rowguid

Of course, you could tweak the resulting T-SQL being output to your liking - but basically, copy&paste those results from the query to your new database, and off you go.

Of course, there are similar system catalog views for foreign key relationships (sys.foreign_keys), check constraints (sys.check_constraints), indexes (sys.indexes and sys.index_columns) and many more.

It's a bit of work - but it can be done on your own time, and you'll learn a lot about SQL Server in the process.

So it's a traditional "make or buy" decision all over again :-)

Marc

marc_s
A: 

A good and free Microsoft tool. You can export the schema and the data.

Microsoft SQL Server Database Publishing Wizard

Rodrigo
A: 

Try DBSourceTools. http://dbsourcetools.codeplex.com
It has schema compare function that will help you create an update script.
Bear in mind though, that you should be using source-code control on your entire database.
This is what DBSourceTools was designed to do - help developers bring their databases under source control.

blorkfish