We are designing a new database and I would like some input in where to put stuff like default values. There are 3 scenarios:
1: New values, created_date field. Should the column have a default value when you do an insert?
2: Updated values, updated_date fiels. I’ve been thinkin about implementing a trigger that sets this to getdate(), other option is in code.
3: country table with country_name, should we enforce a unique constraint directly on the table or make sure code does this?
and last a bit of topic, but we also have an updated_by and created_by (int) in each table that refers to a user_id in the user table. Is it worth the effort to implement this a fk. constraints on all tables?
created_date, updated_date: If these are purely audit columns, showing changes made and by whom – a trigger is appropriate. If your data model relies on these (maybe the record with the last updated_date is considered current) then your app should do it. Your app can then make decisions about what should be current, instead of having it hardcoded into the schema that it’s the ‘last inserted record, according to the server’s current clock settings’.
Country table: Yes, use a unique constraint. Your data (and hence, your queries) won’t make sense without it…you’ll get unexpected join matches, and you won’t be able to enforce referential integrity. Your app can then reliably depend on it being uniquely named (eg., by storing them in a SortedList) as well.
Foreign Key to User table Yes. If it’s worth saving the data in the first place, it’s worth making sure that it’s valid.