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 tableWhen creating a table, you can use the NOT NULL keyword to set a non-empty constraint. The specific syntax format is as follows:
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 tableIf 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:
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 ConstraintThe syntax rules for deleting a non-null constraint when modifying a table are as follows:
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:
|
<<: 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
Encapsulate el-dialog as a component When we use ...
Using the internal function instr in MySQL can re...
1. Pull the redis image docker pull redis 2. Star...
By default, setting width for label and span is in...
This article example shares the specific code of ...
Log in docker login Complete the registration and...
After configuring the tabBar in the WeChat applet...
As a front-end developer, I can’t avoid IE’s pitf...
1. What are the templates for ASP.NET Web applicat...
First, let me briefly introduce what MySQL is; In...
Table of contents Written in front Precautions De...
First configure the project artifacts Configuring...
Preface In MySQL, cross-database queries are main...
introduce In a distributed system, distributed lo...
1. Check the maximum number of open files in the ...