Languages
[Edit]
EN

MySQL - Insert NULL values

0 points
Created by:
Dirask Community
6810

In this article, we would like to show you how to insert NULL values to the table in MySQL.

Quick solution:

INSERT INTO `table_name`
    (`column1`, `column2`, `column3`, `columnN`)
VALUES
    (value1, NULL, NULL, valueN);

Practical example

To show you how to insert NULL values to the table, we will use the following users table:

MySQL - example data used to insert NULL values to the table
MySQL - example data used to insert NULL values to the table

Note:

At the end of this article you can find database preparation SQL queries.

Example

In this example, we will insert rows with NULL values to the users table.

Query

INSERT INTO `users`
	( `name`, `surname`, `department_id`, `salary`)
VALUES
	(NULL, NULL, NULL, NULL),
	('Simon', NULL, NULL, NULL),
	('Taylor', 'Martin', NULL, NULL),
	('Andrew', 'Thompson', 1, NULL),
	('Taylor', 'Martin', 2, '2000');

Result:

MySQL - insert NULL values to the table - result
MySQL - insert NULL values to the table - result

Database preparation

create_tables.sql file:

CREATE TABLE `users` (
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(50),
	`surname` VARCHAR(50),
	`department_id` INT(10),
    `salary` DECIMAL(15,2),
	PRIMARY KEY (`id`)
);

insert_data.sql file:

INSERT INTO `users`
	( `name`, `surname`, `department_id`, `salary`)
VALUES
	('John', 'Stewart', 1, '6000'),
	('Chris', 'Brown', 2, '6000'),
	('Kate', 'Lewis', 3, '4000');

MySQL - Problems

MySQL - Insert NULL values
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join