The course "Mastering Postgres" helped me practice query commands and understand why they're important. Inspiring me to explore the 'why' and 'how' behind each one. Truly a great course!Nakolus
Shorten dev cycles with branching and zero-downtime schema migrations.
We talked about check constraints several videos ago, 'cause it was relevant to whatever we were doing back then. But I wanna 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 wanna 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. But I just wanna 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. So, 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?" And it will say, "Yes, that is null. Thank you for asking." But 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. So, 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. So if you can, if your data model allows for it, you wanna 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. But then, when it comes to indexing, and comparing, and grouping, and sorting, and querying, everything just becomes a lot nicer. So, opt for the default being not null. And then 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. So you're good to go already there. Now by default, these are nullable. So, if we wanna 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. So 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. And there you go. Now 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. So 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. So 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.