Why database designers do not make IDENTITY columns start from the min value rather than 1?

If you find that 2billion values isn’t enough, you’re going to find out that 4billion isn’t enough either (needing more than twice as many of anything over the lifetime of a project, than it was first designed for, is hardly rare*), so you need to take a different approach entirely (possibly long values, possibly something … Read more

SQL: Advantages of an ENUM vs. a one-to-many relationship?

Example shown using PostgreSQL, but other RDBMS’s have similar syntax That’s incorrect. It is not an ISO/IEC/ANSI SQL requirement, so the commercial databases do not provide it (you are supposed to provide Lookup tables). The small end of town implement various “extras”, but do not implement the stricter requirements, or the grunt, of the big … Read more

Best user role permissions database design practice? [closed]

As krokodilko wrote in his comment, it depends on the level of flexibility you need. I have implemented role based permissions for one of my clients as follows: User (user id (PK), user name (unique), password (salted and hashed!), first name, last name, phone etc’) Role (role id (PK), role name (unique), role description) Permission … Read more

How to create sequence if not exists

Postgres 9.5 or later IF NOT EXISTS was added to CREATE SEQUENCE in Postgres 9.5. That’s the simple solution now: CREATE SEQUENCE IF NOT EXISTS myschema.myseq; But consider details of the outdated answer anyway … And you know about serial or IDENTITY columns, right? Auto increment table column Postgres 9.4 or older Sequences share the … Read more

How to represent a 2-D data matrix in a database

RDBMSes aren’t flat. The R part sees to that. What you need is: Table Entity ———— ID Table EntityData —————- EntityID MatrixRow (1, 2, 3…) MatrixColumn (A, B, C, D…) Value Entity:EntityData is a one-to-many relationship; each cell in the matrix has an EntityData row. Now you have a schema that can be analyzed at … Read more