views:

259

answers:

4

Hello!

I'm implementing a web - based application using silverlight with an SQL Server DB on the back end for all the data that the application will display. I want to ensure that the application can be easily scalable and I feel the direction to go in with this is to make the database loosely coupled and not to tie everything up with foreign keys. I've tried searching for some examples but to no avail.

Does anyone have any information or good starting points/samples/examples to help me get off the ground with this?

Help greatly appreciated.

Kind regards,

+1  A: 

Unless you're doing massive amounts of inserts at a time, like with a data warehouse, use foreign keys. Normalization scales like crazy, and you should take advantage of that. Foreign keys are fast, and the constraint really only holds you back if you're inserting millions upon millions of records at a time.

Make sure that you're using integer keys that have a clustered index on them. This should make joining table very rapid. The issues you can get yourself wrapped around without foreign keys are many and frustrating. I just spent all weekend doing so, and we made a conscious choice to not have foreign keys (we have terabytes of data, though).

Eric
Off-topic: could I ask your opinion on using GUID's for an ID field in a table? I'm convinced that this slows down the retrieval of data but i've been told otherwise....What's wrong with using an autoincrementing Int field for the ID column?
Goober
Nothing. It's as good as any and smaller than a GUID (which is 16 bytes long). Unless you expect to have more rows than an int can handle (2^32 or about 4 billion) an autoincrementing int primary key will work fine.
ConcernedOfTunbridgeWells
@Goober: using GUIDs for primary keys *will* slow down retrieval *if* you're joining two or more tables, but it's not orders-of-magnitude slower (a query will take around one-and-half times as long this way), and it won't affect a simple SELECT on a single table. However, there's nothing at all wrong with an autoincrementing int column as the primary key. GUID PKs are best for distributed/replicated databases.
MusiGenesis
I agree wholeheartedly with Concerned and Musi--use GUIDs if and only if you're distributing your database and need to have interoperability between them. I always use integer PK's, and I've yet to run out of numbers (we don't autoincrement fact tables).
Eric
+3  A: 

I think you're mixing up your terminology a bit. "Loosely coupled" refers to the desirability of having software components that aren't so dependent upon each other that they can't function or even compile without being together in the same program. I've never seen the term used to describe the relationships between tables in the same database.

I think if you search on the terms "normalization" and "denormalization" you'll get better results.

MusiGenesis
A: 

Before you even think of such a thing, you need to think about data integrity. Foreign keys exist so that you cannot put records into tables if the primary data they are based on is not there. If you do not use foreign keys, you will sooner or later (probably sooner) end up with worthless data because you don't really know who the customer is that the order is attached to for instance. Foreign keys are data protection, you should never consider not using them.

And even though you think all your data will come from your application, in real life, this is simply not true. Data gets in from multiple applications, from imports of large amounts of data, from the query window (think about when someone decides to update all the prices they aren't going to do that one price at a time from the user interface). Data can get into database from many sources and must be protected at the database level. To do less is to put your entire application and data at risk.

HLGEM
A: 

Intersting comment about database security when data is input through external sources like database scripts.

Purusartha