views:

226

answers:

8

I'm not sure what this practice is actually called, so perhaps someone can edit the title to more accurately reflect my question.

Let's say we have a site that stores objects of different types. Each type of object has its own database (a database of books and assorted information with its tables, a database of CDs and information with its tables, and so on). However, all of the objects have keywords and the keywords should be uniform across all objects, regardless of type. A new database with a few tables is made to store keywords, however each object database is responsible for mapping the object ID to a keyword.

Is that a good practice?

+1  A: 

No, it's a bad idea. By separating them into different databases, you significantly impair your ability to do JOIN queries.

ctacke
Not to mention referential integrity when somebody changes or deletes an object id.
Paul Tomblin
I thought you could do a database1.table1 joined with database2.table2, as long as you used fully qualified names....
Thomas Owens
Paul - Not an issue, as long as you clean up across all the databases. Even if there was one database, you would have to clean up multiple tables. Perhaps you can answer and elaborate on your point.
Thomas Owens
@Thomas - within a database, "referential integrity" is handled for you via foreign keys. You can even have "cascade deletes". Once you cross the database boundary, you're responsible for all that engine-level goo.
GalacticCowboy
True. I would have to give up the foreign key benefit. And that's something that I might not be willing to do.
Thomas Owens
Depends on your database engine. Not all of them support cross-database queries, and I'm fairly certain that triggers will become unusable. What would be the purpose in splitting them? If it was a good idea, then there's be no need for a Table - each entity would just be its own database.
ctacke
+4  A: 

By separating them into different databases, you lose:

  • the ability to do ACID transactions (assuming you aren't using a two-phase commit solution).
  • the ability to have referential integrity.
  • JOINs across tables.
Oddthinking
I can make the case for having referential integrity. All you have to do is connect to each database and ensure that everything is properly cleaned up. Rather than one connection to clean up tables, you have multiple connections to clean up tables.
Thomas Owens
And that's not extensible, maintainable or scalable. Never tried, but do transactions work with cross-database modifications?
ctacke
@ctacke - good question! My guess would be no, but I have no facts to back that up.
Paul Tomblin
I also suspect that the query planner might be able to make better decisions if both tables are in the same database. But again, I have no facts to back that up.
Paul Tomblin
+5  A: 

Is there a reason to have separate databases for each type of object? You would be better off using multiple tables, and joining them. For example, you may have a table GENERIC_OBJECT which holds things that are common across all types, and then a table called BOOK_OBJECT where BOOK_OBJECT.ID = GENERIC_OBJECT.ID for a given book. Another table would be CD_OBJECT where CD_OBJECT.ID = GENERIC_OBJECT.ID for a given CD. Then things like keywords that are common across all objects would be stored in the GENERIC_OBJECT table, and things that are specific to the item would go in the item's corresponding table.

Elie
No, there isn't. I was wondering what impact separate databases would have on design and implementation.
Thomas Owens
It will make dealing with your data much more complicated, in a sentence.
Elie
+1  A: 

It does seem a little bit too seperated but with some well designed views it could work especially if the views are simply lookups.

Why such seperation in the first place?

c00ke
+3  A: 

Thomas, what you're missing in your comment responses to our concerns about referential integrity is that you can't do a foriegn key across two databases. If the two tables are in one database, then you can use foriegn key constraints to ensure that when you delete an object, anything that relies upon its object id is also deleted, and other similar things.

Paul Tomblin
+2  A: 

While it is possible to do joins across databases, I wouldn't generally split the data across databases just because they are of slightly different categories. Others have also mentioned the inability to use referential integrity across databases.

On the other hand, if each type of product has radically different front-end applications, or if you expect each database to become massively large, those might be reasons to consider leaving them in separate databases. (Although scaling isn't a problem for most modern databases).

Syntax example for cross-database joins:

SELECT *
FROM books b
INNER JOIN KeywordDB.dbo.Keywords k
ON b.keywordID = k.keywordID

In this example, you are performing the query from the local database that contains the books table, and you are joining to the other database. (This is a MS SQL syntax example)

BradC
+1  A: 

As everyone has mentioned this is, in general, not a good idea. However, to play devils advocate, I've seen other developers do this. I'm sure that there are some reasons that one might want accomplish this however if absolutely needed (not sure if your asking for solutions but) you might want to use some sort of synchronization to keep the data synchronized. Have all (or what is needed) of the data in both databases.

This also isn't an ideal solution, but if you must uses two different database types, this might be a better way to go about such a thing.

It could at least solve the issues that everyone has been outlining – though keep in mind that it does present a new problem… Is everything in sync?

Good Luck,
Frank

Frank V
A: 

If the decision about whether to use one database or two is yours, I recommend going with just one database. The data in the two tables appears closely related, judging from your question. The size and complexity doesn't seem to merit splitting into two databases.

What's your DBMS? If it's Oracle, DB2, SQL Server, or even MS Access, you shouldn't have any trouble administering a single database with keyword data and object data in logically related tables.

Walter Mitty