A2-03-08.DDL-MySQL NOT NULL Constraint

转载自:http://www.mysqltutorial.org/mysql-not-null-constraint/

 

MySQL NOT NULL Constraint

 

Summary: this tutorial introduces you to the MySQL NOT NULL constraint that helps you keep your data consistent.

Introduction to MySQL NOT NULL constraint

The NOT NULL constraint is a column constraint that forces the values of a column to non-NULL values only.

The syntax of the NOT NULL constraint is as follows:

A column may contain one NOT NULL constraint only, which specifies a rule that the column must not contain any NULL value.

The following CREATE TABLE statement creates the tasks table:

The title and start_date columns have the NOT NULL constraints explicitly. The id column has the PRIMARY KEY constraint, therefore, it implicitly includes a NOT NULL constraint.

The end_date column can have NULL values. This is because when we add a new task we may not know its end date.

It’s a best practice to have the NOT NULL constraint in every column of a table unless you have a good reason not to do so.

Generally, the NULL value makes your queries more complicated. In such cases, you can use the NOT NULL constraint and provide a default value for the column. See the following example:

In this example, the default value for quantity column is 0. Because at the time we add a row to the inventory table, the value of the quantity column should be 0, not NULL.

Add a NOT NULL constraint to an existing column

Typically, you add a NOT NULL constraints to columns when you create the table. However, sometimes, you want to add a NOT NULL constraint to NULL-able column of an existing table. In this case, you use the following steps:

  1. Check the current values of the column.
  2. Update the NULL values to non-null values.
  3. Add the NOT NULL constraint

Let’s take a look at an example.

We insert data into the tasks table for the demonstration.

Now, suppose you want to force users to give estimated end date when creating a new task. To do this, you need to add the NOT NULL constraint to the end_date column of the tasks table.

First, check the value of end_date table. We use the IS NULL operator to check if the value in a column is NULL or not:

MySQL NOT NULL Constraint example

The query returns one row with the end_date value is NULL.

Second, update the NULL values to non-null values. In this case, we can create a rule that if the end_dateis NULL, we make the end date one week after the start date.

Let’s check the change:

MySQL NOT NULL Update NULL values

Third, add the NOT NULL constraint to the code end_date column. To do it, you use the following ALTER TABLE statement:

In our case, the old column name and the new column name must be the same except for the column definition that has the NOT NULL constraint:

Let’s verify the change by using the DESCRIBE statement:

MySQL NOT NULL - Add to existing column

As you see, the NOT NULL constraint was added to the end_date column.

In this tutorial, you have learned how to define NOT NULL constraint for a column and add the NOT NULLconstraint to an existing column.

posted @ 2018-08-23 16:01  zhuntidaoren  阅读(240)  评论(0编辑  收藏  举报