2

I am creating a Posts SQL table where I need to save:
1. If the post is approved;
2. When the post was approved.

I am considering naming these columns IsApproved and ApprovedAt:

create table dbo.Posts ( Id int identity not null, IsApproved bit null, ApprovedAt datetime null ) 

Is there any convention for such a case?

6
  • 1
    SQL Server doesn't support boolean, but your naming convention is reasonable. Commented Nov 12, 2018 at 12:34
  • That was a typo. I corrected to bit. I could also just have only one column named "Approved datetime" which when Null would mean that is not approved. Not sure if it would be worse for queries and confusing in some other cases. What do you think? Commented Nov 12, 2018 at 12:35
  • 2
    Having two columns does allow you to get into contradictory scenarios, an Approved date with no IsApproved set or visa versa. I would implement the IsApproved as a computed column, or logic within your DAO Commented Nov 12, 2018 at 12:37
  • 5
    Never use a nullable BIT column unless there's a really good reason to, because you raise the specter of what it means for a binary condition to be "unknown". Prefer NOT NULL and a sensible default (in this case, 0). Or, as you mentioned, if it can never be the case that IsApproved = 1 and ApprovedAt IS NULL, then make IsApproved a computed column (IsApproved = CONVERT(BIT, CASE WHEN ApprovedAt IS NULL THEN 0 ELSE 1 END)). Commented Nov 12, 2018 at 12:38
  • 2
    I would argue against having two separate columns in this case - for the same reason Jeroen Mostert wrote in his comment - A post that is approved must have an approval date, and a post that has an approval date is obviously approved - so there really is no point of keeping the IsApproved column. As for the naming conventions, Is... and ...Date are very reasonable for bit and date. Commented Nov 12, 2018 at 13:15

1 Answer 1

2

Is there any convention for such a case? No.

But I like your choice (is also my convention for booleans and dates) because:

  • IsApproved is a boolean and starts with Is
  • ApprovedAt is a date and ends with At

I share some "conventions":

https://www.periscopedata.com/blog/better-sql-schema

https://github.com/ghowland/Dasonic

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.