Sql-server – How to split a DB into two in SQL Server 2008

sql server

In my project I have one DB used for everything. I want it to break into two databases. Static tables having look up values are to be stored in one DB and another DB would be having tables with dynamic data. My problem is that how would I use foreign key constraint in between those two DBs. Can someone help me out and suggest a way to proceed, better if I'm provided an example for the same.

I thought of using synonyms for tables and then constraints on synonyms. but later I came to know that synonyms couldn't be used for constraints.

I need to maintain relationships among the tables from both DB as the issue is with update, with a new release I just want to update look up tables and for the same I want to split my DB.

Best Answer

Robin:

If you want to physically separate volatile and static data, I suggest that you look at the filegroups feature. (Scroll down to the middle of that web page to get to the relevant information.)

Filegroups allow you to group tables. This allows you to control their physical location more easily (often-used data could be placed on fast SSD and other data could be placed on slower hard disks, for example), control read/write access (a filegroup can be flagged read-only) and improve backup/restore schemes (a filegroup can be be backed up or restored). Since all of the the tables are in the same database, regardless of filegroup, declarative referential integrity (DRI, also known as "foreign keys") still works.

If you insist on using different databases, you won't be able to use foreign keys and will have to use a different method. This means writing your own triggers (or maybe procedures). Writing your own code means extra work, there is always the possibility of bugs in that code and triggers usually perform worse than DRI. There are other negative aspects of using different databases, most obviously security (you'll have twice as much to manage) and point-in-time recovery (it's harder to get consistency between two databases than one database). Those things might not seem important today, but:

  1. It is easier to do things correctly now than it is to fix things later.
  2. It's best to acquire good habits.