A CHECK is a unique constraint that verifies that the values being added to a column meet a specific requirement that we define. The CHECK constraint uses a Boolean expression to evaluate if the value is valid or not when it is being inserted into or updated in the column. If the check is valid, the database will then insert or update those values to the column. However, if the check is not valid, the database will reject the changes and raise an error in the database.
The CHECK constraint is generally set up when we create the table. Let us look at an example of a CREATE TABLE statement with various CHECK constraints.
CREATE TABLE member (
member_id SERIAL PRIMARY KEY,
first_name VARCHAR (50),
last_name VARCHAR (50),
birth_date DATE CHECK (birth_date > '1900-01-01'),
joined_date DATE CHECK (joined_date > birth_date),
opt_in CHAR(1) CHECK (opt_in IN ('Y','N')),
membership_fee numeric CHECK(membership_fee > 0)
);
Above, we have a table created with four CHECK constraints added with various data types. The first one is set based on the birth_date. It checks that the birth_date is after January 1, 1900. If we enter a birth date before 01/01/1900, we will get an error message:
The second CHECK constraint is based on the joined_date. It verifies that the member’s joined_date is later than the birth_date. It wouldn’t make sense to have a member joining prior to being born.
The third CHECK constraint is based on an opt_in column that is one character. It only allows a Y or N character. It could be argued that this could simply be set up as a Boolean. However, if the data is being sent in from a form, the value may need to be a specific character. We could also add other characters to be checked if needed.
The last CHECK constraint verifies the membership_fee is greater than zero.
You may have noticed in the error message that the constraint name was defined even though we did not set a name. In PostgreSQL, the constraint names are automatically created using the table name, column name, and type of constraint separated by underscores. If we wanted to have a specific name, we could replace the CHECK within the CREATE TABLE statement with a new line:
membership_fee numeric CONSTRAINT positive_fee CHECK(membership_fee > 0)
In this case, we name the CHECK constraint as positive_fee instead of the default member_membership_fee_check name.
We will explore how to add constraints after the table is created in an upcoming tutorial. In doing so, we can add more complex constraints over a series of columns.
The CHECK constraint can be very useful to force data into certain criteria. For example, with the fee that we have here, we're going to have a check there that ensures that the value that's going to be entered in is greater than zero. We can also set it to have a closed range, so it can be between certain values. For example, with the initial fee, we're going to have it such that it's going to be between 1 and 5. And then here with the opt-in, that's going to be a character. We can have it such that a check that the value is either y or n. It won't accept any other values. So let's go ahead and try to insert some data into the table and see what happens.
So this case here, we're going to enter data into the contact table, into the fee, into the initial fee, opt-in with values 1, 2, and y. All these should match the criteria. So they'll go ahead, and then inserts it correctly.
If we try to insert in the fee with minus 1 remember we have a check there to ensure that it is greater than zero. It'll error out making a note that it violates the criteria.
Then if you also remember, the initial fee was checking between 1 and 5. Were passing at 6 in this case here. You'll also notice that it violates that check constraint to insert a data. And lastly, we're going to go ahead and try to insert in data with the last piece of the opt in set as z, you remember, we're checking for y and n. And in this case here, it also violates such a constraint.
So we now query the table. We should see that only one row was inserted, being that all the other three had errored out.
Source: Authored by Vincent Tran