Best table design for application configuration or application option settings?

For config data, I’d use the key/value structure with a row per configuration entry. You’re likely to read this data once and cache it, so performance isn’t an issue. As you point out, adding columns each time the set of config keys changes requires a lot more maintenance. SQL excels at modeling and manipulating arbitrarily … Read more

Is it possible to create a column with a UNIX_TIMESTAMP default in MySQL?

The way MySQL implements the TIMESTAMP data type, it is actually storing the epoch time in the database. So you could just use a TIMESTAMP column with a default of CURRENT_TIMESTAMP and apply the UNIX_TIMESTAMP() to it if you want to display it as an int: CREATE TABLE foo( created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP … Read more

What column data type should I use for storing large amounts of text or html

You should use TEXT like the others said, but there is some important advice every time you use TEXT or BLOB: decouple them form your base table as they really slow down accessing the table. Imagine the following structure: CREATE TABLE article ( id INT(10) UNSIGNED, title VARCHAR(40), author_id INT(10) UNSIGNED, created DATETIME, modified DATETIME … Read more

Django models – how to filter number of ForeignKey objects

The question and selected answer are from 2008 and since then this functionality has been integrated into the django framework. Since this is a top google hit for “django filter foreign key count” I’d like to add an easier solution with a recent django version using Aggregation. from django.db.models import Count cats = A.objects.annotate(num_b=Count(‘b’)).filter(num_b__lt=2) In … Read more

In terms of databases, is “Normalize for correctness, denormalize for performance” a right mantra?

The two most common reasons to denormalize are: Performance Ignorance The former should be verified with profiling, while the latter should be corrected with a rolled-up newspaper 😉 I would say a better mantra would be “normalize for correctness, denormalize for speed – and only when necessary”

Database Architecture for “Badge” System & Arbitrary Criteria (MySQL/PHP)

Given that the badge criteria can be arbitrarily complex, I don’t think you can store it in a database table broken down into “simple” data elements. Trying to write a “rules engine” that can handle arbitrarily complex criteria is going to take you down the path of basically re-writing all the tools that you have … Read more