views:

95

answers:

7

I have been googling a lot and I couldn't find if this even exists or I'm asking for some magic =P

Ok, so here's the deal.

I need to have a way to create a "master-structured" database which will only contain the schemas, structures, tables, store procedures, udfs, etc, everything but real data in SQL SERVER 2005 (if this is available in 2008 let me know, I could try to convince my client to pay for it =P)

Then I want to have several "children" of that master db which implement those schemas, tables, etc but each one has different data.

So when I need to create a new stored procedure or something like that, I just create it on the master database (and of course it's available on its children).

Actually I have several different databases with the same schema and different data. But the problem is to maintain congruency between them. Everytime I create a script to create some SP or add some index or whatever, I have to execute it in every database, and sometimes I could miss one =P

So let's say you have a UNIVERSE (would be the master db) and the universe has SPACES (each one represented by a child db). So the application I'm working on needs to dynamically "clone" SPACES. To do that, we have to create a new database. Nowadays I'm creating a backup of the db being cloned, restoring it as a new one and truncate the tables. I want to be able to create a new "child" of the "master" db, which will maintain the schemas and everything, but will start with empty data.

Hope it's clear... My english is not perfect, sorry about that =P Thanks to all!

A: 

You can put things in master like SPs and call them from anywhere. As far as other objects like tables, you can put them in model and new databases will get them when you create a new database.

However, in order to get new tables to simply pop up in the child databases after being added to the parent, nothing.

It would be possible to create something to look through the databases and script them from a template database, and there are also commercial tools which can help discover differences between databases. You could also have a DDL trigger in the "master" database which went out and did this when you created a new table.

If you kept a nice SPACES template, you could script it out (without data) and create the new database - so there would be no need to TRUNCATE. You can script it out from SQL or an external tool.

Cade Roux
So as far as you know, there isn't exist something like I mentioned? A way of creating a "master-schema" db, and then create a new database and somewhere set it to "use" the "master-schema" already created? =(
emzero
You could use model. But it would only apply to new databases (and every new database on that instance).
Cade Roux
I have updated my answer to include a link to an article describing how model works.
Cade Roux
+1  A: 

I would write my changes as a sql file and use OSQL or SQLCMD via a batchfile to ensure that I repeatedly executed on all the databases without thinking about it.

As an alternative I would use the VisualStudio Database Pro tools or RedGate SQL compare tools to compare and propogate the changes.

David McEwing
+1  A: 

There are kludges, but the mainstream way to handle this is still to use Source Code Control (with all its other attendant benefits.) And SQL Server is increasingly SCC friendly.

Also, for many (most robust) sites it's a per-server issue as much as a per-database issue.

le dorfier
Could you give a further explanation about that? Thanks!
emzero
Sure. In my experience, both installations and upgrades are pretty thoroughly tested hands-off scripting events, and need to be well documented in version control; so simply having a canonical set of database objects that propagate wouldn't be considered a very good idea. And there are other initialization activities (e.g. static table filling, configuration of options, and acceptance testing) that most people would insist on.And if you've got data and object replication and redundancy, it's trickier yet.
le dorfier
+2  A: 

What you really need is to version-control your database schema.

See do-you-source-control-your-databases

If you use SQL Server, I would recommend dbGhost - not expensive and does a great job of:

  • synchronizing 2 databases
  • diff-ing 2 databases
  • creating a database from a set of scripts (I would recommend this version).
  • batch support, so that you can upgrade all your databases using a single batch

You can use this infrastructure for both:

  • rolling development versions to test, integration and production systems
  • rolling your 'updated' system to multiple production deployments (especially in a hosted environment)
van
A: 

Little trivia here. The mssqlsystemresource database works as you describe: is defined once and 'appears' in every database as the special sys schema. Unfortunately the special 'magic' needed to get this working is not available to the user databases. You'll have to use deployment techniques to keep your schema in synk. That is, apply the changes to every database as the other answers already suggested.

Remus Rusanu
A: 

In theory, you could put a trigger on your UNIVERSE.sysobjects table (assuming SQL Server), and then you could enumerate master.dbo.sysdatabases to find all the child databases. If you have a special table that indicates it's a child database, you can reference child.dbo.sysobjects to find it.

Make no mistake, it would be difficult to implement. But it's one way you could do it.

Chris Kaminski
A: 

Well thanks to all for your answers.

Now that I know that the feature I need doesn't exist natively in SQL SERVER, I guess I'm going to apply some source control to the database. Seems the most "elegant" way of doing it.

Which leads us to a new question: What do you use to source control, diff, compare, etc your database? I need some free tools, we can't spent 300usd on a program to do this.

Thanks

PS: I'm new here, should I start a new question about this?

emzero
Better start a new one. This subject comes back often so I recommend to search first, read the discussions and then decide if you still need to ask.I also have a blog entry on the subject http://rusanu.com/2009/05/15/version-control-and-your-database/ but is by no means an exhaustive treatise on the subject...
Remus Rusanu