mongodb schema design for blogs

Article { “_id” : “A”, “title” : “Hello World”, “user_id” : 12345, “text” : ‘My test article’, “comments” : [ { ‘text’ : ‘blah’, ‘user_id’ : 654321, ‘votes’ : [987654]}, { ‘text’ : ‘foo’, ‘user_id’ : 987654, ‘votes’ : [12345, 654321] }, … ] } The basic premise here is that I’ve nested the Comments … Read more

Why is negative id or zero considered a bad practice?

To be clear, this question and answer are about using negative numbers for surrogate keys, not for natural keys. As far as I know, there are three reasons for considering it to be a bad practice. It violates the principle of least surprise. Some people assume all ID numbers are non-negative. Some people use negative … Read more

Database design – articles, blog posts, photos, stories

Here’s one way to implement supertype/subtype tables for your app. First, the supertype table. It contains all the columns common to all subtypes. CREATE TABLE publications ( pub_id INTEGER NOT NULL PRIMARY KEY, pub_type CHAR(1) CHECK (pub_type IN (‘A’, ‘B’, ‘P’, ‘S’)), pub_url VARCHAR(64) NOT NULL UNIQUE, CONSTRAINT publications_superkey UNIQUE (pub_id, pub_type) ); Next, a … Read more

How to constrain a table to contain a single row?

You make sure one of the columns can only contain one value, and then make that the primary key (or apply a uniqueness constraint). CREATE TABLE T1( Lock char(1) not null, /* Other columns */, constraint PK_T1 PRIMARY KEY (Lock), constraint CK_T1_Locked CHECK (Lock=’X’) ) I have a number of these tables in various databases, … Read more

How can I avoid NULLs in my database, while also representing missing data?

Good on you, for eliminating Nulls. I have never allowed Nulls in any of my databases. Of course, if nulls are prohibited, then missing information will have to be handled by some other means. Unfortunately, those other means are much too complex to be discussed in detail here. Actually it is not so hard at … Read more

What is wrong with a transitive dependency?

I’ll explain by an example: ——————————————————————- | Course | Field | Instructor | Instructor Phone | ——————————————————————- | English | Languages | John Doe | 0123456789 | | French | Languages | John Doe | 0123456789 | | Drawing | Art | Alan Smith | 9856321158 | | PHP | Programming | Camella Ford | … Read more