Where should you define foreign keys? Is it better to define foreign keys in the database or in the code part of an application?
Put the foreign keys on the database.  Even if you validate the data in the application before you save it the FK's are a good piece QA backup.  For a first approximation, applications always have data issues.  Leaving controls like this out of the system just invites failure modes where data gets corrupted silently.

There's nothing like working in data warehousing for a few years to see this in action.  You spend your time picking up the pieces after noddy mistakes by application developers who thought they could enforce data integrity in the application code.  Spend any time doing this and you will conclude that application managed data integrity is little more than a conceit. 

In addition, the query optimiser can use foreign keys to infer things about table joins, so FK's will result in more efficient query plans.

There are plenty of other benefits to foreign keys as well.  Do everyone a favour - put the FK's on the database.