EN
MySQL - Insert NULL values
0
points
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:
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:
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');