Home  >  Article  >  Database  >  Is not null an index in mysql?

Is not null an index in mysql?

青灯夜游
青灯夜游Original
2022-06-20 17:56:002347browse

"not null" is not an index but a non-null constraint. The value used to specify the field cannot be null. For fields that use non-null constraints, if no value is specified when adding data, an error will be reported. There are two ways to set non-null constraints: 1. Set when creating a table, the syntax is "CREATE TABLE table name (field name data type NOT NULL);"; 2. Set when modifying the table, the syntax is "ALTER TABLE table name CHANGE COLUMN field name Segment name data type NOT NULL;".

Is not null an index in mysql?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

"not null" is not an index but a non-null constraint.

Non-null constraint (NOT NULL) means that the value of the 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 achieved with the CREATE TABLE or ALTER TABLE statement. 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 will be invalid. At this time, you can set a non-null constraint for the user name field.

Set non-null constraints when creating a table

You can use the NOT NULL keyword to set non-null constraints when creating a table, the specific syntax format As follows:

CREATE TABLE 表名(
字段名 数据类型 NOT NULL
);

Example: Create data table tb_dept4, the specified department name cannot be empty

CREATE TABLE tb_dept4
(
id INT(11) PRIMARY KEY,
name VARCHAR(22) NOT NULL,
location VARCHAR(50)
);

Is not null an index in mysql?

DESC tb_dept4;

Is not null an index in mysql?

Add a non-null constraint when modifying the table

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

The syntax format for setting non-empty constraints when modifying the table is as follows:

ALTER TABLE <表名
CHANGE COLUMN 字段名
字段名 数据类型 NOT NULL;

Example: Modify the data table tb_dept4, the specified department position cannot be empty

ALTER TABLE tb_dept4
CHANGE COLUMN location
location VARCHAR(50) NOT NULL;

Is not null an index in mysql?

【Related recommendations: mysql video tutorial

The above is the detailed content of Is not null an index in mysql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn