Mastering Postgres is such a crisp way to learn this subject. With today’s AI tools writing many of the queries via autocomplete it’s vital to have this knowledge to ensure you know what’s actually happening under the hood. My app’s performance improved overnight after learning more about indexing. Aaron’s excitement for the subject makes it really fun to learn whether I’m next to my code editor or on a treadmill run.Peter Ramsing
Shorten dev cycles with branching and zero-downtime schema migrations.
We talked about check constraints several videos ago, because it was relevant to whatever we were doing back then. I want to talk about a few more constraints, starting with not null, and this will round out the end of this module. I don't know if that's exciting for you.
We've done a lot of data types, but before we look at not null constraints, I do want to remind you just quickly that nulls are weird. Nulls are weird. Every database, you'll run into this when you're comparing nulls, and we're gonna look at this in the querying section even more. I just want to remind you, nulls are unknown values. They're not zero, they're not false, they're not empty strings, they're not empty. It's just simply unknown. When you ask the database, "Does the number one equal this secret thing that I'm holding in my hand?" the database has to respond, "I don't know. What's the secret thing you're holding in your hand? What is this null value? It is unknown, it is unknowable. I cannot compare those two things."
You can say, "Is a column null?" It will say, "Yes, that is null. Thank you for asking." If you say, "Does the string Aaron equal this secret thing that I'm hiding from you?" It must respond, "I do not know," which is a null value. Nulls are a little bit weird.
We will look at the comparisons, the three-valued Boolean logic. We'll look at all of that stuff a little bit further down the line. My last charge to you before we look at some code here is most of the time you want to constrain your columns to be not null. If you can, if your data model allows for it, you want to make your columns not null, you're gonna get a lot of niceties, a lot of nice things out of that, not least of which is enforcement that there must be a value there. When it comes to indexing, and comparing, and grouping, and sorting, and querying, everything just becomes a lot nicer. Opt for the default being not null. If you know that a column is nullable, then of course you can allow it to be nullable. Don't make up your own version of null just to satisfy what some guy told you that you should make it not nullable. Make it not null if it's not null, but otherwise it's fine, just move on.
Let's look at some code. We're gonna start at the top here. We've got id bigint generated always as identity primary key. This primary key adds a not null constraint on this column because a primary key cannot be null. You're good to go already there. Now by default, these are nullable. If we want to make these not null, we have to add a constraint here. You could write it like this. I don't recommend this, so don't pause. Don't pause the video and go do this, check (name is not null). You could do that and it would totally work. It's just not recommended.
Let's drop table products and come back up here, and let's do it the right way. You can do it this way, you can do it this way. It is not as efficient as it could be. We'd just say text not null. There you go. We have created the table.
Let's say that we want price to be not null as well. You can also tack on another check constraint like we talked about several videos ago. You can say, in fact, the price must also be greater than zero. Now, you have both not null and a little bit of domain logic to say, well, zero is not that different than null for our business. Let's make sure that it's greater than zero as well. That's it for nulls.
You can just declare a column as not null, which I think should be your preferred method of operating. Make every column not null unless you have a very good reason to make it nullable. Write it in the not null format when not using a check constraint, but you can add a check constraint onto the end.