+3  A: 

Why not use both?

Foreign keys, check constraints etc are known as "Declarative Referential Integrity" for a reason. They protect your data. What if you add a bulk load next month, or you have to run a SQL script to change data?

Another point would be that the database engine is the correct tool for this.

gbn
Is it possible for an application to define this type of relationship without using a database constraint?
eggdrop
I don't actually know because I'd never consider doing DRI in the client.
gbn
@eggdrop: sure, but you wouldn't *want* to - having the db constraint in place ensures that you can't corrupt your data by writing incorrect code.
Harper Shelby
..and I'm also a developer DBA. I don't really do much client development!
gbn
@Harper - ok I get it. That's why gbn is saying I should use both.
eggdrop
Looking at the diagram I just wasn't sure if the diagram required this at the database level or whether a diagram like this could be defining application logic mixed in with database design.
eggdrop
Maybe the diagram is simply technology-agnostic. It doesn't define how you implement these relationships.
eggdrop
It's a database model http://en.wikipedia.org/wiki/Database_model
gbn
+4  A: 

Since it's a many-to-many relationship, you'll want a cross-reference table rather than a simple foreign key column.

So the Category table does not have a FK to Classified, and Classified does not have a FK to Category. Instead you can have a new table :

XrefCategoryClassified
  FK to Category NOT NULL
  FK to Classified NOT NULL

This is a typical way to implement a many-to-many relationship. And now, instead of worrying about NULLable fields if two records aren't related, you simply care about the existence or non-existence of a xref record

Clyde
That's a good idea. But does this new table need to have foreign keys or can I just leave it open (no keys defined)?
eggdrop
I believe Clyde is implying that the cross-ref table should implement foreign key constraints, and I'm inclined to agree.
Matt
@eggdrop: you need the DBMS to maintain integrity for you; application programmers won't always enforce the rules correctly, especially when the database is used for the fourteenth different application (or second, come to that!). The DBMS should always be told to enforce the integrity of the data to the maximum extent possible, because it is the last line of defense against invalid data being inserted into the database. If some nitwit takes an ODBC-enabled spreadsheet and inserts a record into your tables - ignoring your application - ooops!!! Don't let it happen!
Jonathan Leffler
+1  A: 

Absent compelling reasons to do otherwise, I'd enforce referential integrity at the database level; after all that's (partly) what an RDBS is good for.

And since you'll likely have some sort of mapping table to define the many-to-many relationship between Category and Classified, it seems like a no-brainer to put your constraints there. Your queries will thank you for it later.

Matt
Ok, sounds good. As I mentioned in the comments to gbn, looking at the diagram I just wasn't sure if the diagram required this at the database level or whether the diagram permits defining application logic mixed in with database design. Any idea from the diagram or is this information just not provided here?
eggdrop
Nothing from the diagram, per se. It's just sort of a basic design principal that I'd start with for any such relationship. Look at it this way: you can enforce referential integrity _once_ at the DB level or make sure you do it the same way every time in every piece of code that might update those tables (including one-off bulk loads, etc. as gbn mentioned).
Matt