Database design to store notifications to users [closed]

I’m working on a project utilizing notifications as well, I’m not sure if you got yours sorted out by now or if it might help but this is the table structure I used: Notifications: – ID (PK) – recipient_id – sender_id – activity_type (‘comment on a post’, ‘sent friend request’, etc) – object_type (‘post’, ‘photo’, … Read more

Composite PRIMARY KEY enforces NOT NULL constraints on involved columns

If you need to allow NULL values, use a UNIQUE constraint (or index) instead of a PRIMARY KEY (and add a surrogate PK column – I suggest a serial or IDENTITY column in Postgres 10 or later). Auto increment table column A UNIQUE constraint allows columns to be NULL: CREATE TABLE distributor ( distributor_id GENERATED … Read more

Unique Constraint vs Unique Index

This MSDN article comparing the two is for SQL Server 2000: http://msdn.microsoft.com/en-us/library/aa224827(SQL.80).aspx For most purposes, there’s no difference – the constraint is implemented as an index under the covers. And though there’s the ability to disable the constraint, it doesn’t actually work in SQL Server. It only matters if you want to tweak things like … Read more

Within a trigger function, how to get which fields are being updated

If a “source” doesn’t “send an identifier”, the column will be unchanged. Then you cannot detect whether the current UPDATE was done by the same source as the last one or by a source that did not change the column at all. In other words: this does not work properly. If the “source” is identifiable … Read more

How to understand an EXPLAIN ANALYZE

While not as useful for a simple plan like this, http://explain.depesz.com is really useful. See http://explain.depesz.com/s/t4fi. Note the “stats” tab and the “options” pulldown. Things to note about this plan: The estimated row count (183) is reasonably comparable to the actual row count (25). It’s not hundreds of times more, nor is it 1. You’re … Read more