MySQL not null constraint case explanation

MySQL not null constraint case explanation

The MySQL NOT NULL constraint means that the value of a field cannot be empty. For fields that use non-null constraints, if the user does not specify a value when adding data, the database system will report an error. This can be done through the CREATE TABLE or ALTER TABLE statements. Add the keyword NOT NULL as a qualifier after the definition of a column in the table to constrain the value of the column to not be empty.

For example, in the user information table, if the user name is not added, then this user information is invalid. At this time, you can set a non-null constraint for the username field.

Set a not null constraint when creating a table

When creating a table, you can use the NOT NULL keyword to set a non-empty constraint. The specific syntax format is as follows:

<field name> <data type> NOT NULL;

Example 1

Create a data table tb_dept4 and specify that the department name cannot be empty. The SQL statement and running results are as follows.

mysql> CREATE TABLE tb_dept4
    -> (
    -> id INT(11) PRIMARY KEY,
    -> name VARCHAR(22) NOT NULL,
    -> location VARCHAR(50)
    -> );
Query OK, 0 rows affected (0.37 sec)

mysql> DESC tb_dept3;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(22) | NO | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.06 sec)

Adding a not null constraint when modifying a table

If you forget to set a not-null constraint for a field when creating a table, you can also add a not-null constraint by modifying the table.

The syntax format for setting a non-empty constraint when modifying a table is as follows:

ALTER TABLE <table name>
CHANGE COLUMN <field name>
<field name> <data type> NOT NULL;

Example 2

Modify the data table tb_dept4 and specify that the department location cannot be empty. The SQL statement and running results are as follows.

mysql> ALTER TABLE tb_dept4
    -> CHANGE COLUMN location
    -> location VARCHAR(50) NOT NULL;
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC tb_dept4;
+----------+-------------+------+-----+----------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+----------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(22) | NO | | NULL | |
| location | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+----------+-------+
3 rows in set (0.00 sec)

Remove Not Null Constraint

The syntax rules for deleting a non-null constraint when modifying a table are as follows:

ALTER TABLE <table name>
CHANGE COLUMN <field name> <field name> <data type> NULL;

Example 3

Modify the data table tb_dept4 and delete the non-null constraint of the department location. The SQL statement and running results are as follows.

mysql> ALTER TABLE tb_dept4
    -> CHANGE COLUMN location
    -> location VARCHAR(50) NULL;
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC tb_dept4;
+----------+-------------+------+-----+----------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+----------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(22) | NO | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+----------+-------+
3 rows in set (0.00 sec)

This is the end of this article about the case study of MySQL not null constraint. For more relevant MySQL not null constraint content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to create and delete foreign key constraints in MySQL
  • Specific method to add foreign key constraints in mysql
  • MySQL database constraints and data table design principles
  • Detailed explanation of whether the MySQL database should use foreign key constraints
  • MySQL learning: five major constraints of database tables explained in detail for beginners
  • Detailed explanation of the six common constraint types in MySQL
  • A brief discussion on the difference between MYSQL primary key constraint and unique constraint
  • MySQL Constraints Super Detailed Explanation
  • How to set constraints for tables in MySQL database

<<:  Solve the problem that the IP address obtained using nginx is 127.0.0.1

>>:  Discuss the value of Web standards from four aspects with a mind map

Recommend

Detailed process of configuring NIS in Centos7

Table of contents principle Network environment p...

Is your website suitable for IE8?

During the Olympic Games, IE 8 Beta 2 will be rele...

Detailed explanation of as, question mark and exclamation mark in Typescript

1. The as keyword indicates an assertion In Types...

JavaScript implements click to change the image shape (transform application)

JavaScript clicks to change the shape of the pict...

MySQL decimal unsigned update negative numbers converted to 0

Today, when verifying the concurrency problem of ...

How to use Docker to package and deploy images locally

First time using docker to package and deploy ima...

Python3.6-MySql insert file path, the solution to lose the backslash

As shown below: As shown above, just replace it. ...

Implementation of positioning CSS child elements relative to parent elements

Solution Add position:relative to the parent elem...

Solution to slow response of Tomcat server

1. Analytical thinking 1. Eliminate the machine&#...

Summary of changes in the use of axios in vue3 study notes

Table of contents 1. Basic use of axio 2. How to ...

Detailed explanation of js closure and garbage collection mechanism examples

Table of contents Preface text 1. Closure 1.1 Wha...

A brief discussion on the use of GROUP BY and HAVING in SQL statements

Before introducing the GROUP BY and HAVING clause...