My people are the greatest asset of my company
— most CEOs
Your data is the greatest asset of your company
— Dan Chak
By Dan Chak (2009)
Initial (naïve) Schema
https://danchak99.files.wordpress.com/2014/07/er_0501.jpg
New stuff (constraints) in bold purple
create sequence movies_id_seq; create table movies ( id integer, name varchar(256), length_minutes integer, rating varchar(8), primary key (id) );
create sequence movies_id_seq; create table movies ( id integer not null default nextval('movies_id_seq'), name varchar(256) not null unique check (length(name) > 0), length_minutes integer not null check (length_minutes > 0), rating varchar(8) not null check (rating in ('Unrated', 'G', 'PG', 'PG-13', 'R', 'NC-17')), primary key (id) );
And now you have created the possibility of drift between your database validations and your codebase.
*Caveat from my manager:
* Response:
The database must *always* be the source of truth. The instant any other user than the web app can touch the database you are hosed without db-level constraints.
If you add a 'rating description' column
Multiple joins add friction when coding and may increase query time
*Caveat about performance:
* Response:
True but you can add a view and triggers to make it appear like a non-normalized table for developers.
^ The trade-off is work in the database in order to maintain data integrity. The question is whether it's worth it for your use case.
Things that rarely ever change
Now every time you want a constant you must query the database
*Caveat about performance:
* Response:
True but you can query it once and memoize.
Auditoriums in a movie theatre: want
theatre_id
Deleted primary key id and made + the key
theatre_id
room
WTF if it's a string why? Slow-ish. Plus if you ever need to change the key it's a beast
*Caveat about using domain keys:
* Response:
Agree! Why not keep the original ID key and add a unique constraint on the domain key columns?
But IMO it is worth it to consider pushing logic, validations, everything you can down into the database. Worth a read.
By Dan Chak (2009)